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

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

与我交流

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

相关推荐
用户4099322502122 小时前
为什么Vue 3的计算属性能解决模板臃肿、性能优化和双向同步三大痛点?
前端·ai编程·trae
数据智能老司机2 小时前
构建多智能体系统——使用工具
llm·agent·mcp
数据智能老司机3 小时前
构建一个 DeepSeek 模型——通过键值缓存(Key-Value Cache, KV Cache)解决推理瓶颈
架构·llm·deepseek
在未来等你5 小时前
AI Agent设计模式 Day 3:Self-Ask模式:自我提问驱动的推理链
设计模式·llm·react·ai agent·plan-and-execute
赋范大模型技术社区5 小时前
LangChain 1.0 实战: NL2SQL 数据分析 Agent
数据分析·langchain·实战·agent·教程·nl2sql·langchain1.0
Larcher19 小时前
新手也能学会,100行代码玩AI LOGO
前端·llm·html
架构师日志20 小时前
使用大模型+LangExtract从复杂文本提取结构化数据(三)——提取表格列表类型数据
llm
智泊AI21 小时前
AI圈炸锅了!大模型的下一片蓝海,彻底爆发了!
llm
六月的可乐21 小时前
实战干货-Vue实现AI聊天助手全流程解析
前端·vue.js·ai编程
常先森1 天前
【解密源码】 RAGFlow 切分最佳实践- naive parser 语义切块(excel & csv & txt 篇)
架构·llm·agent