langchain.chains.router.llm_router
模块中的 LLMRouterChain
和 RouterOutputParser
用于创建一个路由链条,可以根据输入动态地选择适当的处理链条。
以下是这两个模块的详细介绍以及它们在代码中的作用:
LLMRouterChain
LLMRouterChain
是一个路由器链条,用于将输入动态地路由到适当的子链条(即不同的 LLMChain
)。它的主要功能是根据输入内容决定哪一个子链条应该处理该输入。
- 创建路由器模板:使用
MULTI_PROMPT_ROUTER_TEMPLATE
和PromptTemplate
创建一个路由器模板。这个模板描述了每种子链条的功能。 - 配置路由器链条:通过
LLMRouterChain.from_llm
方法,用语言模型llm
和路由器模板router_prompt
创建路由器链条。
RouterOutputParser
RouterOutputParser
是一个输出解析器,用于解析 LLMRouterChain
的输出,并确定将输入路由到哪个子链条。它的作用是解析路由器链条的输出,以便准确选择处理输入的子链条。
以下为示例代码:
from langchain.chains.router import MultiPromptChain
from langchain.chains.llm import LLMChain
from langchain.prompts import PromptTemplate
from langchain.chains.router.llm_router import LLMRouterChain, RouterOutputParser
from langchain.chains.router.multi_prompt_prompt import MULTI_PROMPT_ROUTER_TEMPLATE
from config import llm
# 定义足球新闻的模板
football_template = """你是一个足球新闻记者,负责报道有关足球的相关新闻。请你根据给定的标题写一首诗;要求这首诗有四个句子,每个句子里面7个字。不要输出任何除了诗句之外的内容;要严格按要求输出内容。如果你不知道该怎么写的话,就回答不知道。这是标题:
{input}"""
# 定义篮球新闻的模板
basketball_template = """你是一个篮球新闻记者,负责报道有关篮球的相关新闻。请你根据给定的标题写一首诗;要求这首诗有四个句子,每个句子里面5个字。不要输出任何除了诗句之外的内容;要严格按要求输出内容。如果你不知道该怎么写的话,就回答不知道。这是标题:
{input}"""
# 定义一般体育新闻的模板
general_template = """你是一个普通的体育新闻记者,负责报道有关体育的相关新闻。请你根据给定的标题写一篇30字左右的短篇新闻。如果你不知道该怎么写的话,就回答不知道。这是标题:
{input}"""
# 定义每个类型的记者信息
prompt_infos = [
{
"name": "football",
"description": "擅长足球新闻报道的记者",
"prompt_template": football_template,
},
{
"name": "basketball",
"description": "擅长篮球新闻报道的记者",
"prompt_template": basketball_template,
},
{
"name": "general",
"description": "一位普通的体育记者",
"prompt_template": general_template,
},
]
# 创建一个空字典用于存储目标chain
destination_chains = {}
# 为每个记者类型创建一个LLMChain,并将其添加到destination_chains字典中
for p_info in prompt_infos:
name = p_info["name"]
prompt_template = p_info["prompt_template"]
prompt = PromptTemplate(template=prompt_template, input_variables=["input"])
chain = LLMChain(llm=llm, prompt=prompt)
destination_chains[name] = chain
# 设定默认的chain为普通体育新闻的chain
general_prompt = PromptTemplate(template=general_template, input_variables=["input"])
default_chain = LLMChain(llm=llm, prompt=general_prompt, output_key="text")
# 创建一个描述各个目的地chain的字符串
destinations = [f"{p['name']}: {p['description']}" for p in prompt_infos]
destinations_str = "\n".join(destinations)
# 使用MULTI_PROMPT_ROUTER_TEMPLATE创建router的模板
router_template = MULTI_PROMPT_ROUTER_TEMPLATE.format(destinations=destinations_str)
router_prompt = PromptTemplate(
template=router_template,
input_variables=["input"],
output_parser=RouterOutputParser(),
)
# 创建一个LLMRouterChain实例
router_chain = LLMRouterChain.from_llm(llm, router_prompt)
# 创建一个MultiPromptChain实例,包含router_chain, destination_chains和default_chain
chain = MultiPromptChain(
router_chain=router_chain,
destination_chains=destination_chains,
default_chain=default_chain,
verbose=True,
)
# 测试MultiPromptChain的运行,输入不同的标题
print(chain.run("NBA季后赛即将开始")) # 测试篮球新闻
print("--------------------------")
print(chain.run("巴黎奥运会足球比赛")) # 测试足球新闻
print("--------------------------")
print(chain.run("温格高获得2024环法总成绩亚军")) # 测试一般体育新闻
返回的结果如下:
> Entering new MultiPromptChain chain...
basketball: {'input': 'NBA季后赛即将开始,请为这次赛事准备一篇前瞻报道。'}
> Finished chain.
篮球盛宴临,季后赛风起;
群雄逐鹿场,谁将问鼎巅。
--------------------------
> Entering new MultiPromptChain chain...
football: {'input': '巴黎奥运会的足球比赛有哪些亮点?'}
> Finished chain.
奥运巴黎盛会启,足球战火燃激情。
群星闪耀绿茵场,亮点纷呈共期待。
--------------------------
> Entering new MultiPromptChain chain...
general: {'input': '温格高在2024年环法自行车赛中获得了总成绩亚军,这位体育记者应该如何报道这一消息?'}
> Finished chain.
"2024年环法自行车赛,温格高勇夺总成绩亚军,创造个人最佳战绩。"
Process finished with exit code 0