如要在 Vertex AI Agent Engine 部署代理,請選擇下列方法:
-
從虛擬服務專員物件部署:適合在 Colab 等環境中進行互動式開發,可部署記憶體內
local_agent物件。如果代理程式的結構不含複雜的非可序列化元件,最適合使用這個方法。 - 從來源檔案部署:這個方法非常適合自動化工作流程,例如 CI/CD 管道和基礎架構即程式碼工具 (如 Terraform),可實現全自動化部署。這項工具會直接從本機原始碼部署代理程式,不需要 Cloud Storage bucket。
- 從 Dockerfile 部署:這個方法與從來源檔案部署的方法類似。直接從本機原始碼部署代理程式。您不需要 Cloud Storage bucket。如果您需要定義及控管部署的 API 伺服器,這個方法就非常適合。
- 從容器映像檔部署:這個方法與從 Dockerfile 部署的方法類似。部署託管在 Artifact Registry 中的容器映像檔。如果您需要控管容器映像檔的建構程序,並縮短部署延遲時間,請使用這個方法。
- 從 Developer Connect 部署:建議用於透過 Developer Connect 連結的 Git 存放區中管理的專案。這個方法可直接從原始碼簡化代理程式部署作業,並原生支援版本控管、團隊協作和 CI/CD 管道。使用這個方法前,請先按照「設定 Developer Connect Git 存放區連結」一文中的操作說明,設定 Git 存放區連結。
如要開始使用,請按照下列步驟操作:
- 完成必要條件。
- (選用) 設定要部署的代理程式。
- 建立
AgentEngine執行個體。 - (選用) 取得代理程式資源 ID。
- (選用) 列出支援的作業。
- (選用) 授予已部署的代理程式權限。
您也可以使用 Agent Starter Pack 範本進行部署。
必要條件
部署代理程式前,請務必完成下列工作:
(選用) 設定代理程式以進行部署
您可以為代理程式設定下列選用項目:
建立 AgentEngine 執行個體
本節說明如何建立 AgentEngine 執行個體來部署代理程式。
如要在 Vertex AI Agent Engine 部署代理,可以選擇下列方法:
- 從代理物件部署,進行互動式開發。
- 透過 Developer Connect 部署,適用於以 Git 為基礎的工作流程。
- 從原始碼檔案或 Dockerfile 部署,適用於以檔案為基礎的工作流程。
從容器映像檔部署,適用於以映像檔為基礎的工作流程。
Python 物件
如要在 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.
"identity_type": identity_type, # 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.
"agent_framework": agent_framework, # Optional.
},
)
部署作業需要幾分鐘,期間系統會在背景執行下列步驟:
系統會在本機產生一組構件,當中含有:
為了暫存構件,這個軟體包會上傳至 Cloud Storage (位於對應的資料夾)。
各個構件的 Cloud Storage URI 會在 PackageSpec 中指定。
Vertex AI Agent Engine 服務會接收要求、建構容器,並在後端啟動 HTTP 伺服器。
Developer Connect
如要從 Vertex AI 的 Developer Connect 部署,請使用 client.agent_engines.create,並在設定字典中提供 developer_connect_source、entrypoint_module 和 entrypoint_object,以及其他選用設定。這個方法可讓您直接從已連線的 Git 存放區部署程式碼。
remote_agent = client.agent_engines.create(
config={
"developer_connect_source": { # Required.
"git_repository_link": "projects/PROJECT_ID/locations/LOCATION/connections/CONNECTION_ID/gitRepositoryLinks/REPO_ID",
"revision": "main",
"dir": "path/to/dir",
},
"entrypoint_module": "agent", # Required.
"entrypoint_object": "root_agent", # Required.
"requirements_file": "requirements.txt", # Optional.
# Other optional configs:
# "env_vars": {...},
# "service_account": "...",
},
)
Developer Connect 部署作業的參數如下:
developer_connect_source(必填,dict):用於擷取原始碼的設定。詳情請參閱設定 Developer Connect Git 存放區連結。git_repository_link(必要,str):Developer Connect Git 存放區連結資源名稱。revision(必填,str):要擷取的修訂版本 (分支版本、標記或修訂版本 SHA)。dir(必填,str):存放區中代理程式碼的根目錄。
entrypoint_module(必要,str):包含代理程式進入點的 Python 模組名稱,相對於developer_connect_source.dir中指定的目錄。entrypoint_object(必要,str):entrypoint_module中可呼叫物件的名稱,代表代理程式應用程式 (例如root_agent)。requirements_file(選用,str):相對於來源根目錄的 pip 需求檔案路徑。預設值為requirements.txt。
部署作業需要幾分鐘,期間系統會在背景執行下列步驟:
- Vertex AI Agent Engine 服務會從指定的 Git 存放區修訂版本擷取原始碼。
- 服務會從
requirements_file安裝依附元件 (如有提供)。 - 服務會使用指定的
entrypoint_module和entrypoint_object啟動代理程式應用程式。
來源檔案
如要從 Vertex AI 上的來源檔案部署,請使用 client.agent_engines.create,並在設定字典中提供 source_packages、entrypoint_module、entrypoint_object 和 class_methods,以及其他選用設定。使用這種方法時,您不需要傳遞代理程式物件或 Cloud Storage bucket。
remote_agent = client.agent_engines.create(
config={
"source_packages": source_packages, # Required.
"entrypoint_module": entrypoint_module, # Required.
"entrypoint_object": entrypoint_object, # Required.
"class_methods": class_methods, # Required.
"requirements_file": requirements_file, # Optional.
"display_name": display_name, # Optional.
"description": description, # Optional.
"labels": labels, # Optional.
"env_vars": env_vars, # Optional.
"build_options": build_options, # Optional.
"identity_type": identity_type, # 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.
"agent_framework": agent_framework, # Optional.
},
)
內嵌來源部署作業的參數如下:
source_packages(必要,list[str]):要納入部署作業的本機檔案或目錄路徑清單。source_packages中的檔案和目錄總大小不得超過 8 MB。entrypoint_module(必填,str):包含代理進入點的完整 Python 模組名稱 (例如agent_dir.agent)。entrypoint_object(必要,str):entrypoint_module中可呼叫物件的名稱,代表代理程式應用程式 (例如root_agent)。class_methods(必要,list[dict]):定義代理程式公開方法的字典清單。每個字典都包含name(必填)、api_mode(必填) 和parameters欄位。如要進一步瞭解自訂代理程式的方法,請參閱「列出支援的作業」。例如:
"class_methods": [ { "name": "method_name", "api_mode": "", # Possible options are: "", "async", "async_stream", "stream", "bidi_stream" "parameters": { "type": "object", "properties": { "param1": {"type": "string", "description": "Description of param1"}, "param2": {"type": "integer"} }, "required": ["param1"] } } ] ```requirements_file(選用,str):source_packages中指定路徑內的 pip 需求檔案路徑。預設值為封裝來源根目錄中的requirements.txt。
部署作業需要幾分鐘,期間系統會在背景執行下列步驟:
- Vertex AI SDK 會建立
tar.gz封存檔,其中包含source_packages中指定的路徑。 - 這個封存檔會經過編碼,並直接傳送至 Vertex AI API。
- Vertex AI Agent Engine 服務會接收封存檔、解壓縮、從
requirements_file安裝依附元件 (如有提供),並使用指定的entrypoint_module和entrypoint_object啟動代理程式應用程式。
以下是從來源檔案部署代理程式的範例:
from google.cloud.aiplatform import vertexai
# Example file structure:
# /agent_directory
# ├── agent.py
# ├── requirements.txt
# Example agent_directory/agent.py:
# class MyAgent:
# def ask(self, question: str) -> str:
# return f"Answer to {question}"
# root_agent = MyAgent()
remote_agent = client.agent_engines.create(
config={
"display_name": "My Agent",
"description": "An agent deployed from a local source.",
"source_packages": ["agent_directory"],
"entrypoint_module": "agent_directory.agent",
"entrypoint_object": "root_agent",
"requirements_file": "requirements.txt",
"class_methods": [
{"name": "ask", "api_mode": "", "parameters": {
"type": "object",
"properties": {
"question": {"type": "string"}
},
"required": ["question"]
}},
],
# Other optional configs:
# "env_vars": {...},
# "service_account": "...",
}
)
Dockerfile
如要從 Vertex AI 上的 Dockerfile 部署,請採用與從來源檔案部署類似的方法。部署時唯一需要變更的地方,就是將設定中的 entrypoint_module、entrypoint_object 和 (選用) requirements_file 替換為 image_spec。
以下是使用 Dockerfile 部署代理程式的範例:
from google.cloud.aiplatform import vertexai
# Example file structure:
# /current_directory
# ├── agent.py
# ├── main.py
# ├── requirements.txt
# ├── Dockerfile
remote_agent = client.agent_engines.create(
config={
"source_packages": [
"agent.py",
"main.py",
"requirements.txt",
"Dockerfile",
],
"image_spec": {}, # tells AgentEngine to use the Dockerfile
# Other optional configs
}
)
容器映像檔
如要從容器映像檔部署,請先按照「自備容器」的設定說明操作,並確保安裝的 google-cloud-aiplatform 版本符合 >=1.144。接著執行下列程式碼:
remote_agent = client.agent_engines.create(
config={
"container_spec": {
"image_uri": "CONTAINER_IMAGE_URI",
},
# Other optional configs
},
)
其中 CONTAINER_IMAGE_URI 對應於 Artifact Registry 中容器映像檔的 URI (例如 us-central1-docker.pkg.dev/my-project/my-repo/my-image:tag)。
部署延遲時間取決於安裝必要套件的總時間。部署完成後,remote_agent 會對應至在 Vertex AI 上執行的 local_agent 執行個體,並可供查詢或刪除。
remote_agent 物件對應於 AgentEngine 類別,其中包含下列項目:
remote_agent.api_resource,其中包含已部署代理程式的相關資訊。 您也可以呼叫remote_agent.operation_schemas(),傳回remote_agent支援的作業清單。詳情請參閱「支援的作業」。remote_agent.api_client,可進行同步服務互動remote_agent.async_api_client,可進行非同步服務互動
(選用) 取得服務專員資源 ID
每個已部署的代理程式都有專屬 ID。您可以執行下列指令,取得已部署代理程式的資源名稱:
remote_agent.api_resource.name
回應內容應如下列字串所示:
"projects/PROJECT_NUMBER/locations/LOCATION/reasoningEngines/RESOURCE_ID"
其中
PROJECT_ID是已部署代理程式的 Google Cloud 專案 ID。LOCATION是已部署代理程式的執行區域。RESOURCE_ID是已部署代理程式的 ID,屬於reasoningEngine資源。
(選用) 列出支援的作業
每個已部署的代理程式都有支援的作業清單。您可以使用 AgentEngine.operation_schemas 取得已部署代理程式支援的作業清單:
remote_agent.operation_schemas()
每項作業的結構定義都是字典,其中記錄了可呼叫的代理程式方法資訊。支援的作業集取決於您用來開發代理程式的架構:
(選用) 授予部署的代理權限
如果部署的代理程式需要額外權限,請按照「為代理程式設定身分和權限」一文中的操作說明進行設定。