1. 路由链概念
**路由链(RouterChain)**是由LLM根据输入的Prompt去选择具体的某个链。路由链中一般会存在多个Prompt,Prompt结合LLM决定下一步选择哪个链。
2. 路由链的使用场景
路由链一般涉及到2个核心类,LLMRouterChain
和MultiPromptChain
,一起看看官网介绍:
- LLMRouterChain:使用LLM路由到可能的选项中。
- MultiPromptChain:该链可用于在多个提示词之间路由输入,当你有多个提示词并且只想路由到其中一个时,可以用这个链。
一般使用路由链时,有固定的几个步骤:
- 准备多个链的Prompt提示词,然后各自封装成链。
- 将可能路由到的链封装到destination_chains里。
- 构建多提示词和RouterChain ,负责选择下一个要调用的链。
- 构建默认链。
- 使用MultiPromptChain选择某个链,然后再去执行此链。
3. 使用路由链的案例
假设我们有一个常见的场景,根据用户的输入内容选择不同的处理路径,如果没有选到合适的链,则使用默认链。比如:根据用户的输入问题,选择不同的链去处理,如果没选到合适的,则走默认链。
具体代码如下:
ini复制代码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的面向对象的思想的?")
执行结果跟我们预想的一致,执行结果如下:
4. 总结
这篇博客主要介绍了LangChain中的**路由链(RouterChain)**的概念,它主要用在不确定性的场景下,根据提示词,选择具体的某个链去执行。还聊了它的使用场景和具体案例,希望对你有帮助!
读者福利:如果大家对大模型感兴趣,这套大模型学习资料一定对你有用
对于0基础小白入门:
如果你是零基础小白,想快速入门大模型是可以考虑的。
一方面是学习时间相对较短,学习内容更全面更集中。
二方面是可以根据这些资料规划好学习计划和方向。
资源分享
大模型AGI学习包
资料目录
- 成长路线图&学习规划
- 配套视频教程
- 实战LLM
- 人工智能比赛资料
- AI人工智能必读书单
- 面试题合集
《人工智能\大模型入门学习大礼包》,可以扫描下方二维码免费领取!
1.成长路线图&学习规划
要学习一门新的技术,作为新手一定要先学习成长路线图,方向不对,努力白费。
对于从来没有接触过网络安全的同学,我们帮你准备了详细的学习成长路线图&学习规划。可以说是最科学最系统的学习路线,大家跟着这个大的方向学习准没问题。
2.视频教程
很多朋友都不喜欢晦涩的文字,我也为大家准备了视频教程,其中一共有21个章节,每个章节都是当前板块的精华浓缩。
3.LLM
大家最喜欢也是最关心的LLM(大语言模型)
《人工智能\大模型入门学习大礼包》,可以扫描下方二维码免费领取!