微信扫码
和创始人交个朋友
我要投稿
上上篇文章《5分钟理透LangChain的Chain》里用到了顺序链SequentialChain
,它可以将多个链按顺序串起来。本文介绍LangChain里的另外1个重要的链:路由链。
路由链(RouterChain)是由LLM根据输入的Prompt去选择具体的某个链。路由链中一般会存在多个Prompt,Prompt结合LLM决定下一步选择哪个链。
路由链一般涉及到2个核心类,LLMRouterChain
和MultiPromptChain
,一起看看官网介绍:
一般使用路由链时,有固定的几个步骤:
假设我们有一个常见的场景,根据用户的输入内容选择不同的处理路径,如果没有选到合适的链,则使用默认链。比如:根据用户的输入问题,选择不同的链去处理,如果没选到合适的,则走默认链。
具体代码如下:
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("你是怎么理解java的面向对象的思想的?")
执行结果跟我们预想的一致,执行结果如下:
这篇博客主要介绍了LangChain中的路由链(RouterChain)的概念,它主要用在不确定性的场景下,根据提示词,选择具体的某个链去执行。还聊了它的使用场景和具体案例,希望对你有帮助!
本篇完结!欢迎点赞 关注 收藏!!!
53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费场景POC验证,效果验证后签署服务协议。零风险落地应用大模型,已交付160+中大型企业
2025-02-22
手把手教你使用Qwen-Agent开发智能体应用实战教程
2025-02-21
deepseekers 第一个全面拥抱 deepseek 的国产 AI Agent 框架
2025-02-19
LangMem 发布:任何人都能轻松构建智能体记忆!
2025-02-19
深入探究Langchain v0.3:全面解读
2025-02-18
走进Langchain:全面解析
2025-02-17
基于LangChain爬虫增强RAG应用
2025-02-13
LangChat实战DeepSeek-R1模型
2025-02-05
揭秘LangGraph!如何一步一步构建动态订单管理系统?
2024-10-10
2024-04-08
2024-06-03
2024-09-04
2024-08-18
2024-07-13
2024-04-08
2024-06-24
2024-07-10
2024-04-17
2025-02-05
2024-12-02
2024-11-25
2024-10-30
2024-10-11
2024-08-18
2024-08-16
2024-08-04