第 2 章:路由
路由模式概览
借助提示链进行顺序处理,是执行确定性、线性语言模型工作流的基础方法;但在需要自适应响应的场景中,这种方式的适用性就会受到限制。现实世界中的智能体系统,往往需要根据环境状态、用户输入或前一步操作的结果等条件,在多种可能的动作之间做出选择。这种动态决策能力,也就是将控制流导向不同专用函数、工具或子流程的能力,正是通过 路由 机制来实现的。
路由在智能体的运行框架中引入了条件逻辑,使系统从固定的执行路径转向能够根据特定标准进行动态评估,并从一组候选后续动作中选择其一执行,从而具备更灵活、也更具上下文感知能力的行为方式。
例如,一个面向客户咨询的智能体如果具备路由能力,就可以先对传入查询进行分类,以判断用户意图;然后再据此将请求转交给专门的问答智能体、查询账户信息的数据库检索工具,或处理复杂问题的升级流程,而不是默认走单一、预设的响应路径。因此,一个更成熟、具备路由能力的智能体可以:
分析用户查询。
根据其 意图 对查询进行路由:
- 若意图为“查询订单状态”,则路由到与订单数据库交互的子智能体或工具链。
- 若意图为“产品信息”,则路由到负责检索产品目录的子智能体或处理链。
- 若意图为“技术支持”,则路由到可访问排障指南或升级至人工处理的另一条链路。
- 若意图不明确,则路由到负责澄清需求的子智能体或提示链。
路由模式的核心,是一个负责进行评估并引导控制流的机制;这一机制可以通过多种方式实现:
- 基于 LLM 的路由: 可以提示语言模型 LLM 分析输入,并输出一个表示下一步操作或目标去向的标识符或指令。例如,可要求模型“分析以下用户查询,并且只输出类别:‘订单状态’‘产品信息’‘技术支持’或‘其他’”;智能体系统随后读取该输出,并据此推进工作流。
- 基于嵌入的路由: 可先将查询转换为向量嵌入(见第 14 章
RAG),再与不同路径或能力对应的嵌入进行相似度比较,并将查询路由到最相近的一条路径。这种方式适用于语义路由,即决策依据是输入的语义,而不仅仅是关键词。 - 基于规则的路由: 使用预定义的规则或逻辑(如
if-else、switch),依据关键词、模式或从输入中提取出的结构化数据进行分流。它通常比基于 LLM 的路由更快、也更具确定性,但在处理细微差别较多或全新类型的输入时灵活性较弱。 - 基于机器学习模型的路由: 采用判别式模型(如分类器),在少量标注语料上进行专门训练,以完成路由任务。虽然它在概念上与基于嵌入的方法有相通之处,但关键特征在于监督式微调过程,即通过调整模型参数来形成专用的路由函数。它与基于 LLM 的路由不同,因为其决策组件并不是在推理阶段执行提示词的生成模型,而是编码在微调后模型权重中的逻辑。LLM 可能会在预处理阶段用于生成合成数据,以扩充训练集,但并不直接参与实时路由决策。
路由机制可以部署在智能体运行周期的多个环节:既可以在入口处对主任务进行分类,也可以在处理链的中间阶段决定下一步动作,还可以在某个子程序内部,从给定的工具集中选出最合适的一项。
LangChain、LangGraph 与 Google Agent Developer Kit(ADK)等框架,都提供了用于定义和管理这类条件逻辑的显式结构。LangGraph 基于状态的图结构,尤其适合处理那些决策依赖于系统整体累积状态的复杂路由场景。类似地,Google ADK 也提供了用于组织智能体能力与交互模型的基础组件,为实现路由逻辑打下基础。在这些框架的执行环境中,开发者需要定义可能的执行路径,以及决定计算图节点之间如何跳转的函数或基于模型的评估机制。
引入路由之后,系统就不再局限于确定性的顺序处理,而能够构建出更具适应性的执行流,以便对更加多样的输入和状态变化作出动态且恰当的响应。
实际应用与用例
路由模式是设计自适应智能体系统时的一项关键控制机制,它使系统能够随着输入和内部状态的变化动态调整执行路径。通过引入这一必要的条件逻辑层,路由模式在多个领域都具有广泛价值。
在人机交互场景中,例如虚拟助手或 AI 导师,路由常被用于识别用户意图。系统会先对自然语言查询进行初步分析,再决定最合适的后续动作,例如调用特定的信息检索工具、转交给人工坐席,或根据用户表现选择课程中的下一模块。这样一来,系统就不再局限于线性的对话流程,而能够做出更符合上下文的响应。
在自动化数据与文档处理流水线中,路由承担着分类和分发的职能。系统会根据邮件、支持工单或 API 载荷的内容、元数据或格式进行分析,再将每一项输入分派到相应的工作流,例如销售线索录入流程、针对 JSON 或 CSV 格式的专用数据转换函数,或紧急问题的升级处理通道。
在由多种专用工具或智能体构成的复杂系统中,路由扮演的是高层调度器的角色。一个由搜索、总结和分析等不同智能体组成的研究系统,会借助路由器根据当前目标将任务分配给最合适的智能体。同样,AI 编程助手在把代码片段交给正确的专用工具之前,也会先通过路由识别编程语言以及用户意图,例如调试、解释或翻译。
归根结底,路由提供了一种逻辑仲裁能力,而这正是构建功能多样且具备上下文感知能力的系统所不可或缺的。它将智能体从一个按照预定义序列静态执行的系统,转变为能够在变化条件下判断“哪种方式最有效”的动态系统。
动手代码示例(LangChain)
在代码中实现路由,首先需要定义可选路径,以及决定应当走哪条路径的逻辑。LangChain、LangGraph 等框架都为此提供了专门的组件和结构;其中,LangGraph 基于状态的图结构尤其便于对路由逻辑进行可视化和实现。
下面的代码演示了如何使用 LangChain 与 Google 生成式 AI 构建一个简单的类智能体系统:系统设置了一个“协调器”,根据请求意图(预订、信息咨询或意图不明)将用户请求路由到不同的模拟“子智能体”处理器。语言模型先对请求进行分类,再将其委派给相应的处理函数,从而模拟多智能体架构中常见的基础委托模式。
请先安装所需库:
pip install langchain langgraph google-cloud-aiplatform langchain-google-genai google-adk deprecated pydantic此外,你还需要为所选的语言模型(如 OpenAI、Google Gemini、Anthropic)在环境中配置相应的 API 密钥。
# Copyright (c) 2025 Marco Fago
# https://www.linkedin.com/in/marco-fago/
#
# This code is licensed under the MIT License.
# See the LICENSE file in the repository for the full license text.
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough, RunnableBranch
# --- Configuration ---
# Ensure your API key environment variable is set (e.g., GOOGLE_API_KEY)
try:
llm = ChatGoogleGenerativeAI(model="gemini-2.5-flash", temperature=0)
print(f"Language model initialized: {llm.model}")
except Exception as e:
print(f"Error initializing language model: {e}")
llm = None
# --- Define Simulated Sub-Agent Handlers (equivalent to ADK sub_agents) ---
def booking_handler(request: str) -> str:
"""Simulates the Booking Agent handling a request."""
print("\n--- DELEGATING TO BOOKING HANDLER ---")
return f"Booking Handler processed request: '{request}'. Result: Simulated booking action."
def info_handler(request: str) -> str:
"""Simulates the Info Agent handling a request."""
print("\n--- DELEGATING TO INFO HANDLER ---")
return f"Info Handler processed request: '{request}'. Result: Simulated information retrieval."
def unclear_handler(request: str) -> str:
"""Handles requests that couldn't be delegated."""
print("\n--- HANDLING UNCLEAR REQUEST ---")
return f"Coordinator could not delegate request: '{request}'. Please clarify."
# --- Define Coordinator Router Chain (equivalent to ADK coordinator's instruction) ---
# This chain decides which handler to delegate to.
coordinator_router_prompt = ChatPromptTemplate.from_messages([
(
"system",
"""Analyze the user's request and determine which specialist handler should process it.
- If the request is related to booking flights or hotels,
output 'booker'.
- For all other general information questions, output 'info'.
- If the request is unclear or doesn't fit either category,
output 'unclear'.
ONLY output one word: 'booker', 'info', or 'unclear'."""
),
("user", "{request}")
])
if llm:
coordinator_router_chain = coordinator_router_prompt | llm | StrOutputParser()
# --- Define the Delegation Logic (equivalent to ADK's Auto-Flow based on sub_agents) ---
# Use RunnableBranch to route based on the router chain's output.
# Define the branches for the RunnableBranch
branches = {
"booker": RunnablePassthrough.assign(
output=lambda x: booking_handler(x['request']['request'])
),
"info": RunnablePassthrough.assign(
output=lambda x: info_handler(x['request']['request'])
),
"unclear": RunnablePassthrough.assign(
output=lambda x: unclear_handler(x['request']['request'])
),
}
# Create the RunnableBranch. It takes the output of the router chain
# and routes the original input ('request') to the corresponding handler.
delegation_branch = RunnableBranch(
(lambda x: x['decision'].strip() == 'booker', branches["booker"]), # Added .strip()
(lambda x: x['decision'].strip() == 'info', branches["info"]), # Added .strip()
branches["unclear"] # Default branch for 'unclear' or any other output
)
# Combine the router chain and the delegation branch into a single runnable
# The router chain's output ('decision') is passed along with the original input ('request')
# to the delegation_branch.
coordinator_agent = {
"decision": coordinator_router_chain,
"request": RunnablePassthrough()
} | delegation_branch | (lambda x: x['output']) # Extract the final output
# --- Example Usage ---
def main():
if not llm:
print("\nSkipping execution due to LLM initialization failure.")
return
print("--- Running with a booking request ---")
request_a = "Book me a flight to London."
result_a = coordinator_agent.invoke({"request": request_a})
print(f"Final Result A: {result_a}")
print("\n--- Running with an info request ---")
request_b = "What is the capital of Italy?"
result_b = coordinator_agent.invoke({"request": request_b})
print(f"Final Result B: {result_b}")
print("\n--- Running with an unclear request ---")
request_c = "Tell me about quantum physics."
result_c = coordinator_agent.invoke({"request": request_c})
print(f"Final Result C: {result_c}")
if __name__ == "__main__":
main()如上所示,这段 Python 代码基于 LangChain 和 Google 的生成式模型(具体为 gemini-2.5-flash)构建了一个简单的类智能体系统。它定义了三个模拟子智能体处理器:booking_handler、info_handler 和 unclear_handler,分别用于处理不同类型的请求。
其中的核心组件是 coordinator_router_chain。它借助 ChatPromptTemplate 指示语言模型将用户请求归类为 booker、info 或 unclear 三类之一。随后,路由链的输出会由 RunnableBranch 接手,并将原始请求分发给相应的处理函数。RunnableBranch 会根据模型给出的判断结果,将请求数据导向 booking_handler、info_handler 或 unclear_handler。coordinator_agent 则把这些组件整合在一起:先对请求进行路由判断,再交由被选中的处理器执行,最后从处理器的响应中提取最终输出。
main 函数通过三条示例请求演示了系统的使用方式,展示不同输入如何被路由,并分别交由对应的模拟智能体处理。代码中还加入了语言模型初始化失败时的处理逻辑,以增强系统的稳健性。整体结构模拟了一个基础的多智能体框架,即由中央协调器根据意图将任务委派给专用智能体。
动手代码示例(Google ADK)
Agent Development Kit(ADK)是一个面向智能体系统工程化开发的框架,它为定义智能体的能力与行为提供了结构化环境。与基于显式计算图的架构不同,在 ADK 范式中,路由通常是通过定义一组离散的“工具”来实现的,每个工具代表智能体的一项功能。针对用户查询应选择哪一个工具,通常由框架内部逻辑结合底层模型来完成意图与功能处理器之间的匹配。
下面的 Python 代码展示了一个基于 Google ADK 库构建的 ADK 应用示例。它配置了一个“Coordinator”智能体,根据既定指令将用户请求路由到专门的子智能体中去处理,其中“Booker”负责预订任务,“Info”负责通用信息任务;随后,各子智能体再调用具体工具来模拟处理请求,从而展示智能体系统内部的一种基础委托模式。
# Copyright (c) 2025 Marco Fago
#
# This code is licensed under the MIT License.
# See the LICENSE file in the repository for the full license text.
import uuid
from typing import Dict, Any, Optional
from google.adk.agents import Agent
from google.adk.runners import InMemoryRunner
from google.adk.tools import FunctionTool
from google.genai import types
from google.adk.events import Event
# --- Define Tool Functions ---
# These functions simulate the actions of the specialist agents.
def booking_handler(request: str) -> str:
"""
Handles booking requests for flights and hotels.
Args:
request: The user's request for a booking.
Returns:
A confirmation message that the booking was handled.
"""
print("-------------------------- Booking Handler Called ----------------------------")
return f"Booking action for '{request}' has been simulated."
def info_handler(request: str) -> str:
"""
Handles general information requests.
Args:
request: The user's question.
Returns:
A message indicating the information request was handled.
"""
print("-------------------------- Info Handler Called ----------------------------")
return f"Information request for '{request}'. Result: Simulated information retrieval."
def unclear_handler(request: str) -> str:
"""Handles requests that couldn't be delegated."""
return f"Coordinator could not delegate request: '{request}'. Please clarify."
# --- Create Tools from Functions ---
booking_tool = FunctionTool(booking_handler)
info_tool = FunctionTool(info_handler)
# Define specialized sub-agents equipped with their respective tools
booking_agent = Agent(
name="Booker",
model="gemini-2.0-flash",
description="A specialized agent that handles all flight "
"and hotel booking requests by calling the booking tool.",
tools=[booking_tool],
)
info_agent = Agent(
name="Info",
model="gemini-2.0-flash",
description="A specialized agent that provides general information "
"and answers user questions by calling the info tool.",
tools=[info_tool],
)
# Define the parent agent with explicit delegation instructions
coordinator = Agent(
name="Coordinator",
model="gemini-2.0-flash",
instruction=(
"You are the main coordinator. Your only task is to analyze "
"incoming user requests "
"and delegate them to the appropriate specialist agent. Do not try to answer the user directly.\n"
"- For any requests related to booking flights or hotels, delegate to the 'Booker' agent.\n"
"- For all other general information questions, delegate to the 'Info' agent."
),
description="A coordinator that routes user requests to the correct specialist agent.",
# The presence of sub_agents enables LLM-driven delegation (Auto-Flow) by default.
sub_agents=[booking_agent, info_agent],
)
# --- Execution Logic ---
async def run_coordinator(runner: InMemoryRunner, request: str):
"""Runs the coordinator agent with a given request and delegates."""
print(f"\n--- Running Coordinator with request: '{request}' ---")
final_result = ""
try:
user_id = "user_123"
session_id = str(uuid.uuid4())
await runner.session_service.create_session(
app_name=runner.app_name,
user_id=user_id,
session_id=session_id,
)
for event in runner.run(
user_id=user_id,
session_id=session_id,
new_message=types.Content(
role='user',
parts=[types.Part(text=request)],
),
):
if event.is_final_response() and event.content:
# Try to get text directly from event.content to avoid iterating parts
if hasattr(event.content, 'text') and event.content.text:
final_result = event.content.text
elif event.content.parts:
# Fallback: Iterate through parts and extract text (might trigger warning)
text_parts = [part.text for part in event.content.parts if getattr(part, "text", None)]
final_result = "".join(text_parts)
# Assuming the loop should break after the final response
break
print(f"Coordinator Final Response: {final_result}")
return final_result
except Exception as e:
print(f"An error occurred while processing your request: {e}")
return f"An error occurred while processing your request: {e}"
async def main():
"""Main function to run the ADK example."""
print("--- Google ADK Routing Example (ADK Auto-Flow Style) ---")
print("Note: This requires Google ADK installed and authenticated.")
runner = InMemoryRunner(coordinator)
# Example Usage
result_a = await run_coordinator(runner, "Book me a hotel in Paris.")
print(f"Final Output A: {result_a}")
result_b = await run_coordinator(runner, "What is the highest mountain in the world?")
print(f"Final Output B: {result_b}")
result_c = await run_coordinator(runner, "Tell me a random fact.") # Should go to Info
print(f"Final Output C: {result_c}")
result_d = await run_coordinator(runner, "Find flights to Tokyo next month.") # Should go to Booker
print(f"Final Output D: {result_d}")
if __name__ == "__main__":
import nest_asyncio
nest_asyncio.apply()
await main()该脚本由一个主 Coordinator 智能体和两个专用 sub_agents 组成,分别是 Booker 和 Info。每个专用智能体都配备了一个 FunctionTool,用于封装模拟具体动作的 Python 函数:booking_handler 用于模拟航班和酒店预订,info_handler 用于模拟通用信息检索。unclear_handler 则作为协调器无法完成委派时的兜底处理;不过在当前的协调器逻辑中,主流程 run_coordinator 并没有在委派失败时显式调用它。
Coordinator 的首要职责,如其 instruction 中所定义的那样,是分析传入的用户消息,并将其委派给 Booker 或 Info。由于 Coordinator 声明了 sub_agents,这一委派过程会由 ADK 的 Auto-Flow 机制自动完成。run_coordinator 函数负责创建 InMemoryRunner、用户 ID 和会话 ID,然后通过 runner 将请求交给协调器处理。runner.run 会逐条产出事件,而代码则从 event.content 中提取最终响应文本。
main 函数通过不同类型的请求来驱动协调器运行,演示预订类请求如何被路由到 Booker,而信息类请求则如何被路由到 Info。
速览
是什么: 智能体系统常常需要面对多样化的输入和情境,而这些情况无法通过单一的线性流程妥善处理。简单的顺序工作流缺乏基于上下文做决策的能力;如果系统不能为特定任务选择正确的工具或子流程,就会变得僵化且缺乏适应性,从而难以构建能够应对真实世界请求复杂性与多样性的成熟应用。
为什么: 路由模式通过在智能体的运行框架中引入条件逻辑,为这一问题提供了标准化的解决方案。它使系统能够先分析传入查询的意图或性质,再将控制流动态导向最合适的专用工具、函数或子智能体。这一决策过程可以由多种方式驱动,包括提示 LLM、应用预定义规则,或基于嵌入的语义相似度判断。归根结底,路由将静态、预设的执行路径转化为一种能够择优而行、具备上下文感知能力的灵活工作流。
经验法则: 当智能体需要依据用户输入或当前状态,在多种不同的工作流、工具或子智能体之间做出选择时,就应当使用路由模式。对于那些需要先对传入请求进行分拣或分类、再处理不同任务类型的应用而言,路由尤为关键,例如客服机器人需要区分销售咨询、技术支持和账户管理等不同问题。
图示摘要:

图 1:路由模式——以 LLM 作为路由器
要点
路由(Routing) 使智能体,能够根据条件,对工作流中的“下一步”做出动态决策。- 它使智能体能够应对多样化输入并调整自身行为,从而突破纯线性执行模式。
- 路由逻辑可以通过 LLM、规则系统或嵌入相似度等多种方式实现。
- LangGraph 与 Google ADK 等框架都提供了在智能体工作流中定义和管理路由的结构化方法,只是架构侧重点有所不同。
结语
路由模式(Routing pattern)是构建真正动态且响应灵敏的智能体系统的关键一步。通过实现路由,我们得以超越简单的线性执行流,使智能体能够更智能地决定如何处理信息、回应用户,以及调用可用的工具或子智能体。
前文已经展示了路由在客服聊天机器人、复杂数据处理流水线等多个领域中的应用。对输入进行分析并根据条件引导工作流的能力,是构建能够应对真实世界任务内在多样性与变化性的智能体的基础。
LangChain 和 Google ADK 的代码示例展示了两种不同但同样有效的路由实现思路。LangGraph 的图结构以可视化且显式的方式描述状态与转移,因此特别适合用于路由逻辑较为复杂的多步工作流。相比之下,Google ADK 更侧重于定义离散的能力单元(即工具),并由框架负责将用户请求路由到相应的工具处理器,这种方式对于动作集合边界清晰的智能体来说通常更加简洁。
掌握路由模式,是构建能够在不同情境中智能切换处理方式、并根据上下文给出定制化响应或动作的智能体的关键。这也是打造通用且稳健的智能体应用的重要一环。
参考资料
以下是与路由相关的英文参考资料: