skip navigation
skip mega-menu

LangChain’s Router Chains and Callbacks

The LangChain 框架有不同类型的链,包括 Router ChainRouter ChainS允许从给定输入的一组链中动态选择预定义的链. In this blog we are going to explore how you can use Router Chains.

The second LangChain topic we are covering in this blog are callbacksMultiPromptChain 和LangChain模型类支持回调,允许对某些事件做出反应,比如e.g. receiving a response from an OpenAI model or user input received.

RouterChain Example Flow

A typical Router Chain 基础工作流将接收一个输入,选择一个特定的 LLMChain and give a response. 在这篇博客中,我们将探讨如何实现一个可以用下图表示的工作流:

The following actions are executed here:

  • The user produces a text based input.
  • The input is written to a file via a callback.
  • 路由器从五个选项中选择最合适的链:
    - Python programmer
    - Poet
    - Wikipedia Expert
    - Graphical artist
    - UK, US Legal Expert
  • The large language model responds.
  • The output is again written to a file via a callback.

Example Flow Implementation

我们已经使用Python作为一个简单的命令行应用程序实现了上面描述的工作流,它生成一个带有输出的HTML文件.

实现上面描述的工作流的代码在这个Git存储库中:

GitHub - gilfernandes/router_chain_playground:项目致力于探索…

项目致力于探索LLMRouterChain - GitHub - gilfernandes/router_chain_playground的功能:项目…

github.com


We have used a Conda 环境,您可以使用以下命令设置:

conda create --name langchain python=3.10
conda install -c conda-forge openai
conda install -c conda-forge langchain
conda install -c http://conda.anaconda.org/conda-forge prompt_toolkit

在运行脚本之前,请确保将' OPENAI_API_KEY '环境变量与OpenAI API Key一起添加.

The script can be executed with this command:

python ./lang_chain_router_chain.py

Example Execution

Using “gpt-3.5-turbo-0613“我们使用了以下输入,这些输入由指定的LLM链处理:

  • 你能在Python中实现sigmoid函数和它的导数吗?
    python programmer
  • 你能在Python中实现ReLU激活函数和它的衍生物吗?
    python programmer
  • 你能用s型函数的输出生成一个图像吗?
    graphical artist
  • 哪些激活函数在深度学习中常用?
    python programmer
  • 你能给我写一首全球最大的博彩平台英国乡村计算机编程乐趣的诗吗?
    poet
  • 英国和美国法律体系的主要区别是什么?
    legal expert
  • 你能给我解释一下量子计算中量子比特的概念吗?
    wikipedia expert

Here is a transcript of the interaction with the model.

Implementation Details

We have created two scripts:

The main script is lang_chain_router_chain.py 文件写入回调的实现是 FileCallbackHandler.py.

The main script lang_chain_router_chain.py executes three steps:

  • 定义一个带有相应提示符的LLMChain列表

def generate_destination_chains():
"""
创建具有不同提示模板的LLM链列表.
"""
prompt_factory = PromptFactory()
destination_chains = {}
for p_info in prompt_factory.prompt_infos:
name = p_info['name']
prompt_template = p_info['prompt_template']
chain = LLMChain(
llm=cfg.llm,
提示= PromptTemplate(模板= prompt_template input_variables =(“输入”)))
destination_chains[name] = chain
default_chain = ConversationChain(llm=cfg.llm, output_key="text")
return prompt_factory.prompt_infos, destination_chains, default_chain

提示信息在脚本的这一部分:

class PromptFactory():
developer_template = ""你是一个非常聪明的Python程序员. \
为Python中的算法和计算机问题提供答案. \
You explain the code in a detailed manner. \

Here is a question:
{input}"""

poet_template = """你是一个用英文诗歌回应创造性要求的诗人. \
你提供的答案是拜伦勋爵或莎士比亚风格的诗歌. \

Here is a question:
{input}"""

wiki_template = """You are a Wikipedia expert. \
你回答基于维基百科知识的常识问题. \
Your explanations are detailed and in plain English.

Here is a question:
{input}"""

image_creator_template = """您创建了一个图像创建器. \
您可以使用SVG图像提供答案的图形表示.

Here is a question:
{input}"""

legal_expert_template = """你是英国或美国的法律专家. \
你用通俗易懂的语言解释与英国或美国法律制度有关的问题
with a good number of examples.

Here is a question:
{input}"""



prompt_infos = [
{
'name': 'python programmer',
'description': '适合问全球最大的博彩平台编码和算法的问题',
'prompt_template': developer_template
},
{
'name': 'poet',
“description”:“适合为创造性问题创作诗歌”;
'prompt_template': poet_template
},
{
'name': 'wikipedia expert',
description:“很适合回答全球最大的博彩平台一般知识的问题”;
'prompt_template': wiki_template
},
{
'name': 'graphical artist',
'description': '适合回答需要图像输出的问题',
'prompt_template': image_creator_template
},
{
'name': 'legal expert',
description:“擅长回答与英国或美国法律相关的问题”;
'prompt_template': legal_expert_template
}
]
  • 从目的链和默认链生成路由器链.
Def generate_router_chain(prompt_info, destination_chains, default_chain):
"""
Generats the router chains from the prompt infos.
:param prompt_info上面生成的提示信息.
:param destination_chains使用不同提示模板的LLM链
:param default_chain A default chain
"""
目的地= [f"{p['name']}: {p['description']}" for p in prompt_info]
destinations_str = '\n'.join(destinations)
router_template = MULTI_PROMPT_ROUTER_TEMPLATE.format(destinations=destinations_str)
router_prompt = PromptTemplate(
template=router_template,
input_variables=['input'],
output_parser=RouterOutputParser()
)
router_chain = LLMRouterChain.from_llm(cfg.llm, router_prompt)
return MultiPromptChain(
router_chain=router_chain,
destination_chains=destination_chains,
default_chain=default_chain,
verbose=True,
callbacks=[file_ballback_handler]
)
  • 循环遍历用户输入,这样可以保存输出.
while True:
question = prompt(
HTML("Type Your question ('q' to exit, 's' to save to html file): ")
)
if question == 'q':
break
if question == 's':
file_ballback_handler.create_html()
continue
result = chain.run(question)
print(result)
print()

The FileCallbackHandler.py overrides langchain.callbacks.base.BaseCallbackHandler.

It implements

def on_chain_end(self, outputs: Dict[str, Any], **kwargs: Any) -> None:

def on_text(self,text: str, color: Optional[str] = None, end: str = "", **kwargs: Any) -> None:

to capture the LLM output and the user input.

It also provides a method to generate HTML files:

def create_html(self)

Conclusion

LangChain provides convenient abstractions to route user input to specialized chains. 从某种意义上说,你可以制作带有不同“帽子”的链条,这些“帽子”是根据它们的专业化而选择的.

Furthermore LangChain also has a powerful callback system 这样你就可以对锁链引发的内部事件做出反应.


Gil Fernandes, Onepoint Consulting

Subscribe to our newsletter

Sign up here