ADK 的 Python 快速入门¶
本指南展示了如何开始使用 Python Agent Development Kit (ADK)。开始之前,请确保你已安装了以下软件:
- Python 3.9 或更高版本
pip
用于安装包
Installation¶
通过运行以下命令安装 ADK:
建议:创建并激活 Python 虚拟环境
创建一个 Python 虚拟环境:
激活 Python 虚拟环境:
Create an agent project¶
运行 adk create
命令来开始一个新智能体项目。
Explore the agent project¶
创建的智能体项目具有以下结构,其中 agent.py
文件包含智能体的主要控制代码。
Update your agent project¶
agent.py
文件包含一个 root_agent
定义,这是
ADK 智能体所需的唯一元素。你也可以定义工具供智能体
使用。更新生成的 agent.py
代码以包含 get_current_time
工具
供智能体使用,如下列代码所示:
from google.adk.agents.llm_agent import Agent
# Mock 工具实现
def get_current_time(city: str) -> dict:
"""返回指定城市中的当前时间。"""
return {"status": "success", "city": city, "time": "10:30 AM"}
root_agent = Agent(
model='gemini-2.5-flash',
name='root_agent',
description="Tells the current time in a specified city.",
instruction="You are a helpful assistant that tells the current time in cities. Use the 'get_current_time' tool for this purpose.",
tools=[get_current_time],
)
Set your API key¶
此项目使用需要 API 密钥的 Gemini API。如果你 还没有 Gemini API 密钥,请在 Google AI Studio 上的 API 密钥 页面创建一个。
在终端窗口中,将你的 API 密钥写入 .env
文件作为环境变量:
使用 ADK 的其他 AI 模型
ADK 支持使用许多生成式 AI 模型。有关在 ADK 智能体中配置其他模型的更多信息,请参阅 模型与身份验证。
Run your agent¶
你可以使用交互式命令行界面和 adk run
命令运行你的 ADK 智能体,或使用 ADK 提供的
adk web
命令的 ADK 网页用户界面。这两个选项都允许你测试并与你的
智能体交互。
Run with command-line interface¶
使用 adk run
命令行工具运行你的智能体。
Run with web interface¶
ADK 框架提供网页界面,你可用来测试并与 智能体交互。你可以使用以下命令启动网页界面:
此命令启动一个带有你的智能体聊天界面的网页服务器。你可以 在 (http://localhost:8000) 访问网页界面。在 右上角选择智能体并键入请求。
Next: Build your agent¶
现在你已经安装了 ADK 并运行了你的第一个智能体,尝试使用我们的构建指南 构建你自己的智能体: