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) ✗ 

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

与我交流

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

相关推荐
bug菌3 小时前
还在为编程效率发愁?字节跳动Trae如何让你秒变“代码大师“!
后端·ai编程·trae
一只爱撸猫的程序猿3 小时前
创建一个使用 Spring AI 配合 Function Calling 的实际场景案例:智能客服助手
spring boot·aigc·ai编程
量子位3 小时前
华为诺亚首席研究员,也具身智能创业了
ai编程·harmonyos
聚客AI3 小时前
👉FastMCP深度解析:ctx.sample() 如何实现LLM任务逆向委托
人工智能·llm·mcp
老崔AI编程3 小时前
牛马变高铁,用ai编程做100个app第一期:飞机大战
ai编程
POLOAPI4 小时前
震撼!GPT-5已来,我用3种方法将API成本降低90%(附完整代码)
人工智能·chatgpt·ai编程
sorryhc4 小时前
CSR秒开有可能么?(附AI驱动学习实践推理过程)
前端·javascript·ai编程
AI大模型4 小时前
超火爆,星标 28.2 k 的一个开源特权账号管理 (PAM) 工具
程序员·llm·agent
AI大模型4 小时前
星标 17.4 k 开源的企业级智能平台,开箱即用,超级便捷
程序员·llm·agent