热门搜索:和平精英 原神 街篮2 

您的位置:首页 > > 教程攻略 > ai资讯 >5分钟了解LangChain的路由链

5分钟了解LangChain的路由链

来源:互联网 更新时间:2026-07-17 14:16

上篇聊顺序链时,用到了 SequentialChain,它能把多个链按顺序串起来。这次要讲的是 LangChain 里的另一个重要角色——

路由链

,这个名字听起来就有点“智能路由”的味道。

1. 路由链概念

路由链(RouterChain)

的运作逻辑很直观:让 LLM 根据输入的 Prompt 自己去决定下一步该调用哪个链。换句话说,路由链手里握着一组 Prompt 模板,LLM 看完输入内容后,从中挑出最匹配的那个,然后顺着对应的链往下走。

2. 路由链的使用场景

实际使用中,路由链主要跟两个核心类打交道:LLMRouterChainMultiPromptChain。看看官网的描述:

  • LLMRouterChain

    :让 LLM 从多个候选选项中做选择。
  • MultiPromptChain

    :专门用于在多个提示词之间路由输入——当你手头有好几个 Prompt 模板,只想根据输入走其中一条路时,就靠它了。

使用路由链通常有固定的几步:

  1. 准备好各个处理链的 Prompt 模板,分别封装成链。
  2. 把这些链放进 destination_chains 字典里,给每个链起个名字。
  3. 构建一个路由链(RouterChain),用来决定下一步该调用哪个链。
  4. 准备一个默认链,当路由链找不到合适的“目的地”时,就由它兜底。
  5. 最后用 MultiPromptChain 把路由链、目标链和默认链串起来,执行一次调用,链会自动选择并运行。

3. 使用路由链的案例

假设一个很常见的场景:用户输入不同领域的问题(物理、数学、英语),系统要根据问题内容自动选择对应的专业链去回答;如果输入的问题不属于任何一个已知领域,就退回到默认链处理。代码实现如下:

from langchain_openai import ChatOpenAI

model = ChatOpenAI(
    model_name="gpt-3.5-turbo",
    openai_api_key="sk-xxxx",
    openai_api_base="https://api.302.ai/v1",
)


from langchain.chains.router import LLMRouterChain, MultiPromptChain
from langchain.chains.router.llm_router import RouterOutputParser
from langchain.chains.router.multi_prompt_prompt import MULTI_PROMPT_ROUTER_TEMPLATE
from langchain.chains import LLMChain, ConversationChain
from langchain.prompts import PromptTemplate

# 准备2条目的链:一条物理链,一条数学链
# 1. 物理链
physics_template = """
你是一位物理学家,擅长回答物理相关的问题,当你不知道问题的答案时,你就回答不知道。
具体问题如下:
{input}
"""
physics_prompt = PromptTemplate.from_template(physics_template)
physics_chain = LLMChain(llm=model, prompt=physics_prompt)

# 2. 数学链
math_template = """
你是一个数学家,擅长回答数学相关的问题,当你不知道问题的答案时,你就回答不知道。
具体问题如下:
{input}
"""
math_prompt = PromptTemplate.from_template(math_template)
math_chain = LLMChain(llm=model, prompt=math_prompt)

# 3. 英语链
english_template = """
你是一个非常厉害的英语老师,擅长回答英语相关的问题,当你不知道问题的答案时,你就回答不知道。
具体问题如下:
{input}
"""
english_prompt = PromptTemplate.from_template(english_template)
english_chain = LLMChain(llm=model, prompt=english_prompt)


######### 所有可能的目的链
destination_chains = {}
destination_chains["physics"] = physics_chain
destination_chains["math"] = math_chain
destination_chains["english"] = english_chain


######### 默认链
default_chain = ConversationChain(llm=model, output_key="text")

# 让多路由模板 能找到合适的 提示词模板
destinations_template_str = """
physics:擅长回答物理问题
math:擅长回答数学问题
english:擅长回答英语问题
"""
router_template = MULTI_PROMPT_ROUTER_TEMPLATE.format(
    destinations=destinations_template_str
)

# 通过路由提示词模板,构建路由提示词
router_prompt = PromptTemplate(
    template=router_template,
    input_variables=["input"],
    output_parser=RouterOutputParser(),
)

######### 路由链
router_chain = LLMRouterChain.from_llm(llm=model, prompt=router_prompt)

######### 最终的链
multi_prompt_chain = MultiPromptChain(
    router_chain=router_chain,
    destination_chains=destination_chains,
    default_chain=default_chain,
    verbose=True,
)


# multi_prompt_chain.invoke({"input": "重力加速度是多少?"})
# multi_prompt_chain.invoke("y=x^2+2x+1的导数是多少?")
multi_prompt_chain.invoke("将以下英文翻译成中文,只输出中文翻译结果:n The largest community building the future of LLM apps.")
# multi_prompt_chain.invoke("你是怎么理解ja va的面向对象的思想的?")

执行结果和我们预想的一致,分别测试了物理、数学、英语和默认场景,结果如下:

4. 总结

这篇主要聊了 LangChain 里的

路由链(RouterChain)

,它的核心价值在于处理“不确定性”——当不知道用户输入属于哪一类时,让 LLM 自己判断并引导到合适的处理链上。同时介绍了它的两个核心类、使用步骤,以及一个涵盖物理、数学、英语的完整案例。希望对你理解路由链的实际用途有所帮助。

关于宇宙的好的网名有哪些
关于宇宙的好的网名有哪些

类型:角色扮演

大小:1

语言:简体中文

平台:互联网

游戏下载

热门手游

相关攻略

手机号码测吉凶
本站所有软件,都由网友上传,如有侵犯你的版权,请发邮件haolingcc@hotmail.com 联系删除。 版权所有 Copyright@2012-2013 haoling.cc