Google ADK + DeepSeek 快速构建 Agent

本文介绍如何使用 ADK + DeepSeek 创建一个简单的 Agent。

ADK 官方文档地址:google.github.io/adk-docs/

ADK GitHub 仓库地址:github.com/google/adk-...

本文示例代码 GitHub 仓库地址:github.com/jianghushin...

ADK 简介

Agent Development Kit (ADK) 是一个灵活、模块化的框架,用于开发和部署 AI agents。虽然它针对 Gemini 和 Google 生态系统进行了优化,但 ADK 是模型无关的、部署无关的,并且为与其他框架的兼容性而构建。ADK 的设计目标是让代理开发更像是软件开发,使开发者能够更容易地创建、部署和编排从简单任务到复杂工作流程的各种代理架构。

简单来讲,ADK 是 Google 开源的 Agent 开发框架。

前置依赖

编程语言:

Python

安装依赖:

bash 复制代码
$ pip install google-adk litellm

安装完以后,就有了一个叫 adk 命令行工具。

bash 复制代码
$ adk --help
Usage: adk [OPTIONS] COMMAND [ARGS]...

  Agent Development Kit CLI tools.

Options:
  --version  Show the version and exit.
  --help     Show this message and exit.

Commands:
  api_server  Starts a FastAPI server for agents.
  create      Creates a new app in the current folder with prepopulated agent template.
  deploy      Deploys agent to hosted environments.
  eval        Evaluates an agent given the eval sets.
  run         Runs an interactive CLI for a certain agent.
  web         Starts a FastAPI server with Web UI for agents.

快速开始

示例代码如下:

github.com/jianghushin...

python 复制代码
import datetime
from zoneinfo import ZoneInfo

from google.adk.agents import Agent
from google.adk.models.lite_llm import LiteLlm


def get_weather(city: str) -> dict:
    """获取指定城市的当前天气报告

    Args:
        city (str): 需要查询天气的城市名称

    Returns:
        dict: 包含状态和结果/错误消息的字典
    """
    if city == "杭州":
        return {
            "status": "success",
            "report": "杭州天气晴朗,气温 25 摄氏度(77 华氏度)"
        }
    else:
        return {
            "status": "error",
            "error_message": f"未找到'{city}'的天气信息"
        }


def get_current_time(city: str) -> dict:
    """获取指定城市的当前时间

    Args:
        city (str): 需要查询时间的城市名称

    Returns:
        dict: 包含状态和结果/错误消息的字典
    """
    if city == "杭州":
        tz_identifier = "Asia/Shanghai"
    else:
        return {
            "status": "error",
            "error_message": f"未找到{city}的时区信息"
        }

    tz = ZoneInfo(tz_identifier)
    now = datetime.datetime.now(tz)
    report = (
        f'{city}当前时间: {now.strftime("%Y-%m-%d %H:%M:%S %Z%z")}'
    )
    return {"status": "success", "report": report}


root_agent = Agent(
    name="weather_time_agent",
    model=LiteLlm(  # 适配 DeepSeek LLM
        model="deepseek/deepseek-chat",  # 注意:模型名称应该是:provider + / + model_name
        # base_url="...",
        api_key="替换成你的 API Key",
    ),
    description="用于回答城市时间和天气问题的助手代理",
    instruction="你是一个能帮助用户查询城市时间和天气的智能助手",
    tools=[get_weather, get_current_time],
)

示例中 LiteLlm 用于适配 DeepSeek 模型,ADK 是 Google 开发的,所以默认支持 Gemini 模型,为了支持 DeepSeek,需要使用 LiteLlm 进行适配,LiteLlm 支持上百种模型,非常强大。

注意事项:

LiteLlm 参数 model 的模型名称不能直接传 deepseek-chat,需要加上 provider 前缀。

执行命令 adk web 运行 Agent 程序:

bash 复制代码
(ai-examples) ➜  adk git:(main) ✗ adk web
INFO:     Started server process [79491]
INFO:     Waiting for application startup.

+-----------------------------------------------------------------------------+
| ADK Web Server started                                                      |
|                                                                             |
| For local testing, access at http://localhost:8000.                         |
+-----------------------------------------------------------------------------+

INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

浏览器打开 http://127.0.0.1:8000 ,执行 Agent 效果如下:

可以发现,调用了 get_weather 工具,并且由模型返回最终结果。

左侧 Event 可以看到工具调用结果:

图形化展示,效果不错。

此外,Request 可以看到模型配置和调用工具的请求:

Response 自不必说,读者可以自行体验。

另外,adk 还提供了命令行执行方式:

bash 复制代码
(ai-examples) ➜  adk git:(main) ✗ adk run multi_tool_agent
Running agent weather_time_agent, type exit to exit.
[user]: 杭州天气
12:58:24 - LiteLLM:INFO: utils.py:3296 - 
LiteLLM completion() model= deepseek-v3-0324; provider = deepseek
12:58:24 - LiteLLM:INFO: utils.py:3296 - 
LiteLLM completion() model= deepseek-v3-0324; provider = deepseek
[weather_time_agent]: 杭州当前天气晴朗,气温 25 摄氏度(77 华氏度)。
[user]: exit
(ai-examples) ➜  adk git:(main) ✗ 

希望此文能对你有所启发。

与我交流

欢迎通过以下方式联系我:

相关推荐
GJGCY15 小时前
技术解析|中国智能体4类路径深度拆解,这类底座架构优势凸显
人工智能·经验分享·ai·agent·智能体·数字员工
玄同76519 小时前
告别 AgentExecutor:LangChain v1.0+ Agent 模块深度迁移指南与实战全解析
人工智能·语言模型·自然语言处理·langchain·nlp·agent·智能体
GISer_Jing19 小时前
Memory、Rules、Skills、MCP如何重塑AI编程
前端·人工智能·aigc·ai编程
缘友一世20 小时前
张量并行和流水线并行原理深入理解与思考
学习·llm·pp·tp
阿杰学AI20 小时前
AI核心知识74——大语言模型之ReAct 范式(简洁且通俗易懂版)
人工智能·ai·语言模型·自然语言处理·aigc·agent·react范式
github.com/starRTC20 小时前
Claude Code中英文系列教程24:使用钩子hooks扩展 Claude Code 的行为
人工智能·ai编程
高铭杰21 小时前
LlamaIndex实用入门案例(可执行)
agent·llvm·rag·llamaindex
TGITCIC1 天前
LangChain入门(十三)- 6步实操Agent落地大法
langchain·agent·rag·ai agent·ai开发·agent开发·ai智能体开发
CoderJia程序员甲1 天前
GitHub 热榜项目 - 日榜(2026-01-30)
开源·大模型·llm·github·ai教程
Testopia1 天前
AI编程实例 - 爆款文章预测:K-Means聚类与分类算法的实践
人工智能·分类·kmeans·ai编程·聚类