Skip to content

快速入门

本快速入门指南将引导你完成 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:

pip install google-adk

2. 创建智能体项目

项目结构

你需要创建以下项目结构:

parent_folder/
    multi_tool_agent/
        __init__.py
        agent.py
        .env

创建文件夹 multi_tool_agent

mkdir 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 文件:

echo "from . import agent" > multi_tool_agent/__init__.py

你的 __init__.py 现在应该是这样的:

multi_tool_agent/__init__.py
from . import agent

agent.py

在同一文件夹中创建一个 agent.py 文件:

touch multi_tool_agent/agent.py

将以下代码复制并粘贴到 agent.py 中:

multi_tool_agent/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 文件:

touch multi_tool_agent/.env

更多关于此文件的说明,将在下一节设置模型中介绍。

intro_components.png

3. 设置模型

你的智能体理解用户请求和生成响应的能力由大语言模型(LLM)提供支持。你的智能体需要安全地调用这个外部 LLM 服务,这需要身份验证凭据。如果没有有效的身份验证,LLM 服务将拒绝智能体的请求,智能体将无法运行。

  1. Google AI Studio 获取 API 密钥。
  2. 打开位于 (multi_tool_agent/) 内的 .env 文件,并复制粘贴以下代码。
    multi_tool_agent/.env
    GOOGLE_GENAI_USE_VERTEXAI=FALSE
    GOOGLE_API_KEY=在此粘贴你的实际 API 密钥
    
  3. GOOGLE_API_KEY 替换为你的实际 API KEY
  1. 你需要一个现有的 Google Cloud 账户和项目。
  2. 打开位于 (multi_tool_agent/) 内的 .env 文件。复制粘贴以下代码并更新项目 ID 和位置。
    multi_tool_agent/.env
    GOOGLE_GENAI_USE_VERTEXAI=TRUE
    GOOGLE_CLOUD_PROJECT=你的项目 ID
    GOOGLE_CLOUD_LOCATION=位置
    

4. 运行你的智能体

使用终端,导航到你的智能体项目的父目录(例如使用 cd ..):

parent_folder/      <-- 导航到这个目录
    multi_tool_agent/
        __init__.py
        agent.py
        .env

有多种方式可以与你的智能体交互:

运行以下命令来启动开发 UI

adk web

步骤 1: 直接在浏览器中打开提供的 URL(通常是 http://localhost:8000http://127.0.0.1:8000)。

步骤 2: 在 UI 的左上角,你可以在下拉菜单中选择你的智能体。选择 "multi_tool_agent"。

故障排除

如果你在下拉菜单中看不到 "multi_tool_agent",请确保你在智能体文件夹的父文件夹中运行 adk web(即 multi_tool_agent 的父文件夹)。

步骤 3: 现在你可以使用文本框与你的智能体聊天:

adk-web-dev-ui-chat.png

步骤 4: 你还可以通过点击操作来检查单个函数调用、响应和模型响应:

adk-web-dev-ui-function-call.png

步骤 5: 你还可以启用麦克风与你的智能体对话:

语音/视频流的模型支持

为了在 ADK 中使用语音/视频流式处理,你需要使用支持 Live API 的 Gemini 模型。你可以在文档中找到支持 Gemini Live API 的模型 ID

然后你可以替换之前创建的 agent.py 文件中 root_agentmodel 字符串(跳转到该部分)。你的代码应该类似于:

root_agent = Agent(
    name="weather_time_agent",
    model="replace-me-with-model-id", #例如 gemini-2.0-flash-live-001
    ...

adk-web-dev-ui-audio.png

运行以下命令,与你的 Google 搜索智能体聊天。

adk run multi_tool_agent

adk-run.png

要退出,使用 Cmd/Ctrl+C。

adk api_server 使你能够通过单个命令创建本地 FastAPI 服务器,使你能够在部署智能体之前测试本地 cURL 请求。

adk-api-server.png

要了解如何使用 adk api_server 进行测试,请参阅测试文档

📝 示例提示

  • 纽约的天气如何?
  • 纽约现在几点?
  • 巴黎的天气如何?
  • 巴黎现在几点?

🎉 恭喜!

你已经成功创建并与你的第一个使用 ADK 的智能体进行了交互!


🛣️ 下一步

  • 转到教程:了解如何为你的智能体添加记忆、会话、状态:教程
  • 深入高级配置: 探索设置部分,深入了解项目结构、配置和其他接口。
  • 理解核心概念: 了解智能体概念