Deploy to Cloud Run¶
部署到 Cloud Run¶
Cloud Run is a fully managed platform that enables you to run your code directly on top of Google's scalable infrastructure.
Cloud Run 是一个完全托管平台,使您能够直接在 Google 可扩展基础设施上运行代码。
To deploy your agent, you can use either the adk deploy cloud_run command (recommended for Python), or with gcloud run deploy command through Cloud Run.
要部署您的智能体,您可以使用 adk deploy cloud_run 命令 (推荐用于 Python),或通过 Cloud Run 使用 gcloud run deploy 命令。
Agent sample¶
智能体示例¶
For each of the commands, we will reference the Capital Agent sample defined on LLM agent page. We will assume it's in a directory (eg: capital_agent).
对于每个命令,我们将引用 LLM 智能体 页面上定义的 Capital Agent 示例。我们假设它在一个目录中 (例如:capital_agent)。
To proceed, confirm that your agent code is configured as follows:
要继续,请确认您的智能体代码配置如下:
- Agent code is in a file called
agent.pywithin your agent directory. - 智能体代码在智能体目录中名为
agent.py的文件中。 - Your agent variable is named
root_agent. - 您的智能体变量名为
root_agent。 __init__.pyis within your agent directory and containsfrom . import agent.__init__.py在您的智能体目录中,包含from . import agent。- Your
requirements.txtfile is present in the agent directory. - 您的
requirements.txt文件存在于智能体目录中。
- Your application's entry point (the main package and main() function) is in a single Go file. Using main.go is a strong convention.
- 您的应用程序的入口点 (主包和 main() 函数) 在单个 Go 文件中。使用 main.go 是一个强烈的约定。
- Your agent instance is passed to a launcher configuration, typically using agent.NewSingleLoader(yourAgent). The adkgo tool uses this launcher to start your agent with the correct services.
- 您的智能体实例被传递给启动器配置,通常使用 agent.NewSingleLoader(yourAgent)。adkgo 工具使用此启动器以正确的服务启动您的智能体。
- Your go.mod and go.sum files are present in your project directory to manage dependencies.
- 您的 go.mod 和 go.sum 文件存在于项目目录中以管理依赖项。
Refer to the following section for more details. You can also find a sample app in the Github repo. 有关更多详细信息,请参阅以下部分。您还可以在 Github 仓库中找到示例应用程序。
- Agent code is in a file called
CapitalAgent.javawithin your agent directory. - 智能体代码在智能体目录中名为
CapitalAgent.java的文件中。 - Your agent variable is global and follows the format
public static final BaseAgent ROOT_AGENT. - 您的智能体变量是全局的,遵循格式
public static final BaseAgent ROOT_AGENT。 - Your agent definition is present in a static class method.
- 您的智能体定义存在于静态类方法中。
Refer to the following section for more details. You can also find a sample app in the Github repo. 有关更多详细信息,请参阅以下部分。您还可以在 Github 仓库中找到示例应用程序。
Environment variables¶
环境变量¶
Set your environment variables as described in the Setup and Installation guide.
按照设置和安装指南中的描述设置您的环境变量。
export GOOGLE_CLOUD_PROJECT=your-project-id # Your GCP project ID
# 您的 GCP 项目 ID
export GOOGLE_CLOUD_LOCATION=us-central1 # Or your preferred location
# 或您的首选位置
export GOOGLE_GENAI_USE_VERTEXAI=true # Set to true if using Vertex AI
# 如果使用 Vertex AI,则设置为 true
Note
If you want to use Google AI Studio as your LLM provider instead of Vertex AI,
set GOOGLE_GENAI_USE_VERTEXAI=false and provide your GOOGLE_API_KEY.
如果您想使用 Google AI Studio 作为 LLM 提供程序而不是 Vertex AI,
请设置 GOOGLE_GENAI_USE_VERTEXAI=false 并提供您的 GOOGLE_API_KEY。
Deploy using adk deploy cloud_run¶
使用 adk deploy cloud_run 部署¶
The adk deploy cloud_run command creates a Cloud Run deployment for your agent.
It handles building and pushing a container image, setting up environment variables,
and creating the Cloud Run service.
adk deploy cloud_run 命令为您的智能体创建 Cloud Run 部署。它处理构建和推送容器映像、设置环境变量以及创建 Cloud Run 服务。
This command will: 此命令将: 1. Build a container image for your agent. 为您的智能体构建容器映像。 2. Push the image to Google Artifact Registry. 将映像推送到 Google Artifact Registry。 3. Create a Cloud Run service with the necessary environment variables. 创建具有必要环境变量的 Cloud Run 服务。
This command uses the adkgo tool to package your Go application and deploy it to Cloud Run. 此命令使用 adkgo 工具打包您的 Go 应用程序并将其部署到 Cloud Run。
Deploy using gcloud run deploy¶
使用 gcloud run deploy 部署¶
Alternatively, you can deploy using the standard gcloud run deploy command.
This requires you to manually build and push a container image.
或者,您可以使用标准的 gcloud run deploy 命令进行部署。这需要您手动构建和推送容器映像。
-
Build and push the container image: 构建并推送容器映像:
-
Deploy to Cloud Run: 部署到 Cloud Run:
gcloud run deploy capital_agent \ --image gcr.io/$GOOGLE_CLOUD_PROJECT/capital_agent \ --platform managed \ --region $GOOGLE_CLOUD_LOCATION \ --allow-unauthenticated \ --set-env-vars=GOOGLE_CLOUD_PROJECT=$GOOGLE_CLOUD_PROJECT,GOOGLE_CLOUD_LOCATION=$GOOGLE_CLOUD_LOCATION,GOOGLE_GENAI_USE_VERTEXAI=$GOOGLE_GENAI_USE_VERTEXAI
-
Build the Go binary: 构建 Go 二进制文件:
-
Create a Dockerfile: 创建 Dockerfile:
-
Build and push the container image: 构建并推送容器映像:
-
Deploy to Cloud Run: 部署到 Cloud Run:
-
Build the Java application: 构建 Java 应用程序:
-
Create a Dockerfile: 创建 Dockerfile:
-
Build and push the container image: 构建并推送容器映像:
-
Deploy to Cloud Run: 部署到 Cloud Run:
Accessing your deployed agent¶
访问您部署的智能体¶
After deployment, you can access your agent's URL:
部署后,您可以访问您的智能体的 URL:
You can then send requests to your agent using the obtained URL.
然后,您可以使用获得的 URL 向您的智能体发送请求。
Monitoring and logs¶
监控和日志¶
You can monitor your deployed agent using Cloud Run's built-in monitoring:
您可以使用 Cloud Run 的内置监控来监控您部署的智能体: