快速入门¶
本快速入门指南将引导你完成 Agent Development Kit (ADK) 的安装、设置带有多个工具的基本智能体,并在终端或交互式的基于浏览器的开发 UI 中本地运行它。
本快速入门假设你有一个本地 IDE(VS Code、PyCharm 等)以及 Python 3.9+ 和终端访问权限。这种方法将应用程序完全运行在你的机器上,推荐用于内部开发。
1. 设置环境并安装 ADK¶
创建并激活虚拟环境(推荐):
# 创建
python -m venv .venv
# 激活(每次打开新终端时)
# macOS/Linux: source .venv/bin/activate
# Windows CMD: .venv\Scripts\activate.bat
# Windows PowerShell: .venv\Scripts\Activate.ps1
安装 ADK:
2. 创建智能体项目¶
项目结构¶
你需要创建以下项目结构:
创建文件夹 multi_tool_agent
:
Note for Windows users
When using ADK on Windows for the next few steps, we recommend creating
Python files using File Explorer or an IDE because the following commands
(mkdir
, echo
) typically generate files with null bytes and/or incorrect
encoding.
__init__.py
¶
现在在文件夹中创建一个 __init__.py
文件:
你的 __init__.py
现在应该是这样的:
agent.py
¶
在同一文件夹中创建一个 agent.py
文件:
将以下代码复制并粘贴到 agent.py
中:
import datetime
from zoneinfo import ZoneInfo
from google.adk.agents import Agent
def get_weather(city: str) -> dict:
"""Retrieves the current weather report for a specified city.
Args:
city (str): The name of the city for which to retrieve the weather report.
Returns:
dict: status and result or error msg.
"""
if city.lower() == "new york":
return {
"status": "success",
"report": (
"The weather in New York is sunny with a temperature of 25 degrees"
" Celsius (41 degrees Fahrenheit)."
),
}
else:
return {
"status": "error",
"error_message": f"Weather information for '{city}' is not available.",
}
def get_current_time(city: str) -> dict:
"""Returns the current time in a specified city.
Args:
city (str): The name of the city for which to retrieve the current time.
Returns:
dict: status and result or error msg.
"""
if city.lower() == "new york":
tz_identifier = "America/New_York"
else:
return {
"status": "error",
"error_message": (
f"Sorry, I don't have timezone information for {city}."
),
}
tz = ZoneInfo(tz_identifier)
now = datetime.datetime.now(tz)
report = (
f'The current time in {city} is {now.strftime("%Y-%m-%d %H:%M:%S %Z%z")}'
)
return {"status": "success", "report": report}
root_agent = Agent(
name="weather_time_agent",
model="gemini-2.0-flash",
description=(
"Agent to answer questions about the time and weather in a city."
),
instruction=(
"You are a helpful agent who can answer user questions about the time and weather in a city."
),
tools=[get_weather, get_current_time],
)
.env
¶
在同一文件夹中创建一个 .env
文件:
更多关于此文件的说明,将在下一节设置模型中介绍。
3. 设置模型¶
你的智能体理解用户请求和生成响应的能力由大语言模型(LLM)提供支持。你的智能体需要安全地调用这个外部 LLM 服务,这需要身份验证凭据。如果没有有效的身份验证,LLM 服务将拒绝智能体的请求,智能体将无法运行。
- 从 Google AI Studio 获取 API 密钥。
- 打开位于 (
multi_tool_agent/
) 内的.env
文件,并复制粘贴以下代码。 - 将
GOOGLE_API_KEY
替换为你的实际API KEY
。
- 你需要一个现有的 Google Cloud 账户和项目。
- 设置一个 Google Cloud 项目
- 设置 gcloud CLI
- 通过在终端运行
gcloud auth login
来验证 Google Cloud。 - 启用 Vertex AI API。
- 打开位于 (
multi_tool_agent/
) 内的.env
文件。复制粘贴以下代码并更新项目 ID 和位置。
4. 运行你的智能体¶
使用终端,导航到你的智能体项目的父目录(例如使用 cd ..
):
有多种方式可以与你的智能体交互:
运行以下命令来启动开发 UI。
步骤 1: 直接在浏览器中打开提供的 URL(通常是 http://localhost:8000
或 http://127.0.0.1:8000
)。
步骤 2: 在 UI 的左上角,你可以在下拉菜单中选择你的智能体。选择 "multi_tool_agent"。
故障排除
如果你在下拉菜单中看不到 "multi_tool_agent",请确保你在智能体文件夹的父文件夹中运行 adk web
(即 multi_tool_agent 的父文件夹)。
步骤 3: 现在你可以使用文本框与你的智能体聊天:
步骤 4: 你还可以通过点击操作来检查单个函数调用、响应和模型响应:
步骤 5: 你还可以启用麦克风与你的智能体对话:
语音/视频流的模型支持
为了在 ADK 中使用语音/视频流式处理,你需要使用支持 Live API 的 Gemini 模型。你可以在文档中找到支持 Gemini Live API 的模型 ID:
然后你可以替换之前创建的 agent.py
文件中 root_agent
的 model
字符串(跳转到该部分)。你的代码应该类似于:
adk api_server
使你能够通过单个命令创建本地 FastAPI 服务器,使你能够在部署智能体之前测试本地 cURL 请求。
要了解如何使用 adk api_server
进行测试,请参阅测试文档。
📝 示例提示¶
- 纽约的天气如何?
- 纽约现在几点?
- 巴黎的天气如何?
- 巴黎现在几点?
🎉 恭喜!¶
你已经成功创建并与你的第一个使用 ADK 的智能体进行了交互!