测试你的智能体¶
在部署你的智能体之前,你应该对其进行测试,以确保它能按预期工作。在开发环境中测试智能体的最简单方法是使用 adk api_server
命令。此命令将启动一个本地 FastAPI 服务器,你可以运行 cURL 命令或发送 API 请求来测试你的智能体。
本地测试¶
本地测试涉及启动本地 API 服务器、创建会话并向你的智能体发送查询。首先,确保你在正确的工作目录下:
启动本地服务器
接下来,启动本地 FastAPI 服务器:
输出应该类似于:
INFO: Started server process [12345]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
你的服务器现在在本地 http://0.0.0.0:8000
运行。
创建新会话
在 API 服务器仍在运行的情况下,打开一个新的终端窗口或标签页,并使用以下命令为智能体创建新会话:
curl -X POST http://0.0.0.0:8000/apps/my_sample_agent/users/u_123/sessions/s_123 \
-H "Content-Type: application/json" \
-d '{"state": {"key1": "value1", "key2": 42}}'
让我们来分析这个命令:
http://0.0.0.0:8000/apps/my_sample_agent/users/u_123/sessions/s_123
:这将为你的智能体my_sample_agent
(即智能体文件夹的名称)创建一个新会话,其中包含用户 ID(u_123
)和会话 ID(s_123
)。你可以将my_sample_agent
替换为你的智能体文件夹名称。你可以将u_123
替换为特定的用户 ID,将s_123
替换为特定的会话 ID。{"state": {"key1": "value1", "key2": 42}}
:这是可选的。你可以用它来自定义创建会话时智能体的预设状态(字典)。
如果创建成功,应该会返回会话信息。输出应该类似于:
{"id":"s_123","app_name":"my_sample_agent","user_id":"u_123","state":{"state":{"key1":"value1","key2":42}},"events":[],"last_update_time":1743711430.022186}
Info
你不能使用完全相同的用户 ID 和会话 ID 创建多个会话。如果你尝试这样做,可能会看到类似这样的响应:
{"detail":"Session already exists: s_123"}
。要解决这个问题,你可以删除该会话(例如 s_123
),或选择一个不同的会话 ID。
发送查询
有两种方式可以通过 POST 向你的智能体发送查询,分别是 /run
或 /run_sse
路由。
POST http://0.0.0.0:8000/run
:将所有事件收集为一个列表并一次性返回。适合大多数用户(如果你不确定,我们建议使用这个)。POST http://0.0.0.0:8000/run_sse
:以服务器发送事件(Server-Sent-Events)的形式返回,这是一个事件对象流。适合那些希望在事件可用时立即得到通知的用户。使用/run_sse
时,你还可以将streaming
设置为true
以启用令牌级流式传输。
使用 /run
curl -X POST http://0.0.0.0:8000/run \
-H "Content-Type: application/json" \
-d '{
"app_name": "my_sample_agent",
"user_id": "u_123",
"session_id": "s_123",
"new_message": {
"role": "user",
"parts": [{
"text": "Hey whats the weather in new york today"
}]
}
}'
如果使用 /run
,你将同时看到所有事件的完整输出,以列表形式,应该类似于:
[{"content":{"parts":[{"functionCall":{"id":"af-e75e946d-c02a-4aad-931e-49e4ab859838","args":{"city":"new york"},"name":"get_weather"}}],"role":"model"},"invocation_id":"e-71353f1e-aea1-4821-aa4b-46874a766853","author":"weather_time_agent","actions":{"state_delta":{},"artifact_delta":{},"requested_auth_configs":{}},"long_running_tool_ids":[],"id":"2Btee6zW","timestamp":1743712220.385936},{"content":{"parts":[{"functionResponse":{"id":"af-e75e946d-c02a-4aad-931e-49e4ab859838","name":"get_weather","response":{"status":"success","report":"The weather in New York is sunny with a temperature of 25 degrees Celsius (41 degrees Fahrenheit)."}}}],"role":"user"},"invocation_id":"e-71353f1e-aea1-4821-aa4b-46874a766853","author":"weather_time_agent","actions":{"state_delta":{},"artifact_delta":{},"requested_auth_configs":{}},"id":"PmWibL2m","timestamp":1743712221.895042},{"content":{"parts":[{"text":"OK. The weather in New York is sunny with a temperature of 25 degrees Celsius (41 degrees Fahrenheit).\n"}],"role":"model"},"invocation_id":"e-71353f1e-aea1-4821-aa4b-46874a766853","author":"weather_time_agent","actions":{"state_delta":{},"artifact_delta":{},"requested_auth_configs":{}},"id":"sYT42eVC","timestamp":1743712221.899018}]
使用 /run_sse
curl -X POST http://0.0.0.0:8000/run_sse \
-H "Content-Type: application/json" \
-d '{
"app_name": "my_sample_agent",
"user_id": "u_123",
"session_id": "s_123",
"new_message": {
"role": "user",
"parts": [{
"text": "Hey whats the weather in new york today"
}]
},
"streaming": false
}'
你可以将 streaming
设置为 true
以启用令牌级流式传输,这意味着响应将以多个数据块的形式返回给你,输出应该类似于:
data: {"content":{"parts":[{"functionCall":{"id":"af-f83f8af9-f732-46b6-8cb5-7b5b73bbf13d","args":{"city":"new york"},"name":"get_weather"}}],"role":"model"},"invocation_id":"e-3f6d7765-5287-419e-9991-5fffa1a75565","author":"weather_time_agent","actions":{"state_delta":{},"artifact_delta":{},"requested_auth_configs":{}},"long_running_tool_ids":[],"id":"ptcjaZBa","timestamp":1743712255.313043}
data: {"content":{"parts":[{"functionResponse":{"id":"af-f83f8af9-f732-46b6-8cb5-7b5b73bbf13d","name":"get_weather","response":{"status":"success","report":"The weather in New York is sunny with a temperature of 25 degrees Celsius (41 degrees Fahrenheit)."}}}],"role":"user"},"invocation_id":"e-3f6d7765-5287-419e-9991-5fffa1a75565","author":"weather_time_agent","actions":{"state_delta":{},"artifact_delta":{},"requested_auth_configs":{}},"id":"5aocxjaq","timestamp":1743712257.387306}
data: {"content":{"parts":[{"text":"OK. The weather in New York is sunny with a temperature of 25 degrees Celsius (41 degrees Fahrenheit).\n"}],"role":"model"},"invocation_id":"e-3f6d7765-5287-419e-9991-5fffa1a75565","author":"weather_time_agent","actions":{"state_delta":{},"artifact_delta":{},"requested_auth_configs":{}},"id":"rAnWGSiV","timestamp":1743712257.391317}
Info
如果使用 /run_sse
,你应该在每个事件可用时立即看到它。
集成¶
ADK 使用 回调 与第三方可观察性工具集成。这些集成捕获智能体调用和交互的详细跟踪,对于理解行为、调试问题和评估性能至关重要。
- Comet Opik 是一个开源的 LLM 可观察性和评估平台,原生支持 ADK。
部署你的智能体¶
现在你已经验证了智能体的本地运行情况,你已经准备好部署你的智能体了!以下是一些部署智能体的方式:
- 部署到 Agent Engine,这是将你的 ADK 智能体部署到 Google Cloud 的 Vertex AI 上的托管服务的最简单方式。
- 部署到 Cloud Run,你可以完全控制如何使用 Google Cloud 上的无服务器架构扩展和管理你的智能体。