### Install Vertex AI SDK Source: https://github.com/googleapis/python-aiplatform/blob/main/README.rst Install the Vertex AI SDK for Python using pip or uv. ```console pip install google-cloud-aiplatform ``` ```console uv pip install google-cloud-aiplatform ``` -------------------------------- ### Install Dependencies Source: https://github.com/googleapis/python-aiplatform/blob/main/scripts/readme-gen/templates/install_deps.tmpl.rst Install all the required Python packages listed in the requirements.txt file using pip. ```bash $ pip install -r requirements.txt ``` -------------------------------- ### Install Vertex AI SDK with Prediction Support (Windows) Source: https://github.com/googleapis/python-aiplatform/blob/main/google/cloud/aiplatform/prediction/README.md Installs the virtualenv tool, creates a virtual environment, activates it, and installs the Vertex AI SDK with prediction support. Replace `` with your desired environment folder name. ```shell pip install virtualenv virtualenv \Scripts\activate \Scripts\pip.exe install "google-cloud-aiplatform[prediction]>=1.16.0" ``` -------------------------------- ### Install Vertex AI SDK Source: https://github.com/googleapis/python-aiplatform/blob/main/gemini_docs/README.md Install the google-cloud-aiplatform Python package with the specified version. Use the --upgrade --user flags for installation. ```shell pip3 install --upgrade --user "google-cloud-aiplatform>=1.114.0" ``` -------------------------------- ### Install Vertex AI SDK with Prediction Support (Mac/Linux) Source: https://github.com/googleapis/python-aiplatform/blob/main/google/cloud/aiplatform/prediction/README.md Installs the virtualenv tool, creates a virtual environment, activates it, and installs the Vertex AI SDK with prediction support. Replace `` with your desired environment folder name. ```shell pip install virtualenv virtualenv source /bin/activate /bin/pip install "google-cloud-aiplatform[prediction]>=1.16.0" ``` -------------------------------- ### Install google-cloud-aiplatform SDK Source: https://github.com/googleapis/python-aiplatform/blob/main/agentplatform/model_garden/README.md Install or upgrade the google-cloud-aiplatform Python package to the latest version. ```shell pip3 install --upgrade --user "google-cloud-aiplatform>=1.84" ``` -------------------------------- ### Get Help for Python Sample Source: https://github.com/googleapis/python-aiplatform/blob/main/scripts/readme-gen/templates/README.tmpl.rst This command displays the help message for a Python sample script, providing information on its usage and available arguments. ```bash {{get_help(sample.file)|indent}} ``` -------------------------------- ### Fast Tryout Deployment Source: https://github.com/googleapis/python-aiplatform/blob/main/agentplatform/model_garden/README.md Enable an experimental fast-deploy path for popular models, allowing for interactive experimentation without a full production setup. ```python endpoint = model.deploy(fast_tryout_enabled=True) ``` -------------------------------- ### Generative Model with System Instructions Source: https://github.com/googleapis/python-aiplatform/blob/main/gemini_docs/README.md Configures a generative model with system instructions to guide its behavior and response style. This example sets the model to talk like a pirate and avoid rude words. ```python from vertexai.generative_models import GenerativeModel model = GenerativeModel( "gemini-1.0-pro", system_instruction=[ "Talk like a pirate.", "Don\'t use rude words.", ], ) print(model.generate_content("Why is sky blue?")) ``` -------------------------------- ### Install Vertex AI SDK Source: https://github.com/googleapis/python-aiplatform/blob/main/vertexai/generative_models/README.md Install or upgrade the google-cloud-aiplatform Python package to version 1.38 or higher. ```shell pip3 install --upgrade --user "google-cloud-aiplatform>=1.38" ``` -------------------------------- ### Install Vertex AI SDK Source: https://github.com/googleapis/python-aiplatform/blob/main/vertexai/model_garden/README.md Install the google-cloud-aiplatform Python package to use the SDK. Ensure you have a recent version. ```shell pip3 install --upgrade --user "google-cloud-aiplatform>=1.84" ``` -------------------------------- ### Install PyAudio on Windows Source: https://github.com/googleapis/python-aiplatform/blob/main/scripts/readme-gen/templates/install_portaudio.tmpl.rst On Windows, PortAudio is typically installed automatically when PyAudio is installed, often without explicit steps. ```bash pip install pyaudio ``` -------------------------------- ### Install virtualenv and google-cloud-aiplatform on Windows Source: https://github.com/googleapis/python-aiplatform/blob/main/docs/README.rst Installs the virtualenv tool and the google-cloud-aiplatform library in a virtual environment on Windows systems. ```bash pip install virtualenv virtualenv \Scripts\activate \Scripts\pip.exe install google-cloud-aiplatform ``` -------------------------------- ### Install virtualenv and google-cloud-aiplatform on Mac/Linux Source: https://github.com/googleapis/python-aiplatform/blob/main/docs/README.rst Installs the virtualenv tool and the google-cloud-aiplatform library in a virtual environment on Mac or Linux systems. ```bash pip install virtualenv virtualenv source /bin/activate /bin/pip install google-cloud-aiplatform ``` -------------------------------- ### Configure System Instructions for Model Source: https://github.com/googleapis/python-aiplatform/blob/main/vertexai/generative_models/README.md Customize model behavior by providing system instructions during model initialization. This example sets instructions to 'Talk like a pirate.' and 'Don't use rude words.' ```python from vertexai.generative_models import GenerativeModel model = GenerativeModel( "gemini-1.0-pro", system_instruction=[ "Talk like a pirate.", "Don't use rude words.", ], ) print(model.generate_content("Why is sky blue?")) ``` -------------------------------- ### Quick Start: Default Deployment Source: https://github.com/googleapis/python-aiplatform/blob/main/agentplatform/model_garden/README.md Deploys a model using the default configuration. This is suitable for fast prototyping and initial evaluation of model outputs. ```APIDOC ## Quick Start: Default Deployment This is the simplest way to deploy a model. If you provide just a model name, the SDK will use the default deployment configuration. ### Code Example ```python from agentplatform import model_garden model = model_garden.OpenModel("google/paligemma@paligemma-224-float32") endpoint = model.deploy() ``` ### Use Case Fast prototyping, first-time users evaluating model outputs. ``` -------------------------------- ### Quick Start: Default Model Deployment Source: https://github.com/googleapis/python-aiplatform/blob/main/agentplatform/model_garden/README.md Deploy a model using the default configuration by providing only the model name. Ideal for fast prototyping and initial evaluations. ```python from agentplatform import model_garden model = model_garden.OpenModel("google/paligemma@paligemma-224-float32") endpoint = model.deploy() ``` -------------------------------- ### Install PortAudio on Mac OS X Source: https://github.com/googleapis/python-aiplatform/blob/main/scripts/readme-gen/templates/install_portaudio.tmpl.rst Use Homebrew to install PortAudio on macOS. This is a prerequisite for PyAudio. ```bash brew install portaudio ``` -------------------------------- ### Troubleshoot PyAudio Installation on Mac OS X Source: https://github.com/googleapis/python-aiplatform/blob/main/scripts/readme-gen/templates/install_portaudio.tmpl.rst If pip install encounters errors related to 'portaudio.h', use these flags to specify include and library paths. ```bash pip install --global-option='build_ext' --global-option='-I/usr/local/include' --global-option='-L/usr/local/lib' pyaudio ``` -------------------------------- ### Install PortAudio on Debian/Ubuntu Linux Source: https://github.com/googleapis/python-aiplatform/blob/main/scripts/readme-gen/templates/install_portaudio.tmpl.rst Install the PortAudio development package and Python development headers on Debian-based Linux distributions. ```bash apt-get install portaudio19-dev python-all-dev ``` -------------------------------- ### Install Agent Engine Packages Source: https://github.com/googleapis/python-aiplatform/blob/main/gemini_docs/README.md Install the necessary packages for Agent Engine and ADK. Ensure you are using version 1.111 or higher. ```shell pip3 install --upgrade --user "google-cloud-aiplatform[agent_engines,adk]>=1.111" ``` -------------------------------- ### Install Python Development Headers Source: https://github.com/googleapis/python-aiplatform/blob/main/CONTRIBUTING.rst If you encounter errors related to 'Python.h' not being found, install the python-dev package on Debian/Ubuntu systems. ```bash sudo apt-get install python-dev ``` -------------------------------- ### Function Calling with Tools Source: https://github.com/googleapis/python-aiplatform/blob/main/vertexai/generative_models/README.md Implement function calling by defining tools with function declarations. This example defines a 'get_current_weather' function and uses it in a chat. ```python # First, create tools that the model is can use to answer your questions. # Describe a function by specifying it's schema (JsonSchema format) get_current_weather_func = generative_models.FunctionDeclaration( name="get_current_weather", description="Get the current weather in a given location", parameters={ "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA" }, "unit": { "type": "string", "enum": [ "celsius", "fahrenheit", ] } }, "required": [ "location" ] }, ) # Tool is a collection of related functions weather_tool = generative_models.Tool( function_declarations=[get_current_weather_func], ) # Use tools in chat: model = GenerativeModel( "gemini-pro", # You can specify tools when creating a model to avoid having to send them with every request. tools=[weather_tool], ) chat = model.start_chat() # Send a message to the model. The model will respond with a function call. print(chat.send_message("What is the weather like in Boston?")) # Then send a function response to the model. The model will use it to answer. print(chat.send_message( Part.from_function_response( name="get_current_weather", response={ "content": {"weather": "super nice"}, } ), )) ``` -------------------------------- ### Get a Model Source: https://github.com/googleapis/python-aiplatform/blob/main/README.rst Retrieve a model resource using its resource name. ```Python model = aiplatform.Model('/projects/my-project/locations/us-central1/models/{MODEL_ID}') ``` -------------------------------- ### Generate Content with System Instructions Source: https://github.com/googleapis/python-aiplatform/blob/main/gemini_docs/README.md Generates content with specific system instructions to guide the model's behavior. ```APIDOC ## Generate Content with System Instructions ### Description Generates content with specific system instructions to guide the model's behavior. ### Method ```python from vertexai.generative_models import GenerativeModel model = GenerativeModel( "gemini-1.0-pro", system_instruction=[ "Talk like a pirate.", "Don't use rude words.", ], ) print(model.generate_content("Why is sky blue?")) ``` ``` -------------------------------- ### Get Specific Skill Revision Source: https://github.com/googleapis/python-aiplatform/blob/main/README.rst Retrieves a specific skill revision by its resource name. This example gets the first revision from the list. ```Python # Get a specific skill revision by its resource name if revisions_response.skill_revisions: target_revision_name = revisions_response.skill_revisions[0].name revision = client.skills.revisions.get(name=target_revision_name) ``` -------------------------------- ### Create a Client Source: https://github.com/googleapis/python-aiplatform/blob/main/README.rst Demonstrates how to initialize a Gen AI client for Vertex AI. ```APIDOC ## Create a Client ### Description Initializes a Gen AI client for use with Vertex AI, specifying the project and location. ### Method `genai.Client()` ### Parameters - **vertexai** (bool) - Required - Set to `True` to use Vertex AI. - **project** (str) - Required - The Google Cloud project ID. - **location** (str) - Required - The Google Cloud location. ### Request Example ```python genai_client = genai.Client(vertexai=True, project="your-project", location="your-location") ``` ``` -------------------------------- ### Run Python Sample Source: https://github.com/googleapis/python-aiplatform/blob/main/scripts/readme-gen/templates/README.tmpl.rst Use this command to execute a Python sample script. Ensure you have the necessary API enabled and role permissions. ```bash $ python {{sample.file}} ``` -------------------------------- ### Get Predictions Source: https://github.com/googleapis/python-aiplatform/blob/main/docs/README.rst Gets predictions from a deployed model on an endpoint. ```APIDOC ## Get Predictions ### Description Gets predictions from a deployed model on an endpoint. ### Method `Endpoint.predict()` ### Parameters * **instances** (list) - Required - A list of instances to get predictions for. ### Request Example ```python endpoint.predict(instances=[[6.7, 3.1, 4.7, 1.5], [4.6, 3.1, 1.5, 0.2]]) ``` ``` -------------------------------- ### Grounding Responses with Google Search Source: https://github.com/googleapis/python-aiplatform/blob/main/vertexai/generative_models/README.md Configure a GenerativeModel to use Google Search for retrieving information to ground its responses. This example shows how to set up the tool and print the response text and grounding metadata. ```python from vertexai import generative_models from vertexai.generative_models import GenerativeModel, Tool model=GenerativeModel( "gemini-pro", tools=[Tool.from_google_search_retrieval( google_search_retrieval=generative_models.grounding.GoogleSearchRetrieval( dynamic_retrieval_config=generative_models.grounding.DynamicRetrievalConfig( mode=generative_models.grounding.DynamicRetrievalConfig.Mode.MODE_DYNAMIC))) ], ) response = model.generate_content("Who won Euro 2024") print(response.text) # Checking grounding metadata. It contains grounding supports and the follow-up search entry widget. if response.candidates: print(response.candidates[0].grounding_metadata) ``` -------------------------------- ### Get Predictions from Endpoint Source: https://github.com/googleapis/python-aiplatform/blob/main/README.rst Sends instances to an endpoint to get predictions. ```APIDOC ## Get Predictions from Endpoint ### Description This section describes how to send prediction requests to a deployed endpoint, providing instances and receiving predictions. ### Method `endpoint.predict` ### Parameters - `instances` (list) - A list of instances for which to get predictions. ### Request Example ```python endpoint.predict(instances=[[6.7, 3.1, 4.7, 1.5], [4.6, 3.1, 1.5, 0.2]]) ``` ``` -------------------------------- ### Initialize Vertex AI SDK Source: https://github.com/googleapis/python-aiplatform/blob/main/docs/README.rst Initialize the Vertex AI SDK with common configurations like project ID, location, and staging bucket. Environment defaults are used if parameters are not provided. ```Python aiplatform.init( # your Google Cloud Project ID or number # environment default used is not set project='my-project', # the Vertex AI region you will use # defaults to us-central1 location='us-central1', # Google Cloud Storage bucket in same region as location # used to stage artifacts staging_bucket='gs://my_staging_bucket', # custom google.auth.credentials.Credentials # environment default credentials used if not set credentials=my_credentials, # customer managed encryption key resource name # will be applied to all Vertex AI resources if set encryption_spec_key_name=my_encryption_key_name, # the name of the experiment to use to track # logged metrics and parameters experiment='my-experiment', # description of the experiment above experiment_description='my experiment description' ) ``` -------------------------------- ### Build HTML Documentation Source: https://github.com/googleapis/python-aiplatform/blob/main/CONTRIBUTING.rst Build the project's HTML documentation using nox. This command should be run after making documentation changes. ```bash nox -s docs ``` -------------------------------- ### Initialize Vertex AI SDK Source: https://github.com/googleapis/python-aiplatform/blob/main/README.rst Initialize the SDK with common configurations like project ID, region, and GCS bucket. Ensure credentials and encryption spec are set if needed. ```Python aiplatform.init( # your Google Cloud Project ID or number # environment default used is not set project='my-project', # the Vertex AI region you will use # defaults to us-central1 location='us-central1', # Google Cloud Storage bucket in same region as location # used to stage artifacts staging_bucket='gs://my_staging_bucket', # custom google.auth.credentials.Credentials # environment default credentials used if not set credentials=my_credentials, # customer managed encryption key resource name # will be applied to all Vertex AI resources if set encryption_spec_key_name=my_encryption_key_name, # the name of the experiment to use to track # logged metrics and parameters experiment='my-experiment', # description of the experiment above experiment_description='my experiment description' ) ``` -------------------------------- ### Run All Sample Tests Source: https://github.com/googleapis/python-aiplatform/blob/main/CONTRIBUTING.rst Navigate to the samples/snippets directory and run all tests for a specific Python version using nox. ```bash cd samples/snippets nox -s py-3.9 ``` -------------------------------- ### Select a Verified Deployment: By Container and Hardware Source: https://github.com/googleapis/python-aiplatform/blob/main/agentplatform/model_garden/README.md Deploys a model by specifying both a serving container image URI and a hardware configuration. ```APIDOC ## Select a Verified Deployment: By Container and Hardware Specify both a container image and a hardware configuration from the list of verified deployment configurations. ### Code Example ```python endpoint = model.deploy( serving_container_image_uri="us-docker.pkg.dev/vertex-ai/vertex-vision-model-garden-dockers/pytorch-vllm-serve:20250430_0916_RC00_maas", machine_type="a3-highgpu-1g", accelerator_type="NVIDIA_H100_80GB", accelerator_count=1, ) ``` **Use case:** Production configuration, performance tuning, scaling. ``` -------------------------------- ### Fast Tryout Deployment Source: https://github.com/googleapis/python-aiplatform/blob/main/agentplatform/model_garden/README.md Enables an experimental fast-deploy path for popular models by setting `fast_tryout_enabled=True`. ```APIDOC ## Fast Tryout Deployment Enable experimental fast-deploy path for popular models. ### Code Example ```python endpoint = model.deploy(fast_tryout_enabled=True) ``` **Use case:** Interactive experimentation without full production setup. ``` -------------------------------- ### Deploy Model with Container and Hardware Source: https://github.com/googleapis/python-aiplatform/blob/main/vertexai/model_garden/README.md Deploy a model by specifying both a verified container image URI and hardware configuration. This is recommended for production environments and performance optimization. ```python endpoint = model.deploy( serving_container_image_uri="us-docker.pkg.dev/vertex-ai/vertex-vision-model-garden-dockers/pytorch-vllm-serve:20250430_0916_RC00_maas", machine_type="a3-highgpu-1g", accelerator_type="NVIDIA_H100_80GB", accelerator_count=1, ) ``` -------------------------------- ### Create and Activate Virtual Environment Source: https://github.com/googleapis/python-aiplatform/blob/main/scripts/readme-gen/templates/install_deps.tmpl.rst Create a new virtual environment named 'env' and activate it. Samples are compatible with Python 3.7+. ```bash $ virtualenv env $ source env/bin/activate ``` -------------------------------- ### Get Predictions from Endpoint Source: https://github.com/googleapis/python-aiplatform/blob/main/README.rst Send instances to an endpoint to get predictions. The input format should match the deployed model's requirements. ```Python endpoint.predict(instances=[[6.7, 3.1, 4.7, 1.5], [4.6, 3.1, 1.5, 0.2]]) ``` -------------------------------- ### Deploy Model with Container and Hardware Source: https://github.com/googleapis/python-aiplatform/blob/main/agentplatform/model_garden/README.md Deploy a model by specifying both a serving container image URI and hardware configuration. Suitable for production and performance tuning. ```python endpoint = model.deploy( serving_container_image_uri="us-docker.pkg.dev/vertex-ai/vertex-vision-model-garden-dockers/pytorch-vllm-serve:20250430_0916_RC00_maas", machine_type="a3-highgpu-1g", accelerator_type="NVIDIA_H100_80GB", accelerator_count=1, ) ``` -------------------------------- ### Install Pre-commit Hooks Source: https://github.com/googleapis/python-aiplatform/blob/main/CONTRIBUTING.rst Enable pre-commit hooks to automatically enforce linter checks during the commit process. This command installs the hooks into your local repository's .git/hooks directory. ```bash pre-commit install pre-commit installed at .git/hooks/pre-commit ``` -------------------------------- ### Get Skill Source: https://github.com/googleapis/python-aiplatform/blob/main/gemini_docs/README.md Retrieves an existing skill by its resource name. ```APIDOC ## get ### Description Retrieves an existing skill by its resource name. ### Method client.skills.get ### Parameters #### Query Parameters - **name** (string) - Required - The resource name of the skill (e.g., 'projects/{project_id_or_number}/locations/{location}/skills/{skill_id}'). ### Response #### Success Response (200) - **skill_id** (string) - The ID of the skill. - **name** (string) - The resource name of the skill. - **display_name** (string) - The display name of the skill. - **description** (string) - The description of the skill. ``` -------------------------------- ### Get Skill Source: https://github.com/googleapis/python-aiplatform/blob/main/README.rst Retrieves an existing skill by its resource name. ```APIDOC ## Get Skill ### Description Fetches an existing skill from the Skill Registry using its resource name. ### Method `client.skills.get()` ### Parameters - **name** (str) - Required - The resource name of the skill to retrieve. ### Request Example ```python fetched_skill = client.skills.get(name=skill.name) ``` ``` -------------------------------- ### List Model Deployment Options Source: https://github.com/googleapis/python-aiplatform/blob/main/agentplatform/model_garden/README.md Inspect the available deployment configurations, including compatible machine types and container images, for a specific model. ```python model = model_garden.OpenModel("google/paligemma@paligemma-224-float32") deploy_options = model.list_deploy_options() ``` -------------------------------- ### Create Prompt (Dictionary Format) Source: https://github.com/googleapis/python-aiplatform/blob/main/gemini_docs/README.md Define a prompt using a dictionary structure, including prompt data, system instructions, variables, and the model name. Then, use `client.prompts.create()` to register it. ```python prompt = { "prompt_data": { "contents": [{"parts": [{"text": "Hello, {name}! How are you?"}]}], "system_instruction": {"parts": [{"text": "Please answer in a short sentence."}]}, "variables": [ {"name": {"text": "Alice"}}, ], "model": "gemini-2.5-flash", }, } prompt_resource = client.prompts.create( prompt=prompt, ) ``` -------------------------------- ### Get Prompt Source: https://github.com/googleapis/python-aiplatform/blob/main/gemini_docs/README.md Retrieves an existing prompt resource by its ID. ```APIDOC ## get ### Description Retrieves an existing prompt resource by its ID. ### Method client.prompts.get ### Parameters #### Query Parameters - **prompt_id** (string) - Required - The ID of the prompt to retrieve. ### Response #### Success Response (200) - **prompt_id** (string) - The ID of the retrieved prompt. - **prompt_data** (object) - The data of the prompt. - **model** (string) - The model used for the prompt. ``` -------------------------------- ### Get a Model Source: https://github.com/googleapis/python-aiplatform/blob/main/README.rst Retrieves a model resource using its resource name. ```APIDOC ## Get a Model ### Description This section describes how to retrieve a model resource from Vertex AI using its full resource name. ### Method `aiplatform.Model(model_name)` ### Parameters - `model_name` (str) - The resource name of the model (e.g., 'projects/my-project/locations/us-central1/models/{MODEL_ID}'). ### Request Example ```python model = aiplatform.Model('/projects/my-project/locations/us-central1/models/{MODEL_ID}') ``` ``` -------------------------------- ### Get an Existing Skill Source: https://github.com/googleapis/python-aiplatform/blob/main/README.rst Retrieves an existing skill by its resource name. ```Python fetched_skill = client.skills.get(name=skill.name) ``` -------------------------------- ### Select a Verified Deployment: By Hardware Source: https://github.com/googleapis/python-aiplatform/blob/main/agentplatform/model_garden/README.md Deploys a model using a specified hardware configuration, including machine type, accelerator type, and count. ```APIDOC ## Select a Verified Deployment: By Hardware Specify a hardware configuration from the list of verified deployment configurations. ### Code Example ```python endpoints = model.deploy( machine_type="a3-highgpu-1g", accelerator_type="NVIDIA_H100_80GB", accelerator_count=1, ) ``` ``` -------------------------------- ### Get Model Evaluation Source: https://github.com/googleapis/python-aiplatform/blob/main/README.rst Retrieves the model evaluation resource for a given model. ```APIDOC ## Get Model Evaluation ### Description This section explains how to retrieve a specific model evaluation resource for a given model. It can retrieve the first evaluation by default or a specific one using its ID. ### Method `model.get_model_evaluation` ### Parameters - `evaluation_id` (str, optional) - The ID of the specific model evaluation to retrieve. ### Request Example ```python model = aiplatform.Model('projects/my-project/locations/us-central1/models/{MODEL_ID}') # returns the first evaluation with no arguments, you can also pass the evaluation ID evaluation = model.get_model_evaluation() eval_metrics = evaluation.metrics ``` ``` -------------------------------- ### Run a Single Sample Test Source: https://github.com/googleapis/python-aiplatform/blob/main/CONTRIBUTING.rst Navigate to the samples/snippets directory and run a specific sample test by its name using nox. ```bash cd samples/snippets nox -s py-3.9 -- -k ``` -------------------------------- ### Get Skill Revision Source: https://github.com/googleapis/python-aiplatform/blob/main/gemini_docs/README.md Retrieves a specific skill revision by its resource name. ```APIDOC ## Get Skill Revision ### Description Retrieves a specific skill revision by its resource name. ### Method ```python client.skills.revisions.get(name=target_revision_name) ``` ### Parameters #### Path Parameters - **name** (str) - Required - The resource name of the skill revision. ``` -------------------------------- ### Get Skill Revision Source: https://github.com/googleapis/python-aiplatform/blob/main/README.rst Retrieves a specific revision of a skill by its resource name. ```APIDOC ## Get Skill Revision ### Description Fetches a specific revision of a skill using its resource name. ### Method `client.skills.revisions.get()` ### Parameters - **name** (str) - Required - The resource name of the skill revision to retrieve. ### Request Example ```python revision = client.skills.revisions.get(name=target_revision_name) ``` ``` -------------------------------- ### Deploy Model with Explanation Metadata Source: https://github.com/googleapis/python-aiplatform/blob/main/README.rst Use explanation metadata when deploying a model to an endpoint or uploading a model. The metadata is obtained using `builder.get_metadata_protobuf()`. ```Python explanation_metadata = builder.get_metadata_protobuf() # To deploy a model to an endpoint with explanation model.deploy(..., explanation_metadata=explanation_metadata) # To deploy a model to a created endpoint with explanation endpoint.deploy(..., explanation_metadata=explanation_metadata) # To upload a model with explanation aiplatform.Model.upload(..., explanation_metadata=explanation_metadata) ``` -------------------------------- ### Import Vertex AI SDK Source: https://github.com/googleapis/python-aiplatform/blob/main/gemini_docs/README.md Imports the Vertex AI SDK library. This is the first step before initializing the SDK. ```python import vertexai ``` -------------------------------- ### Get Model Evaluation Source: https://github.com/googleapis/python-aiplatform/blob/main/docs/README.rst Retrieves a specific model evaluation resource for a given model. ```APIDOC ## Get Model Evaluation ### Description Retrieves a specific model evaluation resource for a given model. ### Method `Model.get_model_evaluation()` ### Parameters * **evaluation_id** (str) - Optional - The ID of the model evaluation to retrieve. ### Request Example ```python model = aiplatform.Model('projects/my-project/locations/us-central1/models/{MODEL_ID}') evaluation = model.get_model_evaluation() ``` ``` -------------------------------- ### Deploy Model on Spot VM Source: https://github.com/googleapis/python-aiplatform/blob/main/agentplatform/model_garden/README.md Deploy a model on Spot VMs to reduce costs. This is beneficial for cost-sensitive development and batch workloads. ```python endpoint = model.deploy(spot=True) ``` -------------------------------- ### Get Model Evaluation Source: https://github.com/googleapis/python-aiplatform/blob/main/README.rst Retrieve a specific model evaluation resource. Can fetch the first evaluation or by evaluation ID. ```Python model = aiplatform.Model('projects/my-project/locations/us-central1/models/{MODEL_ID}') # returns the first evaluation with no arguments, you can also pass the evaluation ID evaluation = model.get_model_evaluation() eval_metrics = evaluation.metrics ``` -------------------------------- ### Optimize Prompt (Data-Driven) Source: https://github.com/googleapis/python-aiplatform/blob/main/gemini_docs/README.md For data-driven prompt optimization, use the `optimize` method with `vapo_config`. This requires specifying either a service account or project number and a config path. Logging is recommended to monitor progress. ```python import logging project_number = PROJECT_NUMBER # replace with your project number service_account = f"{project_number}-compute@developer.gserviceaccount.com" vapo_config = types.PromptOptimizerVAPOConfig( config_path="gs://your-bucket/config.json", service_account_project_number=project_number, wait_for_completion=False ) # Set up logging to see the progress of the optimization job logging.basicConfig(encoding='utf-8', level=logging.INFO, force=True) result = client.prompt_optimizer.optimize(method="vapo", config=vapo_config) ``` -------------------------------- ### Get Predictions from Endpoint Source: https://github.com/googleapis/python-aiplatform/blob/main/docs/README.rst Sends prediction requests to a deployed model on a Vertex AI Endpoint. Accepts a list of instances for prediction. ```Python endpoint.predict(instances=[[6.7, 3.1, 4.7, 1.5], [4.6, 3.1, 1.5, 0.2]]) ``` -------------------------------- ### Select a Verified Deployment: By Container Image Source: https://github.com/googleapis/python-aiplatform/blob/main/agentplatform/model_garden/README.md Deploys a model using a specified serving container image URI from the verified deployment configurations. ```APIDOC ## Select a Verified Deployment: By Container Image Specify a container image from the list of verified deployment configurations. ### Code Example ```python endpoint = model.deploy( serving_container_image_uri="us-docker.pkg.dev/vertex-ai/vertex-vision-model-garden-dockers/pytorch-vllm-serve:20250430_0916_RC00_maas", ) ``` ``` -------------------------------- ### Initialize Vertex AI SDK Source: https://github.com/googleapis/python-aiplatform/blob/main/gemini_docs/README.md Initializes the Vertex AI SDK with your project ID and location. This must be done before using any other SDK functionalities. ```python vertexai.init(project='my-project', location='us-central1') ``` -------------------------------- ### Get TensorFlow 2 Explainable AI Metadata Source: https://github.com/googleapis/python-aiplatform/blob/main/README.rst Retrieve metadata for TensorFlow 2 models in dictionary format. Uses the `google.cloud.aiplatform.explain.metadata.tf.v2` module. ```Python from google.cloud.aiplatform.explain.metadata.tf.v2 import saved_model_metadata_builder builder = saved_model_metadata_builder.SavedModelMetadataBuilder('gs://python/to/my/model/dir') generated_md = builder.get_metadata() ``` -------------------------------- ### List Model Deployment Configurations Source: https://github.com/googleapis/python-aiplatform/blob/main/vertexai/model_garden/README.md Inspect the available deployment configurations, such as compatible machine types and container images, for a specific model. ```python model = model_garden.OpenModel("google/paligemma@paligemma-224-float32") deploy_options = model.list_deploy_options() ``` -------------------------------- ### Get TensorFlow 1 Explainable AI Metadata Source: https://github.com/googleapis/python-aiplatform/blob/main/README.rst Retrieve metadata for TensorFlow 1 models in dictionary format. Requires the `google.cloud.aiplatform.explain.metadata.tf.v1` module. ```Python from google.cloud.aiplatform.explain.metadata.tf.v1 import saved_model_metadata_builder builder = saved_model_metadata_builder.SavedModelMetadataBuilder( 'gs://python/to/my/model/dir', tags=[tf.saved_model.tag_constants.SERVING] ) generated_md = builder.get_metadata() ``` -------------------------------- ### Deploy Agent to Agent Engine Source: https://github.com/googleapis/python-aiplatform/blob/main/gemini_docs/README.md Initialize Vertex AI and create a remote agent instance. This prepares the agent for deployment to Agent Engine. ```python vertexai.init( project='my-project', location='us-central1', staging_bucket="gs://my-staging-bucket", ) remote_app = vertexai.agent_engines.create( app, requirements=["google-cloud-aiplatform[agent_engines,adk]"], ) ``` -------------------------------- ### Instantiate Vertex AI Client Source: https://github.com/googleapis/python-aiplatform/blob/main/README.rst Instantiate a Vertex AI client for generative AI features. Replace 'my-project' and 'us-central1' with your project ID and location. ```Python import vertexai from vertexai import types # Instantiate GenAI client from Vertex SDK # Replace with your project ID and location client = vertexai.Client(project='my-project', location='us-central1') ``` -------------------------------- ### Update Skill Metadata Source: https://github.com/googleapis/python-aiplatform/blob/main/README.rst Updates an existing skill's metadata or underlying implementation. This example shows updating the display name and description. ```Python # Update skill metadata updated_skill = client.skills.update( name=skill.name, config={ "display_name": "Updated Weather Skill", "description": "Provides localized current weather conditions and multi-day forecasts.", }, ) ``` -------------------------------- ### Test Agent Locally Source: https://github.com/googleapis/python-aiplatform/blob/main/gemini_docs/README.md Test the locally defined ADK Agent by streaming queries. This example queries the exchange rate from USD to SEK. ```python async for event in app.async_stream_query( user_id="user-id", message="What is the exchange rate from US dollars to SEK today?", ): print(event) ``` -------------------------------- ### Run All System Tests Source: https://github.com/googleapis/python-aiplatform/blob/main/CONTRIBUTING.rst Execute all system tests for the project using the nox 'system' session. ```bash nox -s system ``` -------------------------------- ### Create Gen AI Client Source: https://github.com/googleapis/python-aiplatform/blob/main/README.rst Initializes a client for the Gen AI SDK, specifying Vertex AI integration, project, and location. ```Python # Create a Client in the Gen AI SDK genai_client = genai.Client(vertexai=True, project="your-project", location="your-location") # Call generate_content() with the prompt response = genai_client.models.generate_content( model=retrieved_prompt.prompt_data.model, contents=retrieved_prompt.assemble_contents(), ) ``` -------------------------------- ### Read Training Data URIs Source: https://github.com/googleapis/python-aiplatform/blob/main/README.rst Access environment variables to get the format and URIs for training, validation, and test data splits during custom training. ```Python os.environ['AIP_DATA_FORMAT'] # provides format of data os.environ['AIP_TRAINING_DATA_URI'] # uri to training split os.environ['AIP_VALIDATION_DATA_URI'] # uri to validation split os.environ['AIP_TEST_DATA_URI'] # uri to test split ``` -------------------------------- ### Deploy Model with Explanation Metadata Source: https://github.com/googleapis/python-aiplatform/blob/main/docs/README.rst Use explanation metadata when deploying a model to an endpoint or uploading a model to Vertex AI. This enables explainability features for predictions. ```Python explanation_metadata = builder.get_metadata_protobuf() # To deploy a model to an endpoint with explanation model.deploy(..., explanation_metadata=explanation_metadata) # To deploy a model to a created endpoint with explanation endpoint.deploy(..., explanation_metadata=explanation_metadata) # To upload a model with explanation aiplatform.Model.upload(..., explanation_metadata=explanation_metadata) ``` -------------------------------- ### Get a Model Evaluation Source: https://github.com/googleapis/python-aiplatform/blob/main/docs/README.rst Retrieves a specific model evaluation resource for a given Vertex AI Model. Can fetch the first evaluation or one by its ID. ```Python model = aiplatform.Model('projects/my-project/locations/us-central1/models/{MODEL_ID}') # returns the first evaluation with no arguments, you can also pass the evaluation ID evaluation = model.get_model_evaluation() eval_metrics = evaluation.metrics ``` -------------------------------- ### Clone Repository Source: https://github.com/googleapis/python-aiplatform/blob/main/scripts/readme-gen/templates/install_deps.tmpl.rst Clone the python-docs-samples repository from GitHub to your local machine. ```bash $ git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git ``` -------------------------------- ### Generate Content with Prompt (Gen AI SDK) Source: https://github.com/googleapis/python-aiplatform/blob/main/gemini_docs/README.md After creating or retrieving a prompt, use the Gen AI SDK's `generate_content` method. The `assemble_contents()` utility function on Prompt objects converts the prompt into a format suitable for `generate_content`. ```python from google import genai from google.genai import types as genai_types # Create a Client in the Gen AI SDK genai_client = genai.Client(vertexai=True, project="your-project", location="your-location") # Call generate_content() with the prompt response = genai_client.models.generate_content( model=retrieved_prompt.prompt_data.model, contents=retrieved_prompt.assemble_contents(), ) ``` -------------------------------- ### Get Serving Container Specification Source: https://github.com/googleapis/python-aiplatform/blob/main/google/cloud/aiplatform/prediction/README.md Retrieve the serving container specification for the built image using `local_model.get_serving_container_spec()`. This is useful for understanding the container's configuration. ```python local_model.get_serving_container_spec() ``` -------------------------------- ### Spot VM Deployment Source: https://github.com/googleapis/python-aiplatform/blob/main/agentplatform/model_garden/README.md Schedules workloads on Spot VMs for lower cost by setting `spot=True` during deployment. ```APIDOC ## Spot VM Deployment Schedule workloads on Spot VMs for lower cost. ### Code Example ```python endpoint = model.deploy(spot=True) ``` **Use case:** Cost-sensitive development and batch workloads. ``` -------------------------------- ### Get Existing Image Dataset Source: https://github.com/googleapis/python-aiplatform/blob/main/docs/README.rst Retrieve a previously created image dataset using its resource name. Replace '{DATASET_ID}' with the actual dataset ID. ```Python dataset = aiplatform.ImageDataset('projects/my-project/location/us-central1/datasets/{DATASET_ID}') ``` -------------------------------- ### Deploy Model with EULA Acceptance Source: https://github.com/googleapis/python-aiplatform/blob/main/agentplatform/model_garden/README.md Deploy models that require license agreement acceptance by passing `eula=True`. Use this for the first-time deployment of EULA-protected models. ```python model = model_garden.OpenModel("google/gemma2@gemma-2-27b-it") endpoint = model.deploy(eula=True) ``` -------------------------------- ### Get Existing Image Dataset Source: https://github.com/googleapis/python-aiplatform/blob/main/README.rst Retrieve a previously created image dataset using its full resource name, including project, location, and dataset ID. ```Python dataset = aiplatform.ImageDataset('projects/my-project/location/us-central1/datasets/{DATASET_ID}') ``` -------------------------------- ### Get a Vertex AI Model Source: https://github.com/googleapis/python-aiplatform/blob/main/docs/README.rst Retrieves a Vertex AI Model resource using its resource name. This is useful for subsequent operations like deployment or evaluation. ```Python model = aiplatform.Model('/projects/my-project/locations/us-central1/models/{MODEL_ID}') ``` -------------------------------- ### Data-Driven Prompt Optimization with VAPOConfig Source: https://github.com/googleapis/python-aiplatform/blob/main/README.rst Launch a data-driven prompt optimization job using VAPOConfig. This requires a project number and a GCS path to the configuration file. Ensure logging is set up for progress monitoring. ```Python from vertexai import types import logging project_number = PROJECT_NUMBER # replace with your project number service_account = f"{project_number}-compute@developer.gserviceaccount.com" vapo_config = vertexai.types.PromptOptimizerVAPOConfig( config_path="gs://your-bucket/config.json", service_account_project_number=project_number, wait_for_completion=False ) # Set up logging to see the progress of the optimization job logging.basicConfig(encoding='utf-8', level=logging.INFO, force=True) result = client.prompts.launch_optimization_job(method=types.PromptOptimizerMethod.VAPO, config=vapo_config) ``` -------------------------------- ### Create Pipeline Run Source: https://github.com/googleapis/python-aiplatform/blob/main/README.rst Instantiate and run a Vertex AI PipelineJob. Configure display name and caching behavior. ```Python # Instantiate PipelineJob object pl = PipelineJob( display_name="My first pipeline", # Whether or not to enable caching # True = always cache pipeline step result ) ``` -------------------------------- ### Get TensorFlow 2 Explainable AI Metadata Source: https://github.com/googleapis/python-aiplatform/blob/main/docs/README.rst Retrieve metadata in dictionary format from TensorFlow 2 models using SavedModelMetadataBuilder. This version does not require specific tags. ```Python from google.cloud.aiplatform.explain.metadata.tf.v2 import saved_model_metadata_builder builder = saved_model_metadata_builder.SavedModelMetadataBuilder('gs://python/to/my/model/dir') generated_md = builder.get_metadata() ``` -------------------------------- ### Get TensorFlow 1 Explainable AI Metadata Source: https://github.com/googleapis/python-aiplatform/blob/main/docs/README.rst Retrieve metadata in dictionary format from TensorFlow 1 models using SavedModelMetadataBuilder. Ensure the correct tags are provided. ```Python from google.cloud.aiplatform.explain.metadata.tf.v1 import saved_model_metadata_builder builder = saved_model_metadata_builder.SavedModelMetadataBuilder( 'gs://python/to/my/model/dir', tags=[tf.saved_model.tag_constants.SERVING] ) generated_md = builder.get_metadata() ``` -------------------------------- ### Clone and Set Up Local Repository Source: https://github.com/googleapis/python-aiplatform/blob/main/CONTRIBUTING.rst Clone the python-aiplatform repository from your GitHub fork and configure remotes to pull changes from the upstream repository. ```bash cd ${HOME} git clone git@github.com:USERNAME/python-aiplatform.git hack-on-python-aiplatform cd hack-on-python-aiplatform # Configure remotes such that you can pull changes from the googleapis/python-aiplatform # repository into your local repository. git remote add upstream git@github.com:googleapis/python-aiplatform.git # fetch and merge changes from upstream into main git fetch upstream git merge upstream/main ``` -------------------------------- ### Initialize Vertex AI Client Source: https://github.com/googleapis/python-aiplatform/blob/main/gemini_docs/README.md Initialize the Vertex AI client with your project ID and desired location. This client is used to interact with Vertex AI services. ```python client = vertexai.Client(project='my-project', location='us-central1') ``` -------------------------------- ### Deploy Hugging Face Model Source: https://github.com/googleapis/python-aiplatform/blob/main/vertexai/model_garden/README.md Deploy a model directly from Hugging Face using its model ID. This is useful for leveraging community models without custom container setup. ```python model = model_garden.OpenModel("Qwen/Qwen2-1.5B-Instruct") endpoint = model.deploy() ``` -------------------------------- ### Create and Import Text Dataset Source: https://github.com/googleapis/python-aiplatform/blob/main/docs/README.rst Create a text dataset and then import data from a GCS source. Ensure the import schema URI is correctly specified for the dataset type. ```Python from google.cloud import aiplatform my_dataset = aiplatform.TextDataset.create( display_name="my-dataset") my_dataset.import_data( gcs_source=['gs://path/to/my/dataset.csv'], import_schema_uri=aiplatform.schema.dataset.ioformat.text.multi_label_classification ) ``` -------------------------------- ### Define Function for Model Use Source: https://github.com/googleapis/python-aiplatform/blob/main/gemini_docs/README.md Defines a function declaration, including its schema, name, description, and parameters, that a generative model can use to answer questions. This example defines a 'get_current_weather' function. ```python # First, create tools that the model is can use to answer your questions. # Describe a function by specifying it's schema (JsonSchema format) get_current_weather_func = generative_models.FunctionDeclaration( name="get_current_weather", description="Get the current weather in a given location", parameters={ "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA" }, "unit": { "type": "string", "enum": [ "celsius", "fahrenheit", ] } }, "required": [ "location" ] }, ) # Tool is a collection of related functions weather_tool = generative_models.Tool( function_declarations=[get_current_weather_func], ) ``` -------------------------------- ### Deploy Model with Specific Hardware Source: https://github.com/googleapis/python-aiplatform/blob/main/agentplatform/model_garden/README.md Deploy a model using a specified machine type, accelerator type, and count from the verified deployment configurations. ```python endpoints = model.deploy( machine_type="a3-highgpu-1g", accelerator_type="NVIDIA_H100_80GB", accelerator_count=1, ) ``` -------------------------------- ### Create Skill Source: https://github.com/googleapis/python-aiplatform/blob/main/README.rst Demonstrates how to create a new skill in the Skill Registry with optional custom ID. ```APIDOC ## Create Skill ### Description Creates a new skill in the Skill Registry. You can optionally specify a custom string identifier using the `skill_id` configuration parameter. ### Method `client.skills.create()` ### Parameters - **display_name** (str) - Required - The display name for the skill. - **description** (str) - Required - A description of the skill's functionality. - **config** (dict) - Optional - Configuration for the skill, including `local_path` and `skill_id`. - **local_path** (str) - The local path to the skill's implementation. - **skill_id** (str) - A custom string identifier for the skill. ### Request Example ```python skill = client.skills.create( display_name="weather_skill", description="Retrieves the weather for a given location", config={ "local_path": "./weather_skill_dir", "skill_id": "my-custom-weather-skill", }, ) ``` ``` -------------------------------- ### Get Existing Skill by Resource Name Source: https://github.com/googleapis/python-aiplatform/blob/main/gemini_docs/README.md Retrieve an existing skill using its full resource name, which follows the format 'projects/{project_id_or_number}/locations/{location}/skills/{skill_id}'. The `skill.name` attribute from a `create()` call provides this format directly. ```python # The `name` parameter must be a string in the following format: # 'projects/{project_id_or_number}/locations/{location}/skills/{skill_id}' # # Examples: # 'projects/my-project/locations/us-central1/skills/1234567890' # 'projects/my-project/locations/us-central1/skills/my-custom-weather-skill' # # Note: If you have a `Skill` object (e.g., from a `create()` call), # `skill.name` already contains the resource name in the correct format. fetched_skill = client.skills.get(name=skill.name) ```