通过这个快速入门,您将学习如何创建一个简单的 Agent,并使用 ADK Streaming 实现与其进行低延迟、双向的语音和视频通信。我们将安装 ADK,设置一个基础的“Google 搜索”Agent,尝试使用 adk web 工具通过 Streaming 运行 Agent,然后解释如何使用 ADK Streaming 和 FastAPI 自行构建一个简单的异步 Web 应用。

注意: 本指南假设您有在 Windows、Mac 和 Linux 环境下使用终端的经验。

支持语音/视频流的模型

语音和视频流需要支持 Live API 的 Gemini 模型。您可以在文档中找到支持该功能的 模型 ID:

1. 设置环境并安装 ADK

创建并激活虚拟环境(推荐):

bash 6 行 展开 收起
# Create
python3 -m venv .venv
# Activate (each new terminal)
# macOS/Linux: source .venv/bin/activate
# Windows CMD: .venv\Scripts\activate.bat
# Windows PowerShell: .venv\Scripts\Activate.ps1

安装 ADK:

bash 1 行 展开 收起
pip install google-adk

2. 项目结构

创建以下文件夹结构,并包含空文件:

console 6 行 展开 收起
adk-streaming/  # Project folder
└── app/ # the web app folder
    ├── .env # Gemini API key
    └── google_search_agent/ # Agent folder
        ├── __init__.py # Python package
        └── agent.py # Agent definition

agent.py

将以下代码块复制粘贴到 agent.py 文件中。

对于 model,请按照前面 Models section 中的描述仔细检查模型 ID。

py 17 行 展开 收起
from google.adk.agents import Agent
from google.adk.tools import google_search  # Import the tool

root_agent = Agent(
   # Agent 的独特名称。
   name="basic_search_agent",
   # Agent 将使用的大型语言模型(LLM)。
   # 请填写支持实时功能的最新模型ID。
   # https://adk.dev/live/get-started/streaming-python/#supported-models
   model="...",
   # Agent 用途的简短描述。
   description="Agent to answer questions using Google Search.",
   # 设置 Agent 行为的说明。
   instruction="You are an expert researcher. You always stick to the facts.",
   # 添加 google_search 工具以通过 Google 搜索执行信息获取。
   tools=[google_search]
)

agent.py 是存储你所有 Agent 逻辑的地方,并且你必须定义一个 root_agent。

注意你是如何轻松地将 Google Search 功能] 与基础集成的。Agent 类和 google_search 工具处理了与 LLM 和搜索 API 的复杂交互,使你能够专注于 Agent 的目的和行为。

intro_components.png

将以下代码块复制粘贴到 __init__.py 文件中。

py 1 行 展开 收起
from . import agent

3. 设置平台

要运行 Agent,请从 Google AI Studio 或 Google Cloud Agent Platform 中选择一个平台:

有关从 ADK Agent 连接到 Google Cloud 的更多信息,请参见 连接到 Google Cloud 和 Agent 平台 。

4. 使用 adk web 尝试 Agent

现在可以尝试 Agent 了。运行以下命令以启动 开发界面。首先,确保将当前目录设置为 app:

shell 1 行 展开 收起
cd app

另外,使用以下命令设置 SSL_CERT_FILE 变量。这对于后续的语音和视频测试是必需的。

然后,运行开发者界面:

shell 1 行 展开 收起
adk web
在浏览器中**直接打开提供的 URL**(通常是 `http://localhost:8000` 或 `http://127.0.0.1:8000`)。此连接完全在您的本地机器上进行。选择 `google_search_agent`。

尝试语音和视频

要尝试语音,请重新加载浏览器,点击麦克风按钮启用语音输入,并用语音提出以下问题。Agent 将使用 google_search 工具获取最新信息以回答这些问题。您将实时听到语音回答。

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

要尝试视频,请重新加载浏览器,点击摄像头按钮启用视频输入,并提出类似“你看见什么?”的问题。Agent 将回答视频输入中看到的内容。

注意事项

  • 您无法使用文字聊天与 native-audio 模型。当在 adk web 输入文本消息时,您将看到错误。

停止工具

按 Ctrl-C 来停止控制台上的 adk web。

ADK 流媒体说明

模型回调(before_model_callback 和 after_model_callback)不会在流媒体路径上调用;ADK 仅在 run_async 路径上执行它们。Agent 回调(before_agent_callback、after_agent_callback)和工具回调(before_tool_callback、after_tool_callback)在流媒体过程中会执行,LongRunningFunctionTool 和 ExampleTool 也会执行。在工作流 Agent 中,仅 SequentialAgent 支持流媒体:LoopAgent 和 ParallelAgent 会引发 NotImplementedError。

恭喜!您已成功创建并与 ADK 的第一个流媒体 Agent 互动!

下一步:构建自定义流媒体应用

构建自定义服务器 讲解了使用 ADK 构建自定义异步 Web 应用的服务器端和客户端代码,实现实时双向音频和文本通信。从那里,会话与流媒体循环 深入涵盖了应用程序生命周期,事件 涵盖了一切,run_live() 回传给您。


原文:Build live streaming agent with Python · 固定源版本

最后更新 2026-11-12 23:26 . history