Vertex AI SDK for Python¶
Gemini API and Generative AI on Vertex AI¶
Note
For Gemini API and Generative AI on Vertex AI, please reference Vertex Generative AI SDK for Python
Vertex AI: Google Vertex AI is an integrated suite of machine learning tools and services for building and using ML models with AutoML or custom code. It offers both novices and experts the best workbench for the entire machine learning development lifecycle.
Quick Start¶
In order to use this library, you first need to go through the following steps:
Installation¶
Install this library in a virtualenv using pip. virtualenv is a tool to create isolated Python environments. The basic problem it addresses is one of dependencies and versions, and indirectly permissions.
With virtualenv, it’s possible to install this library without needing system install permissions, and without clashing with the installed system dependencies.
Mac/Linux¶
pip install virtualenv
virtualenv <your-env>
source <your-env>/bin/activate
<your-env>/bin/pip install google-cloud-aiplatform
Windows¶
pip install virtualenv
virtualenv <your-env>
<your-env>\Scripts\activate
<your-env>\Scripts\pip.exe install google-cloud-aiplatform
Supported Python Versions¶
Python >= 3.8
Deprecated Python Versions¶
Python <= 3.7.
The last version of this library compatible with Python 3.6 is google-cloud-aiplatform==1.12.1.
Overview¶
This section provides a brief overview of the Vertex AI SDK for Python. You can also reference the notebooks in vertex-ai-samples for examples.
All publicly available SDK features can be found in the google/cloud/aiplatform
directory.
Under the hood, Vertex SDK builds on top of GAPIC, which stands for Google API CodeGen.
The GAPIC library code sits in google/cloud/aiplatform_v1
and google/cloud/aiplatform_v1beta1
,
and it is auto-generated from Google’s service proto files.
For most developers’ programmatic needs, they can follow these steps to figure out which libraries to import:
Look through
google/cloud/aiplatform
first – Vertex SDK’s APIs will almost always be easier to use and more concise comparing with GAPICIf the feature that you are looking for cannot be found there, look through
aiplatform_v1
to see if it’s available in GAPICIf it is still in beta phase, it will be available in
aiplatform_v1beta1
If none of the above scenarios could help you find the right tools for your task, please feel free to open a github issue and send us a feature request.
Importing¶
Vertex AI SDK resource based functionality can be used by importing the following namespace:
from google.cloud import aiplatform
Initialization¶
Initialize the SDK to store common configurations that you use with the SDK.
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'
)
Datasets¶
Vertex AI provides managed tabular, text, image, and video datasets. In the SDK, datasets can be used downstream to train models.
To create a tabular dataset:
my_dataset = aiplatform.TabularDataset.create(
display_name="my-dataset", gcs_source=['gs://path/to/my/dataset.csv'])
You can also create and import a dataset in separate steps:
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
)
To get a previously created Dataset:
dataset = aiplatform.ImageDataset('projects/my-project/location/us-central1/datasets/{DATASET_ID}')
Vertex AI supports a variety of dataset schemas. References to these schemas are available under the
aiplatform.schema.dataset
namespace. For more information on the supported dataset schemas please refer to the
Preparing data docs.
Training¶
The Vertex AI SDK for Python allows you train Custom and AutoML Models.
You can train custom models using a custom Python script, custom Python package, or container.
Preparing Your Custom Code
Vertex AI custom training enables you to train on Vertex AI datasets and produce Vertex AI models. To do so your script must adhere to the following contract:
It must read datasets from the environment variables populated by the training service:
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
Please visit Using a managed dataset in a custom training application for a detailed overview.
It must write the model artifact to the environment variable populated by the training service:
os.environ['AIP_MODEL_DIR']
Running Training
job = aiplatform.CustomTrainingJob(
display_name="my-training-job",
script_path="training_script.py",
container_uri="us-docker.pkg.dev/vertex-ai/training/tf-cpu.2-2:latest",
requirements=["gcsfs==0.7.1"],
model_serving_container_image_uri="us-docker.pkg.dev/vertex-ai/prediction/tf2-cpu.2-2:latest",
)
model = job.run(my_dataset,
replica_count=1,
machine_type="n1-standard-4",
accelerator_type='NVIDIA_TESLA_K80',
accelerator_count=1)
In the code block above my_dataset is managed dataset created in the Dataset section above. The model variable is a managed Vertex AI model that can be deployed or exported.
AutoMLs¶
The Vertex AI SDK for Python supports AutoML tabular, image, text, video, and forecasting.
To train an AutoML tabular model:
dataset = aiplatform.TabularDataset('projects/my-project/location/us-central1/datasets/{DATASET_ID}')
job = aiplatform.AutoMLTabularTrainingJob(
display_name="train-automl",
optimization_prediction_type="regression",
optimization_objective="minimize-rmse",
)
model = job.run(
dataset=dataset,
target_column="target_column_name",
training_fraction_split=0.6,
validation_fraction_split=0.2,
test_fraction_split=0.2,
budget_milli_node_hours=1000,
model_display_name="my-automl-model",
disable_early_stopping=False,
)
Models¶
To get a model:
model = aiplatform.Model('/projects/my-project/locations/us-central1/models/{MODEL_ID}')
To upload a model:
model = aiplatform.Model.upload(
display_name='my-model',
artifact_uri="gs://python/to/my/model/dir",
serving_container_image_uri="us-docker.pkg.dev/vertex-ai/prediction/tf2-cpu.2-2:latest",
)
To deploy a model:
endpoint = model.deploy(machine_type="n1-standard-4",
min_replica_count=1,
max_replica_count=5
machine_type='n1-standard-4',
accelerator_type='NVIDIA_TESLA_K80',
accelerator_count=1)
Please visit Importing models to Vertex AI for a detailed overview:
Model Evaluation¶
The Vertex AI SDK for Python currently supports getting model evaluation metrics for all AutoML models.
To list all model evaluations for a model:
model = aiplatform.Model('projects/my-project/locations/us-central1/models/{MODEL_ID}')
evaluations = model.list_model_evaluations()
To get the model evaluation resource for a given model:
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
You can also create a reference to your model evaluation directly by passing in the resource name of the model evaluation:
evaluation = aiplatform.ModelEvaluation(
evaluation_name='projects/my-project/locations/us-central1/models/{MODEL_ID}/evaluations/{EVALUATION_ID}')
Alternatively, you can create a reference to your evaluation by passing in the model and evaluation IDs:
evaluation = aiplatform.ModelEvaluation(
evaluation_name={EVALUATION_ID},
model_id={MODEL_ID})
Batch Prediction¶
To create a batch prediction job:
model = aiplatform.Model('/projects/my-project/locations/us-central1/models/{MODEL_ID}')
batch_prediction_job = model.batch_predict(
job_display_name='my-batch-prediction-job',
instances_format='csv',
machine_type='n1-standard-4',
gcs_source=['gs://path/to/my/file.csv'],
gcs_destination_prefix='gs://path/to/my/batch_prediction/results/',
service_account='my-sa@my-project.iam.gserviceaccount.com'
)
You can also create a batch prediction job asynchronously by including the sync=False argument:
batch_prediction_job = model.batch_predict(..., sync=False)
# wait for resource to be created
batch_prediction_job.wait_for_resource_creation()
# get the state
batch_prediction_job.state
# block until job is complete
batch_prediction_job.wait()
Endpoints¶
To create an endpoint:
endpoint = aiplatform.Endpoint.create(display_name='my-endpoint')
To deploy a model to a created endpoint:
model = aiplatform.Model('/projects/my-project/locations/us-central1/models/{MODEL_ID}')
endpoint.deploy(model,
min_replica_count=1,
max_replica_count=5,
machine_type='n1-standard-4',
accelerator_type='NVIDIA_TESLA_K80',
accelerator_count=1)
To get predictions from endpoints:
endpoint.predict(instances=[[6.7, 3.1, 4.7, 1.5], [4.6, 3.1, 1.5, 0.2]])
To undeploy models from an endpoint:
endpoint.undeploy_all()
To delete an endpoint:
endpoint.delete()
Pipelines¶
To create a Vertex AI Pipeline run and monitor until completion:
# Instantiate PipelineJob object
pl = PipelineJob(
display_name="My first pipeline",
# Whether or not to enable caching
# True = always cache pipeline step result
# False = never cache pipeline step result
# None = defer to cache option for each pipeline component in the pipeline definition
enable_caching=False,
# Local or GCS path to a compiled pipeline definition
template_path="pipeline.json",
# Dictionary containing input parameters for your pipeline
parameter_values=parameter_values,
# GCS path to act as the pipeline root
pipeline_root=pipeline_root,
)
# Execute pipeline in Vertex AI and monitor until completion
pl.run(
# Email address of service account to use for the pipeline run
# You must have iam.serviceAccounts.actAs permission on the service account to use it
service_account=service_account,
# Whether this function call should be synchronous (wait for pipeline run to finish before terminating)
# or asynchronous (return immediately)
sync=True
)
To create a Vertex AI Pipeline without monitoring until completion, use submit instead of run:
# Instantiate PipelineJob object
pl = PipelineJob(
display_name="My first pipeline",
# Whether or not to enable caching
# True = always cache pipeline step result
# False = never cache pipeline step result
# None = defer to cache option for each pipeline component in the pipeline definition
enable_caching=False,
# Local or GCS path to a compiled pipeline definition
template_path="pipeline.json",
# Dictionary containing input parameters for your pipeline
parameter_values=parameter_values,
# GCS path to act as the pipeline root
pipeline_root=pipeline_root,
)
# Submit the Pipeline to Vertex AI
pl.submit(
# Email address of service account to use for the pipeline run
# You must have iam.serviceAccounts.actAs permission on the service account to use it
service_account=service_account,
)
Explainable AI: Get Metadata¶
To get metadata in dictionary format from TensorFlow 1 models:
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()
To get metadata in dictionary format from TensorFlow 2 models:
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()
To use Explanation Metadata in endpoint deployment and model upload:
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)
Cloud Profiler¶
Cloud Profiler allows you to profile your remote Vertex AI Training jobs on demand and visualize the results in Vertex AI Tensorboard.
To start using the profiler with TensorFlow, update your training script to include the following:
from google.cloud.aiplatform.training_utils import cloud_profiler
...
cloud_profiler.init()
Next, run the job with with a Vertex AI TensorBoard instance. For full details on how to do this, visit https://cloud.google.com/vertex-ai/docs/experiments/tensorboard-overview
Finally, visit your TensorBoard in your Google Cloud Console, navigate to the “Profile” tab, and click the Capture Profile button. This will allow users to capture profiling statistics for the running jobs.
Next Steps¶
Read the Client Library Documentation for Vertex AI API to see other available methods on the client.
Read the Vertex AI API Product documentation to learn more about the product and see How-to Guides.
View this README to see the full list of Cloud APIs that we cover.
Note
Because this client uses grpc
library, it is safe to
share instances across threads. In multiprocessing scenarios, the best
practice is to create client instances after the invocation of
os.fork()
by multiprocessing.pool.Pool
or
multiprocessing.Process
.
API Reference¶
- Google Cloud Aiplatform SDK
- Types for Google Cloud Aiplatform SDK API
- Types for Google Cloud Aiplatform V1 Schema Predict Instance v1 API
- Types for Google Cloud Aiplatform V1beta1 Schema Predict Instance v1beta1 API
- Types for Google Cloud Aiplatform V1 Schema Predict Params v1 API
- Types for Google Cloud Aiplatform V1beta1 Schema Predict Params v1beta1 API
- Types for Google Cloud Aiplatform V1 Schema Predict Prediction v1 API
- Types for Google Cloud Aiplatform V1beta1 Schema Predict Prediction v1beta1 API
- Types for Google Cloud Aiplatform V1 Schema Trainingjob Definition v1 API
- Types for Google Cloud Aiplatform V1beta1 Schema Trainingjob Definition v1beta1 API
- Custom Prediction Routine and Local Predict
- Services for Google Cloud Aiplatform v1 API
- DatasetService
- EndpointService
- FeaturestoreOnlineServingService
- FeaturestoreService
- IndexEndpointService
- IndexService
- JobService
- MatchService
- MetadataService
- MigrationService
- ModelGardenService
- ModelService
- PipelineService
- PredictionService
- ScheduleService
- SpecialistPoolService
- TensorboardService
- VizierService
- Types for Google Cloud Aiplatform v1 API
- Services for Google Cloud Aiplatform v1beta1 API
- DatasetService
- DeploymentResourcePoolService
- EndpointService
- FeatureOnlineStoreAdminService
- FeatureOnlineStoreService
- FeatureRegistryService
- FeaturestoreOnlineServingService
- FeaturestoreService
- IndexEndpointService
- IndexService
- JobService
- MatchService
- MetadataService
- MigrationService
- ModelGardenService
- ModelService
- PersistentResourceService
- PipelineService
- PredictionService
- ScheduleService
- SpecialistPoolService
- TensorboardService
- VizierService
- Types for Google Cloud Aiplatform v1beta1 API
Changelog¶
For a list of all google-cloud-aiplatform
releases:
- Changelog
- 1.73.0 (2024-11-19)
- 1.72.0 (2024-11-12)
- 1.71.1 (2024-10-31)
- 1.71.0 (2024-10-29)
- 1.70.0 (2024-10-08)
- 1.69.0 (2024-10-01)
- 1.68.0 (2024-09-24)
- 1.67.1 (2024-09-18)
- 1.67.0 (2024-09-17)
- 1.66.0 (2024-09-11)
- 1.65.0 (2024-09-04)
- 1.64.0 (2024-08-27)
- 1.63.0 (2024-08-20)
- 1.62.0 (2024-08-13)
- 1.61.0 (2024-08-05)
- 1.60.0 (2024-07-24)
- 1.59.0 (2024-07-09)
- 1.58.0 (2024-07-03)
- 1.57.0 (2024-06-26)
- 1.56.0 (2024-06-18)
- 1.55.0 (2024-06-12)
- 1.54.1 (2024-06-07)
- 1.54.0 (2024-06-06)
- 1.53.0 (2024-05-30)
- 1.52.0 (2024-05-21)
- 1.51.0 (2024-05-10)
- 1.50.0 (2024-05-02)
- 1.49.0 (2024-04-27)
- 1.48.0 (2024-04-17)
- 1.47.0 (2024-04-06)
- 1.46.0 (2024-03-30)
- 1.45.0 (2024-03-28)
- 1.44.0 (2024-03-14)
- 1.43.0 (2024-02-29)
- 1.42.1 (2024-02-15)
- 1.42.0 (2024-02-15)
- 1.41.0 (2024-02-05)
- 1.40.0 (2024-01-24)
- 1.39.0 (2024-01-05)
- 1.38.1 (2023-12-13)
- 1.38.0 (2023-12-11)
- 1.37.0 (2023-12-05)
- 1.36.4 (2023-11-16)
- 1.36.3 (2023-11-14)
- 1.36.2 (2023-11-10)
- 1.36.1 (2023-11-07)
- 1.36.0 (2023-10-31)
- 1.35.0 (2023-10-10)
- 1.34.0 (2023-10-02)
- 1.33.1 (2023-09-20)
- 1.33.0 (2023-09-18)
- 1.32.0 (2023-09-05)
- 1.31.1 (2023-08-24)
- 1.31.0 (2023-08-21)
- 1.30.1 (2023-08-11)
- 1.30.0 (2023-08-10)
- 1.29.0 (2023-08-02)
- 1.28.1 (2023-07-18)
- 1.28.0 (2023-07-08)
- 1.27.1 (2023-07-06)
- 1.27.0 (2023-06-30)
- 1.26.1 (2023-06-21)
- 1.26.0 (2023-06-07)
- 1.25.0 (2023-05-09)
- 1.24.1 (2023-04-21)
- 1.24.0 (2023-04-12)
- 1.23.0 (2023-03-15)
- 1.22.1 (2023-02-28)
- 1.22.0 (2023-02-16)
- 1.21.0 (2023-01-13)
- 1.20.0 (2022-12-15)
- 1.19.1 (2022-12-08)
- 1.19.0 (2022-11-17)
- 1.18.3 (2022-11-01)
- 1.18.3 (2022-10-31)
- 1.18.2 (2022-10-20)
- 1.18.1 (2022-10-10)
- 1.18.0 (2022-10-03)
- 1.17.1 (2022-09-15)
- 1.17.0 (2022-09-07)
- 1.16.1 (2022-08-02)
- 1.16.0 (2022-07-27)
- 1.15.1 (2022-07-18)
- 1.15.0 (2022-06-29)
- 1.14.0 (2022-06-08)
- 1.13.1 (2022-05-26)
- 1.13.0 (2022-05-09)
- 1.12.1 (2022-04-20)
- 1.12.0 (2022-04-07)
- 1.11.0 (2022-03-03)
- 1.10.0 (2022-02-07)
- 1.9.0 (2021-12-29)
- 1.8.1 (2021-12-14)
- 1.8.0 (2021-12-03)
- 1.7.1 (2021-11-16)
- 1.7.0 (2021-11-06)
- 1.6.2 (2021-11-01)
- 1.6.1 (2021-10-25)
- 1.6.0 (2021-10-12)
- 1.5.0 (2021-09-30)
- 1.4.3 (2021-09-17)
- 1.4.2 (2021-09-10)
- 1.4.1 (2021-09-07)
- 1.4.0 (2021-08-30)
- 1.3.0 (2021-07-30)
- 1.2.0 (2021-07-14)
- 1.1.1 (2021-06-22)
- 1.1.0 (2021-06-17)
- 1.0.1 (2021-05-21)
- 1.0.0 (2021-05-19)
- 0.9.0 (2021-05-17)
- 0.8.0 (2021-05-11)
- 0.7.1 (2021-04-14)
- 0.7.0 (2021-04-14)
- 0.6.0 (2021-03-22)
- 0.5.1 (2021-03-01)
- 0.5.0 (2021-02-17)
- 0.4.0 (2021-01-08)
- 0.3.1 (2020-11-13)
- 0.3.0 (2020-11-05)