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

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

与我交流

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

相关推荐
AI大模型6 小时前
别再瞎学大模型了,这份GitHub神级课程火爆全网(附GitHub 仓库)
程序员·llm·agent
孟健7 小时前
出海创业,第 1 美元到第 1000 美元,到底要怎么赚?
ai编程
舒一笑7 小时前
🚀 PandaCoder 2.0.0 - ES DSL Monitor & SQL Monitor 震撼发布!
后端·ai编程·intellij idea
viperrrrrrrrrr79 小时前
Agent向量存储中的记忆衰退与记忆过载解决方案
langchain·大模型·agent·rag
jz-炸芯片的zero9 小时前
【Zephyr存储专题】16_内存泄露检测可视化脚本自动化
自动化·嵌入式·ai编程·zephyr
rengang6610 小时前
智能化的重构建议:大模型分析代码结构,提出可读性和性能优化建议
人工智能·性能优化·重构·ai编程
慕晨师兄12 小时前
RAG从入门到精通-文档切分策略
ai编程
云起SAAS12 小时前
ai周公解梦抖音快手微信小程序看广告流量主开源
微信小程序·小程序·ai编程·看广告变现轻·ai周公解梦
后端小肥肠13 小时前
放弃漫画内卷!育儿赛道才是黑马,用 Coze 智能体做10w+育儿漫画,成品直接发
人工智能·agent·coze
Tang102416 小时前
Cursor AI 编程工具指南
ai编程·cursor