使用 Vertex AI Agent Engine 前,請先確認環境已設定完成。您必須擁有 Google Cloud 已啟用帳單的專案、具備必要權限、設定 Cloud Storage 值區,並安裝 Vertex AI SDK for Python。請參閱下列主題,確保您已準備好開始使用 Vertex AI Agent Engine。
如需簡化 Vertex AI Agent Engine 環境設定和部署作業的 Terraform 參考範例,請考慮探索 agent-starter-pack。
使用 Google Cloud設定
您可以透過下列方式設定 Vertex AI Agent Engine:建立 Google Cloud 專案或註冊快速模式的 Vertex AI: Google Cloud
Google Cloud 專案
每項專案都可用專案編號或專案 ID 來識別。PROJECT_NUMBER 是系統在您建立專案時自動產生的,而 PROJECT_ID 則是由您或專案建立者所建立。如要設定專案,請按照下列步驟操作:
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Vertex AI, Cloud Storage, Cloud Logging, Cloud Monitoring, Telemetry, and Cloud Trace APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. -
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Vertex AI, Cloud Storage, Cloud Logging, Cloud Monitoring, Telemetry, and Cloud Trace APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.
快速模式
按照「Vertex AI in express mode」中的操作說明,在快速模式下設定 Vertex AI Agent Engine。
以快速模式設定 Vertex AI Agent Engine 後,即可跳至「安裝並初始化 Python 適用的 Vertex AI SDK」步驟。
取得必要角色
如要取得使用 Vertex AI Agent Engine 所需的權限,請要求管理員授予專案的「Vertex AI 使用者」 (roles/aiplatform.user) IAM 角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。
為代理設定身分和權限
設定身分和權限時,您可以選擇下列做法:
代理程式身分 (建議) (搶先版):使用 Identity Access Management (IAM) 代理程式身分,在 Vertex AI Agent Engine Runtime 上使用代理程式時,提供安全性和存取權管理功能。代理身分與每位代理人員相關聯。
服務帳戶:服務帳戶會與您部署至 Vertex AI Agent Engine 的代理程式共用。服務帳戶有兩種選項:
- 預設服務代理:根據預設,代理程式會使用 AI Platform Reasoning Engine 服務代理。這個 Google 代管的服務帳戶具有「Vertex AI Reasoning Engine 服務代理人」角色 (
roles/aiplatform.reasoningEngineServiceAgent),內含已部署代理程式所需的預設權限。 - 自訂服務帳戶:您可以指定代理程式使用的服務帳戶。這樣一來,您就能更精細地控管授予代理商的權限。
- 預設服務代理:根據預設,代理程式會使用 AI Platform Reasoning Engine 服務代理。這個 Google 代管的服務帳戶具有「Vertex AI Reasoning Engine 服務代理人」角色 (
服務專員身分
如要在部署代理程式前設定 IAM 政策,可以建立代理程式身分,不必部署代理程式程式碼。如要這麼做,請只使用 identity_type 欄位建立 Agent Engine 執行個體:
remote_app = agent_engines.create(
agent=app,
config={
"identity_type": types.IdentityType.AGENT_IDENTITY,
},
)
使用代理程式身分建立 Agent Engine 執行個體後,您可以執行下列操作:
為代理程式身分佈建下列建議角色:
roles/aiplatform.expressUser:授予執行推論、工作階段和記憶體的存取權。roles/serviceusage.serviceUsageConsumer:授予代理程式使用專案配額和 Vertex AI SDK 的權限。
視用途需要,授予代理程式身分其他角色。
使用
agent_engine.update(...)新增代理程式碼。
預設服務代理
系統預設會使用 AI Platform Reasoning Engine 服務代理程式。如要查看預設權限的完整清單,請參閱 IAM 說明文件。
如果代理程式需要的權限超出預設集,您可以授予這項服務代理其他角色:
前往「IAM」頁面,然後勾選「包含 Google 提供的角色授予項目」核取方塊。
找出與
service-PROJECT_NUMBER@gcp-sa-aiplatform-re.iam.gserviceaccount.com相符的主體。按一下編輯按鈕,然後按一下儲存按鈕,將必要角色新增至主體。
手動產生預設服務代理
在部署 Vertex AI Agent Engine 時,系統會自動佈建 Reasoning Engine 服務代理程式,但有時您可能需要預先手動產生該代理程式。如果您需要授予服務代理人特定角色,確保部署程序具備必要權限並避免潛在部署失敗,這點就特別重要。
如要手動產生 Reasoning Engine 服務代理,請按照下列步驟操作:
使用 Google Cloud CLI 產生 Reasoning Engine 服務代理程式。
gcloud beta services identity create --service=aiplatform.googleapis.com --project=PROJECT-ID-OR-PROJECT-NUMBER前往「IAM」頁面,然後按一下「授予存取權」。
在「新增主體」部分的「New principals」(新增主體) 欄位中輸入
service-PROJECT_NUMBER@gcp-sa-aiplatform-re.iam.gserviceaccount.com。在「指派角色」部分,找出並選取所需角色。
按一下 [Save] 按鈕。
自訂服務帳戶
如要使用自己的服務帳戶,您必須授予該帳戶執行代理程式所需的權限。自訂服務帳戶可能需要「Vertex AI 使用者」角色 (roles/aiplatform.user)。
如果沒有服務帳戶,請建立一個。請參閱「建立服務帳戶」。
將代理程式碼所需的任何其他角色授予服務帳戶。
如要使用這個服務帳戶部署代理程式,請授予您這個自訂服務帳戶的「服務帳戶使用者」角色 (
roles/iam.serviceAccountUser)。部署代理程式時,請指定自訂服務帳戶的電子郵件地址。詳情請參閱「設定自訂服務帳戶」。
跨專案自訂服務帳戶
如果自訂服務帳戶來自其他專案,您需要在服務帳戶所在的專案,以及部署代理程式的專案中進行額外設定。
停用跨專案服務帳戶使用組織政策:在服務帳戶所在的專案中,請確保
iam.disableCrossProjectServiceAccountUsage組織政策未強制執行。詳情請參閱「停用跨專案服務帳戶使用強制執行」。授予 Vertex AI 服務代理程式權限:在服務帳戶所在的專案中,將「服務帳戶權杖建立者」(
roles/iam.serviceAccountTokenCreator) 角色授予您打算部署代理程式的專案 Vertex AI 服務代理程式 (service-RESOURCE_PROJECT_NUMBER@gcp-sa-aiplatform.iam.gserviceaccount.com)。授予自訂服務帳戶權限:在您打算部署代理程式的專案中,將必要角色授予自訂服務帳戶。這通常包括 Vertex AI 使用者角色 (
roles/aiplatform.user) 和代理程式碼所需的任何其他角色。
(選用) 建立 Cloud Storage bucket
是否需要 Cloud Storage 值區,取決於 Vertex AI SDK for Python 是否需要先暫存代理程式程式碼,再進行部署:
從來源檔案部署:代理程式以檔案形式存在。Python 適用的 Vertex AI SDK 可直接將這些檔案打包並上傳至部署服務,因此不需要 Cloud Storage 中繼值區。
從代理物件部署:代理存在於記憶體中。Python 適用的 Vertex AI SDK 會封裝這個物件,並上傳至 Cloud Storage bucket,做為部署服務的暫存區。
從來源檔案部署
如果從來源檔案部署代理程式,則不需要 Cloud Storage bucket。
從物件部署
從代理物件部署時,Vertex AI Agent Engine 會在部署程序中,將已部署代理的構件暫存至 Cloud Storage 值區。請確認經過驗證可使用 Vertex AI 的主體 (您本人或服務帳戶) 具有這個 bucket 的 Storage Admin 存取權。這是必要步驟,因為 Vertex AI SDK for Python 會將程式碼寫入這個值區。
如果已設定好儲存空間,可以略過這個步驟。否則,請按照建立值區的標準操作說明進行。
請要求管理員授予您專案的「儲存空間管理員」 (roles/storage.admin) IAM 角色。
-
In the Get started section, do the following:
- Enter a globally unique name that meets the bucket naming requirements.
- To add a
bucket label,
expand the Labels section (),
click add_box
Add label, and specify a
keyand avaluefor your label.
-
In the Choose where to store your data section, do the following:
- Select a Location type.
- Choose a location where your bucket's data is permanently stored from the Location type drop-down menu.
- If you select the dual-region location type, you can also choose to enable turbo replication by using the relevant checkbox.
- To set up cross-bucket replication, select
Add cross-bucket replication via Storage Transfer Service and
follow these steps:
Set up cross-bucket replication
- In the Bucket menu, select a bucket.
In the Replication settings section, click Configure to configure settings for the replication job.
The Configure cross-bucket replication pane appears.
- To filter objects to replicate by object name prefix, enter a prefix that you want to include or exclude objects from, then click Add a prefix.
- To set a storage class for the replicated objects, select a storage class from the Storage class menu. If you skip this step, the replicated objects will use the destination bucket's storage class by default.
- Click Done.
-
In the Choose how to store your data section, do the following:
- Select a default storage class for the bucket or Autoclass for automatic storage class management of your bucket's data.
- To enable hierarchical namespace, in the Optimize storage for data-intensive workloads section, select Enable hierarchical namespace on this bucket.
- In the Choose how to control access to objects section, select whether or not your bucket enforces public access prevention, and select an access control method for your bucket's objects.
-
In the Choose how to protect object data section, do the
following:
- Select any of the options under Data protection that you
want to set for your bucket.
- To enable soft delete, click the Soft delete policy (For data recovery) checkbox, and specify the number of days you want to retain objects after deletion.
- To set Object Versioning, click the Object versioning (For version control) checkbox, and specify the maximum number of versions per object and the number of days after which the noncurrent versions expire.
- To enable the retention policy on objects and buckets, click the Retention (For compliance) checkbox, and then do the following:
- To enable Object Retention Lock, click the Enable object retention checkbox.
- To enable Bucket Lock, click the Set bucket retention policy checkbox, and choose a unit of time and a length of time for your retention period.
- To choose how your object data will be encrypted, expand the Data encryption section (), and select a Data encryption method.
- Select any of the options under Data protection that you
want to set for your bucket.
安裝並初始化 Vertex AI SDK for Python
本節假設您已設定 Python 開發環境,或是使用 Colab (或任何其他已為您設定的合適執行階段)。
(選用) 設定虛擬環境
此外,也建議您設定虛擬環境,隔離依附元件。
安裝
為盡量減少必須安裝的依附元件集,我們已將依附元件分成以下幾類:
agent_engines:部署至 Vertex AI Agent Engine 時所需的一組套件。adk:相容的 Agent Development Kit 套件組合。langchain:相容的 LangChain 和 LangGraph 套件組合。ag2:相容的 AG2 套件組合。llama_index:相容的 LlamaIndex 套件組合。
安裝 Python 適用的 Vertex AI SDK 時,您可以指定所需依附元件 (以半形逗號分隔)。如要安裝所有擴充功能,請按照下列步驟操作:
pip install google-cloud-aiplatform[agent_engines,adk,langchain,ag2,llama_index]>=1.112.0如要在 Agent Engine 上使用 Agent2Agent (A2A),您也必須安裝 a2a-sdk 套件:
pip install a2a-sdk>=0.3.4驗證
Colab
請執行下列程式碼:
from google.colab import auth
auth.authenticate_user(project_id="PROJECT_ID")
Cloud Shell
您無需採取任何動作。
本機殼層
執行下列指令:
gcloud auth application-default login快速模式
如果您使用 Vertex AI 的快速模式,則無須採取任何行動。
匯入並初始化 SDK
執行下列程式碼,匯入並初始化 Vertex AI Agent Engine 的 SDK:
Google Cloud 專案
import vertexai
from vertexai import agent_engines # For the prebuilt templates
client = vertexai.Client( # For service interactions via client.agent_engines
project="PROJECT_ID",
location="LOCATION",
)
其中
快速模式
如果您使用快捷模式的 Vertex AI,請執行下列程式碼:
import vertexai
from vertexai import agent_engines # For the prebuilt ADK template
client = vertexai.Client( # For service interactions via client.agent_engines
api_key="API_KEY"
)
其中 API_KEY 是用來驗證代理程式的 API 金鑰。