本指南向您展示如何使用 Python 的 Agent Development Kit(ADK)快速启动。在开始之前,请确保您已安装以下内容:

  • Python 3.10 或更高版本
  • pip 用于安装软件包

安装

通过运行以下命令来安装 ADK:

shell 1 行 展开 收起
pip install google-adk
建议:创建并激活一个 Python 虚拟环境

创建一个 Python 虚拟环境:

shell 1 行 展开 收起
python3 -m venv .venv

激活 Python 虚拟环境:

创建一个 Agent 项目

运行 adk create 命令以启动一个新的 Agent 项目。

shell 1 行 展开 收起
adk create my_agent

探索 Agent 项目

创建的 Agent 项目具有以下结构,其中 agent.py 文件包含 Agent 的主要控制代码。

none 4 行 展开 收起
my_agent/
    agent.py      # main agent code
    .env          # API keys or project IDs
    __init__.py

更新您的 Agent 项目

agent.py 文件包含一个 root_agent 定义,这是 ADK Agent 的唯一必要元素。您还可以为 Agent 定义要使用的工具。更新生成的 agent.py 代码以包含一个 get_current_time 工具供 Agent 使用,如以下代码所示:

python 14 行 展开 收起
from google.adk.agents.llm_agent import Agent

# 模拟工具实现
def get_current_time(city: str) -> dict:
    """Returns the current time in a specified city."""
    return {"status": "success", "city": city, "time": "10:30 AM"}

root_agent = Agent(
    model='gemini-flash-latest',
    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],
)

设置您的 API 密钥

该项目使用 Gemini API,这需要一个 API 密钥。如果您还没有 Gemini API 密钥,请在 Google AI Studio 的 API Keys 页面](#) 创建一个密钥。

在终端窗口中,将您的 API 密钥写入 .env 文件作为环境变量:

使用其他 AI 模型与 ADK

ADK 支持使用许多生成式 AI 模型。有关在 ADK Agent 中配置其他模型的更多信息,请参见 模型 & 身份验证。

运行您的 Agent

您可以使用交互式命令行界面运行您的 ADK Agent,使用 adk run 命令或 ADK 提供的 ADK Web 用户界面,通过 adk web 命令。 这两种选项都允许您测试和与您的 Agent 进行交互。

使用命令行界面运行

使用 adk run 命令行工具运行您的 Agent。

console 1 行 展开 收起
adk run my_agent

adk-run.png

使用网页界面运行

ADK 框架提供了网页界面,您可以用它来测试和与您的 Agent 互动。您可以使用以下命令启动网页界面:

console 1 行 展开 收起
adk web --port 8000
此命令启动一个带有聊天界面的Web服务器以供您的 Agent 使用。您可以在`http://localhost:8000`访问Web界面。在左上角选择 Agent 并输入请求。

adk-web-dev-ui-chat.png

下一步:构建您的 Agent

现在您已经安装了 ADK 并运行了第一个 Agent,尝试按照我们的构建指南构建您自己的 Agent:


原文:Python Quickstart for ADK · 固定源版本

最后更新 2026-11-12 22:49 . history