Here's how to get your credentials set up so your agent can run on the Vertex AI engine:
1. Set Up Application Default Credentials (ADC)
The easiest and most recommended way to set up ADC for local development is by using the gcloud CLI.
Steps:
Install Google Cloud SDK: If you haven't already, install the Google Cloud SDK. Follow the instructions here: https://cloud.google.com/sdk/docs/install
Initialize the gcloud CLI:
Bash
gcloud init
This command will guide you through setting up your default project and zone/region. Make sure to select the Google Cloud project where your Vertex AI resources are located.
Authenticate Application Default Credentials:
Bash
gcloud auth application-default login
This command will open a web browser, prompt you to log in with your Google account, and grant access to the Google Cloud SDK. Once authorized, it stores your credentials in a well-known location on your local file system (~/.config/gcloud/application_default_credentials.json on Linux/macOS, or %APPDATA%\gcloud\application_default_credentials.json on Windows).
These are the credentials that your Python application (and the vertexai library) will automatically pick up.
2. Verify Your Project Configuration
Ensure that your code is configured to use the correct Google Cloud project ID. While ADC will pick up credentials, you often need to explicitly tell Vertex AI which project to operate within.
You likely have a config.py file or similar where you define your Google Cloud project ID and region. Make sure these are accurate.
Example (from config.py or similar):
Python
# config.py
class Config:
PROJECT_ID = "your-gcp-project-id" # Replace with your actual project ID
REGION = "us-central1" # Or your desired region
# ... other configurations
And in your agent_on_ai_engine.py (or wherever you initialize Vertex AI):
Python
import vertexai
# Initialize Vertex AI with your project and region
vertexai.init(project="your-gcp-project-id", location="us-central1")
# ... rest of your code to deploy and run the agent
Make sure your-gcp-project-id and us-central1 (or your chosen region) match the project you authenticated with in step 1.
3. Service Account (for Production or Specific Roles)
While gcloud auth application-default login is great for local development, for production environments or if you need your application to run with specific, granular permissions, you should use a service account.
Steps to use a Service Account:
Create a Service Account:
Go to the Google Cloud Console: https://console.cloud.google.com/
Navigate to IAM & Admin > Service Accounts.
Click + CREATE SERVICE ACCOUNT.
Give it a name, ID, and description.
Grant roles: This is critical. For a Vertex AI agent, you'll typically need roles like:
Vertex AI User (roles/aiplatform.user)
Service Account User (roles/iam.serviceAccountUser) - often needed if the service account needs to impersonate other service accounts or run Cloud Functions/Run.
Storage Object Viewer (roles/storage.objectViewer) or Storage Object Admin if your agent needs to read/write from Cloud Storage buckets (e.g., for RAG).
BigQuery Data Viewer / BigQuery Job User if interacting with BigQuery.
Grant the principle of least privilege. Only grant the roles absolutely necessary for your agent's functionality.
Click Done.
Generate a JSON Key for the Service Account:
On the Service Accounts page, click on the service account you just created.
Go to the Keys tab.
Click ADD KEY > Create new key.
Select JSON and click CREATE.
A JSON key file will be downloaded to your computer. Keep this file secure! Do not commit it to version control.
Set GOOGLE_APPLICATION_CREDENTIALS Environment Variable:
Open your terminal/command prompt.
Set the GOOGLE_APPLICATION_CREDENTIALS environment variable to the full path of the downloaded JSON key file.
On Linux/macOS:
Bash
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your-service-account-key.json"
On Windows (Command Prompt):
DOS
set GOOGLE_APPLICATION_CREDENTIALS="C:\path\to\your-service-account-key.json"
On Windows (PowerShell):
PowerShell
$env:GOOGLE_APPLICATION_CREDENTIALS="C:\path\to\your-service-account-key.json"
This environment variable tells ADC to use this specific key file for authentication. You'll need to set this every time you open a new terminal session, or add it to your shell's profile script (e.g., .bashrc, .zshrc, config.fish).
After performing step 1 (or step 3 if you're using a service account), try running your Agent on the Vertex AI engine again. The google.auth.default() function should now successfully find your credentials.
No comments:
Post a Comment