forked from Archives/langchain
7f8727bbcd
Unpolished router examples to help flesh out abstractions and use cases ![Screenshot 2023-05-02 at 7 02 58 PM](https://user-images.githubusercontent.com/130488702/235820394-389e5584-db0b-415e-a260-2824b5555167.png) --------- Co-authored-by: Shreya Rajpal <shreya.rajpal@gmail.com>
166 lines
5.3 KiB
Plaintext
166 lines
5.3 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": "b89de9f3",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"prompt_infos = [\n",
|
|
" (\"physics\", \"Good for answering questions about physics\", physics_template),\n",
|
|
" (\"math\", \"Good for answering math questions\", math_template)\n",
|
|
"]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"id": "db679975",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"chain = MultiPromptChain.from_prompts(OpenAI(), *zip(*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 that is in thermal equilibrium with its environment. It is emitted by all objects regardless of their temperature, but the intensity and spectral distribution of the radiation depends on the temperature of the body. As the temperature increases, the intensity of the radiation also increases and the peak wavelength shifts to shorter wavelengths.\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, we first need to identify all of the prime numbers between 40 and 50. These are 41, 43, 47, and 49. We then need to check which of these, when added to 1, will be divisible by 3. The prime number that fits this criteria is 43. Therefore, the answer 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 name of the type of cloud that usually brings rain is called a cumulonimbus cloud. These clouds are typically tall and dark with a flat base and anvil-shaped top. They form when warm, moist air rises rapidly and condenses into water droplets, which eventually become heavy enough to fall as rain.\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
|
|
}
|