將代理部署至 Agent Runtime 後,代理就能從遠端處理要求。本文說明如何根據開發工作流程部署代理,包括從執行物件、本機來源檔案、Dockerfile、託管於 Artifact Registry 的容器映像檔,或直接透過連結的 Git 存放區。
如要在 Agent Runtime 部署代理,請選擇下列其中一種方法:
Developer Connect
建議用於透過 Developer Connect 連結的 Git 存放區中管理的專案。這個方法可直接從原始碼簡化代理部署作業,並原生支援版本管控、團隊協作和 CI/CD 管道。使用這個方法前,請按照「設定 Developer Connect Git 存放區連結」中的操作說明,設定 Git 存放區連結。
這個部署方法僅適用於 Python。
來源檔案
非常適合用於自動化工作流程,例如 CI/CD 管道和 Terraform 等基礎架構即程式碼工具,可實現完全宣告式和自動化部署。這項指令會直接從本機原始碼部署代理,不需要 Cloud Storage bucket。
這個部署方法僅適用於 Python。
Dockerfile
與從來源檔案部署類似。您可以直接從本機原始碼部署代理,不需使用 Cloud Storage bucket。如果您需要定義及控管部署的 API 伺服器,這個方法就很合適。部署的容器必須遵守執行階段合約。
這種部署方法適用於任何語言。本頁範例使用 Python。
容器映像檔
與從 Dockerfile 部署類似。您部署了託管於 Artifact Registry 的容器映像檔。如果您需要控管容器映像檔的建構程序,並縮短部署延遲時間,請使用這個方法。容器映像檔必須符合執行階段合約。
這種部署方法適用於任何語言。本頁範例使用 Python。
Agent Platform SDK
適合在 Colab 等環境中進行互動式開發,可部署記憶體內 local_agent 物件。如果代理的結構不含複雜、無法序列化的元件,這個方法最適合。
如要開始使用,請按照下列步驟操作:
必要條件
部署代理前,請務必完成下列工作:
選用:設定要部署的代理
您可以為代理進行選用設定。本節中的範例使用 Python。
建立 Agent Platform 執行個體
本節說明如何建立 Agent Platform 執行個體,以便部署代理。您可以選擇下列其中一種方法:
Developer Connect
如要透過 Agent Platform 的 Developer Connect 部署,請使用 client.runtimes.create,並在設定字典中提供 developer_connect_source、entrypoint_module 和 entrypoint_object,以及其他選用設定。這個方法可讓您直接從已連結的 Git 存放區部署程式碼。
這個部署方法僅適用於 Python。
remote_agent = client.runtimes.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。
部署作業需要幾分鐘才能完成,期間會在背景執行下列步驟:
- Agent Runtime 服務會從指定的 Git 存放區修訂版本擷取原始碼。
- 服務會從
requirements_file安裝依附元件 (如有提供)。 - 服務會使用指定的
entrypoint_module和entrypoint_object啟動代理程式應用程式。
部署作業延遲時間取決於安裝必要套件所需的總時間。部署完成後,remote_agent 會對應至在 Agent Platform 上執行的 local_agent 執行個體,可供查詢或刪除。
remote_agent 物件對應至 AgentRuntime 類別,其中包含下列項目:
remote_agent.api_resource其中包含已部署代理程式的相關資訊。 您也可以呼叫remote_agent.operation_schemas(),傳回remote_agent支援的作業清單。詳情請參閱「支援的操作」。remote_agent.api_client可進行同步服務互動remote_agent.async_api_client可進行非同步服務互動
來源檔案
如要從 Agent Platform 上的來源檔案部署,請使用 client.runtimes.create,並在設定字典中提供 source_packages、entrypoint_module、entrypoint_object 和 class_methods,以及其他選用設定。使用這個方法時,您不需要傳遞代理物件或 Cloud Storage bucket。
這個部署方法僅適用於 Python。
# 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.runtimes.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": "", # Options: "", "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):選用。pip 需求檔案的路徑,位於source_packages指定的路徑中。預設為封裝來源根目錄中的requirements.txt。
部署作業需要幾分鐘才能完成,期間會在背景執行下列步驟:
- Agent Platform SDK 會為
source_packages中指定的路徑建立tar.gz封存檔。 - 這個封存檔會經過編碼,並直接傳送至 Agent Platform API。
- Agent Runtime 服務會接收並解壓縮封存檔、從
requirements_file安裝依附元件 (如有提供),並使用指定的entrypoint_module和entrypoint_object啟動代理應用程式。
部署作業延遲時間取決於安裝必要套件所需的總時間。部署完成後,remote_agent 會對應至在 Agent Platform 上執行的 local_agent 執行個體,可供查詢或刪除。
remote_agent 物件對應至 AgentRuntime 類別,其中包含下列項目:
remote_agent.api_resource其中包含已部署代理程式的相關資訊。 您也可以呼叫remote_agent.operation_schemas(),傳回remote_agent支援的作業清單。詳情請參閱「支援的操作」。remote_agent.api_client可進行同步服務互動remote_agent.async_api_client可進行非同步服務互動
Dockerfile
在 Agent Platform 上從 Dockerfile 部署的方式與從來源檔案部署類似,差別在於您要在設定中使用 image_spec,而非 entrypoint_module、entrypoint_object 和 requirements_file。從 Dockerfile 建構的容器必須遵守執行階段合約。
這種部署方法適用於任何語言。本節中的範例使用 Python。
以下是使用 Dockerfile 部署代理程式的範例:
# Example file structure: # /current_directory # ├── agent.py # ├── main.py # ├── requirements.txt # ├── Dockerfile remote_agent = client.runtimes.create( config={ "source_packages": [ "agent.py", "main.py", "requirements.txt", "Dockerfile", ], "image_spec": {}, # tells Agent Runtime to use the Dockerfile # Other optional configs "display_name": "Dockerfile agent", } )
部署作業延遲時間取決於安裝必要套件所需的總時間。部署完成後,remote_agent 會對應至在 Agent Platform 上執行的 local_agent 執行個體,可供查詢或刪除。
remote_agent 物件對應至 AgentRuntime 類別,其中包含下列項目:
remote_agent.api_resource其中包含已部署代理程式的相關資訊。 您也可以呼叫remote_agent.operation_schemas(),傳回remote_agent支援的作業清單。詳情請參閱「支援的操作」。remote_agent.api_client可進行同步服務互動remote_agent.async_api_client可進行非同步服務互動
容器映像檔
如要從容器映像檔部署,請先按照「自備容器」的設定操作說明,確保安裝符合 >=1.144 的 google-cloud-aiplatform 版本。容器映像檔必須符合執行階段合約。
這種部署方法適用於任何語言。本節中的範例使用 Python。
以下範例說明如何使用容器映像檔部署代理程式:
remote_agent = client.runtimes.create( config={ "container_spec": { "image_uri": "CONTAINER_IMAGE_URI", }, # Other optional configs "display_name": "Container image agent", }, )
其中 CONTAINER_IMAGE_URI 對應至 Artifact Registry 中容器映像檔的 URI (例如 us-central1-docker.pkg.dev/my-project/my-repo/my-image:tag)。
部署作業延遲時間取決於安裝必要套件所需的總時間。部署完成後,remote_agent 會對應至在 Agent Platform 上執行的 local_agent 執行個體,可供查詢或刪除。
remote_agent 物件對應至 AgentRuntime 類別,其中包含下列項目:
remote_agent.api_resource其中包含已部署代理程式的相關資訊。 您也可以呼叫remote_agent.operation_schemas(),傳回remote_agent支援的作業清單。詳情請參閱「支援的操作」。remote_agent.api_client可進行同步服務互動remote_agent.async_api_client可進行非同步服務互動
Agent Platform SDK
如要在 Agent Platform 部署代理,請使用 client.runtimes.create 傳入 local_agent 物件和任何選用設定:
remote_agent = client.runtimes.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 中指定。
- Agent Runtime 服務會接收要求、建構容器,並在後端啟動 HTTP 伺服器。
部署作業延遲時間取決於安裝必要套件所需的總時間。部署完成後,remote_agent 會對應至在 Agent Platform 上執行的 local_agent 執行個體,可供查詢或刪除。
remote_agent 物件對應至 AgentRuntime 類別,其中包含下列項目:
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資源。
選用:列出支援的操作
每個部署的代理都有一份支援作業清單。您可以使用 AgentRuntime.operation_schemas 取得已部署代理支援的作業清單:
remote_agent.operation_schemas()
每項作業的結構定義都是字典,記錄可呼叫的代理方法資訊。支援的操作組合取決於您開發代理時使用的架構:
選用:授予已部署代理權限
如果需要授予已部署代理任何額外權限,請按照「設定代理的身分和權限」中的操作說明進行。
選用:使用企業基礎架構部署代理
如要部署代理,並提供包含安全性和管理層的完整環境,請複製並自訂下列 Application Design Center 範本: