Langchain Chain – RouterChain A routing chain that routes based on input correlations

Original text: Langchain Chain – RouterChain routing chain based on input correlation – Zhihu

Table of contents

close

1. What is RouterChain?

2. MultiPromptChain

3. LLMRouterChain

4. Process review

5. About “Playing with Langchain”

Link to the previous section: Langchain Chain – Connect all tools to create more complex applications! (zsxq.com)

Through the previous section, we introduced what is Chian of the Langchain framework. It can be used as a base for AI applications to create applications with complex functions by connecting different tools. At the same time, we also introduced the most basic type of Chain – LLMChain.

At the beginning of this section, we will introduce other more complex Chains and talk about their application scenarios. Let us start with RouterChain!

1. What is RouterChain?

Let’s review what we have learned so far. We can create an llmchain and place a prompt template in it. This chain can receive a user input and output a result; if we want it to become an expert robot in a certain direction, we can It connects to an external data source, uses LLMchain, connects to a txt text, and connects a document retriever to the chain to complete this requirement. In what scenarios do we need to use RouterChain?

Let’s consider this requirement: If the expert robot we want to set up is not only used in a certain professional field, but in multiple professional fields, then don’t we have to copy n such chains, and each access is different? txt text?

Yes, in such a scenario, RouterChain will be needed! According to Langchain’s introduction, Router Chain is often used in conjunction with downstream destination chains to form an architecture of “one gateway routing + multiple sub-chains”, which can automatically route to the most relevant downstream chain based on user input:

The picture above is a schematic diagram of a RouterChain usage scenario. We can see that a RouterChain connects multiple downstream sub-chains. Each chain is a small application. When the RouterChain receives user input, it can be routed to On the sub-chain most relevant to the input, the output is generated by the sub-chain; for example, if the user input is “Please help me write a poem”, when RouterChain receives it, it will automatically route to the sub-chain “Poet”, which will output result.

Through RouterChain, we can create an AI application that can solve a variety of complex problems.

According to Langchain, a standard RouterChain application should contain two standard components:

1. RouterChain: It is a chain application in itself, capable of selecting downstream sub-chains based on user input; the Langchain framework provides a variety of RouterChains, of which LLMRouterChain is highlighted > and EmbeddingRouterChain two types:

LLMRouterChain Put user input into the large language model, and let the large language model perform routing in the form of Prompt

EmbeddingRouterChain uses vector search to input user

2. Sub-chain DestinationChain: literally translated as destination chain, that is, the chain to which it is routed. According to the above figure, we will have 4 destination chains, namely lawyer chain, sales chain, english teacher chain and poet chain

2. MultiPromptChain

When we configure the routing chain and downstream sub-chains, we need to connect these two chains through a MultiPromptChain. First, let us take a look at the MultiPromptChain provided by the Langchain framework!

The MultiPrompt provided by Langchain contains the following required parameters:

router_chain:Receives a RouterChain instance for routing as a routing chain

default_chain: Receives an LLMChain instance. When the Router Chain cannot find a suitable downstream sub-chain, it will automatically route to the default chain, which can be considered as a backup alternative chain.

destination_chains: Receives a Mapping[str, LLMChain] dictionary. The key is the name of the destination chain that can be routed to, and the value is the LLMChain instance of the destination chain.

Additionally, there are other main optional parameters:

memory: Receives a BaseMemory instance, which can add contextual memory to the routing chain

verbose: bool value, if True, the calling process of the chain will be printed.

Next, let’s take a look at the two types of RouterChain.

3. LLMRouterChain

LLMRouterChain puts user input into the large language model, and uses the prompt form to let the large language model perform routing. If this chain is used as RouterChain, then two core parameters need to be passed in, as the base large language model, and as routing prompt.

The Prompt when using LLMRouterChain for routing may be more complicated, because it includes the analysis of user input, routing guidance, and at the same time, it must be constrained that its output must be parsable by MultiPromptChain; Lanchain provides a MULTI_PROMPT_ROUTER_TEMPLATE , the router_prompt that can be used by MultiPromptChain can be directly generated through the .format() method of this template; of course, the formatting of the .format() method also has a certain template, and we can directly create router_prompt according to the following process:

1. First, define 4 sub-chain prompt templates

lawyer_template = """ You are a legal advisor and you need to give professional legal advice based on the questions raised by users. If you don't know, please say "I don't know" and please do not provide content beyond the scope of the text. , and don’t create your own content.


User asked:
{input}
"""


sales_template = """ You are a sales consultant. You need to introduce the product entered by the user. You need to provide basic information about the product, its usage and warranty terms.


Items entered by the user:
{input}
"""


english_teacher_template = """You are an English teacher. For the Chinese vocabulary input by the user, you need to provide the corresponding English words, including the part of speech, the corresponding phrases and sentences.


Chinese words entered by the user:
{input}
"""


poet_template=""" You are a poet and you need to compose a poem based on the topic entered by the user.


Topic entered by user:
{input}
"""

2. Next, we need to create a “routing directory”. This directory will record the sub-chain name, sub-chain description and sub-chain prompt template. There are two main purposes of constructing this directory. One is to facilitate the creation of sub-chains, and the other is to Conveniently generate routing chain for routing PromptTemplate

prompt_infos = [
    {
        "name": "lawyer",
        "description": "Professional when consulting on legal issues",
        "prompt_template": lawyer_template,
    },
    {
        "name": "sales",
        "description": "Very professional when consulting product information",
        "prompt_template": sales_template,
    },
    {
        "name": "english teacher",
        "description": "Able to answer English questions well",
        "prompt_template": english_teacher_template,
    },
    {
        "name": "poet",
        "description": "Poem writing is very professional",
        "prompt_template": poet_template,
    },
]

3. Next, we need to prepare for the creation of MULTI_PROMPT_ROUTER_TEMPLATE

#Create a list to store the corresponding sub-chain names and descriptions
destinations = [f"{p['name']}: {p['description']}" for p in prompt_infos]


#Concatenate the description into a str
destinations_str = "\
".join(destinations)

The text output by destinations_str is as follows. This format is the formatted text copy received by MultiPromptRputer Tempalte:

lawyer: Good for answering questions about law

sales: Good for answering sales questions

english teacher: Good for answering english teaching questions

poet: Good for making poets

4. Next use MULTI_PROMPT_ROUTER_TEMPLATE.format() to format

from langchain.chains.router.multi_prompt_prompt import MULTI_PROMPT_ROUTER_TEMPLATE


router_template = MULTI_PROMPT_ROUTER_TEMPLATE.format(destinations=destinations_str)

After formatting is completed, it will generate a str. This string can be used as a prompt template. Let’s print the following router_template

Given a raw text input to a language model select the model prompt best suited for the input. You will be given the names of the available prompts and a description of what the prompt is best suited for. You may also revise the original input if you think that revising it will ultimately lead to a better response from the language model.

<< FORMATTING >>

Return a markdown code snippet with a JSON object formatted to look like:

“`json

{{

“destination”: string \ name of the prompt to use or “DEFAULT”

“next_inputs”: string \ a potentially modified version of the original input

}}

“`

REMEMBER: “destination” MUST be one of the candidate prompt names specified below OR it can be “DEFAULT” if the input is not well suited for any of the candidate prompts.

REMEMBER: “next_inputs” can just be the original input if you don’t think any modifications are needed.

<< CANDIDATE PROMPTS >>

lawyer: Very professional when consulting on legal issues.

sales: Very professional when inquiring about product information

english teacher: able to answer English questions well

poet: Very professional in poetry writing

<< INPUT >>

{input}

<< OUTPUT >>

5. With router_template, we need to generate a PromptTemplate instance. We can see that the above template contains an input as input, and we need to use RouterOutputParser as the output parser.

from langchain.chains.router.llm_router import RouterOutputParser


router_prompt = PromptTemplate(
    template=router_template,
    input_variables=["input"],
    output_parser=RouterOutputParser(),
)

6. After completing this step, we can define LLMRouterChain!

from langchain.chains.router.llm_router import LLMRouterChain
from langchain.llms import OpenAI
import os


#Create an OpenAI as the basis for a large language model
llm = OpenAI()


os.environ["OPENAI_API_KEY"]="your key"


router_chain = LLMRouterChain.from_llm(llm, router_prompt)

The above steps are a bit complicated, but they are key steps in building complex applications through large language models. After completing the above steps, we already have a RouterChain as a routing chain, leaving only:

1. Generate each sub-chain

2. Generate a default chain as a back-up chain

2. Use MultiPromptChain to connect the sub-chain and routing chain

Let’s go step by step:

# First, create a candidate chain including all downstream sub-chains
candadite_chains = {}


#Traverse the routing directory, generate each sub-chain and put it into the candidate chain dictionary
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)
    candadite_chains[name] = chain


# Generate default chain
default_chain = ConversationChain(llm=llm, output_key="text")

The three major elements necessary for MultiPromptChain: router_chain, destination_chain and default_chain have been prepared. Let’s finally assemble it!

chain = MultiPromptChain(
    router_chain=router_chain,
    destination_chains=candadite_chains,
    default_chain=default_chain,
    verbose=True,
)

After assembly, let’s give it a try!

print(chain.run("Help me write a poem about spring"))

4. Process review

Looking back at the above process, what have we done?

1. Create sub-chains and default chains

2. Create a routing chain, and the routing template of the routing chain needs to be specially designed according to the sub-chain.

3. Create MultiPromptChain to assemble the routing chain, default chain and sub-chain into a whole

5. About “Playing with Langchain”

“Playing with Langchain” is a knowledge planet of Langchain framework and Langchain development. We will share high-quality content about Langchain every week. Welcome to join us!