You're about to embark on an exciting journey into the world of AI with Google Cloud's Vertex AI! If you've been curious about building and deploying machine learning models, or even diving into the latest generative AI capabilities, all from the comfort of your Python environment, then you've come to the right place. This comprehensive guide will walk you through everything you need to know to get started and leverage the power of Vertex AI with Python.
Ready to unlock the full potential of your AI projects? Let's begin!
How to Use Vertex AI in Python: A Step-by-Step Guide
Vertex AI is Google Cloud's unified machine learning platform, designed to simplify the entire MLOps lifecycle. From data preparation and model training to deployment and monitoring, Vertex AI provides a suite of tools and services that integrate seamlessly with Python.
How To Use Vertex Ai In Python |
Step 1: Setting Up Your Google Cloud Project and Environment
Before we can start writing any Python code, we need to ensure our Google Cloud Project is properly configured and our local development environment is ready.
1.1 Create a Google Cloud Project (If You Don't Have One)
The very first thing you'll need is a Google Cloud Project. If you're new to Google Cloud, you can get started with a free trial that often includes $300 in credits, which is more than enough to explore Vertex AI's capabilities.
Go to the
.Google Cloud Console Click on the project dropdown at the top and select "New Project".
Give your project a meaningful name and click "Create".
1.2 Enable Necessary APIs
Vertex AI relies on several Google Cloud APIs. You'll need to enable them in your project.
In the Google Cloud Console, navigate to "APIs & Services" > "Enabled APIs & Services".
Click "+ Enable APIs and Services".
Search for and enable the following APIs:
Vertex AI API
Compute Engine API (often enabled by default, but good to check)
If you plan to use BigQuery datasets, enable the BigQuery API as well.
1.3 Install and Configure Google Cloud CLI (gcloud)
The gcloud
CLI is essential for authenticating your local environment with Google Cloud and managing your project resources.
Download and install the Google Cloud CLI from the official documentation:
https://cloud.google.com/sdk/docs/install After installation, open your terminal or command prompt and run:
Bashgcloud init
This command will guide you through authenticating with your Google account and selecting your newly created Google Cloud project.
Next, set up application default credentials, which Python libraries will use for authentication:
Bashgcloud auth application-default login
This will open a browser window to complete the authentication process.
1.4 Set Up Your Python Environment
It's highly recommended to use a virtual environment for your Python projects to manage dependencies cleanly.
Create a virtual environment:
Bashpython -m venv vertex_env
Activate the virtual environment:
On Windows:
Bash.\vertex_env\Scripts\activate
On macOS/Linux:
Bashsource vertex_env/bin/activate
You should see
(vertex_env)
prepended to your terminal prompt, indicating the virtual environment is active.Install the Vertex AI SDK for Python:
Bashpip install --upgrade google-cloud-aiplatform
This package includes both the Vertex AI SDK and the underlying client library, giving you access to high-level functionalities and more granular control when needed.
Step 2: Initializing Vertex AI and Project Configuration
QuickTip: Repetition reinforces learning.
Now that your environment is set up, let's write some Python code to initialize the Vertex AI SDK and configure it with your project details.
import vertexai
from google.cloud import aiplatform # The underlying client library
# Replace with your actual Google Cloud Project ID and desired region
PROJECT_ID = "your-gcp-project-id"
REGION = "us-central1" # Or your preferred region like 'europe-west4'
# Initialize Vertex AI
# This connects your Python code to your Vertex AI project in the specified region.
vertexai.init(project=PROJECT_ID, location=REGION)
print(f"Vertex AI initialized for project: {PROJECT_ID} in region: {REGION}")
Remember to replace your-gcp-project-id
with your actual Google Cloud Project ID! You can find this in your Google Cloud Console dashboard.
Step 3: Working with Generative AI (Gemini Models)
One of the most exciting aspects of Vertex AI is access to Google's powerful generative AI models, including the Gemini family. Let's see how to interact with them for text generation.
3.1 Basic Text Generation
from vertexai.generative_models import GenerativeModel, Part
# Load a Gemini model (e.g., gemini-pro)
model = GenerativeModel("gemini-pro")
# Define your prompt
prompt = "Write a short, creative story about a robot who discovers a love for painting."
# Generate content
response = model.generate_content(prompt)
print("--- Generated Story ---")
print(response.text)
print("-----------------------")
The generate_content
method is your gateway to interacting with these models. You can experiment with various prompts to see the model's capabilities.
3.2 Chat Conversations with Gemini
Generative models often shine in conversational contexts. The SDK makes it easy to maintain a chat history.
from vertexai.generative_models import GenerativeModel
model = GenerativeModel("gemini-pro")
chat = model.start_chat()
print("--- Chat with Gemini ---")
while True:
user_message = input("You: ")
if user_message.lower() in ["exit", "quit"]:
break
response = chat.send_message(user_message)
print(f"Gemini: {response.text}")
print("------------------------")
Notice how chat.send_message()
maintains the context of the conversation, allowing for more natural and coherent interactions.
3.3 Multimodal Inputs (Text and Images)
Gemini models can handle multimodal inputs. Let's send an image along with text.
To run this example, you'll need an image file and a Google Cloud Storage bucket to host it.
Upload an image to a Cloud Storage bucket. For example, let's say you upload
my_image.jpg
togs://your-bucket-name/my_image.jpg
.Ensure the service account running your code has permission to read from this bucket.
from vertexai.generative_models import GenerativeModel, Part
import vertexai
# Assuming your image is in a GCS bucket
GCS_IMAGE_URI = "gs://cloud-samples-data/generative-ai/image/scones.jpg" # Example public image
# Or replace with your own: "gs://your-bucket-name/your_image.jpg"
model = GenerativeModel("gemini-pro-vision") # Use the vision model for image inputs
image_part = Part.from_uri(GCS_IMAGE_URI, mime_type="image/jpeg")
text_part = Part.from_text("What is in this image and what are some key details?")
# Generate content with both text and image
response = model.generate_content([image_part, text_part])
print("--- Multimodal Response ---")
print(response.text)
print("-------------------------")
The Part.from_uri
method is key here, allowing you to reference images (and other media types) stored in Google Cloud Storage.
Step 4: Automating Machine Learning Workflows with AutoML
Vertex AI's AutoML capabilities allow you to train high-quality models with minimal code, perfect for those without deep ML expertise or for quickly benchmarking model performance. This section will focus on a conceptual step-by-step for AutoML, as a full example requires a dataset setup.
Tip: Take notes for easier recall later.
4.1 Prepare Your Dataset
Vertex AI datasets are a core component for AutoML. You'll need your data in a suitable format (e.g., CSV for tabular, JSONL for text, or organized directories for images) and uploaded to a Google Cloud Storage bucket.
# Conceptual: Upload your data to Google Cloud Storage
# Example for a CSV file:
# from google.cloud import storage
# client = storage.Client()
# bucket = client.bucket("your-bucket-name")
# blob = bucket.blob("your_data.csv")
# blob.upload_from_filename("local_data.csv")
# GCS_DATA_URI = "gs://your-bucket-name/your_data.csv"
4.2 Create a Vertex AI Dataset
from google.cloud import aiplatform
# Replace with your actual GCS data URI
GCS_DATA_URI = "gs://cloud-samples-data/ai-platform-unified/datasets/tabular/petfinder-mini.csv"
# Create a tabular dataset (adjust for image/text/video as needed)
dataset = aiplatform.TabularDataset.create(
display_name="my-automl-tabular-dataset",
gcs_source=[GCS_DATA_URI],
)
print(f"Dataset created: {dataset.resource_name}")
The TabularDataset.create()
method initiates the dataset creation process. Similar methods exist for ImageDataset
, TextDataset
, and VideoDataset
.
4.3 Train an AutoML Model
Once your dataset is ready, you can kick off an AutoML training job.
from google.cloud import aiplatform
# Assuming 'dataset' was created in the previous step
# For tabular classification, specify the target column
TARGET_COLUMN = "AdoptionSpeed" # Replace with your target column
# Define the training job
job = aiplatform.AutoMLTabularTrainingJob(
display_name="my-automl-classification-job",
optimization_prediction_type="classification", # or "regression"
column_transformations=[
# Define transformations for your columns if necessary
# Example: aiplatform.ColumnTransformation(name="feature_col_1", type="numeric"),
],
)
# Run the training job
# The budget_milli_node_hours determines how long AutoML will train.
# Higher budget usually means better model, but also higher cost.
model = job.run(
dataset=dataset,
target_column=TARGET_COLUMN,
training_fraction_split=0.8, # 80% for training
validation_fraction_split=0.1, # 10% for validation
test_fraction_split=0.1, # 10% for testing
budget_milli_node_hours=1000, # 1 node hour
model_display_name="my-automl-model",
)
print(f"AutoML training job started. Model resource name: {model.resource_name}")
# This is an asynchronous operation, the model will be available after training completes.
AutoML will automatically preprocess your data, try various model architectures, and tune hyperparameters to find the best performing model within your specified budget.
Step 5: Deploying and Getting Predictions from Models
After training, you'll want to deploy your model to an endpoint to get online predictions (low-latency, real-time inferences) or run batch predictions (for large datasets).
5.1 Deploying an AutoML Model to an Endpoint
from google.cloud import aiplatform
# Assuming 'model' was trained in the previous step
# Or load an existing model:
# model = aiplatform.Model(model_name="projects/PROJECT_ID/locations/REGION/models/MODEL_ID")
# Deploy the model to an endpoint
endpoint = model.deploy(
deployed_model_display_name="my-automl-endpoint",
machine_type="n1-standard-2", # Choose an appropriate machine type
min_replica_count=1,
max_replica_count=1,
sync=True, # Wait for deployment to complete
)
print(f"Model deployed to endpoint: {endpoint.resource_name}")
Deployment can take several minutes. The sync=True
argument ensures your script waits until the deployment is complete before proceeding.
5.2 Getting Online Predictions
Once deployed, you can send data to your endpoint for real-time predictions.
from google.cloud import aiplatform
# Assuming 'endpoint' was deployed in the previous step
# Or load an existing endpoint:
# endpoint = aiplatform.Endpoint(endpoint_name="projects/PROJECT_ID/locations/REGION/endpoints/ENDPOINT_ID")
# Prepare your instance for prediction (format depends on your model's input)
# For tabular data, this is typically a list of dictionaries, one per instance
instances = [
{"feature_col_1": 10, "feature_col_2": "A", "feature_col_3": 5.5},
{"feature_col_1": 12, "feature_col_2": "B", "feature_col_3": 6.0},
]
# Get predictions
predictions = endpoint.predict(instances=instances)
print("--- Online Predictions ---")
for prediction in predictions.predictions:
print(prediction)
print("------------------------")
The format of instances
is crucial and depends on the type of data your model was trained on. Refer to the Vertex AI documentation for specific input formats for different model types.
5.3 Performing Batch Predictions
QuickTip: Break down long paragraphs into main ideas.
For large datasets where low latency isn't critical, batch predictions are more cost-effective.
from google.cloud import aiplatform
# Assuming 'model' is your trained model, and you have input data in GCS
# Input data should be in a format like CSV, JSONL, or TFRecord
GCS_BATCH_INPUT_URI = "gs://your-bucket-name/batch_input.csv"
GCS_BATCH_OUTPUT_URI = "gs://your-bucket-name/batch_output_predictions/"
# Create and run a batch prediction job
batch_prediction_job = aiplatform.BatchPredictionJob.create(
job_display_name="my-batch-prediction-job",
model_name=model.resource_name, # Use the resource name of your trained model
instances_format="csv", # Or "jsonl", "tf-record"
predictions_format="csv", # Or "jsonl", "bigquery"
gcs_source=[GCS_BATCH_INPUT_URI],
gcs_destination_prefix=GCS_BATCH_OUTPUT_URI,
# machine_type="n1-standard-4", # Optional: Specify machine type for custom models
# starting_replica_count=1, # Optional: Number of machines for batch prediction
# max_replica_count=5, # Optional
)
batch_prediction_job.wait_for_resource_creation()
print(f"Batch prediction job created: {batch_prediction_job.resource_name}")
print("Monitor job status in Google Cloud Console.")
# Once complete, predictions will be in GCS_BATCH_OUTPUT_URI
Batch prediction jobs are asynchronous, meaning they run in the background. You'll typically monitor their progress in the Google Cloud Console or programmatically.
Step 6: Cleaning Up Resources
It's crucial to clean up your Vertex AI resources to avoid incurring unnecessary costs, especially after experimenting.
# Undeploy the model from the endpoint
if 'endpoint' in locals() and endpoint.resource_name:
print(f"Undeploying model from endpoint: {endpoint.resource_name}...")
endpoint.undeploy_all()
endpoint.delete()
print("Endpoint undeployed and deleted.")
# Delete the trained model (optional, if you don't need it anymore)
if 'model' in locals() and model.resource_name:
print(f"Deleting model: {model.resource_name}...")
model.delete()
print("Model deleted.")
# Delete the dataset (optional)
if 'dataset' in locals() and dataset.resource_name:
print(f"Deleting dataset: {dataset.resource_name}...")
dataset.delete()
print("Dataset deleted.")
# For batch prediction jobs, they usually clean up automatically after completion,
# but you can delete the job resource if needed.
# if 'batch_prediction_job' in locals() and batch_prediction_job.resource_name:
# print(f"Deleting batch prediction job: {batch_prediction_job.resource_name}...")
# batch_prediction_job.delete()
# print("Batch prediction job deleted.")
print("Cleanup complete!")
Always exercise caution when deleting resources, ensuring you don't remove anything critical for ongoing projects.
Further Exploration: Custom Training and MLOps Tools
While this guide focused on getting started, Vertex AI offers much more:
Custom Training: If AutoML doesn't meet your needs, you can run your own custom training code (TensorFlow, PyTorch, scikit-learn, etc.) on Vertex AI's managed infrastructure. You can define custom training jobs, use pre-built containers, or even bring your own Docker images.
Vertex AI Pipelines: For complex, multi-step ML workflows, Vertex AI Pipelines allow you to orchestrate your entire MLOps process, from data ingestion to model deployment and monitoring, using Kubeflow Pipelines.
Vertex AI Workbench: A fully managed Jupyter notebook environment that integrates seamlessly with other Vertex AI services.
Vertex AI Feature Store: For managing and serving ML features at scale.
Vertex AI Model Monitoring: To detect drift and skew in your deployed models.
Vertex AI Vizier: A black-box optimization service for hyperparameter tuning.
10 Related FAQ Questions
How to install the Vertex AI Python SDK?
You can install the Vertex AI Python SDK using pip: pip install --upgrade google-cloud-aiplatform
.
How to authenticate to Vertex AI from a local machine?
Authenticate by running gcloud auth application-default login
in your terminal after installing and initializing the Google Cloud CLI.
How to use Vertex AI for text generation with Python?
Import GenerativeModel
from vertexai.generative_models
, instantiate a model like "gemini-pro"
, and call model.generate_content("your prompt")
.
Tip: Don’t skip the details — they matter.
How to handle multimodal inputs in Vertex AI with Python?
For multimodal inputs (like text and images), use the GenerativeModel
with a Part.from_uri()
for image references, ensuring the model supports vision (e.g., "gemini-pro-vision"
).
How to create a dataset in Vertex AI using Python?
Use aiplatform.TabularDataset.create()
, aiplatform.ImageDataset.create()
, etc., specifying the GCS URI of your data.
How to train an AutoML model for tabular data in Python?
After creating a TabularDataset
, use aiplatform.AutoMLTabularTrainingJob
to define and run your training job, specifying the target column and budget.
How to deploy a model to an endpoint for online predictions in Vertex AI?
Call the .deploy()
method on your trained aiplatform.Model
object, specifying machine_type
, min_replica_count
, and max_replica_count
.
How to get predictions from a deployed Vertex AI endpoint?
After deploying, use the endpoint.predict(instances=...)
method, passing your input data as a list of dictionaries or other appropriate format.
How to perform batch predictions in Vertex AI using Python?
Use aiplatform.BatchPredictionJob.create()
with your model's resource name and the GCS URIs for input and output data.
How to clean up Vertex AI resources using Python?
Call the .undeploy_all()
and .delete()
methods on your Endpoint
objects, and then the .delete()
method on your Model
and Dataset
objects.
This page may contain affiliate links — we may earn a small commission at no extra cost to you.
💡 Breath fresh Air with this Air Purifier with washable filter.