Skip to main contentü¶úÔ∏èüîó LangChainDocsUse casesIntegrationsAPICommunityChat our docsLangSmithJS/TS DocsSearchCTRLKGet startedIntroductionInstallationQuickstartLangChain Expression LanguageInterfaceHow toBind runtime argsConfigurable AlternativesAdd fallbacksRun arbitrary functionsUse RunnableParallel/RunnableMapRoute between multiple RunnablesCookbookLangChain Expression Language (LCEL)Why use LCEL?ModulesModel I/‚ÄãORetrievalChainsMemoryAgentsCallbacksModulesSecurityGuidesMoreLangChain Expression LanguageHow toRoute between multiple RunnablesOn this pageRoute between multiple RunnablesThis notebook covers how to do routing in the LangChain Expression Language.Routing allows you to create non-deterministic chains where the output of a previous step defines the next step. Routing helps provide structure and consistency around interactions with LLMs.There are two ways to perform routing:Using a RunnableBranch.Writing custom factory function that takes the input of a previous step and returns a runnable. Importantly, this should return a runnable and NOT actually execute.We'll illustrate both methods using a two step sequence where the first step classifies an input question as being about LangChain, Anthropic, or Other, then routes to a corresponding prompt chain.Using a RunnableBranch‚ÄãA RunnableBranch is initialized with a list of (condition, runnable) pairs and a default runnable. It selects which branch by passing each condition the input it's invoked with. It selects the first condition to evaluate to True, and runs the corresponding runnable to that condition with the input. If no provided conditions match, it runs the default runnable.Here's an example of what it looks like in action:from langchain.prompts import PromptTemplatefrom langchain.chat_models import ChatAnthropicfrom langchain.schema.output_parser import StrOutputParserFirst, let's create a chain that will identify incoming questions as being about LangChain, Anthropic, or Other:chain =
This notebook covers how to do routing in the LangChain Expression Language.
This notebook covers how to do routing in the LangChain Expression Language. ->: Skip to main contentü¶úÔ∏èüîó LangChainDocsUse casesIntegrationsAPICommunityChat our docsLangSmithJS/TS DocsSearchCTRLKGet startedIntroductionInstallationQuickstartLangChain Expression LanguageInterfaceHow toBind runtime argsConfigurable AlternativesAdd fallbacksRun arbitrary functionsUse RunnableParallel/RunnableMapRoute between multiple RunnablesCookbookLangChain Expression Language (LCEL)Why use LCEL?ModulesModel I/‚ÄãORetrievalChainsMemoryAgentsCallbacksModulesSecurityGuidesMoreLangChain Expression LanguageHow toRoute between multiple RunnablesOn this pageRoute between multiple RunnablesThis notebook covers how to do routing in the LangChain Expression Language.Routing allows you to create non-deterministic chains where the output of a previous step defines the next step. Routing helps provide structure and consistency around interactions with LLMs.There are two ways to perform routing:Using a RunnableBranch.Writing custom factory function that takes the input of a previous step and returns a runnable. Importantly, this should return a runnable and NOT actually execute.We'll illustrate both methods using a two step sequence where the first step classifies an input question as being about LangChain, Anthropic, or Other, then routes to a corresponding prompt chain.Using a RunnableBranch‚ÄãA RunnableBranch is initialized with a list of (condition, runnable) pairs and a default runnable. It selects which branch by passing each condition the input it's invoked with. It selects the first condition to evaluate to True, and runs the corresponding runnable to that condition with the input. If no provided conditions match, it runs the default runnable.Here's an example of what it looks like in action:from langchain.prompts import PromptTemplatefrom langchain.chat_models import ChatAnthropicfrom langchain.schema.output_parser import StrOutputParserFirst, let's create a chain that will identify incoming questions as being about LangChain, Anthropic, or Other:chain =