5分钟为Google ADK 快速接入Deepseek国产大模型

1,007 阅读5分钟

5分钟为Google ADK 接入Deepseek国产大模型

快速教程:如何在Google ADK中集成国产Deepseek大模型。通过LiteLLM适配器,轻松让你的AI Agent使用强大的Deepseek模型,享受更高性价比的AI服务。本教程涵盖环境配置、代码实现和实际测试,让你快速掌握国产大模型的接入技巧。

什么是 Google ADK + Deepseek 组合

Google ADK (Agent Development Kit) 是Google推出的AI代理开发工具包,而Deepseek是国产优秀的大语言模型。通过LiteLLM适配器,我们可以轻松在Google ADK中集成Deepseek模型,享受更高性价比的AI服务。

今天我们用5分钟时间,教你如何为Google ADK接入Deepseek国产大模型。相比使用Google自家的Gemini模型,Deepseek具有以下优势:

  • 🌏 本土化服务 - 更稳定的国内网络访问,Gemini使用限制location,相当于禁用了
  • 🎯 强大性能 - Deepseek-Chat在多项基准测试中表现优异

学完这篇文章,你将具备以下能力:

  • 熟练配置Google ADK + Deepseek开发环境
  • 掌握使用LiteLLM适配器集成第三方模型
  • 学会测试和调试Deepseek驱动的AI Agent

1. 环境准备与依赖安装

虚拟环境配置(强烈推荐)

# 创建专用虚拟环境
python -m venv .venv

# 激活虚拟环境
# macOS/Linux:
source .venv/bin/activate
# Windows CMD:
# .venv\Scripts\activate.bat
# Windows PowerShell:
# .venv\Scripts\Activate.ps1

安装必要依赖

# 安装Google ADK
pip install google-adk

# 安装LiteLLM(用于连接Deepseek)
pip install litellm

2. 获取Deepseek API密钥

  1. 访问 Deepseek平台 注册账户
  2. 在控制台中创建API密钥
  3. 保存好你的API密钥,稍后需要配置到环境变量中

3. Agent项目构建

项目结构

parent_folder/
    deepseek_agent/
        __init__.py
        agent.py
        .env

创建项目文件夹

mkdir deepseek_agent/

创建 __init__.py

echo "from . import agent" > deepseek_agent/__init__.py

核心Agent实现

创建 deepseek_agent/agent.py 文件:

deepseek_agent/agent.py

import datetime
from zoneinfo import ZoneInfo
from google.adk.agents import Agent

# 关键:导入LiteLLM适配器
from google.adk.models.lite_llm import LiteLlm

def get_weather(city: str) -> dict:
    """Retrieves the current weather report for a specified city.

    Args:
        city (str): The name of the city for which to retrieve the weather report.

    Returns:
        dict: status and result or error msg.
    """
    if city.lower() in ["beijing", "北京"]:
        return {
            "status": "success",
            "report": (
                "北京今天天气晴朗,温度25摄氏度(77华氏度)。"
            ),
        }
    else:
        return {
            "status": "error",
            "error_message": f"抱歉,我没有'{city}'的天气信息。",
        }


def get_current_time(city: str) -> dict:
    """Returns the current time in a specified city.

    Args:
        city (str): The name of the city for which to retrieve the current time.

    Returns:
        dict: status and result or error msg.
    """

    if city.lower() in ["beijing", "北京"]:
        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}


# 核心配置:使用LiteLLM连接Deepseek模型
root_agent = Agent(
    name="weather_time_agent",
    model=LiteLlm(model="deepseek/deepseek-chat"),  # 关键改动
    description=(
        "一个可以回答城市时间和天气问题的智能助手"
    ),
    instruction=(
        "你是一个智能助手,可以回答用户关于城市时间和天气的问题。请始终使用中文回复,语气要友好亲切。"
    ),
    tools=[get_weather, get_current_time],
)

4. 配置环境变量

创建 deepseek_agent/.env 文件:

deepseek_agent/.env

# Deepseek API配置
DEEPSEEK_API_KEY=YOUR_DEEPSEEK_API_KEY_HERE

重要:将 YOUR_DEEPSEEK_API_KEY_HERE 替换为你从Deepseek平台获取的实际API密钥。

5. 运行你的Deepseek Agent

切换到Agent项目的上级目录:

parent_folder/      <-- 进入到这个目录
    deepseek_agent/
        __init__.py
        agent.py
        .env

方式1:可视化Web界面(推荐)

启动Web开发环境:

adk web

操作步骤:

  1. 浏览器访问 http://localhost:8000
  2. 左上角下拉菜单选择 "deepseek_agent"
  3. 开始与基于Deepseek模型的Agent对话

方式2:命令行终端

adk run deepseek_agent

方式3:API服务接口

adk api_server

💡 实战测试用例

测试你的Deepseek Agent:

中文对话测试:

用户:北京现在的天气怎么样?
Agent:🌤️ 北京今天天气晴朗,温度25摄氏度(77华氏度)。

时间查询测试:

用户:北京现在几点了?
Agent:🕐 北京的当前时间是 2024年12月25日 14:30:15 CST+0800

英文查询测试:

用户:What is the time in Beijing?
Agent:🕐 Beijing的当前时间是 2024122514:30:15 CST+0800

异常处理测试:

用户:上海的天气如何?
Agent:❌ 抱歉,我没有'上海'的天气信息。

🔧 技术要点解析

关键代码变更

相比使用Gemini模型,接入Deepseek只需要两个关键改动:

  1. 导入LiteLLM适配器
from google.adk.models.lite_llm import LiteLlm
  1. 指定Deepseek模型
model=LiteLlm(model="deepseek/deepseek-chat")

LiteLLM的优势

LiteLLM是一个统一的API接口,支持100+种大语言模型,包括:

  • OpenAI GPT系列
  • Anthropic Claude
  • 国产模型(Deepseek等)
  • 开源模型(Llama、Mistral等)

支持更多国产模型

LiteLLM支持的更多模型请查看:docs.litellm.ai/docs/provid…

🎉 成就解锁!

恭喜你成功为Google ADK接入了Deepseek国产大模型🎊!

你已经掌握了:

  • ✅ 使用LiteLLM适配器连接第三方模型
  • ✅ 配置Deepseek API密钥和环境变量
  • ✅ 创建基于国产大模型的AI Agent
  • ✅ 测试和调试Deepseek驱动的智能助手

现在你可以享受更高性价比的AI服务,接入国产大模型,不受国外模型的限制,继续探索更多模型和功能吧!🚀