RunnableBranch in LangChain conditionally branches to one of the Runnable chains based on the user input. RunnableBranch acts like an if-elif-else structure routing input to different chains based on a specified condition.
How RunnableBranch Works
You can pass a list of (condition, Runnable) pairs to the RunnableBranch class to create a conditional routing runnable that evaluates each condition in sequence and executes the corresponding chain, falling back to the default runnable if none of the conditions match. For example-
RunnableBranch(
(lambda x: route_query(x["query"]) == "support", support_chain),
(lambda x: route_query(x["query"]) == "sales", sales_chain),
general_chain
)
LangChain RunnableBranch Example
This is the same example which was used in the RunnableLambda in LangChain With Examples post, covering the scenario where different specialized bots are used to handle different types of queries. A RunnableBranch ensures branching out to the support agent or sales agent based on the condition, with a "General" fallback for anything else.
Example Code
from langchain_core.prompts import PromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_ollama import ChatOllama
from langchain_core.runnables import RunnableBranch
support_template = "You are a support agent. User input: {query}. Provide 3 reasons for the issue."
sales_template = "You are a sales specialist. User input: {query}. Provide 3 reasons to buy the product and 3 for not buying."
general_template = "You are a general assistant. User input: {query}. Provide a helpful response."
model = ChatOllama(model="llama3.1", temperature=0.5)
# 1. Define specialized chains
support_chain = ChatPromptTemplate.from_template(support_template) | model | StrOutputParser()
sales_chain = ChatPromptTemplate.from_template(sales_template) | model | StrOutputParser()
general_chain = ChatPromptTemplate.from_template(general_template) | model | StrOutputParser()
# 2. Define routing logic
def route_query(input: str) -> str:
query = input.lower()
print(f"Routing query: {query}")
if "support" in query or "issue" in query:
return "support"
elif "price" in query or "buy" in query:
return "sales"
else:
return "general"
branch = RunnableBranch(
(lambda x: route_query(x["query"]) == "support", support_chain),
(lambda x: route_query(x["query"]) == "sales", sales_chain),
general_chain
)
# Example usage - Should go to support
response1 = branch.invoke({"query": "I have a login issue with my account."})
print(response1)
# Should go to sales
response2 = branch.invoke({"query": "Request to buy a 3D printer."})
print(response2)
# Should go to general chain
response3 = branch.invoke({"query": "Does company provide transportation services?"})
print(response3)
That's all for this topic RunnableBranch in LangChain With Examples. If you have any doubt or any suggestions to make please drop a comment. Thanks!
Related Topics
You may also like-
No comments:
Post a Comment