mirror of
https://github.com/hwchase17/langchain
synced 2024-11-06 03:20:49 +00:00
180 lines
5.8 KiB
Plaintext
180 lines
5.8 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "a5cf6c49",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Router Chains: Selecting from multiple prompts with MultiPromptChain\n",
|
|
"\n",
|
|
"This notebook demonstrates how to use the `RouterChain` paradigm to create a chain that dynamically selects the prompt to use for a given input. Specifically we show how to use the `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,
|
|
"id": "e8d624d4",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from langchain.chains.router import MultiPromptChain\n",
|
|
"from langchain.llms import OpenAI"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"id": "8d11fa5c",
|
|
"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",
|
|
"\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}\"\"\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"id": "d0b8856e",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"prompt_infos = [\n",
|
|
" {\n",
|
|
" \"name\": \"physics\", \n",
|
|
" \"description\": \"Good for answering questions about physics\", \n",
|
|
" \"prompt_template\": physics_template\n",
|
|
" },\n",
|
|
" {\n",
|
|
" \"name\": \"math\", \n",
|
|
" \"description\": \"Good for answering math questions\", \n",
|
|
" \"prompt_template\": math_template\n",
|
|
" }\n",
|
|
"]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"id": "db679975",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"chain = MultiPromptChain.from_prompts(OpenAI(), prompt_infos, verbose=True)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"id": "90fd594c",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\n",
|
|
"\n",
|
|
"\u001b[1m> Entering new MultiPromptChain chain...\u001b[0m\n",
|
|
"physics: {'input': 'What is black body radiation?'}\n",
|
|
"\u001b[1m> Finished chain.\u001b[0m\n",
|
|
"\n",
|
|
"\n",
|
|
"Black body radiation is the emission of electromagnetic radiation from a body due to its temperature. It is a type of thermal radiation that is emitted from the surface of all objects that are at a temperature above absolute zero. It is a spectrum of radiation that is influenced by the temperature of the body and is independent of the composition of the emitting material.\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print(chain.run(\"What is black body radiation?\"))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"id": "b8c83765",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\n",
|
|
"\n",
|
|
"\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",
|
|
"The first prime number greater than 40 such that one plus the prime number is divisible by 3 is 43. To solve this problem, we can break down the question into two parts: finding the first prime number greater than 40, and then finding a number that is divisible by 3. \n",
|
|
"\n",
|
|
"The first step is to find the first prime number greater than 40. A prime number is a number that is only divisible by 1 and itself. The next prime number after 40 is 41.\n",
|
|
"\n",
|
|
"The second step is to find a number that is divisible by 3. To do this, we can add 1 to 41, which gives us 42. Now, we can check if 42 is divisible by 3. 42 divided by 3 is 14, so 42 is divisible by 3.\n",
|
|
"\n",
|
|
"Therefore, the answer to the question is 43.\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print(chain.run(\"What is the first prime number greater than 40 such that one plus the prime number is divisible by 3\"))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"id": "74c6bba7",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"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> Finished chain.\u001b[0m\n",
|
|
"The type of cloud that typically produces rain is called a cumulonimbus cloud. This type of cloud is characterized by its large vertical extent and can produce thunderstorms and heavy precipitation. Is there anything else you'd like to know?\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print(chain.run(\"What is the name of the type of cloud that rins\"))"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "venv",
|
|
"language": "python",
|
|
"name": "venv"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.11.3"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|