部署代理
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
如要在 Vertex AI Agent Engine 部署代理,請按照下列步驟操作:
完成必要條件 。
(選用) 設定要部署的代理程式 。
建立 AgentEngine
執行個體 。
(選用) 取得代理程式資源 ID 。
(選用) 列出支援的作業 。
(選用) 授予已部署的代理程式權限 。
您也可以使用 Agent Starter Pack 範本進行部署。
注意: Vertex AI Agent Engine 部署作業僅支援 Python。
必要條件
部署代理程式前,請務必完成下列工作:
設定環境 。
開發代理程式 。
您可以為代理程式設定下列選用項目:
定義套件需求
提供代理程式部署所需的套件組合。這組套件可以是 pip 要安裝的項目清單,也可以是符合需求檔案格式 的檔案路徑。請遵循下列最佳做法:
固定套件版本,確保建構可重現。常見的追蹤套件包括:google-cloud-aiplatform
、cloudpickle
、langchain
、langchain-core
、langchain-google-vertexai
和 pydantic
。
盡可能減少代理程式中的依附元件數量。這樣一來,更新依附元件和代理程式時,重大變更的數量就會減少。
如果代理程式沒有任何依附元件,您可以將 requirements
設為 None
:
requirements = None
如果代理程式使用架構專屬範本,開發代理程式時,您應指定匯入的 SDK 版本 (例如 1.112.0
)。
ADK
requirements = [
"google-cloud-aiplatform[agent_engines,adk]" ,
# any other dependencies
]
A2A
Preview
This feature is
subject to the "Pre-GA Offerings Terms" in the General Service Terms section of the
Service Specific
Terms .
Pre-GA features are available "as is" and might have limited support.
For more information, see the
launch stage descriptions .
requirements = [
"google-cloud-aiplatform[agent_engines]" ,
"a2a-sdk>=0.3.4"
# any other dependencies
]
LangChain
requirements = [
"google-cloud-aiplatform[agent_engines,langchain]" ,
# any other dependencies
]
LangGraph
requirements = [
"google-cloud-aiplatform[agent_engines,langgraph]" ,
# any other dependencies
]
AG2
requirements = [
"google-cloud-aiplatform[agent_engines,ag2]" ,
# any other dependencies
]
LlamaIndex
以下操作說明適用於 LlamaIndex 查詢管道:
requirements = [
"google-cloud-aiplatform[agent_engines,llama_index]" ,
# any other dependencies
]
您也可以使用套件 requirements
執行下列操作:
定義其他套件
您可以加入本機檔案或目錄,其中包含本機所需的 Python 來源檔案。相較於套件需求 ,這項功能可讓您使用自行開發的私有公用程式,這些公用程式在 PyPI 或 GitHub 上無法取得。
如果代理程式不需要任何額外套件,您可以將 extra_packages
設為 None
:
extra_packages = None
你也可以使用 extra_packages
執行下列操作:
加入單一檔案 (例如 agents/agent.py
):
extra_packages = [ "agents/agent.py" ]
在整個目錄中加入檔案集 (例如 agents/
):
extra_packages = [ "agents" ] # directory that includes agents/agent.py
指定 Python Wheel 二進位檔 (例如 path/to/python_package.whl
):
requirements = [
"google-cloud-aiplatform[agent_engines,adk]" ,
"cloudpickle==3.0" ,
"python_package.whl" , # install from the whl file that was uploaded
]
extra_packages = [ "path/to/python_package.whl" ] # bundle the whl file for uploading
定義環境變數
如果代理程式依附於環境變數,您可以在 env_vars=
引數中指定這些變數。如果代理程式不依附任何環境變數,可以將其設為 None
:
env_vars = None
警告: 請勿設定下列環境變數:GOOGLE_CLOUD_PROJECT
、GOOGLE_CLOUD_QUOTA_PROJECT
、GOOGLE_CLOUD_LOCATION
、PORT
、K_SERVICE
、K_REVISION
、K_CONFIGURATION
和 GOOGLE_APPLICATION_CREDENTIALS
。此外,請避免使用 GOOGLE_CLOUD_AGENT_ENGINE
前置字元,以免與 Vertex AI Agent Engine 環境變數發生命名衝突。
如要指定環境變數,有幾種不同的做法:
字典
env_vars = {
"VARIABLE_1" : "VALUE_1" ,
"VARIABLE_2" : "VALUE_2" ,
}
# These environment variables will become available in Vertex AI Agent Engine
# through `os.environ`, e.g.
#
# import os
# os.environ["VARIABLE_1"] # will have the value "VALUE_1"
#
# and
#
# os.environ["VARIABLE_2"] # will have the value "VALUE_2"
#
如要參照 Secret Manager 中的密鑰,並將其做為環境變數 (例如 CLOUD_SQL_CREDENTIALS_SECRET
),請先按照操作說明在專案 中建立 CLOUD_SQL_CREDENTIALS_SECRET
的密鑰 ,然後指定環境變數,如下所示:
env_vars = {
# ... (other environment variables and their values)
"CLOUD_SQL_CREDENTIALS_SECRET" : { "secret" : SECRET_ID
, "version" : SECRET_VERSION_ID
},
}
其中
SECRET_VERSION_ID
是密鑰版本的 ID。
SECRET_ID
是密鑰的 ID。
注意: 您只能參照與已部署代理程式在同一個專案中管理的密鑰 (及其版本)。
接著,您可以在代理程式碼 中參照密鑰,如下所示:
secret = os . environ . get ( "CLOUD_SQL_CREDENTIALS_SECRET" )
if secret :
# Secrets are stored as strings, so use json.loads to parse JSON
# payloads.
return json . loads ( secret )
清單
注意: 這個選項不支援整合 Secret Manager。如要指定受管理密鑰,您必須以字典形式指定環境變數。
env_vars = [ "VARIABLE_1" , "VARIABLE_2" ]
# This corresponds to the following code snippet:
#
# import os
#
# env_vars = {
# "VARIABLE_1": os.environ["VARIABLE_1"],
# "VARIABLE_2": os.environ["VARIABLE_2"],
# }
此外,您也需要按照「為代理程式設定身分和權限 」一文中的操作說明,授予代理程式 Secret Manager Secret Accessor (roles/secretmanager.secretAccessor
) 權限。
定義自訂資源控管
您可以為代理程式指定執行階段資源控制項,例如應用程式執行個體的數量下限和上限、每個容器的資源限制,以及每個容器的並行數。
min_instances
:隨時保持執行的應用程式執行個體數量下限,範圍為 [0, 10]
。預設值為 1。
注意: 這項功能目前處於預先發布版,因此即使您設定的執行個體數量下限較高,系統也不會針對代理程式閒置的時間向您收費。這項計費方式日後可能會有變動。
max_instances
:可啟動的應用程式執行個體數量上限,用來處理增加的流量,範圍為 [1, 1000]
。預設值為 100。如果啟用 VPC-SC 或 PSC-I,可接受的範圍為 [1, 100]
。
resource_limits
:每個容器的資源限制。系統僅支援 cpu
和 memory
鍵。預設值為 {"cpu": "4", "memory": "4Gi"}
。
cpu
僅支援 1
、2
、4
、6
和 8
。詳情請參閱「設定 CPU 分配 」。
memory
唯一支援的值為 1Gi
、2Gi
、...32Gi
。
如要瞭解不同記憶體值所需的 CPU,請參閱「設定記憶體限制 」。
container_concurrency
:每個容器和代理程式伺服器的並行數。建議值為 2 * cpu
+ 1。預設值為 9
。
remote_agent = client . agent_engines . create (
agent = local_agent ,
config = {
"min_instances" : 1 ,
"max_instances" : 10 ,
"resource_limits" : { "cpu" : "4" , "memory" : "8Gi" },
"container_concurrency" : 9 ,
# ... other configs
}
)
定義建構選項
您可以為代理程式指定建構選項,例如建構代理程式容器映像檔時要執行的安裝指令碼。這項功能可用於安裝系統依附元件 (例如 gcloud cli
、npx
) 或其他自訂設定。指令碼會以 Root 權限執行。
如要使用安裝指令碼,請建立名為 installation_scripts
的目錄,並將殼層指令碼放在該目錄中:
.
├── ...
└── installation_scripts/
└── install.sh
接著,在 extra_packages
中指定 installation_scripts
目錄,並在 build_options
中指定指令碼路徑:
extra_packages = [ ..., "installation_scripts/install.sh" ]
build_options = { "installation_scripts" : [ "installation_scripts/install.sh" ]}
您可以使用下列常見的安裝指令碼:
install_npx.sh
#!/bin/bash
# Exit immediately if a command exits with a non-zero status.
set -e
echo "--- Installing System-Wide Node.js v20.x ---"
# 1. Install prerequisites
apt-get update
apt-get install -y ca-certificates curl gnupg
# 2. Add the NodeSource repository GPG key
mkdir -p /etc/apt/keyrings
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
# 3. Add the NodeSource repository for Node.js v20
NODE_MAJOR = 20
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_ $NODE_MAJOR .x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list
# 4. Update package lists again and install Node.js
apt-get update
apt-get install nodejs -y
echo "--- System-wide Node.js installation complete ---"
echo "Verifying versions:"
# These commands will now work for ANY user because node and npx
# are installed in /usr/bin/ which is in everyone's default PATH.
node -v
npm -v
npx -v
install_uvx.sh
#!/bin/bash
# Exit immediately if a command exits with a non-zero status.
set -e
echo "Starting setup..."
# Install uv
apt-get update
apt-get install -y curl
curl -LsSf https://astral.sh/uv/install.sh | env UV_INSTALL_DIR = "/usr/local/bin" sh
# These commands will now work for ANY user because uv and uvx
# are installed in /usr/local/bin/ which is in everyone's default PATH.
uv --version
uvx --version
install_gcloud_cli.sh
#!/bin/bash
# Exit immediately if a command exits with a non-zero status.
set -e
apt-get install -y curl gpg
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | gpg --dearmor -o /usr/share/keyrings/cloud.google.gpg
echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" | tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
apt-get update -y && apt-get install google-cloud-cli -y
gcloud --version
定義 Cloud Storage 資料夾
如果暫存構件對應於 Cloud Storage bucket 中的現有資料夾 ,則會遭到覆寫。如有需要,您可以指定暫存構件的 Cloud Storage 資料夾。如果您不介意覆寫預設資料夾中的檔案,可以將 gcs_dir_name
設為 None
:
gcs_dir_name = None
為避免覆寫檔案 (例如用於開發、測試和實際工作等不同環境),您可以設定對應的資料夾,並指定要將構件暫存至哪個資料夾:
gcs_dir_name = "dev" # or "staging" or "prod"
如要避免發生碰撞,可以產生隨機 uuid
:
import uuid
gcs_dir_name = str ( uuid . uuid4 ())
定義顯示名稱
您可以為 ReasoningEngine
資源設定顯示名稱:
display_name = "Currency Exchange Rate Agent (Staging)"
定義說明
您可以設定 ReasoningEngine
資源的說明:
description = """
An agent that has access to tools for looking up the exchange rate.
If you run into any issues, please contact the dev team.
"""
定義標籤
您可以將 ReasoningEngine
資源的標籤設為鍵/值字串組合的字典。範例如下:
labels = { "author" : "username" , "version" : "latest" }
您可以將自訂服務帳戶設為已部署代理程式的身分,而非預設身分 。
如要這麼做,請在建立或更新 Agent Engine 執行個體時,將自訂服務帳戶的電子郵件地址指定為 service_account
,例如:
# Create a new instance
client . agent_engines . create (
agent = local_agent ,
config = {
"service_account" : "my-custom-service-account@my-project.iam.gserviceaccount.com" ,
# ...
},
)
# Update an existing instance
resource_name = "projects/ {project_id} /locations/ {location} /reasoningEngines/ {reasoning_engine_id} "
client . agent_engines . update (
name = resource_name ,
agent = local_agent ,
config = {
"service_account" : "my-new-custom-service-account@my-project.iam.gserviceaccount.com" ,
# ...
},
)
注意: 請只指定服務帳戶電子郵件地址,而非完整資源 URI,例如 projects/{project_id}/serviceAccounts/{service_account_email}
。
如果您已設定 Private Service Connect 介面和 DNS 對接 ,部署代理程式時可以指定網路附件和私人 DNS 對接:
remote_agent = client . agent_engines . create (
agent = local_agent ,
config = {
"psc_interface_config" : {
"network_attachment" : "NETWORK_ATTACHMENT " ,
"dns_peering_configs" : [
{
"domain" : "DOMAIN_SUFFIX " ,
"target_project" : "TARGET_PROJECT " ,
"target_network" : "TARGET_NETWORK " ,
},
],
},
},
)
其中
NETWORK_ATTACHMENT
是網路附件的名稱或完整路徑。如果網路連結是在專案 (例如共用虛擬私有雲主專案) 中建立,但您使用 Agent Engine 的位置不同,則需要傳遞網路連結的完整路徑。
DOMAIN_SUFFIX
是您在設定私人 DNS 對等互連時建立的私人 Cloud DNS 區域 DNS 名稱。
TARGET_PROJECT
是代管虛擬私有雲網路的專案。可以與網路附加專案不同。
TARGET_NETWORK
是虛擬私有雲網路名稱。
您可以設定多個代理程式,使用單一共用網路附件,或專屬的獨立網路附件。如要使用共用網路連結,請在您建立的每個代理程式的 psc_interface_config
中提供相同的網路連結。
您可以使用自訂金鑰加密代理程式的靜態資料。詳情請參閱「Agent Engine」一節中的「客戶自行管理的加密金鑰 (CMEK) 」。
如要為代理程式設定自訂金鑰 (CMEK),您需要在建立 Agent Engine 執行個體時,將金鑰資源名稱提供給 encryption_spec
參數。
# The fully qualified key name
kms_key_name = "projects/PROJECT_ID
/locations/LOCATION
/keyRings/KEY_RING
/cryptoKeys/KEY_NAME
"
remote_agent = client . agent_engines . create (
agent = local_agent ,
config = {
"encryption_spec" : { "kms_key_name" : kms_key_name },
# ... other parameters
},
)
建立 AgentEngine
執行個體
如要在 Vertex AI 上部署代理程式,請使用 client.agent_engines.create
傳遞 local_agent
物件和任何選用設定 :
remote_agent = client . agent_engines . create (
agent = local_agent , # Optional.
config = {
"requirements" : requirements , # Optional.
"extra_packages" : extra_packages , # Optional.
"gcs_dir_name" : gcs_dir_name , # Optional.
"display_name" : display_name , # Optional.
"description" : description , # Optional.
"labels" : labels , # Optional.
"env_vars" : env_vars , # Optional.
"build_options" : build_options , # Optional.
"service_account" : service_account , # Optional.
"min_instances" : min_instances , # Optional.
"max_instances" : max_instances , # Optional.
"resource_limits" : resource_limits , # Optional.
"container_concurrency" : container_concurrency , # Optional
"encryption_spec" : encryption_spec , # Optional.
},
)
部署作業需要幾分鐘,期間系統會在背景執行下列步驟:
系統會在本地產生下列成果套件:
*.pkl
對應於 local_agent 的 Pickle 檔案。
requirements.txt
:包含套件需求 的文字檔。
dependencies.tar.gz
,其中包含任何額外套件 。
套件會上傳至 Cloud Storage (位於對應的資料夾 ),用於暫存構件。
各個構件的 Cloud Storage URI 會在 PackageSpec 中指定。
Vertex AI Agent Engine 服務會接收要求、建構容器,並在後端啟動 HTTP 伺服器。
部署延遲時間取決於安裝必要套件的總時間。部署完成後,remote_agent
會對應至在 Vertex AI 上執行的 local_agent
執行個體,並可查詢或刪除。與代理程式的本機執行個體不同。
remote_agent
物件對應於 AgentEngine
類別,其中包含下列屬性:
(選用) 取得服務專員資源 ID
每個已部署的代理程式都有專屬 ID。您可以執行下列指令,取得已部署代理程式的資源名稱:
remote_agent . api_resource . name
回應應如下列字串所示:
"projects/PROJECT_NUMBER /locations/LOCATION /reasoningEngines/RESOURCE_ID "
其中
(選用) 列出支援的作業
每個已部署的代理程式都有支援的作業清單。您可以執行下列指令,取得已部署代理程式支援的作業清單:
remote_agent . operation_schemas ()
每項作業的結構定義都是字典,其中記錄了可呼叫的代理程式方法資訊。支援的作業集取決於您用來開發代理程式的架構:
(選用) 授予已部署的代理程式權限
如果部署的代理程式需要額外權限,請按照「為代理程式設定身分和權限 」一文中的操作說明進行設定。
後續步驟