在 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 存储桶。
您只能将此部署方法与 Python 搭配使用。
Dockerfile
类似于从源文件进行部署。您可以直接从本地源代码部署代理,而无需使用 Cloud Storage 存储桶。如果您需要定义并控制所部署的 API 服务器,则此方法非常适合。已部署的容器必须遵循运行时合同。
您可以使用此部署方法搭配任何语言。本页面上的示例使用 Python。
容器映像
与从 Dockerfile 部署类似。您部署了托管在 Artifact Registry 中的容器映像。如果您需要控制容器映像的构建流程并缩短部署延迟时间,请使用此方法。容器映像必须遵循运行时合同。
您可以使用此部署方法搭配任何语言。本页面上的示例使用 Python。
Agent Platform SDK
非常适合在 Colab 等环境中进行交互式开发,可用于部署内存中的 local_agent 对象。此方法最适合结构不包含复杂且不可序列化组件的代理。
如需开始使用,请按以下步骤操作:
- 完成前提条件
- 可选:配置智能体以进行部署
- 创建 Agent Platform 实例
- 可选:获取智能体资源 ID
- 可选:列出支持的操作
- 可选:为所部署的智能体授予权限
前提条件
在部署智能体之前,请确保您已完成以下任务:
可选:配置智能体以进行部署
您可以为智能体进行可选配置。本部分中的示例使用 Python。
创建 Agent Platform 实例
本部分介绍如何创建 Agent Platform 实例以部署代理。您可以选择以下方法:
Developer Connect
如需从 Developer Connect 在 Agent Platform 上部署,请使用 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 存储桶。
您只能将此部署方法与 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中的文件和目录的总大小不应超过 8MB。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):可选:source_packages中指定的路径内某个 pip 要求文件的路径。默认为打包源代码根目录中的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
容器映像
如需从容器映像进行部署,请先按照自带容器的设置说明操作,确保安装的 google-cloud-aiplatform 版本满足 >=1.144 的要求。容器映像必须遵循运行时合同。
您可以使用此部署方法搭配任何语言。本部分中的示例使用 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
每个已部署的智能体都有一个唯一标识符。您可以运行以下命令来获取所部署智能体的资源名称:
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()
每个操作的架构都是一个字典,用于记录您可以调用的代理方法的相关信息。支持的操作集取决于您用于开发智能体的框架:
可选:为所部署的智能体授予权限
如果需要向所部署的代理授予任何其他权限,请按照为代理设置身份和权限中的说明操作。
可选:使用企业基础架构部署智能体
如需部署具有完整环境(包括安全层和治理层)的代理,请复制并自定义以下 App Design Center 模板: