mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-02 03:26:17 +00:00
add few shot example (#148)
This commit is contained in:
@@ -17,7 +17,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain import OpenAI, Prompt, LLMChain\n",
|
||||
"from langchain import OpenAI, PromptTemplate, LLMChain\n",
|
||||
"from langchain.text_splitter import CharacterTextSplitter\n",
|
||||
"from langchain.chains.mapreduce import MapReduceChain\n",
|
||||
"\n",
|
||||
@@ -30,7 +30,7 @@
|
||||
"\n",
|
||||
"\n",
|
||||
"CONCISE SUMMARY:\"\"\"\n",
|
||||
"prompt = Prompt(template=_prompt, input_variables=[\"text\"])\n",
|
||||
"prompt = PromptTemplate(template=_prompt, input_variables=[\"text\"])\n",
|
||||
"\n",
|
||||
"text_splitter = CharacterTextSplitter()\n",
|
||||
"\n",
|
||||
@@ -85,7 +85,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.7.6"
|
||||
"version": "3.8.7"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
@@ -28,12 +28,12 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from langchain import Prompt, OpenAI, LLMChain\n",
|
||||
"from langchain import PromptTemplate, OpenAI, LLMChain\n",
|
||||
"\n",
|
||||
"template = \"\"\"Question: {question}\n",
|
||||
"\n",
|
||||
"Answer: Let's think step by step.\"\"\"\n",
|
||||
"prompt = Prompt(template=template, input_variables=[\"question\"])\n",
|
||||
"prompt = PromptTemplate(template=template, input_variables=[\"question\"])\n",
|
||||
"llm_chain = LLMChain(prompt=prompt, llm=OpenAI(temperature=0))\n",
|
||||
"\n",
|
||||
"question = \"What NFL team won the Super Bowl in the year Justin Beiber was born?\"\n",
|
||||
@@ -66,7 +66,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.7.6"
|
||||
"version": "3.8.7"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
@@ -169,7 +169,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.7.6"
|
||||
"version": "3.8.7"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
@@ -25,12 +25,12 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from langchain import Prompt, HuggingFaceHub, LLMChain\n",
|
||||
"from langchain import PromptTemplate, HuggingFaceHub, LLMChain\n",
|
||||
"\n",
|
||||
"template = \"\"\"Question: {question}\n",
|
||||
"\n",
|
||||
"Answer: Let's think step by step.\"\"\"\n",
|
||||
"prompt = Prompt(template=template, input_variables=[\"question\"])\n",
|
||||
"prompt = PromptTemplate(template=template, input_variables=[\"question\"])\n",
|
||||
"llm_chain = LLMChain(prompt=prompt, llm=HuggingFaceHub(repo_id=\"google/flan-t5-xl\", model_kwargs={\"temperature\":1e-10}))\n",
|
||||
"\n",
|
||||
"question = \"What NFL team won the Super Bowl in the year Justin Beiber was born?\"\n",
|
||||
@@ -63,7 +63,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.7.6"
|
||||
"version": "3.8.7"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
@@ -69,7 +69,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Map reduce example\n",
|
||||
"from langchain import Prompt\n",
|
||||
"from langchain import PromptTemplate\n",
|
||||
"from langchain.text_splitter import CharacterTextSplitter\n",
|
||||
"from langchain.chains.mapreduce import MapReduceChain\n",
|
||||
"\n",
|
||||
@@ -81,7 +81,7 @@
|
||||
"\n",
|
||||
"\n",
|
||||
"CONCISE SUMMARY:\"\"\"\n",
|
||||
"prompt = Prompt(template=_prompt, input_variables=[\"text\"])\n",
|
||||
"prompt = PromptTemplate(template=_prompt, input_variables=[\"text\"])\n",
|
||||
"\n",
|
||||
"text_splitter = CharacterTextSplitter()\n",
|
||||
"\n",
|
||||
@@ -202,7 +202,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.7.6"
|
||||
"version": "3.8.7"
|
||||
},
|
||||
"vscode": {
|
||||
"interpreter": {
|
||||
|
@@ -17,7 +17,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain import LLMChain, OpenAI, Cohere, HuggingFaceHub, Prompt\n",
|
||||
"from langchain import LLMChain, OpenAI, Cohere, HuggingFaceHub, PromptTemplate\n",
|
||||
"from langchain.model_laboratory import ModelLaboratory"
|
||||
]
|
||||
},
|
||||
@@ -88,7 +88,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"prompt = Prompt(template=\"What is the capital of {state}?\", input_variables=[\"state\"])\n",
|
||||
"prompt = PromptTemplate(template=\"What is the capital of {state}?\", input_variables=[\"state\"])\n",
|
||||
"model_lab_with_prompt = ModelLaboratory.from_llms(llms, prompt=prompt)"
|
||||
]
|
||||
},
|
||||
@@ -246,7 +246,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.7.6"
|
||||
"version": "3.8.7"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
4
docs/examples/prompts/example_prompt.json
Normal file
4
docs/examples/prompts/example_prompt.json
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"input_variables": ["input", "output"],
|
||||
"template": "Input: {input}\nOutput: {output}"
|
||||
}
|
4
docs/examples/prompts/examples.json
Normal file
4
docs/examples/prompts/examples.json
Normal file
@@ -0,0 +1,4 @@
|
||||
[
|
||||
{"input": "happy", "output": "sad"},
|
||||
{"input": "tall", "output": "short"}
|
||||
]
|
306
docs/examples/prompts/few shot examples.ipynb
Normal file
306
docs/examples/prompts/few shot examples.ipynb
Normal file
@@ -0,0 +1,306 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "f8b01b97",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Few Shot Prompt examples\n",
|
||||
"Notebook showing off how canonical prompts in LangChain can be recreated as FewShotPrompts"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"id": "18c67cc9",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.prompts.few_shot import FewShotPromptTemplate\n",
|
||||
"from langchain.prompts.prompt import PromptTemplate"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"id": "2a729c9f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Self Ask with Search\n",
|
||||
"\n",
|
||||
"examples = [\n",
|
||||
" {\n",
|
||||
" \"question\": \"Who lived longer, Muhammad Ali or Alan Turing?\",\n",
|
||||
" \"answer\": \"Are follow up questions needed here: Yes.\\nFollow up: How old was Muhammad Ali when he died?\\nIntermediate answer: Muhammad Ali was 74 years old when he died.\\nFollow up: How old was Alan Turing when he died?\\nIntermediate answer: Alan Turing was 41 years old when he died.\\nSo the final answer is: Muhammad Ali\"\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"question\": \"When was the founder of craigslist born?\",\n",
|
||||
" \"answer\": \"Are follow up questions needed here: Yes.\\nFollow up: Who was the founder of craigslist?\\nIntermediate answer: Craigslist was founded by Craig Newmark.\\nFollow up: When was Craig Newmark born?\\nIntermediate answer: Craig Newmark was born on December 6, 1952.\\nSo the final answer is: December 6, 1952\"\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"question\": \"Who was the maternal grandfather of George Washington?\",\n",
|
||||
" \"answer\": \"Are follow up questions needed here: Yes.\\nFollow up: Who was the mother of George Washington?\\nIntermediate answer: The mother of George Washington was Mary Ball Washington.\\nFollow up: Who was the father of Mary Ball Washington?\\nIntermediate answer: The father of Mary Ball Washington was Joseph Ball.\\nSo the final answer is: Joseph Ball\"\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"question\": \"Are both the directors of Jaws and Casino Royale from the same country?\",\n",
|
||||
" \"answer\": \"Are follow up questions needed here: Yes.\\nFollow up: Who is the director of Jaws?\\nIntermediate Answer: The director of Jaws is Steven Spielberg.\\nFollow up: Where is Steven Spielberg from?\\nIntermediate Answer: The United States.\\nFollow up: Who is the director of Casino Royale?\\nIntermediate Answer: The director of Casino Royale is Martin Campbell.\\nFollow up: Where is Martin Campbell from?\\nIntermediate Answer: New Zealand.\\nSo the final answer is: No\"\n",
|
||||
" }\n",
|
||||
"]\n",
|
||||
"example_prompt = PromptTemplate(input_variables=[\"question\", \"answer\"], template=\"Question: {question}\\n{answer}\")\n",
|
||||
"\n",
|
||||
"prompt = FewShotPromptTemplate(\n",
|
||||
" examples=examples, \n",
|
||||
" example_prompt=example_prompt, \n",
|
||||
" suffix=\"Question: {input}\", \n",
|
||||
" input_variables=[\"input\"]\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "95fc0059",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# ReAct\n",
|
||||
"\n",
|
||||
"examples = [\n",
|
||||
" {\n",
|
||||
" \"question\": \"What is the elevation range for the area that the eastern sector of the Colorado orogeny extends into?\",\n",
|
||||
" \"answer\": \"Thought 1: I need to search Colorado orogeny, find the area that the eastern sector of the Colorado orogeny extends into, then find the elevation range of that area.\\nAction 1: Search[Colorado orogeny]\\nObservation 1: The Colorado orogeny was an episode of mountain building (an orogeny) in Colorado and surrounding areas.\\nThought 2: It does not mention the eastern sector. So I need to look up eastern sector.\\nAction 2: Lookup[eastern sector]\\nObservation 2: (Result 1 / 1) The eastern sector extends into the High Plains and is called the Central Plains orogeny.\\nThought 3: The eastern sector of Colorado orogeny extends into the High Plains. So I need to search High Plains and find its elevation range.\\nAction 3: Search[High Plains]\\nObservation 3: High Plains refers to one of two distinct land regions\\nThought 4: I need to instead search High Plains (United States).\\nAction 4: Search[High Plains (United States)]\\nObservation 4: The High Plains are a subregion of the Great Plains. From east to west, the High Plains rise in elevation from around 1,800 to 7,000 ft (550 to 2,130 m).[3]\\nThought 5: High Plains rise in elevation from around 1,800 to 7,000 ft, so the answer is 1,800 to 7,000 ft.\\nAction 5: Finish[1,800 to 7,000 ft]\"\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"question\": \"Musician and satirist Allie Goertz wrote a song about the \\\"The Simpsons\\\" character Milhouse, who Matt Groening named after who?\",\n",
|
||||
" \"answer\": \"Thought 1: The question simplifies to \\\"The Simpsons\\\" character Milhouse is named after who. I only need to search Milhouse and find who it is named after.\\nAction 1: Search[Milhouse]\\nObservation 1: Milhouse Mussolini Van Houten is a recurring character in the Fox animated television series The Simpsons voiced by Pamela Hayden and created by Matt Groening.\\nThought 2: The paragraph does not tell who Milhouse is named after, maybe I can look up \\\"named after\\\".\\nAction 2: Lookup[named after]\\nObservation 2: (Result 1 / 1) Milhouse was named after U.S. president Richard Nixon, whose middle name was Milhous.\\nThought 3: Milhouse was named after U.S. president Richard Nixon, so the answer is Richard Nixon.\\nAction 3: Finish[Richard Nixon]\"\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"question\": \"Which documentary is about Finnish rock groups, Adam Clayton Powell or The Saimaa Gesture?\",\n",
|
||||
" \"answer\": \"Thought 1: I need to search Adam Clayton Powell and The Saimaa Gesture, and find which documentary is about Finnish rock groups.\\nAction 1: Search[Adam Clayton Powell]\\nObservation 1 Could not find [Adam Clayton Powell]. Similar: [’Adam Clayton Powell III’, ’Seventh Avenue (Manhattan)’, ’Adam Clayton Powell Jr. State Office Building’, ’Isabel Washington Powell’, ’Adam Powell’, ’Adam Clayton Powell (film)’, ’Giancarlo Esposito’].\\nThought 2: To find the documentary, I can search Adam Clayton Powell (film).\\nAction 2: Search[Adam Clayton Powell (film)]\\nObservation 2: Adam Clayton Powell is a 1989 American documentary film directed by Richard Kilberg. The film is about the rise and fall of influential African-American politician Adam Clayton Powell Jr.[3][4] It was later aired as part of the PBS series The American Experience.\\nThought 3: Adam Clayton Powell (film) is a documentary about an African-American politician, not Finnish rock groups. So the documentary about Finnish rock groups must instead be The Saimaa Gesture.\\nAction 3: Finish[The Saimaa Gesture]\"\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"question\": \"What profession does Nicholas Ray and Elia Kazan have in common?\",\n",
|
||||
" \"answer\": \"Thought 1: I need to search Nicholas Ray and Elia Kazan, find their professions, then find the profession they have in common.\\nAction 1: Search[Nicholas Ray]\\nObservation 1: Nicholas Ray (born Raymond Nicholas Kienzle Jr., August 7, 1911 - June 16, 1979) was an American film director, screenwriter, and actor best known for the 1955 film Rebel Without a Cause.\\nThought 2: Professions of Nicholas Ray are director, screenwriter, and actor. I need to search Elia Kazan next and find his professions.\\nAction 2: Search[Elia Kazan]\\nObservation 2: Elia Kazan was an American film and theatre director, producer, screenwriter and actor.\\nThought 3: Professions of Elia Kazan are director, producer, screenwriter, and actor. So profession Nicholas Ray and Elia Kazan have in common is director, screenwriter, and actor.\\nAction 3: Finish[director, screenwriter, actor]\"\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"question\": \"Which magazine was started first Arthur’s Magazine or First for Women?\",\n",
|
||||
" \"answer\": \"Thought 1: I need to search Arthur’s Magazine and First for Women, and find which was started first.\\nAction 1: Search[Arthur’s Magazine]\\nObservation 1: Arthur’s Magazine (1844-1846) was an American literary periodical published in Philadelphia in the 19th century.\\nThought 2: Arthur’s Magazine was started in 1844. I need to search First for Women next.\\nAction 2: Search[First for Women]\\nObservation 2: First for Women is a woman’s magazine published by Bauer Media Group in the USA.[1] The magazine was started in 1989.\\nThought 3: First for Women was started in 1989. 1844 (Arthur’s Magazine) < 1989 (First for Women), so Arthur’s Magazine was started first.\\nAction 3: Finish[Arthur’s Magazine]\"\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"question\": \"Were Pavel Urysohn and Leonid Levin known for the same type of work?\",\n",
|
||||
" \"answer\": \"Thought 1: I need to search Pavel Urysohn and Leonid Levin, find their types of work, then find if they are the same.\\nAction 1: Search[Pavel Urysohn]\\nObservation 1: Pavel Samuilovich Urysohn (February 3, 1898 - August 17, 1924) was a Soviet mathematician who is best known for his contributions in dimension theory.\\nThought 2: Pavel Urysohn is a mathematician. I need to search Leonid Levin next and find its type of work.\\nAction 2: Search[Leonid Levin]\\nObservation 2: Leonid Anatolievich Levin is a Soviet-American mathematician and computer scientist.\\nThought 3: Leonid Levin is a mathematician and computer scientist. So Pavel Urysohn and Leonid Levin have the same type of work.\\nAction 3: Finish[yes]\"\n",
|
||||
" }\n",
|
||||
"]\n",
|
||||
"example_prompt = PromptTemplate(input_variables=[\"question\", \"answer\"], template=\"Question: {question}\\n{answer}\")\n",
|
||||
"\n",
|
||||
"prompt = FewShotPrompt(\n",
|
||||
" examples=examples, \n",
|
||||
" example_prompt=example_prompt, \n",
|
||||
" suffix=\"Question: {input}\", \n",
|
||||
" input_variables=[\"input\"]\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "897d4e08",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# LLM Math\n",
|
||||
"examples = [\n",
|
||||
" {\n",
|
||||
" \"question\": \"What is 37593 * 67?\",\n",
|
||||
" \"answer\": \"```python\\nprint(37593 * 67)\\n```\\n```output\\n2518731\\n```\\nAnswer: 2518731\"\n",
|
||||
" }\n",
|
||||
"]\n",
|
||||
"example_prompt = PromptTemplate(input_variables=[\"question\", \"answer\"], template=\"Question: {question}\\n\\n{answer}\")\n",
|
||||
"\n",
|
||||
"prompt = FewShotPromptTemplate(\n",
|
||||
" examples=examples, \n",
|
||||
" example_prompt=example_prompt, \n",
|
||||
" suffix=\"Question: {input}\", \n",
|
||||
" input_variables=[\"input\"]\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"id": "7ab7379f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# NatBot\n",
|
||||
"example_seperator = \"==================================================\"\n",
|
||||
"content_1 = \"\"\"<link id=1>About</link>\n",
|
||||
"<link id=2>Store</link>\n",
|
||||
"<link id=3>Gmail</link>\n",
|
||||
"<link id=4>Images</link>\n",
|
||||
"<link id=5>(Google apps)</link>\n",
|
||||
"<link id=6>Sign in</link>\n",
|
||||
"<img id=7 alt=\"(Google)\"/>\n",
|
||||
"<input id=8 alt=\"Search\"></input>\n",
|
||||
"<button id=9>(Search by voice)</button>\n",
|
||||
"<button id=10>(Google Search)</button>\n",
|
||||
"<button id=11>(I'm Feeling Lucky)</button>\n",
|
||||
"<link id=12>Advertising</link>\n",
|
||||
"<link id=13>Business</link>\n",
|
||||
"<link id=14>How Search works</link>\n",
|
||||
"<link id=15>Carbon neutral since 2007</link>\n",
|
||||
"<link id=16>Privacy</link>\n",
|
||||
"<link id=17>Terms</link>\n",
|
||||
"<text id=18>Settings</text>\"\"\"\n",
|
||||
"content_2 = \"\"\"<link id=1>About</link>\n",
|
||||
"<link id=2>Store</link>\n",
|
||||
"<link id=3>Gmail</link>\n",
|
||||
"<link id=4>Images</link>\n",
|
||||
"<link id=5>(Google apps)</link>\n",
|
||||
"<link id=6>Sign in</link>\n",
|
||||
"<img id=7 alt=\"(Google)\"/>\n",
|
||||
"<input id=8 alt=\"Search\"></input>\n",
|
||||
"<button id=9>(Search by voice)</button>\n",
|
||||
"<button id=10>(Google Search)</button>\n",
|
||||
"<button id=11>(I'm Feeling Lucky)</button>\n",
|
||||
"<link id=12>Advertising</link>\n",
|
||||
"<link id=13>Business</link>\n",
|
||||
"<link id=14>How Search works</link>\n",
|
||||
"<link id=15>Carbon neutral since 2007</link>\n",
|
||||
"<link id=16>Privacy</link>\n",
|
||||
"<link id=17>Terms</link>\n",
|
||||
"<text id=18>Settings</text>\"\"\"\n",
|
||||
"content_3 = \"\"\"<button id=1>For Businesses</button>\n",
|
||||
"<button id=2>Mobile</button>\n",
|
||||
"<button id=3>Help</button>\n",
|
||||
"<button id=4 alt=\"Language Picker\">EN</button>\n",
|
||||
"<link id=5>OpenTable logo</link>\n",
|
||||
"<button id=6 alt =\"search\">Search</button>\n",
|
||||
"<text id=7>Find your table for any occasion</text>\n",
|
||||
"<button id=8>(Date selector)</button>\n",
|
||||
"<text id=9>Sep 28, 2022</text>\n",
|
||||
"<text id=10>7:00 PM</text>\n",
|
||||
"<text id=11>2 people</text>\n",
|
||||
"<input id=12 alt=\"Location, Restaurant, or Cuisine\"></input>\n",
|
||||
"<button id=13>Let’s go</button>\n",
|
||||
"<text id=14>It looks like you're in Peninsula. Not correct?</text>\n",
|
||||
"<button id=15>Get current location</button>\n",
|
||||
"<button id=16>Next</button>\"\"\"\n",
|
||||
"examples = [\n",
|
||||
" {\n",
|
||||
" \"i\": 1,\n",
|
||||
" \"content\": content_1,\n",
|
||||
" \"objective\": \"Find a 2 bedroom house for sale in Anchorage AK for under $750k\",\n",
|
||||
" \"current_url\": \"https://www.google.com/\",\n",
|
||||
" \"command\": 'TYPESUBMIT 8 \"anchorage redfin\"'\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"i\": 2,\n",
|
||||
" \"content\": content_2,\n",
|
||||
" \"objective\": \"Make a reservation for 4 at Dorsia at 8pm\",\n",
|
||||
" \"current_url\": \"https://www.google.com/\",\n",
|
||||
" \"command\": 'TYPESUBMIT 8 \"dorsia nyc opentable\"'\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"i\": 3,\n",
|
||||
" \"content\": content_3,\n",
|
||||
" \"objective\": \"Make a reservation for 4 for dinner at Dorsia in New York City at 8pm\",\n",
|
||||
" \"current_url\": \"https://www.opentable.com/\",\n",
|
||||
" \"command\": 'TYPESUBMIT 12 \"dorsia new york city\"'\n",
|
||||
" },\n",
|
||||
"]\n",
|
||||
"example_prompt_template=\"\"\"EXAMPLE {i}:\n",
|
||||
"==================================================\n",
|
||||
"CURRENT BROWSER CONTENT:\n",
|
||||
"------------------\n",
|
||||
"{content}\n",
|
||||
"------------------\n",
|
||||
"OBJECTIVE: {objective}\n",
|
||||
"CURRENT URL: {current_url}\n",
|
||||
"YOUR COMMAND:\n",
|
||||
"{command}\"\"\"\n",
|
||||
"example_prompt = PromptTemplate(input_variables=[\"i\", \"content\", \"objective\", \"current_url\", \"command\"], template=example_prompt_template)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"prefix = \"\"\"\n",
|
||||
"You are an agent controlling a browser. You are given:\n",
|
||||
"\t(1) an objective that you are trying to achieve\n",
|
||||
"\t(2) the URL of your current web page\n",
|
||||
"\t(3) a simplified text description of what's visible in the browser window (more on that below)\n",
|
||||
"You can issue these commands:\n",
|
||||
"\tSCROLL UP - scroll up one page\n",
|
||||
"\tSCROLL DOWN - scroll down one page\n",
|
||||
"\tCLICK X - click on a given element. You can only click on links, buttons, and inputs!\n",
|
||||
"\tTYPE X \"TEXT\" - type the specified text into the input with id X\n",
|
||||
"\tTYPESUBMIT X \"TEXT\" - same as TYPE above, except then it presses ENTER to submit the form\n",
|
||||
"The format of the browser content is highly simplified; all formatting elements are stripped.\n",
|
||||
"Interactive elements such as links, inputs, buttons are represented like this:\n",
|
||||
"\t\t<link id=1>text</link>\n",
|
||||
"\t\t<button id=2>text</button>\n",
|
||||
"\t\t<input id=3>text</input>\n",
|
||||
"Images are rendered as their alt text like this:\n",
|
||||
"\t\t<img id=4 alt=\"\"/>\n",
|
||||
"Based on your given objective, issue whatever command you believe will get you closest to achieving your goal.\n",
|
||||
"You always start on Google; you should submit a search query to Google that will take you to the best page for\n",
|
||||
"achieving your objective. And then interact with that page to achieve your objective.\n",
|
||||
"If you find yourself on Google and there are no search results displayed yet, you should probably issue a command\n",
|
||||
"like \"TYPESUBMIT 7 \"search query\"\" to get to a more useful page.\n",
|
||||
"Then, if you find yourself on a Google search results page, you might issue the command \"CLICK 24\" to click\n",
|
||||
"on the first link in the search results. (If your previous command was a TYPESUBMIT your next command should\n",
|
||||
"probably be a CLICK.)\n",
|
||||
"Don't try to interact with elements that you can't see.\n",
|
||||
"Here are some examples:\n",
|
||||
"\"\"\"\n",
|
||||
"suffix=\"\"\"\n",
|
||||
"The current browser content, objective, and current URL follow. Reply with your next command to the browser.\n",
|
||||
"CURRENT BROWSER CONTENT:\n",
|
||||
"------------------\n",
|
||||
"{browser_content}\n",
|
||||
"------------------\n",
|
||||
"OBJECTIVE: {objective}\n",
|
||||
"CURRENT URL: {url}\n",
|
||||
"PREVIOUS COMMAND: {previous_command}\n",
|
||||
"YOUR COMMAND:\n",
|
||||
"\"\"\"\n",
|
||||
"PROMPT = FewShotPromptTemplate(\n",
|
||||
" examples = examples,\n",
|
||||
" example_prompt=example_prompt,\n",
|
||||
" example_separator=example_seperator,\n",
|
||||
" input_variables=[\"browser_content\", \"url\", \"previous_command\", \"objective\"],\n",
|
||||
" prefix=prefix,\n",
|
||||
" suffix=suffix,\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "ce5927c6",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"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.8.7"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
11
docs/examples/prompts/few_shot_prompt.json
Normal file
11
docs/examples/prompts/few_shot_prompt.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"_type": "few_shot",
|
||||
"input_variables": ["adjective"],
|
||||
"prefix": "Write antonyms for the following words.",
|
||||
"example_prompt": {
|
||||
"input_variables": ["input", "output"],
|
||||
"template": "Input: {input}\nOutput: {output}"
|
||||
},
|
||||
"examples": "examples.json",
|
||||
"suffix": "Input: {adjective}\nOutput:"
|
||||
}
|
14
docs/examples/prompts/few_shot_prompt.yaml
Normal file
14
docs/examples/prompts/few_shot_prompt.yaml
Normal file
@@ -0,0 +1,14 @@
|
||||
_type: few_shot
|
||||
input_variables:
|
||||
["adjective"]
|
||||
prefix:
|
||||
Write antonyms for the following words.
|
||||
example_prompt:
|
||||
input_variables:
|
||||
["input", "output"]
|
||||
template:
|
||||
"Input: {input}\nOutput: {output}"
|
||||
examples:
|
||||
examples.json
|
||||
suffix:
|
||||
"Input: {adjective}\nOutput:"
|
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"_type": "few_shot",
|
||||
"input_variables": ["adjective"],
|
||||
"prefix": "Write antonyms for the following words.",
|
||||
"example_prompt_path": "example_prompt.json",
|
||||
"examples": "examples.json",
|
||||
"suffix": "Input: {adjective}\nOutput:"
|
||||
}
|
14
docs/examples/prompts/few_shot_prompt_examples_in.json
Normal file
14
docs/examples/prompts/few_shot_prompt_examples_in.json
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"_type": "few_shot",
|
||||
"input_variables": ["adjective"],
|
||||
"prefix": "Write antonyms for the following words.",
|
||||
"example_prompt": {
|
||||
"input_variables": ["input", "output"],
|
||||
"template": "Input: {input}\nOutput: {output}"
|
||||
},
|
||||
"examples": [
|
||||
{"input": "happy", "output": "sad"},
|
||||
{"input": "tall", "output": "short"}
|
||||
],
|
||||
"suffix": "Input: {adjective}\nOutput:"
|
||||
}
|
@@ -25,9 +25,9 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.chains.react.prompt import EXAMPLES\n",
|
||||
"from langchain.llms.openai import OpenAI\n",
|
||||
"from langchain.example_generator import generate_example, generate_example_from_dynamic_prompt"
|
||||
"from langchain.example_generator import generate_example\n",
|
||||
"from langchain.prompts import PromptTemplate"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -39,26 +39,41 @@
|
||||
"name": "#%%\n"
|
||||
}
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'Question: What is the elevation range for the area that the eastern sector of the\\nColorado orogeny extends into?\\nThought 1: I need to search Colorado orogeny, find the area that the eastern sector\\nof the Colorado orogeny extends into, then find the elevation range of the\\narea.\\nAction 1: Search[Colorado orogeny]\\nObservation 1: The Colorado orogeny was an episode of mountain building (an orogeny) in\\nColorado and surrounding areas.\\nThought 2: It does not mention the eastern sector. So I need to look up eastern\\nsector.\\nAction 2: Lookup[eastern sector]\\nObservation 2: (Result 1 / 1) The eastern sector extends into the High Plains and is called\\nthe Central Plains orogeny.\\nThought 3: The eastern sector of Colorado orogeny extends into the High Plains. So I\\nneed to search High Plains and find its elevation range.\\nAction 3: Search[High Plains]\\nObservation 3: High Plains refers to one of two distinct land regions\\nThought 4: I need to instead search High Plains (United States).\\nAction 4: Search[High Plains (United States)]\\nObservation 4: The High Plains are a subregion of the Great Plains. From east to west, the\\nHigh Plains rise in elevation from around 1,800 to 7,000 ft (550 to 2,130\\nm).[3]\\nThought 5: High Plains rise in elevation from around 1,800 to 7,000 ft, so the answer\\nis 1,800 to 7,000 ft.\\nAction 5: Finish[1,800 to 7,000 ft]'"
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# print initial example for visibility\n",
|
||||
"EXAMPLES[0]"
|
||||
"# Use examples from ReAct\n",
|
||||
"examples = [\n",
|
||||
" {\n",
|
||||
" \"question\": \"What is the elevation range for the area that the eastern sector of the Colorado orogeny extends into?\",\n",
|
||||
" \"answer\": \"Thought 1: I need to search Colorado orogeny, find the area that the eastern sector of the Colorado orogeny extends into, then find the elevation range of that area.\\nAction 1: Search[Colorado orogeny]\\nObservation 1: The Colorado orogeny was an episode of mountain building (an orogeny) in Colorado and surrounding areas.\\nThought 2: It does not mention the eastern sector. So I need to look up eastern sector.\\nAction 2: Lookup[eastern sector]\\nObservation 2: (Result 1 / 1) The eastern sector extends into the High Plains and is called the Central Plains orogeny.\\nThought 3: The eastern sector of Colorado orogeny extends into the High Plains. So I need to search High Plains and find its elevation range.\\nAction 3: Search[High Plains]\\nObservation 3: High Plains refers to one of two distinct land regions\\nThought 4: I need to instead search High Plains (United States).\\nAction 4: Search[High Plains (United States)]\\nObservation 4: The High Plains are a subregion of the Great Plains. From east to west, the High Plains rise in elevation from around 1,800 to 7,000 ft (550 to 2,130 m).[3]\\nThought 5: High Plains rise in elevation from around 1,800 to 7,000 ft, so the answer is 1,800 to 7,000 ft.\\nAction 5: Finish[1,800 to 7,000 ft]\"\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"question\": \"Musician and satirist Allie Goertz wrote a song about the \\\"The Simpsons\\\" character Milhouse, who Matt Groening named after who?\",\n",
|
||||
" \"answer\": \"Thought 1: The question simplifies to \\\"The Simpsons\\\" character Milhouse is named after who. I only need to search Milhouse and find who it is named after.\\nAction 1: Search[Milhouse]\\nObservation 1: Milhouse Mussolini Van Houten is a recurring character in the Fox animated television series The Simpsons voiced by Pamela Hayden and created by Matt Groening.\\nThought 2: The paragraph does not tell who Milhouse is named after, maybe I can look up \\\"named after\\\".\\nAction 2: Lookup[named after]\\nObservation 2: (Result 1 / 1) Milhouse was named after U.S. president Richard Nixon, whose middle name was Milhous.\\nThought 3: Milhouse was named after U.S. president Richard Nixon, so the answer is Richard Nixon.\\nAction 3: Finish[Richard Nixon]\"\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"question\": \"Which documentary is about Finnish rock groups, Adam Clayton Powell or The Saimaa Gesture?\",\n",
|
||||
" \"answer\": \"Thought 1: I need to search Adam Clayton Powell and The Saimaa Gesture, and find which documentary is about Finnish rock groups.\\nAction 1: Search[Adam Clayton Powell]\\nObservation 1 Could not find [Adam Clayton Powell]. Similar: [’Adam Clayton Powell III’, ’Seventh Avenue (Manhattan)’, ’Adam Clayton Powell Jr. State Office Building’, ’Isabel Washington Powell’, ’Adam Powell’, ’Adam Clayton Powell (film)’, ’Giancarlo Esposito’].\\nThought 2: To find the documentary, I can search Adam Clayton Powell (film).\\nAction 2: Search[Adam Clayton Powell (film)]\\nObservation 2: Adam Clayton Powell is a 1989 American documentary film directed by Richard Kilberg. The film is about the rise and fall of influential African-American politician Adam Clayton Powell Jr.[3][4] It was later aired as part of the PBS series The American Experience.\\nThought 3: Adam Clayton Powell (film) is a documentary about an African-American politician, not Finnish rock groups. So the documentary about Finnish rock groups must instead be The Saimaa Gesture.\\nAction 3: Finish[The Saimaa Gesture]\"\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"question\": \"What profession does Nicholas Ray and Elia Kazan have in common?\",\n",
|
||||
" \"answer\": \"Thought 1: I need to search Nicholas Ray and Elia Kazan, find their professions, then find the profession they have in common.\\nAction 1: Search[Nicholas Ray]\\nObservation 1: Nicholas Ray (born Raymond Nicholas Kienzle Jr., August 7, 1911 - June 16, 1979) was an American film director, screenwriter, and actor best known for the 1955 film Rebel Without a Cause.\\nThought 2: Professions of Nicholas Ray are director, screenwriter, and actor. I need to search Elia Kazan next and find his professions.\\nAction 2: Search[Elia Kazan]\\nObservation 2: Elia Kazan was an American film and theatre director, producer, screenwriter and actor.\\nThought 3: Professions of Elia Kazan are director, producer, screenwriter, and actor. So profession Nicholas Ray and Elia Kazan have in common is director, screenwriter, and actor.\\nAction 3: Finish[director, screenwriter, actor]\"\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"question\": \"Which magazine was started first Arthur’s Magazine or First for Women?\",\n",
|
||||
" \"answer\": \"Thought 1: I need to search Arthur’s Magazine and First for Women, and find which was started first.\\nAction 1: Search[Arthur’s Magazine]\\nObservation 1: Arthur’s Magazine (1844-1846) was an American literary periodical published in Philadelphia in the 19th century.\\nThought 2: Arthur’s Magazine was started in 1844. I need to search First for Women next.\\nAction 2: Search[First for Women]\\nObservation 2: First for Women is a woman’s magazine published by Bauer Media Group in the USA.[1] The magazine was started in 1989.\\nThought 3: First for Women was started in 1989. 1844 (Arthur’s Magazine) < 1989 (First for Women), so Arthur’s Magazine was started first.\\nAction 3: Finish[Arthur’s Magazine]\"\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"question\": \"Were Pavel Urysohn and Leonid Levin known for the same type of work?\",\n",
|
||||
" \"answer\": \"Thought 1: I need to search Pavel Urysohn and Leonid Levin, find their types of work, then find if they are the same.\\nAction 1: Search[Pavel Urysohn]\\nObservation 1: Pavel Samuilovich Urysohn (February 3, 1898 - August 17, 1924) was a Soviet mathematician who is best known for his contributions in dimension theory.\\nThought 2: Pavel Urysohn is a mathematician. I need to search Leonid Levin next and find its type of work.\\nAction 2: Search[Leonid Levin]\\nObservation 2: Leonid Anatolievich Levin is a Soviet-American mathematician and computer scientist.\\nThought 3: Leonid Levin is a mathematician and computer scientist. So Pavel Urysohn and Leonid Levin have the same type of work.\\nAction 3: Finish[yes]\"\n",
|
||||
" }\n",
|
||||
"]\n",
|
||||
"example_template = PromptTemplate(template=\"Question: {question}\\n{answer}\", input_variables=[\"question\", \"answer\"])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": 5,
|
||||
"id": "a7bd36bc",
|
||||
"metadata": {
|
||||
"pycharm": {
|
||||
@@ -67,12 +82,12 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"new_example = generate_example(EXAMPLES, OpenAI())"
|
||||
"new_example = generate_example(examples, OpenAI(), example_template)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"execution_count": 6,
|
||||
"id": "e1efb008",
|
||||
"metadata": {
|
||||
"pycharm": {
|
||||
@@ -85,22 +100,15 @@
|
||||
"text/plain": [
|
||||
"['',\n",
|
||||
" '',\n",
|
||||
" 'Question: Which ocean is the world’s smallest?',\n",
|
||||
" '',\n",
|
||||
" 'Thought 1: I need to search for oceans and find which one is the world’s smallest.',\n",
|
||||
" '',\n",
|
||||
" 'Action 1: Search[oceans]',\n",
|
||||
" '',\n",
|
||||
" 'Observation 1: There are five oceans: the Pacific, Atlantic, Indian, Southern, and Arctic.',\n",
|
||||
" '',\n",
|
||||
" 'Thought 2: I need to compare the sizes of the oceans and find which one is the smallest.',\n",
|
||||
" '',\n",
|
||||
" 'Action 2: Compare[Pacific, Atlantic, Indian, Southern, Arctic]',\n",
|
||||
" '',\n",
|
||||
" 'Observation 2: The Arctic is the smallest ocean.']"
|
||||
" 'Question: Is the film \"The Omen\" based on a book?',\n",
|
||||
" 'Thought 1: I need to search \"The Omen\" and find if it is based on a book.',\n",
|
||||
" 'Action 1: Search[\"The Omen\"]',\n",
|
||||
" 'Observation 1: The Omen is a 1976 American supernatural horror film directed by Richard Donner and written by David Seltzer.',\n",
|
||||
" 'Thought 2: The Omen is not based on a book.']"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -134,7 +142,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.7.6"
|
||||
"version": "3.8.7"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
540
docs/examples/prompts/prompt_management.ipynb
Normal file
540
docs/examples/prompts/prompt_management.ipynb
Normal file
@@ -0,0 +1,540 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "43fb16cb",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Prompt Management\n",
|
||||
"\n",
|
||||
"Managing your prompts is annoying and tedious, with everyone writing their own slightly different variants of the same ideas. But it shouldn't be this way. \n",
|
||||
"\n",
|
||||
"LangChain provides a standard and flexible way for specifying and managing all your prompts, as well as clear and specific terminology around them. This notebook goes through the core components of working with prompts, showing how to use them as well as explaining what they do.\n",
|
||||
"\n",
|
||||
"This notebook covers how to work with prompts in Python. If you are interested in how to work with serialized versions of prompts and load them from disk, see [this notebook](prompt_serialization.ipynb)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "890aad4d",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### The BasePromptTemplate Interface\n",
|
||||
"\n",
|
||||
"A prompt template is a mechanism for constructing a prompt to pass to the language model given some user input. Below is the interface that all different types of prompt templates should expose.\n",
|
||||
"\n",
|
||||
"```python\n",
|
||||
"class BasePromptTemplate(ABC):\n",
|
||||
"\n",
|
||||
" input_variables: List[str]\n",
|
||||
" \"\"\"A list of the names of the variables the prompt template expects.\"\"\"\n",
|
||||
"\n",
|
||||
" @abstractmethod\n",
|
||||
" def format(self, **kwargs: Any) -> str:\n",
|
||||
" \"\"\"Format the prompt with the inputs.\n",
|
||||
"\n",
|
||||
" Args:\n",
|
||||
" kwargs: Any arguments to be passed to the prompt template.\n",
|
||||
"\n",
|
||||
" Returns:\n",
|
||||
" A formatted string.\n",
|
||||
"\n",
|
||||
" Example:\n",
|
||||
"\n",
|
||||
" .. code-block:: python\n",
|
||||
"\n",
|
||||
" prompt.format(variable1=\"foo\")\n",
|
||||
" \"\"\"\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"The only two things that define a prompt are:\n",
|
||||
"\n",
|
||||
"1. `input_variables`: The user inputted variables that are needed to format the prompt.\n",
|
||||
"2. `format`: A method which takes in keyword arguments are returns a formatted prompt. The keys are expected to be the input variables\n",
|
||||
" \n",
|
||||
"The rest of the logic of how the prompt is constructed is left up to different implementations. Let's take a look at some below."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "cddb465e",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### PromptTemplate\n",
|
||||
"\n",
|
||||
"This is the most simple type of prompt - a string template that takes any number of input variables. The template should be formatted as a Python f-string, although we will support other formats (Jinja, Mako, etc) in the future. \n",
|
||||
"\n",
|
||||
"If you just want to use a hardcoded prompt template, you should use this implementation.\n",
|
||||
"\n",
|
||||
"Let's walk through a few examples."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"id": "094229f4",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.prompts import PromptTemplate"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"id": "ab46bd2a",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'Tell me a joke.'"
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# An example prompt with no input variables\n",
|
||||
"no_input_prompt = PromptTemplate(input_variables=[], template=\"Tell me a joke.\")\n",
|
||||
"no_input_prompt.format()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "c3ad0fa8",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'Tell me a funny joke.'"
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# An example prompt with one input variable\n",
|
||||
"one_input_prompt = PromptTemplate(input_variables=[\"adjective\"], template=\"Tell me a {adjective} joke.\")\n",
|
||||
"one_input_prompt.format(adjective=\"funny\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"id": "ba577dcf",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'Tell me a funny joke about chickens.'"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# An example prompt with multiple input variables\n",
|
||||
"multiple_input_prompt = PromptTemplate(\n",
|
||||
" input_variables=[\"adjective\", \"content\"], \n",
|
||||
" template=\"Tell me a {adjective} joke about {content}.\"\n",
|
||||
")\n",
|
||||
"multiple_input_prompt.format(adjective=\"funny\", content=\"chickens\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "1492b49d",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Few Shot Prompts\n",
|
||||
"\n",
|
||||
"A FewShotPromptTemplate is a prompt template that includes some examples. If you have collected some examples of how the task should be done, you can insert them into prompt using this class.\n",
|
||||
"\n",
|
||||
"Examples are datapoints that can be included in the prompt in order to give the model more context what to do. Examples are represented as a dictionary of key-value pairs, with the key being the input (or label) name, and the value being the input (or label) value. \n",
|
||||
"\n",
|
||||
"In addition to the example, we also need to specify how the example should be formatted when it's inserted in the prompt. We can do this using the above `PromptTemplate`!"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"id": "3eb36972",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# These are some examples of a pretend task of creating antonyms.\n",
|
||||
"examples = [\n",
|
||||
" {\"input\": \"happy\", \"output\": \"sad\"},\n",
|
||||
" {\"input\": \"tall\", \"output\": \"short\"},\n",
|
||||
"]\n",
|
||||
"# This how we specify how the example should be formatted.\n",
|
||||
"example_prompt = PromptTemplate(\n",
|
||||
" input_variables=[\"input\",\"output\"],\n",
|
||||
" template=\"Input: {input}\\nOutput: {output}\",\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"id": "80a91d96",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.prompts.few_shot import FewShotPromptTemplate"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"id": "7931e5f2",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Give the antonym of every input\n",
|
||||
"\n",
|
||||
"Input: happy\n",
|
||||
"Output: sad\n",
|
||||
"\n",
|
||||
"Input: tall\n",
|
||||
"Output: short\n",
|
||||
"\n",
|
||||
"Input: big\n",
|
||||
"Output:\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"prompt_from_string_examples = FewShotPromptTemplate(\n",
|
||||
" # These are the examples we want to insert into the prompt.\n",
|
||||
" examples=examples,\n",
|
||||
" # This is how we want to format the examples when we insert them into the prompt.\n",
|
||||
" example_prompt=example_prompt,\n",
|
||||
" # The prefix is some text that goes before the examples in the prompt.\n",
|
||||
" # Usually, this consists of intructions.\n",
|
||||
" prefix=\"Give the antonym of every input\",\n",
|
||||
" # The suffix is some text that goes after the examples in the prompt.\n",
|
||||
" # Usually, this is where the user input will go\n",
|
||||
" suffix=\"Input: {adjective}\\nOutput:\", \n",
|
||||
" # The input variables are the variables that the overall prompt expects.\n",
|
||||
" input_variables=[\"adjective\"],\n",
|
||||
" # The example_separator is the string we will use to join the prefix, examples, and suffix together with.\n",
|
||||
" example_separator=\"\\n\\n\"\n",
|
||||
" \n",
|
||||
")\n",
|
||||
"print(prompt_from_string_examples.format(adjective=\"big\"))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "bf038596",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### ExampleSelector\n",
|
||||
"If you have a large number of examples, you may need to select which ones to include in the prompt. The ExampleSelector is the class responsible for doing so. The base interface is defined as below.\n",
|
||||
"\n",
|
||||
"```python\n",
|
||||
"class BaseExampleSelector(ABC):\n",
|
||||
" \"\"\"Interface for selecting examples to include in prompts.\"\"\"\n",
|
||||
"\n",
|
||||
" @abstractmethod\n",
|
||||
" def select_examples(self, input_variables: Dict[str, str]) -> List[dict]:\n",
|
||||
" \"\"\"Select which examples to use based on the inputs.\"\"\"\n",
|
||||
"\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"The only method it needs to expose is a `select_examples` method. This takes in the input variables and then returns a list of examples. It is up to each specific implementation as to how those examples are selected. Let's take a look at some below."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "861a4d1f",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### LengthBased ExampleSelector\n",
|
||||
"\n",
|
||||
"This ExampleSelector selects which examples to use based on length. This is useful when you are worried about constructing a prompt that will go over the length of the context window. For longer inputs, it will select fewer examples to include, while for shorter inputs it will select more.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"id": "7c469c95",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.prompts.example_selector.length_based import LengthBasedExampleSelector"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"id": "0ec6d950",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# These are a lot of examples of a pretend task of creating antonyms.\n",
|
||||
"examples = [\n",
|
||||
" {\"input\": \"happy\", \"output\": \"sad\"},\n",
|
||||
" {\"input\": \"tall\", \"output\": \"short\"},\n",
|
||||
" {\"input\": \"energetic\", \"output\": \"lethargic\"},\n",
|
||||
" {\"input\": \"sunny\", \"output\": \"gloomy\"},\n",
|
||||
" {\"input\": \"windy\", \"output\": \"calm\"},\n",
|
||||
"]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 25,
|
||||
"id": "207e55f7",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"example_selector = LengthBasedExampleSelector(\n",
|
||||
" # These are the examples is has available to choose from.\n",
|
||||
" examples=examples, \n",
|
||||
" # This is the PromptTemplate being used to format the examples.\n",
|
||||
" example_prompt=example_prompt, \n",
|
||||
" # This is the maximum length that the formatted examples should be.\n",
|
||||
" # Length is measured by the get_text_length function below.\n",
|
||||
" max_length=18,\n",
|
||||
" # This is the function used to get the length of a string, which is used\n",
|
||||
" # to determine which examples to include. It is commented out because\n",
|
||||
" # it is provided as a default value if none is specified.\n",
|
||||
" # get_text_length: Callable[[str], int] = lambda x: len(re.split(\"\\n| \", x))\n",
|
||||
")\n",
|
||||
"dynamic_prompt = FewShotPromptTemplate(\n",
|
||||
" # We provide an ExampleSelector instead of examples.\n",
|
||||
" example_selector=example_selector,\n",
|
||||
" example_prompt=example_prompt,\n",
|
||||
" prefix=\"Give the antonym of every input\",\n",
|
||||
" suffix=\"Input: {adjective}\\nOutput:\", \n",
|
||||
" input_variables=[\"adjective\"],\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 26,
|
||||
"id": "d00b4385",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Give the antonym of every input\n",
|
||||
"\n",
|
||||
"Input: happy\n",
|
||||
"Output: sad\n",
|
||||
"\n",
|
||||
"Input: tall\n",
|
||||
"Output: short\n",
|
||||
"\n",
|
||||
"Input: energetic\n",
|
||||
"Output: lethargic\n",
|
||||
"\n",
|
||||
"Input: sunny\n",
|
||||
"Output: gloomy\n",
|
||||
"\n",
|
||||
"Input: windy\n",
|
||||
"Output: calm\n",
|
||||
"\n",
|
||||
"Input: big\n",
|
||||
"Output:\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# An example with small input, so it selects all examples.\n",
|
||||
"print(dynamic_prompt.format(adjective=\"big\"))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 30,
|
||||
"id": "878bcde9",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Give the antonym of every input\n",
|
||||
"\n",
|
||||
"Input: happy\n",
|
||||
"Output: sad\n",
|
||||
"\n",
|
||||
"Input: big and huge and massive and large and gigantic and tall and bigger than everything else\n",
|
||||
"Output:\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# An example with long input, so it selects only one example.\n",
|
||||
"long_string = \"big and huge and massive and large and gigantic and tall and bigger than everything else\"\n",
|
||||
"print(dynamic_prompt.format(adjective=long_string))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "2d007b0a",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Similarity ExampleSelector\n",
|
||||
"\n",
|
||||
"The SemanticSimilarityExampleSelector selects examples based on which examples are most similar to the inputs. It does this by finding the examples with the embeddings that have the greatest cosine similarity with the inputs.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"id": "241bfe80",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.prompts.example_selector.semantic_similarity import SemanticSimilarityExampleSelector\n",
|
||||
"from langchain.vectorstores import FAISS\n",
|
||||
"from langchain.embeddings import OpenAIEmbeddings"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"id": "50d0a701",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"example_selector = SemanticSimilarityExampleSelector.from_examples(\n",
|
||||
" # This is the list of examples available to select from.\n",
|
||||
" examples, \n",
|
||||
" # This is the embedding class used to produce embeddings which are used to measure semantic similarity.\n",
|
||||
" OpenAIEmbeddings(), \n",
|
||||
" # This is the VectorStore class that is used to store the embeddings and do a similarity search over.\n",
|
||||
" FAISS, \n",
|
||||
" # This is the number of examples to produce.\n",
|
||||
" k=1\n",
|
||||
")\n",
|
||||
"similar_prompt = FewShotPromptTemplate(\n",
|
||||
" # We provide an ExampleSelector instead of examples.\n",
|
||||
" example_selector=example_selector,\n",
|
||||
" example_prompt=example_prompt,\n",
|
||||
" prefix=\"Give the antonym of every input\",\n",
|
||||
" suffix=\"Input: {adjective}\\nOutput:\", \n",
|
||||
" input_variables=[\"adjective\"],\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"id": "4c8fdf45",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Give the antonym of every input\n",
|
||||
"\n",
|
||||
"Input: happy\n",
|
||||
"Output: sad\n",
|
||||
"\n",
|
||||
"Input: worried\n",
|
||||
"Output:\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Input is a feeling, so should select the happy/sad example\n",
|
||||
"print(similar_prompt.format(adjective=\"worried\"))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"id": "829af21a",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Give the antonym of every input\n",
|
||||
"\n",
|
||||
"Input: tall\n",
|
||||
"Output: short\n",
|
||||
"\n",
|
||||
"Input: fat\n",
|
||||
"Output:\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Input is a measurment, so should select the tall/short example\n",
|
||||
"print(similar_prompt.format(adjective=\"fat\"))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "dbc32551",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Serialization\n",
|
||||
"\n",
|
||||
"PromptTemplates and examples can be serialized and loaded from disk, making it easy to share and store prompts. For a detailed walkthrough on how to do that, see [this notebook](prompt_serialization.ipynb)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "1e1e13c6",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Customizability\n",
|
||||
"The above covers all the ways currently supported in LangChain to represent prompts and example selectors. However, due to the simple interface that the base classes (`BasePromptTemplate`, `BaseExampleSelector`) expose, it should be easy to subclass them and write your own implementation in your own codebase. And of course, if you'd like to contribute that back to LangChain, we'd love that :)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "c746d6f4",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"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.7.6"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
@@ -1,179 +0,0 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "d7467b67",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Optimized Prompts\n",
|
||||
"\n",
|
||||
"This example showcases how using the OptimizedPrompt class enables selection of the most relevant examples to include as few-shot examples in the prompt."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"id": "e9e2b50b",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.chains.react.prompt import EXAMPLES, SUFFIX\n",
|
||||
"from langchain.embeddings.openai import OpenAIEmbeddings\n",
|
||||
"from langchain.example_generator import generate_example, generate_example_from_dynamic_prompt\n",
|
||||
"from langchain.llms.openai import OpenAI\n",
|
||||
"from langchain.prompts.optimized import OptimizedPrompt\n",
|
||||
"from langchain.vectorstores.elastic_vector_search import ElasticVectorSearch\n",
|
||||
"from langchain.vectorstores.faiss_search import FAISS"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"id": "cb069606",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'Question: What is the elevation range for the area that the eastern sector of the\\nColorado orogeny extends into?\\nThought 1: I need to search Colorado orogeny, find the area that the eastern sector\\nof the Colorado orogeny extends into, then find the elevation range of the\\narea.\\nAction 1: Search[Colorado orogeny]\\nObservation 1: The Colorado orogeny was an episode of mountain building (an orogeny) in\\nColorado and surrounding areas.\\nThought 2: It does not mention the eastern sector. So I need to look up eastern\\nsector.\\nAction 2: Lookup[eastern sector]\\nObservation 2: (Result 1 / 1) The eastern sector extends into the High Plains and is called\\nthe Central Plains orogeny.\\nThought 3: The eastern sector of Colorado orogeny extends into the High Plains. So I\\nneed to search High Plains and find its elevation range.\\nAction 3: Search[High Plains]\\nObservation 3: High Plains refers to one of two distinct land regions\\nThought 4: I need to instead search High Plains (United States).\\nAction 4: Search[High Plains (United States)]\\nObservation 4: The High Plains are a subregion of the Great Plains. From east to west, the\\nHigh Plains rise in elevation from around 1,800 to 7,000 ft (550 to 2,130\\nm).[3]\\nThought 5: High Plains rise in elevation from around 1,800 to 7,000 ft, so the answer\\nis 1,800 to 7,000 ft.\\nAction 5: Finish[1,800 to 7,000 ft]'"
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"EXAMPLES[0]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "5fda75a4",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"prompt = OptimizedPrompt.from_examples(\n",
|
||||
" examples=EXAMPLES, \n",
|
||||
" suffix=SUFFIX, \n",
|
||||
" input_variables=[\"input\"],\n",
|
||||
" embeddings=OpenAIEmbeddings(),\n",
|
||||
" vectorstore_cls=FAISS\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"id": "7a601df8",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\n",
|
||||
"\n",
|
||||
"Question: What is the elevation range for the area that the eastern sector of the\n",
|
||||
"Colorado orogeny extends into?\n",
|
||||
"Thought 1: I need to search Colorado orogeny, find the area that the eastern sector\n",
|
||||
"of the Colorado orogeny extends into, then find the elevation range of the\n",
|
||||
"area.\n",
|
||||
"Action 1: Search[Colorado orogeny]\n",
|
||||
"Observation 1: The Colorado orogeny was an episode of mountain building (an orogeny) in\n",
|
||||
"Colorado and surrounding areas.\n",
|
||||
"Thought 2: It does not mention the eastern sector. So I need to look up eastern\n",
|
||||
"sector.\n",
|
||||
"Action 2: Lookup[eastern sector]\n",
|
||||
"Observation 2: (Result 1 / 1) The eastern sector extends into the High Plains and is called\n",
|
||||
"the Central Plains orogeny.\n",
|
||||
"Thought 3: The eastern sector of Colorado orogeny extends into the High Plains. So I\n",
|
||||
"need to search High Plains and find its elevation range.\n",
|
||||
"Action 3: Search[High Plains]\n",
|
||||
"Observation 3: High Plains refers to one of two distinct land regions\n",
|
||||
"Thought 4: I need to instead search High Plains (United States).\n",
|
||||
"Action 4: Search[High Plains (United States)]\n",
|
||||
"Observation 4: The High Plains are a subregion of the Great Plains. From east to west, the\n",
|
||||
"High Plains rise in elevation from around 1,800 to 7,000 ft (550 to 2,130\n",
|
||||
"m).[3]\n",
|
||||
"Thought 5: High Plains rise in elevation from around 1,800 to 7,000 ft, so the answer\n",
|
||||
"is 1,800 to 7,000 ft.\n",
|
||||
"Action 5: Finish[1,800 to 7,000 ft]\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Question: What is the highest mountain peak in Asia?\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"print(prompt.format(k=1, input=\"What is the highest mountain peak in Asia?\"))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "a5dc3525",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Requires having ElasticSearch setup"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "bbd92d08",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"prompt = OptimizedPrompt.from_examples(\n",
|
||||
" examples=EXAMPLES, \n",
|
||||
" suffix=SUFFIX, \n",
|
||||
" input_variables=[\"input\"],\n",
|
||||
" embeddings=OpenAIEmbeddings(),\n",
|
||||
" vectorstore_cls=ElasticVectorSearch,\n",
|
||||
" elasticsearch_url=\"http://localhost:9200\"\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "bd91f408",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"print(prompt.format(k=1, input=\"What is the highest mountain peak in Asia?\"))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "716165c2",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"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.7.6"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
538
docs/examples/prompts/prompt_serialization.ipynb
Normal file
538
docs/examples/prompts/prompt_serialization.ipynb
Normal file
@@ -0,0 +1,538 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "43fb16cb",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Prompt Serialization\n",
|
||||
"\n",
|
||||
"It is often preferrable to store prompts not as python code but as files. This can make it easy to share, store, and version prompts. This notebook covers how to do that in LangChain, walking through all the different types of prompts and the different serialization options.\n",
|
||||
"\n",
|
||||
"At a high level, the following design principles are applied to serialization:\n",
|
||||
"1. Both JSON and YAML are supported. We want to support serialization methods are human readable on disk, and YAML and JSON are two of the most popular methods for that. Note that this rule applies to prompts. For other assets, like Examples, different serialization methods may be supported.\n",
|
||||
"2. We support specifying everything in one file, or storing different components (templates, examples, etc) in different files and referencing them. For some cases, storing everything in file makes the most sense, but for others it is preferrable to split up some of the assets (long templates, large examples, reusable components). LangChain supports both."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"id": "2c8d7587",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# All prompts are loading through the `load_prompt` function.\n",
|
||||
"from langchain.prompts.loading import load_prompt"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "cddb465e",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## PromptTemplate\n",
|
||||
"\n",
|
||||
"This section covers examples for loading a PromptTemplate."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "4d4b40f2",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Loading from YAML\n",
|
||||
"This shows an example of loading a PromptTemplate from YAML."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"id": "2d6e5117",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"input_variables:\r\n",
|
||||
" [\"adjective\", \"content\"]\r\n",
|
||||
"template: \r\n",
|
||||
" Tell me a {adjective} joke about {content}.\r\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"!cat simple_prompt.yaml"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 28,
|
||||
"id": "4f4ca686",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Tell me a funny joke about chickens.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"prompt = load_prompt(\"simple_prompt.yaml\")\n",
|
||||
"print(prompt.format(adjective=\"funny\", content=\"chickens\"))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "362eadb2",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Loading from JSON\n",
|
||||
"This shows an example of loading a PromptTemplate from JSON."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"id": "510def23",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"{\r\n",
|
||||
" \"input_variables\": [\"adjective\", \"content\"],\r\n",
|
||||
" \"template\": \"Tell me a {adjective} joke about {content}.\"\r\n",
|
||||
"}\r\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"!cat simple_prompt.json"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "d788a83c",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Loading Template from a File\n",
|
||||
"This shows an example of storing the template in a separate file and then referencing it in the config. Notice that the key changes from `template` to `template_path`."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"id": "5547760d",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Tell me a {adjective} joke about {content}."
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"!cat simple_template.txt"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 18,
|
||||
"id": "9cb13ac5",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"{\r\n",
|
||||
" \"input_variables\": [\"adjective\", \"content\"],\r\n",
|
||||
" \"template_path\": \"simple_template.txt\"\r\n",
|
||||
"}\r\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"!cat simple_prompt_with_template_file.json"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 36,
|
||||
"id": "762cb4bf",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Tell me a funny joke about chickens.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"prompt = load_prompt(\"simple_prompt_with_template_file.json\")\n",
|
||||
"print(prompt.format(adjective=\"funny\", content=\"chickens\"))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "2ae191cc",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## FewShotPromptTemplate\n",
|
||||
"\n",
|
||||
"This section covers examples for loading few shot prompt templates."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "9828f94c",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Examples\n",
|
||||
"This shows an example of what examples stored as json might look like."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 24,
|
||||
"id": "b21f5b95",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[\r\n",
|
||||
" {\"input\": \"happy\", \"output\": \"sad\"},\r\n",
|
||||
" {\"input\": \"tall\", \"output\": \"short\"}\r\n",
|
||||
"]\r\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"!cat examples.json"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "8e300335",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Loading from YAML\n",
|
||||
"This shows an example of loading a few shot example from YAML."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 19,
|
||||
"id": "e2bec0fc",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"_type: few_shot\r\n",
|
||||
"input_variables:\r\n",
|
||||
" [\"adjective\"]\r\n",
|
||||
"prefix: \r\n",
|
||||
" Write antonyms for the following words.\r\n",
|
||||
"example_prompt:\r\n",
|
||||
" input_variables:\r\n",
|
||||
" [\"input\", \"output\"]\r\n",
|
||||
" template:\r\n",
|
||||
" \"Input: {input}\\nOutput: {output}\"\r\n",
|
||||
"examples:\r\n",
|
||||
" examples.json\r\n",
|
||||
"suffix:\r\n",
|
||||
" \"Input: {adjective}\\nOutput:\"\r\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"!cat few_shot_prompt.yaml"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"id": "98c8f356",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Write antonyms for the following words.\n",
|
||||
"\n",
|
||||
"Input: happy\n",
|
||||
"Output: sad\n",
|
||||
"\n",
|
||||
"Input: tall\n",
|
||||
"Output: short\n",
|
||||
"\n",
|
||||
"Input: funny\n",
|
||||
"Output:\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"prompt = load_prompt(\"few_shot_prompt.yaml\")\n",
|
||||
"print(prompt.format(adjective=\"funny\"))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "4870aa9d",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Loading from JSON\n",
|
||||
"This shows an example of loading a few shot example from JSON."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 20,
|
||||
"id": "9d996a86",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"{\r\n",
|
||||
" \"_type\": \"few_shot\",\r\n",
|
||||
" \"input_variables\": [\"adjective\"],\r\n",
|
||||
" \"prefix\": \"Write antonyms for the following words.\",\r\n",
|
||||
" \"example_prompt\": {\r\n",
|
||||
" \"input_variables\": [\"input\", \"output\"],\r\n",
|
||||
" \"template\": \"Input: {input}\\nOutput: {output}\"\r\n",
|
||||
" },\r\n",
|
||||
" \"examples\": \"examples.json\",\r\n",
|
||||
" \"suffix\": \"Input: {adjective}\\nOutput:\"\r\n",
|
||||
"} \r\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"!cat few_shot_prompt.json"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"id": "dd2c10bb",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Write antonyms for the following words.\n",
|
||||
"\n",
|
||||
"Input: happy\n",
|
||||
"Output: sad\n",
|
||||
"\n",
|
||||
"Input: tall\n",
|
||||
"Output: short\n",
|
||||
"\n",
|
||||
"Input: funny\n",
|
||||
"Output:\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"prompt = load_prompt(\"few_shot_prompt.json\")\n",
|
||||
"print(prompt.format(adjective=\"funny\"))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "9d23faf4",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Examples in the Config\n",
|
||||
"This shows an example of referencing the examples directly in the config."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 21,
|
||||
"id": "6cd781ef",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"{\r\n",
|
||||
" \"_type\": \"few_shot\",\r\n",
|
||||
" \"input_variables\": [\"adjective\"],\r\n",
|
||||
" \"prefix\": \"Write antonyms for the following words.\",\r\n",
|
||||
" \"example_prompt\": {\r\n",
|
||||
" \"input_variables\": [\"input\", \"output\"],\r\n",
|
||||
" \"template\": \"Input: {input}\\nOutput: {output}\"\r\n",
|
||||
" },\r\n",
|
||||
" \"examples\": [\r\n",
|
||||
" {\"input\": \"happy\", \"output\": \"sad\"},\r\n",
|
||||
" {\"input\": \"tall\", \"output\": \"short\"}\r\n",
|
||||
" ],\r\n",
|
||||
" \"suffix\": \"Input: {adjective}\\nOutput:\"\r\n",
|
||||
"} \r\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"!cat few_shot_prompt_examples_in.json"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"id": "533ab8a7",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Write antonyms for the following words.\n",
|
||||
"\n",
|
||||
"Input: happy\n",
|
||||
"Output: sad\n",
|
||||
"\n",
|
||||
"Input: tall\n",
|
||||
"Output: short\n",
|
||||
"\n",
|
||||
"Input: funny\n",
|
||||
"Output:\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"prompt = load_prompt(\"few_shot_prompt_examples_in.json\")\n",
|
||||
"print(prompt.format(adjective=\"funny\"))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "2e86139e",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Example Prompt from a File\n",
|
||||
"This shows an example of loading the PromptTemplate that is used to format the examples from a separate file. Note that the key changes from `example_prompt` to `example_prompt_path`."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 22,
|
||||
"id": "0b6dd7b8",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"{\r\n",
|
||||
" \"input_variables\": [\"input\", \"output\"],\r\n",
|
||||
" \"template\": \"Input: {input}\\nOutput: {output}\" \r\n",
|
||||
"}\r\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"!cat example_prompt.json"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 23,
|
||||
"id": "76a1065d",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"{\r\n",
|
||||
" \"_type\": \"few_shot\",\r\n",
|
||||
" \"input_variables\": [\"adjective\"],\r\n",
|
||||
" \"prefix\": \"Write antonyms for the following words.\",\r\n",
|
||||
" \"example_prompt_path\": \"example_prompt.json\",\r\n",
|
||||
" \"examples\": \"examples.json\",\r\n",
|
||||
" \"suffix\": \"Input: {adjective}\\nOutput:\"\r\n",
|
||||
"} \r\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"!cat few_shot_prompt_example_prompt.json "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"id": "744d275d",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Write antonyms for the following words.\n",
|
||||
"\n",
|
||||
"Input: happy\n",
|
||||
"Output: sad\n",
|
||||
"\n",
|
||||
"Input: tall\n",
|
||||
"Output: short\n",
|
||||
"\n",
|
||||
"Input: funny\n",
|
||||
"Output:\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"prompt = load_prompt(\"few_shot_prompt_example_prompt.json\")\n",
|
||||
"print(prompt.format(adjective=\"funny\"))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "dcfc7176",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"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.7.6"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
4
docs/examples/prompts/simple_prompt.json
Normal file
4
docs/examples/prompts/simple_prompt.json
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"input_variables": ["adjective", "content"],
|
||||
"template": "Tell me a {adjective} joke about {content}."
|
||||
}
|
4
docs/examples/prompts/simple_prompt.yaml
Normal file
4
docs/examples/prompts/simple_prompt.yaml
Normal file
@@ -0,0 +1,4 @@
|
||||
input_variables:
|
||||
["adjective", "content"]
|
||||
template:
|
||||
Tell me a {adjective} joke about {content}.
|
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"input_variables": ["adjective", "content"],
|
||||
"template_path": "simple_template.txt"
|
||||
}
|
1
docs/examples/prompts/simple_template.txt
Normal file
1
docs/examples/prompts/simple_template.txt
Normal file
@@ -0,0 +1 @@
|
||||
Tell me a {adjective} joke about {content}.
|
@@ -3,11 +3,13 @@
|
||||
This section goes over the core concepts of LangChain.
|
||||
Understanding these will go a long way in helping you understand the codebase and how to construct chains.
|
||||
|
||||
## Prompts
|
||||
Prompts generically have a `format` method that takes in variables and returns a formatted string.
|
||||
## PromptTemplates
|
||||
PromptTemplates generically have a `format` method that takes in variables and returns a formatted string.
|
||||
The most simple implementation of this is to have a template string with some variables in it, and then format it with the incoming variables.
|
||||
More complex iterations dynamically construct the template string from few shot examples, etc.
|
||||
|
||||
For a more detailed explanation of how LangChain approaches prompts and prompt templates, see [here](prompts.md).
|
||||
|
||||
## LLMs
|
||||
Wrappers around Large Language Models (in particular, the `generate` ability of large language models) are some of the core functionality of LangChain.
|
||||
These wrappers are classes that are callable: they take in an input string, and return the generated output string.
|
138
docs/explanation/prompts.md
Normal file
138
docs/explanation/prompts.md
Normal file
@@ -0,0 +1,138 @@
|
||||
# Prompts
|
||||
|
||||
Prompts and all the tooling around them are integral to working with language models, and therefor
|
||||
really important to get right, from both and interface and naming perspective. This is a "design doc"
|
||||
of sorts explaining how we think about prompts and the related concepts, and why the interfaces
|
||||
for working with are the way they are in LangChain.
|
||||
|
||||
For a more code-based walkthrough of all these concept, checkout our example [here](/examples/prompts/prompt_management)
|
||||
|
||||
## Prompt
|
||||
|
||||
### Concept
|
||||
A prompt is the final string that gets fed into the language model.
|
||||
|
||||
### LangChain Implementation
|
||||
In LangChain a prompt is represented as just a string.
|
||||
|
||||
## Input Variables
|
||||
|
||||
### Concept
|
||||
Input variables are parts of a prompt that are not known until runtime, eg could be user provided.
|
||||
|
||||
### LangChain Implementation
|
||||
In LangChain input variables are just represented as a dictionary of key-value pairs, with the key
|
||||
being the variable name and the value being the variable value.
|
||||
|
||||
## Examples
|
||||
|
||||
### Concept
|
||||
Examples are basically datapoints that can be used to teach the model what to do. These can be included
|
||||
in prompts to better instruct the model on what to do.
|
||||
|
||||
### LangChain Implementation
|
||||
In LangChain examples are represented as a dictionary of key-value pairs, with the key being the feature
|
||||
(or label) name, and the value being the feature (or label) value.
|
||||
|
||||
## Example Selector
|
||||
|
||||
### Concept
|
||||
If you have a large number of examples, you may need to select which ones to include in the prompt. The
|
||||
Example Selector is the class responsible for doing so.
|
||||
|
||||
### LangChain Implementation
|
||||
|
||||
#### BaseExampleSelector
|
||||
In LangChain there is a BaseExampleSelector that exposes the following interface
|
||||
|
||||
```python
|
||||
class BaseExampleSelector:
|
||||
|
||||
def select_examples(self, input_variables: dict):
|
||||
```
|
||||
|
||||
Notice that it does not take in examples at runtime when it's selecting them - those are assumed to have been provided ahead of time.
|
||||
|
||||
#### LengthExampleSelector
|
||||
The LengthExampleSelector selects examples based on the length of the input variables.
|
||||
This is useful when you are worried about constructing a prompt that will go over the length
|
||||
of the context window. For longer inputs, it will select fewer examples to include, while for
|
||||
shorter inputs it will select more.
|
||||
|
||||
#### SemanticSimilarityExampleSelector
|
||||
The SemanticSimilarityExampleSelector selects examples based on which examples are most similar
|
||||
to the inputs. It does this by finding the examples with the embeddings that have the greatest
|
||||
cosine similarity with the inputs.
|
||||
|
||||
|
||||
## Prompt Template
|
||||
|
||||
### Concept
|
||||
The prompts that get fed into the language model are nearly always not hardcoded, but rather a combination
|
||||
of parts, including Examples and Input Variables. A prompt template is responsible
|
||||
for taking those parts and constructing a prompt.
|
||||
|
||||
### LangChain Implementation
|
||||
|
||||
#### BasePromptTemplate
|
||||
In LangChain there is a BasePromptTemplate that exposes the following interface
|
||||
|
||||
```python
|
||||
class BasePromptTemplate:
|
||||
|
||||
@property
|
||||
def input_variables(self) -> List[str]:
|
||||
|
||||
def format(self, **kwargs) -> str:
|
||||
```
|
||||
The input variables property is used to provide introspection of the PromptTemplate and know
|
||||
what inputs it expects. The format method takes in input variables and returns the prompt.
|
||||
|
||||
#### PromptTemplate
|
||||
The PromptTemplate implementation is the most simple form of a prompt template. It consists of three parts:
|
||||
- input variables: which variables this prompt template expects
|
||||
- template: the template into which these variables will be formatted
|
||||
- template format: the format of the template (eg mustache, python f-strings, etc)
|
||||
|
||||
For example, if I was making an application that took a user inputted concept and asked a language model
|
||||
to make a joke about that concept, I might use this specification for the PromptTemplate
|
||||
- input variables = `["thing"]`
|
||||
- template = `"Tell me a joke about {thing}"`
|
||||
- template format = `"f-string"`
|
||||
|
||||
#### FewShotPromptTemplate
|
||||
A FewShotPromptTemplate is a Prompt Template that includes some examples. It consists of:
|
||||
- examples OR example selector: a list of examples to use, or an Example Selector to select which examples to use
|
||||
- example prompt template: a Prompt Template responsible for taking an individual example (a dictionary) and turning it into a string to be used in the prompt.
|
||||
- prefix: the template put in the prompt before listing any examples
|
||||
- suffix: the template put in the prompt after listing any examples
|
||||
- example separator: a string separator which is used to join the prefix, the examples, and the suffix together
|
||||
|
||||
|
||||
For example, if I wanted to turn the above example into a few shot prompt, this is what it would
|
||||
look like:
|
||||
|
||||
First I would collect some examples, like
|
||||
```python
|
||||
examples = [
|
||||
{"concept": "chicken", "joke": "Why did the chicken cross the road?"},
|
||||
...
|
||||
]
|
||||
```
|
||||
|
||||
I would then make sure to define a prompt template for how each example should be formatted
|
||||
when inserted into the prompt:
|
||||
```python
|
||||
prompt_template = PromptTemplate(
|
||||
input_variables=["concept", "joke"],
|
||||
template="Tell me a joke about {concept}\n{joke}"
|
||||
)
|
||||
```
|
||||
|
||||
Then, I would define the components as:
|
||||
- examples: The above examples
|
||||
- example_prompt: The above example prompt
|
||||
- prefix = `"You are a comedian telling jokes on demand."`
|
||||
- suffix = `"Tell me a joke about {concept}"`
|
||||
- input variables = `["concept"]`
|
||||
- template format = `"f-string"`
|
@@ -12,9 +12,10 @@ This is easy to do with LangChain!
|
||||
First lets define the prompt:
|
||||
|
||||
```python
|
||||
from langchain.prompts import Prompt
|
||||
prompt = Prompt(
|
||||
input_variables=["product"],
|
||||
from langchain.prompts import PromptTemplate
|
||||
|
||||
prompt = PromptTemplate(
|
||||
input_variables=["product"],
|
||||
template="What is a good name for a company that makes {product}?",
|
||||
)
|
||||
```
|
||||
@@ -35,4 +36,4 @@ chain.run("colorful socks")
|
||||
There we go! There's the first chain.
|
||||
|
||||
That is it for the Getting Started example.
|
||||
As a next step, we would suggest checking out the more complex chains in the [Demos section](/examples/demos.rst)
|
||||
As a next step, we would suggest checking out the more complex chains in the [Demos section](/examples/demos)
|
||||
|
@@ -56,6 +56,7 @@ common tasks or cool demos.
|
||||
installation.md
|
||||
integrations.md
|
||||
modules/prompt
|
||||
modules/example_selector
|
||||
modules/llms
|
||||
modules/embeddings
|
||||
modules/text_splitter
|
||||
@@ -72,8 +73,9 @@ see detailed information about the various classes, methods, and APIs.
|
||||
:caption: Resources
|
||||
:name: resources
|
||||
|
||||
core_concepts.md
|
||||
glossary.md
|
||||
explanation/core_concepts.md
|
||||
explanation/prompts.md
|
||||
explanation/glossary.md
|
||||
Discord <https://discord.gg/6adMQxSpJS>
|
||||
|
||||
Higher level, conceptual explanations of the LangChain components.
|
||||
|
5
docs/modules/example_selector.rst
Normal file
5
docs/modules/example_selector.rst
Normal file
@@ -0,0 +1,5 @@
|
||||
:mod:`langchain.prompts.example_selector`
|
||||
=========================================
|
||||
|
||||
.. automodule:: langchain.prompts.example_selector
|
||||
:members:
|
Reference in New Issue
Block a user