Google ADK Python Skill
您是Google Agent Development Kit (ADK) Python的专家指南 - 一个开源的、代码优先的工具包,用于构建、评估和部署AI代理。
使用此技能的时机
当用户需要时使用此技能:
- 构建具有工具集成和编排能力的AI代理
- 创建具有分层协调功能的多代理系统
- 为可预测的流水线实现工作流代理(顺序、并行、循环)
- 将LLM驱动的代理与Google搜索、代码执行或自定义工具集成
- 将代理部署到Vertex AI Agent Engine、Cloud Run或自定义基础设施
- 系统地评估和测试代理性能
- 实现工具执行的人工参与审批流程
核心概念
代理类型
LlmAgent: 具有动态路由和自适应行为的LLM驱动代理
- 用名称、模型、指令、描述和工具定义
- 支持子代理进行委托和协调
- 基于上下文的智能决策
Workflow Agents: 结构化的、可预测的编排模式
- SequentialAgent: 按定义顺序执行代理
- ParallelAgent: 并发运行多个代理
- LoopAgent: 使用迭代逻辑重复执行
BaseAgent: 自定义代理实现的基础
关键组件
Tools Ecosystem:
- 预构建工具(google_search、code_execution)
- 将自定义Python函数作为工具
- OpenAPI规范集成
- 用于人工批准的工具确认流程
Multi-Agent Architecture:
- 分层代理组合
- 针对特定领域的专业代理
- 用于委托的协调器代理
安装
# 稳定版本(推荐)
pip install google-adk
# 开发版本(最新功能)
pip install git+https://github.com/google/adk-python.git@main
实现模式
带有工具的单代理
from google.adk.agents import LlmAgent
from google.adk.tools import google_search
agent = LlmAgent(
name="search_assistant",
model="gemini-2.5-flash",
instruction="You are a helpful assistant that searches the web for information.",
description="Search assistant for web queries",
tools=[google_search]
)
多代理系统
from google.adk.agents import LlmAgent
# 专业代理
researcher = LlmAgent(
name="Researcher",
model="gemini-2.5-flash",
instruction="Research topics thoroughly using web search.",
tools=[google_search]
)
writer = LlmAgent(
name="Writer",
model="gemini-2.5-flash",
instruction="Write clear, engaging content based on research.",
)
# 协调器代理
coordinator = LlmAgent(
name="Coordinator",
model="gemini-2.5-flash",
instruction="Delegate tasks to researcher and writer agents.",
sub_agents=[researcher, writer]
)
自定义工具创建
from google.adk.tools import Tool
def calculate_sum(a: int, b: int) -> int:
"""Calculate the sum of two numbers."""
return a + b
# 将函数转换为工具
sum_tool = Tool.from_function(calculate_sum)
agent = LlmAgent(
name="calculator",
model="gemini-2.5-flash",
tools=[sum_tool]
)
顺序工作流
from google.adk.agents import SequentialAgent
workflow = SequentialAgent(
name="research_workflow",
agents=[researcher, summarizer, writer]
)
并行工作流
from google.adk.agents import ParallelAgent
parallel_research = ParallelAgent(
name="parallel_research",
agents=[web_researcher, paper_researcher, expert_researcher]
)
人工参与循环
from google.adk.tools import google_search
# 需要确认的工具
agent = LlmAgent(
name="careful_searcher",
model="gemini-2.5-flash",
tools=[google_search],
tool_confirmation=True # 执行前需要批准
)
部署选项
Cloud Run 部署
# 容器化代理
docker build -t my-agent .
# 部署到 Cloud Run
gcloud run deploy my-agent --image my-agent
Vertex AI Agent Engine
# 部署到 Vertex AI 以实现可扩展的代理托管
# 与 Google Cloud 的托管基础设施集成
自定义基础设施
# 在本地或自定义服务器上运行代理
# 对部署环境的完全控制
模型支持
优化用于 Gemini:
- gemini-2.5-flash
- gemini-2.5-pro
- gemini-1.5-flash
- gemini-1.5-pro
模型无关: 虽然针对 Gemini 优化,但 ADK 通过标准 API 支持其他 LLM 提供商。
最佳实践
- 代码优先哲学: 在 Python 中定义代理,便于版本控制、测试和灵活性
- 模块化设计: 创建针对特定领域的专业代理,组合成系统
- 工具集成: 利用预构建工具,用自定义函数扩展
- 评估: 系统地针对测试用例测试代理
- 安全性: 为敏感操作实现确认流程
- 分层结构: 使用协调器代理处理复杂的多代理工作流
- 工作流选择: 对于可预测的流水线选择工作流代理,对于动态路由选择 LLM 代理
常见用例
- 研究助手: 网络搜索 + 总结 + 报告生成
- 代码助手: 代码执行 + 文档 + 调试
- 客户支持: 查询路由 + 知识库 + 升级
- 内容创作: 研究 + 写作 + 编辑流水线
- 数据分析: 数据获取 + 处理 + 可视化
- 任务自动化: 具有条件逻辑的多步骤工作流
开发 UI
ADK 包括内置界面,用于:
- 交互式测试代理行为
- 调试工具调用和响应
- 评估代理性能
- 迭代代理设计
资源
- GitHub: https://github.com/google/adk-python
- 文档: https://google.github.io/adk-docs/
- llms.txt: https://raw.githubusercontent.com/google/adk-python/refs/heads/main/llms.txt
实现工作流
实现基于 ADK 的代理时:
- 定义需求: 确定代理能力和所需工具
- 选择架构: 单代理、多代理或基于工作流
- 选择工具: 预构建、自定义函数或 OpenAPI 集成
- 实现代理: 创建带有指令和工具的代理定义
- 本地测试: 使用开发 UI 进行迭代
- 添加评估: 为系统验证创建测试用例
- 部署: 选择 Cloud Run、Vertex AI 或自定义基础设施
- 监控: 跟踪代理性能并迭代
记住: ADK 将代理开发视为传统软件工程 - 使用版本控制、编写测试并遵循工程最佳实践。