Docs: add LCEL to chains/foundational/router (#12211)

pull/12214/head
Bagatur 12 months ago committed by GitHub
parent 69d9eae5cd
commit 55f0f8dae8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -346,7 +346,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.1"
"version": "3.9.1"
}
},
"nbformat": 4,

@ -7,7 +7,168 @@
"source": [
"# Router\n",
"\n",
"This notebook demonstrates how to use the `RouterChain` paradigm to create a chain that dynamically selects the next chain to use for a given input. \n",
"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.\n",
"\n",
"As a very simple example, let's suppose we have two templates optimized for different types of questions, and we want to choose the template based on the user input."
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "8d11fa5c",
"metadata": {},
"outputs": [],
"source": [
"from langchain.prompts import PromptTemplate\n",
"\n",
"\n",
"physics_template = \"\"\"You are a very smart physics professor. \\\n",
"You are great at answering questions about physics in a concise and easy to understand manner. \\\n",
"When you don't know the answer to a question you admit that you don't know.\n",
"\n",
"Here is a question:\n",
"{input}\"\"\"\n",
"physics_prompt = PromptTemplate.from_template(physics_template)\n",
"\n",
"math_template = \"\"\"You are a very good mathematician. You are great at answering math questions. \\\n",
"You are so good because you are able to break down hard problems into their component parts, \\\n",
"answer the component parts, and then put them together to answer the broader question.\n",
"\n",
"Here is a question:\n",
"{input}\"\"\"\n",
"math_prompt = PromptTemplate.from_template(math_template)"
]
},
{
"cell_type": "markdown",
"id": "892bb71f-e4f4-431e-8321-fe6a40e71b78",
"metadata": {},
"source": [
"## Using LCEL\n",
"\n",
"We can easily do this 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. \n",
"\n",
"If no provided conditions match, it runs the default runnable."
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "f2c4cdb4-1108-491c-9f6f-bbceeb452e29",
"metadata": {},
"outputs": [],
"source": [
"from langchain.chat_models import ChatOpenAI\n",
"from langchain.schema.output_parser import StrOutputParser\n",
"from langchain.schema.runnable import RunnableBranch"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "49308c5c-8722-4fb0-b78d-3b1dac0e656d",
"metadata": {},
"outputs": [],
"source": [
"general_prompt = PromptTemplate.from_template(\n",
" \"You are a helpful assistant. Answer the question as accurately as you can.\\n\\n{input}\"\n",
")\n",
"prompt_branch = RunnableBranch(\n",
" (lambda x: x[\"topic\"] == \"math\", math_prompt),\n",
" (lambda x: x[\"topic\"] == \"physics\", physics_prompt),\n",
" general_prompt\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "750da8ec-7c1c-4a0e-9c94-e3a1da49319b",
"metadata": {},
"outputs": [],
"source": [
"from typing import Literal\n",
"\n",
"from langchain.pydantic_v1 import BaseModel\n",
"from langchain.output_parsers.openai_functions import PydanticAttrOutputFunctionsParser\n",
"from langchain.utils.openai_functions import convert_pydantic_to_openai_function\n",
"\n",
"\n",
"class TopicClassifier(BaseModel):\n",
" \"Classify the topic of the user question\"\n",
" \n",
" topic: Literal[\"math\", \"physics\", \"general\"]\n",
" \"The topic of the user question. One of 'math', 'phsyics' or 'general'.\"\n",
"\n",
"\n",
"classifier_function = convert_pydantic_to_openai_function(TopicClassifier)\n",
"llm = ChatOpenAI().bind(functions=[classifier_function], function_call={\"name\": \"TopicClassifier\"}) \n",
"parser = PydanticAttrOutputFunctionsParser(pydantic_schema=TopicClassifier, attr_name=\"topic\")\n",
"classifier_chain = llm | parser"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "35be97db-2b31-4503-af56-2cae802a9822",
"metadata": {},
"outputs": [],
"source": [
"from operator import itemgetter\n",
"\n",
"from langchain.schema.output_parser import StrOutputParser\n",
"from langchain.schema.runnable import RunnablePassthrough\n",
"\n",
"\n",
"final_chain = (\n",
" RunnablePassthrough.assign(topic=itemgetter(\"input\") | classifier_chain) \n",
" | prompt_branch \n",
" | ChatOpenAI()\n",
" | StrOutputParser()\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "9b161436-432b-4ecd-9752-5f458a7b1d54",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"Thank you for your kind words! I'll be happy to help you with this math question.\\n\\nTo find the first prime number greater than 40 that satisfies the given condition, we need to follow a step-by-step approach. \\n\\nFirstly, let's list the prime numbers greater than 40:\\n41, 43, 47, 53, 59, 61, 67, 71, ...\\n\\nNow, we need to check if one plus each of these prime numbers is divisible by 3. We can do this by calculating the remainder when dividing each number by 3.\\n\\nFor 41, (41 + 1) % 3 = 42 % 3 = 0. It is divisible by 3.\\n\\nFor 43, (43 + 1) % 3 = 44 % 3 = 2. It is not divisible by 3.\\n\\nFor 47, (47 + 1) % 3 = 48 % 3 = 0. It is divisible by 3.\\n\\nSince 41 and 47 are both greater than 40 and satisfy the condition, the first prime number greater than 40 such that one plus the prime number is divisible by 3 is 41.\\n\\nTherefore, the answer to the question is 41.\""
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"final_chain.invoke(\n",
" {\"input\": \"What is the first prime number greater than 40 such that one plus the prime number is divisible by 3?\"}\n",
")"
]
},
{
"cell_type": "markdown",
"id": "aa11d8fb-b9f2-4427-9c7f-2146f84cba72",
"metadata": {},
"source": [
"For more on routing with LCEL [head here](/docs/expression_language/how_to/routing)."
]
},
{
"cell_type": "markdown",
"id": "681af961-388e-4b37-9572-4f084365abba",
"metadata": {},
"source": [
"## [Legacy] RouterChain\n",
"\n",
"::note:: The preferred approach as of version `0.0.293` is to use LCEL as above.\n",
"\n",
"Here we show how to use the `RouterChain` paradigm to create a chain that dynamically selects the next chain to use for a given input. \n",
"\n",
"Router chains are made up of two components:\n",
"\n",
@ -15,12 +176,12 @@
"- `destination_chains`: chains that the router chain can route to\n",
"\n",
"\n",
"In this notebook, we will focus on the different types of routing chains. We will show these routing chains used in a `MultiPromptChain` to create a question-answering chain that selects the prompt which is most relevant for a given question, and then answers the question using that prompt."
"In this example, we will focus on the different types of routing chains. We will show these routing chains used in a `MultiPromptChain` to create a question-answering chain that selects the prompt which is most relevant for a given question, and then answers the question using that prompt."
]
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 16,
"id": "e8d624d4",
"metadata": {},
"outputs": [],
@ -28,36 +189,22 @@
"from langchain.chains.router import MultiPromptChain\n",
"from langchain.llms import OpenAI\n",
"from langchain.chains import ConversationChain\n",
"from langchain.chains.llm import LLMChain\n",
"from langchain.prompts import PromptTemplate"
"from langchain.chains.llm import LLMChain"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "8d11fa5c",
"cell_type": "markdown",
"id": "83cea2d5",
"metadata": {},
"outputs": [],
"source": [
"physics_template = \"\"\"You are a very smart physics professor. \\\n",
"You are great at answering questions about physics in a concise and easy to understand manner. \\\n",
"When you don't know the answer to a question you admit that you don't know.\n",
"\n",
"Here is a question:\n",
"{input}\"\"\"\n",
"\n",
"### [Legacy] LLMRouterChain\n",
"\n",
"math_template = \"\"\"You are a very good mathematician. You are great at answering math questions. \\\n",
"You are so good because you are able to break down hard problems into their component parts, \\\n",
"answer the component parts, and then put them together to answer the broader question.\n",
"\n",
"Here is a question:\n",
"{input}\"\"\""
"This chain uses an LLM to determine how to route things."
]
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 17,
"id": "d0b8856e",
"metadata": {},
"outputs": [],
@ -78,7 +225,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 18,
"id": "de2dc0f0",
"metadata": {},
"outputs": [],
@ -88,7 +235,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 19,
"id": "f27c154a",
"metadata": {},
"outputs": [],
@ -103,19 +250,9 @@
"default_chain = ConversationChain(llm=llm, output_key=\"text\")"
]
},
{
"cell_type": "markdown",
"id": "83cea2d5",
"metadata": {},
"source": [
"## LLMRouterChain\n",
"\n",
"This chain uses an LLM to determine how to route things."
]
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 20,
"id": "60142895",
"metadata": {},
"outputs": [],
@ -126,7 +263,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 21,
"id": "60769f96",
"metadata": {},
"outputs": [],
@ -144,7 +281,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 22,
"id": "db679975",
"metadata": {},
"outputs": [],
@ -159,7 +296,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 23,
"id": "90fd594c",
"metadata": {},
"outputs": [
@ -169,12 +306,26 @@
"text": [
"\n",
"\n",
"\u001b[1m> Entering new MultiPromptChain chain...\u001b[0m\n",
"\u001b[1m> Entering new MultiPromptChain chain...\u001b[0m\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/bagatur/langchain/libs/langchain/langchain/chains/llm.py:280: UserWarning: The predict_and_parse method is deprecated, instead pass an output parser directly to LLMChain.\n",
" warnings.warn(\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"physics: {'input': 'What is black body radiation?'}\n",
"\u001b[1m> Finished chain.\u001b[0m\n",
"\n",
"\n",
"Black body radiation is the term used to describe the electromagnetic radiation emitted by a “black body”—an object that absorbs all radiation incident upon it. A black body is an idealized physical body that absorbs all incident electromagnetic radiation, regardless of frequency or angle of incidence. It does not reflect, emit or transmit energy. This type of radiation is the result of the thermal motion of the body's atoms and molecules, and it is emitted at all wavelengths. The spectrum of radiation emitted is described by Planck's law and is known as the black body spectrum.\n"
"Black body radiation is the thermal electromagnetic radiation within or surrounding a body in thermodynamic equilibrium with its environment, or emitted by a black body (an idealized physical body which absorbs all incident electromagnetic radiation). It is a characteristic of the temperature of the body; if the body has a uniform temperature, the radiation is also uniform across the spectrum of frequencies. The spectral characteristics of the radiation are determined by the temperature of the body, which implies that a black body at a given temperature will emit the same amount of radiation at every frequency.\n"
]
}
],
@ -184,7 +335,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 24,
"id": "b8c83765",
"metadata": {},
"outputs": [
@ -194,12 +345,33 @@
"text": [
"\n",
"\n",
"\u001b[1m> Entering new MultiPromptChain chain...\u001b[0m\n",
"\u001b[1m> Entering new MultiPromptChain chain...\u001b[0m\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/bagatur/langchain/libs/langchain/langchain/chains/llm.py:280: UserWarning: The predict_and_parse method is deprecated, instead pass an output parser directly to LLMChain.\n",
" warnings.warn(\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"math: {'input': 'What is the first prime number greater than 40 such that one plus the prime number is divisible by 3?'}\n",
"\u001b[1m> Finished chain.\u001b[0m\n",
"?\n",
"\n",
"The answer is 43. One plus 43 is 44 which is divisible by 3.\n"
"\n",
"The first prime number greater than 40 such that one plus the prime number is divisible by 3 is 43. This can be seen by breaking down the problem:\n",
"\n",
"1) We know that a prime number is a number that is only divisible by itself and one. \n",
"2) We also know that if a number is divisible by 3, the sum of its digits must be divisible by 3. \n",
"\n",
"So, if we want to find the first prime number greater than 40 such that one plus the prime number is divisible by 3, we can start counting up from 40, testing each number to see if it is prime and if the sum of the number and one is divisible by three. \n",
"\n",
"The first number we come to that satisfies these conditions is 43.\n"
]
}
],
@ -213,7 +385,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 25,
"id": "74c6bba7",
"metadata": {},
"outputs": [
@ -223,10 +395,26 @@
"text": [
"\n",
"\n",
"\u001b[1m> Entering new MultiPromptChain chain...\u001b[0m\n",
"None: {'input': 'What is the name of the type of cloud that rains?'}\n",
"\u001b[1m> Entering new MultiPromptChain chain...\u001b[0m\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/bagatur/langchain/libs/langchain/langchain/chains/llm.py:280: UserWarning: The predict_and_parse method is deprecated, instead pass an output parser directly to LLMChain.\n",
" warnings.warn(\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"physics: {'input': 'What is the name of the type of cloud that rains?'}\n",
"\u001b[1m> Finished chain.\u001b[0m\n",
" The type of cloud that rains is called a cumulonimbus cloud. It is a tall and dense cloud that is often accompanied by thunder and lightning.\n"
"\n",
"\n",
"The type of cloud that rains is called a cumulonimbus cloud.\n"
]
}
],
@ -239,14 +427,14 @@
"id": "239d4743",
"metadata": {},
"source": [
"## EmbeddingRouterChain\n",
"## [Legacy] EmbeddingRouterChain\n",
"\n",
"The `EmbeddingRouterChain` uses embeddings and similarity to route between destination chains."
]
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 26,
"id": "55c3ed0e",
"metadata": {},
"outputs": [],
@ -258,7 +446,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 27,
"id": "572a5082",
"metadata": {},
"outputs": [],
@ -271,18 +459,10 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 28,
"id": "50221efe",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Using embedded DuckDB without persistence: data will be transient\n"
]
}
],
"outputs": [],
"source": [
"router_chain = EmbeddingRouterChain.from_names_and_descriptions(\n",
" names_and_descriptions, Chroma, CohereEmbeddings(), routing_keys=[\"input\"]\n",
@ -291,7 +471,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 29,
"id": "ff7996a0",
"metadata": {},
"outputs": [],
@ -306,7 +486,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 30,
"id": "99270cc9",
"metadata": {},
"outputs": [
@ -321,7 +501,7 @@
"\u001b[1m> Finished chain.\u001b[0m\n",
"\n",
"\n",
"Black body radiation is the emission of energy from an idealized physical body (known as a black body) that is in thermal equilibrium with its environment. It is emitted in a characteristic pattern of frequencies known as a black-body spectrum, which depends only on the temperature of the body. The study of black body radiation is an important part of astrophysics and atmospheric physics, as the thermal radiation emitted by stars and planets can often be approximated as black body radiation.\n"
"Black body radiation is the electromagnetic radiation emitted by a black body, which is an idealized physical body that absorbs all incident electromagnetic radiation. This radiation is related to the temperature of the body, with higher temperatures leading to higher radiation levels. The spectrum of the radiation is continuous, and is described by the Planck's law of black body radiation.\n"
]
}
],
@ -331,7 +511,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 31,
"id": "b5ce6238",
"metadata": {},
"outputs": [
@ -344,9 +524,9 @@
"\u001b[1m> Entering new MultiPromptChain chain...\u001b[0m\n",
"math: {'input': 'What is the first prime number greater than 40 such that one plus the prime number is divisible by 3?'}\n",
"\u001b[1m> Finished chain.\u001b[0m\n",
"?\n",
"\n",
"Answer: The first prime number greater than 40 such that one plus the prime number is divisible by 3 is 43.\n"
"\n",
"The first prime number greater than 40 such that one plus the prime number is divisible by 3 is 43. This is because 43 is a prime number, and 1 + 43 = 44, which is divisible by 3.\n"
]
}
],
@ -357,14 +537,6 @@
" )\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "20f3d047",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
@ -383,7 +555,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.3"
"version": "3.9.1"
}
},
"nbformat": 4,

Loading…
Cancel
Save