简介
Agent Development Kit (ADK) 是一个灵活且模块化的框架,专为开发和部署AI智能体而设计。虽然ADK针对Gemini和Google生态系统进行了优化,但它与模型无关、与部署环境无关,并兼容其他框架。ADK的核心理念是让智能体开发更接近传统软件开发模式,从而帮助开发者更轻松地构建、部署和编排智能体架构——无论是处理简单任务,还是管理复杂工作流。
核心特性
-
丰富的工具生态:利用预置工具、自定义函数、OpenAPI规范或集成现有工具,赋予智能体多样化能力,实现与谷歌生态系统的深度整合。
-
代码优先开发:直接通过Python定义智能体逻辑、工具与任务编排,获得终极灵活性、可测试性与版本控制能力。
-
模块化多智能体系统:通过将多个专用智能体组合成灵活层级,设计可扩展的应用程序架构。
-
随处部署:轻松将智能体容器化部署至Cloud Run,或通过Vertex AI智能体引擎实现无缝扩展。
智能体开发套件(ADK) 旨在帮助开发者构建、管理、评估和部署由AI驱动的智能体。它提供了一个强大而灵活的环境,可用于创建对话式和非对话式智能体,使其能够处理复杂的任务和工作流。
LiteLLM
在介绍ADK之前,我们需要系统介绍ADK默认的大模型调用工具--LiteLLM的使用方法。在后续使用ADK时,选择LLM模型是必要的环节,而ADK目前只支持使用LiteLLM进行模型调用。
LiteLLM官网:LiteLLM
通过OpenAI标准输入/输出格式调用100+大语言模型
如何使用LiteLLM
您可以通过以下两种方式使用 LiteLLM:
-
1. LiteLLM 代理服务器(LLM 网关) 作为服务端调用 100+ 大语言模型、支持负载均衡、实现跨项目成本追踪
-
2. LiteLLM Python SDK 作为 Python 客户端调用 100+ 大语言模型、支持负载均衡、提供成本追踪功能
LiteLLM Python SDK
安装:
pip install litellm -i https://mirrors.aliyun.com/pypi/simple/
# 截至到目前新版不兼容ADK,请指定下面的版本号安装
pip install litellm==1.67.2
调用模型
示例:调用Deepseek
from litellm import completion
# 调用deepseek在线模型
response = completion(
model="deepseek/deepseek-chat",
api_base="https://api.deepseek.com",
api_key="sk-xxxxxd",
messages=[{ "content": "嘘寒问暖,不如打笔巨款!","role": "user"}]
)
print(response)
print(response.choices[0].message.content)
在 LiteLLM 上部署和调用多厂商模型指南:Providers | liteLLM
快速开始
安装ADK
pip install google-adk -i https://mirrors.aliyun.com/pypi/simple/
配置LiteLLM
首先我们需要创建一个模型,也被称为ADK的模型调用,这里需要采用ADK中封装的LiteLLM库来完成,需要注意的是,这个适配器负责配置好 model、认证、连接参数,然后统一管理模型调用接口。
from google.adk.models.lite_llm import LiteLlm
# 使用LiteLLM调用在线模型
model_client = LiteLlm(
model="deepseek/deepseek-chat",
api_base="https://api.deepseek.com",
api_key="sk-xxxxxd",
)
创建Agent
from google.adk.agents import Agent
# 创建Agent智能体
agent = Agent(
name="AI智能助手",
model=model_client,
description="AI智能助手",
instruction="你是一个乐于助人的AI智能助手",
)
创建Session
用于保存Agent运行过程中的多轮对话内容,ADK提供了一个InMemorySessionService类,其核心作用如下:
-
保存每轮用户输入
-
保存每轮模型输出
-
保存工作调用过程中的中间数据
-
提供历史对话上下文,让模型能基于完整对话继续推理
# 创建Session
# 在正式开启对话之前,我们需要创建一个初始session对象
# 为了方便区分不同的对话信息,该对象需要设置 APP名称、用户ID、对话ID,方便后续进行查询和调用:
APP_NAME = "app01"
USER_ID = "user01"
SESSION_ID = "session01"
session = session_service.create_session(
app_name=APP_NAME,
user_id=USER_ID,
session_id=SESSION_ID,
)
print(f"创建初始Session: {session.app_name} - {session.user_id} - {session.id}")
在后续对话中,我们实际上是带入了session_service进行对话,并读取session_service里面的历史对话信息作为上下文。这里可以将独单创建的session对象看作是一个中间对象或者一个备份。
创建 Runner
智能体执行器 Runner,是ADK中真正让Agent运行起来的引擎,其核心功能和Agents SDK中的Runner类似,具体作用如下:
-
会话管理:自动读取/写入 SessionService,维护历史信息。
-
Agent调用:调用指定的Agent完成推理和工具调用
-
输入输出流转:吧用户输入交给Agent,把Agent输出返回。
-
流程控制:支持多轮对话、子Agent委托、工具调用等。
-
生命周期管理:处理每次对话流程的完整生命周期
Runner 是Agent的管理者,负责组织每次对话交互的完整生命周期。
# 创建智能体执行器
runner = Runner(
agent=agent,
app_name="app01",
session_service=session_service
)
执行对话
from google.genai import types
async def call_agent_async(query: str, runner, user_id, session_id):
"""向agent发送查询并打印最终响应"""
print(f"\n>>> 用户查询: {query}")
# 将用户消息转换为ADK格式
content = types.Content(role='user', parts=[types.Part(text=query)])
final_response_text = "Agent未生成最终响应。" # Default
# Key Concept: run_async 会执行Agent逻辑并持续生成事件流(Events)。
# 我们通过遍历事件流来获取最终答案。
async for event in runner.run_async(user_id=user_id, session_id=session_id, new_message=content):
# 您可以取消下面这行的注释,以查看执行过程中的所有事件
# print(f" [Event] Author: {event.author}, Type: {type(event).__name__}, Final: {event.is_final_response()}, Content: {event.content}")
# Key Concept: is_final_response() 用于标记当前对话轮次(turn)的终结消息
if event.is_final_response():
if event.content and event.content.parts:
# 假设响应文本位于首部分
final_response_text = event.content.parts[0].text
elif event.actions and event.actions.escalate: # Handle potential errors/escalations
final_response_text = f"Agent escalated: {event.error_message or 'No specific message.'}"
# Add more checks here if needed (e.g., specific error codes)
break # 找到最终响应后停止处理事件
print(f"<<< Agent Response: {final_response_text}")
这段代码定义了一个 call_agent_async 异步函数,用来:
-
1、接收用户查询(Query)
-
2、调用Agent执行推理。
-
3、获取并返回最终的Agent回复。
其核心运行流程是:
-
将用户查询转为ADK需要的格式。
-
通过runner.run_async()运行Agent
-
通过事件驱动机制获取和处理每一轮的消息、工具调用、回复。
-
当最终响应到达时,停止循环并返回Agent的最终回答。
完整代码
import asyncio
from google.adk.agents import Agent
from google.adk.models.lite_llm import LiteLlm # For multi-model support
from google.adk.sessions import InMemorySessionService
from google.adk.runners import Runner
from google.genai import types # For creating message Content/Parts
# 使用LiteLLM调用在线模型
model_client = LiteLlm(
model="deepseek/deepseek-chat",
api_base="https://api.deepseek.com",
api_key="sk-xxxxxx",
)
# 创建Agent智能体
agent = Agent(
name="AI智能助手",
model=model_client,
description="AI智能助手",
instruction="你是一个乐于助人的AI智能助手",
)
# 创建Session会话管理器
# 这个对话管理示例实际会在内存中进行历史对话信息管理。而具体的对话信息会保存在.sessions属性里,默认为空。
session_service = InMemorySessionService()
# 创建Session
# 在正式开启对话之前,我们需要创建一个初始session对象
# 为了方便区分不同的对话信息,该对象需要设置 APP名称、用户ID、对话ID,方便后续进行查询和调用:
APP_NAME = "app01"
USER_ID = "user01"
SESSION_ID = "session01"
session = session_service.create_session(
app_name=APP_NAME,
user_id=USER_ID,
session_id=SESSION_ID,
)
print(f"创建初始Session: {session.app_name} - {session.user_id} - {session.id}")
# 创建智能体执行器
runner = Runner(
agent=agent,
app_name="app01",
session_service=session_service
)
# 执行对话
async def call_agent_async(query: str, runner, user_id, session_id):
"""向agent发送查询并打印最终响应"""
print(f"\n>>> 用户: {query}")
# 将用户消息转换为ADK格式
content = types.Content(role='user', parts=[types.Part(text=query)])
final_response_text = "Agent未生成最终响应。" # Default
# Key Concept: run_async 会执行Agent逻辑并持续生成事件流(Events)。
# 我们通过遍历事件流来获取最终答案。
async for event in runner.run_async(user_id=user_id, session_id=session_id, new_message=content):
# 您可以取消下面这行的注释,以查看执行过程中的所有事件
print(f" [Event] Author: {event.author}, Type: {type(event).__name__}, Final: {event.is_final_response()}, Content: {event.content}")
# Key Concept: is_final_response() 用于标记当前对话轮次(turn)的终结消息
if event.is_final_response():
if event.content and event.content.parts:
# 假设响应文本位于首部分
final_response_text = event.content.parts[0].text
elif event.actions and event.actions.escalate: # Handle potential errors/escalations
final_response_text = f"Agent escalated: {event.error_message or 'No specific message.'}"
# Add more checks here if needed (e.g., specific error codes)
break # 找到最终响应后停止处理事件
print(f"<<< Agent Response: {final_response_text}")
if __name__ == '__main__':
query = "你是谁?"
asyncio.run(call_agent_async(
query=query,
runner=runner,
user_id=USER_ID,
session_id=SESSION_ID
)
)
运行结果:
创建初始Session: app01 - user01 - session01
>>> 用户: 你是谁?
[Event] Author: AI智能助手, Type: Event, Final: True, Content: parts=[Part(video_metadata=None, thought=None, code_execution_result=None, executable_code=None, file_data=None, function_call=None, function_response=None, inline_data=None, text='我是AI智能助手,一个乐于助人的AI伙伴!随时为你解答问题、提供建议或陪你聊天。有什么我可以帮你的吗? 😊')] role='model'
<<< Agent Response: 我是AI智能助手,一个乐于助人的AI伙伴!随时为你解答问题、提供建议或陪你聊天。有什么我可以帮你的吗? 😊
Process finished with exit code 0