Docs refactor (#480)

Big docs refactor! Motivation is to make it easier for people to find
resources they are looking for. To accomplish this, there are now three
main sections:

- Getting Started: steps for getting started, walking through most core
functionality
- Modules: these are different modules of functionality that langchain
provides. Each part here has a "getting started", "how to", "key
concepts" and "reference" section (except in a few select cases where it
didnt easily fit).
- Use Cases: this is to separate use cases (like summarization, question
answering, evaluation, etc) from the modules, and provide a different
entry point to the code base.

There is also a full reference section, as well as extra resources
(glossary, gallery, etc)

Co-authored-by: Shreya Rajpal <ShreyaR@users.noreply.github.com>
This commit is contained in:
Harrison Chase
2023-01-02 08:24:09 -08:00
committed by GitHub
parent c5f0af9398
commit 985496f4be
164 changed files with 4326 additions and 2586 deletions

View File

@@ -0,0 +1,68 @@
# Create a custom example selector
In this tutorial, we'll create a custom example selector that selects examples every alternate example given a list of examples.
An `ExampleSelector` must implement two methods:
1. An `add_example` method which takes in an example and adds it into the ExampleSelector
2. A `select_examples` method which takes in input variables (which are meant to be user input) and returns a list of examples to use in the few shot prompt.
Let's implement a custom `ExampleSelector` that just selects two examples at random.
:::{note}
Take a look at the current set of example selector implementations supported in LangChain [here](../getting_started.md).
:::
<!-- TODO(shreya): Add the correct link. -->
## Implement custom example selector
```python
from langchain.prompts.example_selector.base import BaseExampleSelector
from typing import Dict, List
import numpy as np
class CustomExampleSelector(BaseExampleSelector):
def __init__(self, examples: List[Dict[str, str]]):
self.examples = examples
def add_example(self, example: Dict[str, str]) -> None:
"""Add new example to store for a key."""
self.examples.append(example)
def select_examples(self, input_variables: Dict[str, str]) -> List[dict]:
"""Select which examples to use based on the inputs."""
return np.random.choice(self.examples, size=2, replace=False)
```
## Use custom example selector
```python
examples = [
{"foo": "1"},
{"foo": "2"},
{"foo": "3"}
]
# Initialize example selector.
example_selector = CustomExampleSelector(examples)
# Select examples
example_selector.select_examples({"foo": "foo"})
# -> array([{'foo': '2'}, {'foo': '3'}], dtype=object)
# Add new example to the set of examples
example_selector.add_example({"foo": "4"})
example_selector.examples
# -> [{'foo': '1'}, {'foo': '2'}, {'foo': '3'}, {'foo': '4'}]
# Select examples
example_selector.select_examples({"foo": "foo"})
# -> array([{'foo': '1'}, {'foo': '4'}], dtype=object)
```

View File

@@ -0,0 +1,75 @@
# Create a custom prompt template
Let's suppose we want the LLM to generate English language explanations of a function given its name. To achieve this task, we will create a custom prompt template that takes in the function name as input, and formats the prompt template to provide the source code of the function.
## Why are custom prompt templates needed?
LangChain provides a set of default prompt templates that can be used to generate prompts for a variety of tasks. However, there may be cases where the default prompt templates do not meet your needs. For example, you may want to create a prompt template with specific dynamic instructions for your language model. In such cases, you can create a custom prompt template.
:::{note}
Take a look at the current set of default prompt templates [here](../prompt_templates.md).
:::
<!-- TODO(shreya): Add correct link here. -->
## Create a custom prompt template
The only two requirements for all prompt templates are:
1. They have a input_variables attribute that exposes what input variables this prompt template expects.
2. They expose a format method which takes in keyword arguments corresponding to the expected input_variables and returns the formatted prompt.
Let's create a custom prompt template that takes in the function name as input, and formats the prompt template to provide the source code of the function.
First, let's create a function that will return the source code of a function given its name.
```python
import inspect
def get_source_code(function_name):
# Get the source code of the function
return inspect.getsource(function_name)
```
Next, we'll create a custom prompt template that takes in the function name as input, and formats the prompt template to provide the source code of the function.
```python
from langchain.prompts import BasePromptTemplate
from pydantic import BaseModel
class FunctionExplainerPromptTemplate(BasePromptTemplate, BaseModel):
""" A custom prompt template that takes in the function name as input, and formats the prompt template to provide the source code of the function. """
@validator("input_variables")
def validate_input_variables(cls, v):
""" Validate that the input variables are correct. """
if len(v) != 1 or "function_name" not in v:
raise ValueError("function_name must be the only input_variable.")
return v
def format(self, **kwargs) -> str:
# Get the source code of the function
source_code = get_source_code(kwargs["function_name"])
# Generate the prompt to be sent to the language model
prompt = f"""
Given the function name and source code, generate an English language explanation of the function.
Function Name: {kwargs["function_name"]}
Source Code:
{source_code}
Explanation:
"""
return prompt
```
## Use the custom prompt template
Now that we have created a custom prompt template, we can use it to generate prompts for our task.
```python
fn_explainer = FunctionExplainerPromptTemplate(input_variables=["function_name"])
# Generate a prompt for the function "get_source_code"
prompt = fn_explainer.format(function_name=get_source_code)
print(prompt)
```

View File

@@ -0,0 +1,4 @@
{
"input_variables": ["input", "output"],
"template": "Input: {input}\nOutput: {output}"
}

View File

@@ -0,0 +1,4 @@
[
{"input": "happy", "output": "sad"},
{"input": "tall", "output": "short"}
]

View File

@@ -0,0 +1,359 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "f8b01b97",
"metadata": {},
"source": [
"# Provide few shot examples to a prompt\n",
"\n",
"In this tutorial, we'll learn how to create a prompt template that uses few shot examples.\n",
"\n",
"We'll use the `FewShotPromptTemplate` class to create a prompt template that uses few shot examples. This class either takes in a set of examples, or an `ExampleSelector` object. In this tutorial, we'll go over both options.\n",
"\n",
"### Use Case\n",
"\n",
"In this tutorial, we'll configure few shot examples for self-ask with search.\n"
]
},
{
"cell_type": "markdown",
"id": "a619ed8e",
"metadata": {},
"source": [
"## Using an example set"
]
},
{
"cell_type": "markdown",
"id": "d8fafee8",
"metadata": {},
"source": [
"### Create the example set\n",
"\n",
"To get started, create a list of few shot examples. Each example should be a dictionary with the keys being the input variables and the values being the values for those input variables.\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "2a729c9f",
"metadata": {},
"outputs": [],
"source": [
"from langchain.prompts.few_shot import FewShotPromptTemplate\n",
"from langchain.prompts.prompt import PromptTemplate\n",
"\n",
"examples = [\n",
" {\n",
" \"question\": \"Who lived longer, Muhammad Ali or Alan Turing?\",\n",
" \"answer\": \n",
"\"\"\"\n",
"Are follow up questions needed here: Yes.\n",
"Follow up: How old was Muhammad Ali when he died?\n",
"Intermediate answer: Muhammad Ali was 74 years old when he died.\n",
"Follow up: How old was Alan Turing when he died?\n",
"Intermediate answer: Alan Turing was 41 years old when he died.\n",
"So the final answer is: Muhammad Ali\n",
"\"\"\"\n",
" },\n",
" {\n",
" \"question\": \"When was the founder of craigslist born?\",\n",
" \"answer\": \n",
"\"\"\"\n",
"Are follow up questions needed here: Yes.\n",
"Follow up: Who was the founder of craigslist?\n",
"Intermediate answer: Craigslist was founded by Craig Newmark.\n",
"Follow up: When was Craig Newmark born?\n",
"Intermediate answer: Craig Newmark was born on December 6, 1952.\n",
"So the final answer is: December 6, 1952\n",
"\"\"\"\n",
" },\n",
" {\n",
" \"question\": \"Who was the maternal grandfather of George Washington?\",\n",
" \"answer\":\n",
"\"\"\"\n",
"Are follow up questions needed here: Yes.\n",
"Follow up: Who was the mother of George Washington?\n",
"Intermediate answer: The mother of George Washington was Mary Ball Washington.\n",
"Follow up: Who was the father of Mary Ball Washington?\n",
"Intermediate answer: The father of Mary Ball Washington was Joseph Ball.\n",
"So the final answer is: Joseph Ball\n",
"\"\"\"\n",
" },\n",
" {\n",
" \"question\": \"Are both the directors of Jaws and Casino Royale from the same country?\",\n",
" \"answer\":\n",
"\"\"\"\n",
"Are follow up questions needed here: Yes.\n",
"Follow up: Who is the director of Jaws?\n",
"Intermediate Answer: The director of Jaws is Steven Spielberg.\n",
"Follow up: Where is Steven Spielberg from?\n",
"Intermediate Answer: The United States.\n",
"Follow up: Who is the director of Casino Royale?\n",
"Intermediate Answer: The director of Casino Royale is Martin Campbell.\n",
"Follow up: Where is Martin Campbell from?\n",
"Intermediate Answer: New Zealand.\n",
"So the final answer is: No\n",
"\"\"\"\n",
" }\n",
"]"
]
},
{
"cell_type": "markdown",
"id": "601ca01b",
"metadata": {},
"source": [
"### Create a formatter for the few shot examples\n",
"\n",
"Configure a formatter that will format the few shot examples into a string. This formatter should be a `PromptTemplate` object."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "bfb5d9fb",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Question: Who lived longer, Muhammad Ali or Alan Turing?\n",
"\n",
"Are follow up questions needed here: Yes.\n",
"Follow up: How old was Muhammad Ali when he died?\n",
"Intermediate answer: Muhammad Ali was 74 years old when he died.\n",
"Follow up: How old was Alan Turing when he died?\n",
"Intermediate answer: Alan Turing was 41 years old when he died.\n",
"So the final answer is: Muhammad Ali\n",
"\n"
]
}
],
"source": [
"example_prompt = PromptTemplate(input_variables=[\"question\", \"answer\"], template=\"Question: {question}\\n{answer}\")\n",
"\n",
"print(example_prompt.format(**examples[0]))"
]
},
{
"cell_type": "markdown",
"id": "ac682392",
"metadata": {},
"source": [
"### Feed examples and formatter to `FewShotPromptTemplate`\n",
"\n",
"Finally, create a `FewShotPromptTemplate` object. This object takes in the few shot examples and the formatter for the few shot examples."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "d6d87358",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Question: Who lived longer, Muhammad Ali or Alan Turing?\n",
"\n",
"Are follow up questions needed here: Yes.\n",
"Follow up: How old was Muhammad Ali when he died?\n",
"Intermediate answer: Muhammad Ali was 74 years old when he died.\n",
"Follow up: How old was Alan Turing when he died?\n",
"Intermediate answer: Alan Turing was 41 years old when he died.\n",
"So the final answer is: Muhammad Ali\n",
"\n",
"\n",
"Question: When was the founder of craigslist born?\n",
"\n",
"Are follow up questions needed here: Yes.\n",
"Follow up: Who was the founder of craigslist?\n",
"Intermediate answer: Craigslist was founded by Craig Newmark.\n",
"Follow up: When was Craig Newmark born?\n",
"Intermediate answer: Craig Newmark was born on December 6, 1952.\n",
"So the final answer is: December 6, 1952\n",
"\n",
"\n",
"Question: Who was the maternal grandfather of George Washington?\n",
"\n",
"Are follow up questions needed here: Yes.\n",
"Follow up: Who was the mother of George Washington?\n",
"Intermediate answer: The mother of George Washington was Mary Ball Washington.\n",
"Follow up: Who was the father of Mary Ball Washington?\n",
"Intermediate answer: The father of Mary Ball Washington was Joseph Ball.\n",
"So the final answer is: Joseph Ball\n",
"\n",
"\n",
"Question: Are both the directors of Jaws and Casino Royale from the same country?\n",
"\n",
"Are follow up questions needed here: Yes.\n",
"Follow up: Who is the director of Jaws?\n",
"Intermediate Answer: The director of Jaws is Steven Spielberg.\n",
"Follow up: Where is Steven Spielberg from?\n",
"Intermediate Answer: The United States.\n",
"Follow up: Who is the director of Casino Royale?\n",
"Intermediate Answer: The director of Casino Royale is Martin Campbell.\n",
"Follow up: Where is Martin Campbell from?\n",
"Intermediate Answer: New Zealand.\n",
"So the final answer is: No\n",
"\n",
"\n",
"Question: Who was the father of Mary Ball Washington?\n"
]
}
],
"source": [
"prompt = FewShotPromptTemplate(\n",
" examples=examples, \n",
" example_prompt=example_prompt, \n",
" suffix=\"Question: {input}\", \n",
" input_variables=[\"input\"]\n",
")\n",
"\n",
"print(prompt.format(input=\"Who was the father of Mary Ball Washington?\"))"
]
},
{
"cell_type": "markdown",
"id": "2bbdc79b",
"metadata": {},
"source": [
"## Using an example selector\n",
"\n",
"### Feed examples into `ExampleSelector`\n",
"\n",
"We will reuse the example set and the formatter from the previous section. However, instead of feeding the examples directly into the `FewShotPromptTemplate` object, we will feed them into an `ExampleSelector` object.\n",
"\n",
"\n",
"In this tutorial, we will use the `SemanticSimilarityExampleSelector` class. This class selects few shot examples based on their similarity to the input. It uses an embedding model to compute the similarity between the input and the few shot examples, as well as a vector store to perform the nearest neighbor search."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "63281992",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Examples most similar to the input: Who was the father of Mary Ball Washington?\n",
"\n",
"\n",
"question: Who was the maternal grandfather of George Washington?\n",
"answer: \n",
"Are follow up questions needed here: Yes.\n",
"Follow up: Who was the mother of George Washington?\n",
"Intermediate answer: The mother of George Washington was Mary Ball Washington.\n",
"Follow up: Who was the father of Mary Ball Washington?\n",
"Intermediate answer: The father of Mary Ball Washington was Joseph Ball.\n",
"So the final answer is: Joseph Ball\n",
"\n"
]
}
],
"source": [
"from langchain.prompts.example_selector import SemanticSimilarityExampleSelector\n",
"from langchain.vectorstores import FAISS\n",
"from langchain.embeddings import OpenAIEmbeddings\n",
"\n",
"\n",
"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",
"\n",
"# Select the most similar example to the input.\n",
"question = \"Who was the father of Mary Ball Washington?\"\n",
"selected_examples = example_selector.select_examples({\"question\": question})\n",
"print(f\"Examples most similar to the input: {question}\")\n",
"for example in selected_examples:\n",
" print(\"\\n\")\n",
" for k, v in example.items():\n",
" print(f\"{k}: {v}\")"
]
},
{
"cell_type": "markdown",
"id": "90e3d062",
"metadata": {},
"source": [
"### Feed example selector into `FewShotPromptTemplate`\n",
"\n",
"Finally, create a `FewShotPromptTemplate` object. This object takes in the example selector and the formatter for the few shot examples."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "96cb35b2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Question: Who was the maternal grandfather of George Washington?\n",
"\n",
"Are follow up questions needed here: Yes.\n",
"Follow up: Who was the mother of George Washington?\n",
"Intermediate answer: The mother of George Washington was Mary Ball Washington.\n",
"Follow up: Who was the father of Mary Ball Washington?\n",
"Intermediate answer: The father of Mary Ball Washington was Joseph Ball.\n",
"So the final answer is: Joseph Ball\n",
"\n",
"\n",
"Question: Who was the father of Mary Ball Washington?\n"
]
}
],
"source": [
"prompt = FewShotPromptTemplate(\n",
" example_selector=example_selector, \n",
" example_prompt=example_prompt, \n",
" suffix=\"Question: {input}\", \n",
" input_variables=[\"input\"]\n",
")\n",
"\n",
"print(prompt.format(input=\"Who was the father of Mary Ball Washington?\"))"
]
}
],
"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.10.9"
},
"vscode": {
"interpreter": {
"hash": "b1677b440931f40d89ef8be7bf03acb108ce003de0ac9b18e8d43753ea2e7103"
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View 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:"
}

View 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:"

View File

@@ -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:"
}

View 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:"
}

View File

@@ -0,0 +1,772 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "43fb16cb",
"metadata": {},
"source": [
"# Getting Started\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 and 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 template, consisting of 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": "b2dd6154",
"metadata": {},
"source": [
"## Alternative formats\n",
"\n",
"This section shows how to use alternative formats besides \"f-string\" to format prompts."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "53b41b6a",
"metadata": {},
"outputs": [],
"source": [
"# Jinja2\n",
"template = \"\"\"\n",
"{% for item in items %}\n",
"Question: {{ item.question }}\n",
"Answer: {{ item.answer }}\n",
"{% endfor %}\n",
"\"\"\"\n",
"items=[{\"question\": \"foo\", \"answer\": \"bar\"},{\"question\": \"1\", \"answer\": \"2\"}]\n",
"jinja2_prompt = PromptTemplate(\n",
" input_variables=[\"items\"], \n",
" template=template,\n",
" template_format=\"jinja2\"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "ba8aabd3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'\\n\\nQuestion: foo\\nAnswer: bar\\n\\nQuestion: 1\\nAnswer: 2\\n'"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"jinja2_prompt.format(items=items)"
]
},
{
"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": 7,
"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": 8,
"id": "80a91d96",
"metadata": {},
"outputs": [],
"source": [
"from langchain.prompts import FewShotPromptTemplate"
]
},
{
"cell_type": "code",
"execution_count": 9,
"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": 10,
"id": "7c469c95",
"metadata": {},
"outputs": [],
"source": [
"from langchain.prompts.example_selector import LengthBasedExampleSelector"
]
},
{
"cell_type": "code",
"execution_count": 11,
"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": 12,
"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=25,\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": 13,
"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": 14,
"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 much much much much much 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 much much much much much bigger than everything else\"\n",
"print(dynamic_prompt.format(adjective=long_string))"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "e4bebcd9",
"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: small\n",
"\n",
"Input: enthusiastic\n",
"Output:\n"
]
}
],
"source": [
"# You can add an example to an example selector as well.\n",
"new_example = {\"input\": \"big\", \"output\": \"small\"}\n",
"dynamic_prompt.example_selector.add_example(new_example)\n",
"print(dynamic_prompt.format(adjective=\"enthusiastic\"))"
]
},
{
"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": 16,
"id": "241bfe80",
"metadata": {},
"outputs": [],
"source": [
"from langchain.prompts.example_selector import SemanticSimilarityExampleSelector\n",
"from langchain.vectorstores import FAISS\n",
"from langchain.embeddings import OpenAIEmbeddings"
]
},
{
"cell_type": "code",
"execution_count": 17,
"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": 18,
"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": 19,
"id": "829af21a",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Give the antonym of every input\n",
"\n",
"Input: happy\n",
"Output: sad\n",
"\n",
"Input: fat\n",
"Output:\n"
]
}
],
"source": [
"# Input is a measurement, so should select the tall/short example\n",
"print(similar_prompt.format(adjective=\"fat\"))"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "3c16fe23",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Give the antonym of every input\n",
"\n",
"Input: happy\n",
"Output: sad\n",
"\n",
"Input: joyful\n",
"Output:\n"
]
}
],
"source": [
"# You can add new examples to the SemanticSimilarityExampleSelector as well\n",
"similar_prompt.example_selector.add_example({\"input\": \"enthusiastic\", \"output\": \"apathetic\"})\n",
"print(similar_prompt.format(adjective=\"joyful\"))"
]
},
{
"cell_type": "markdown",
"id": "bc35afd0",
"metadata": {},
"source": [
"### Maximal Marginal Relevance ExampleSelector\n",
"\n",
"The MaxMarginalRelevanceExampleSelector selects examples based on a combination of which examples are most similar to the inputs, while also optimizing for diversity. It does this by finding the examples with the embeddings that have the greatest cosine similarity with the inputs, and then iteratively adding them while penalizing them for closeness to already selected examples.\n"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "ac95c968",
"metadata": {},
"outputs": [],
"source": [
"from langchain.prompts.example_selector import MaxMarginalRelevanceExampleSelector"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "db579bea",
"metadata": {},
"outputs": [],
"source": [
"example_selector = MaxMarginalRelevanceExampleSelector.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=2\n",
")\n",
"mmr_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": 23,
"id": "cd76e344",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Give the antonym of every input\n",
"\n",
"Input: happy\n",
"Output: sad\n",
"\n",
"Input: windy\n",
"Output: calm\n",
"\n",
"Input: worried\n",
"Output:\n"
]
}
],
"source": [
"# Input is a feeling, so should select the happy/sad example as the first one\n",
"print(mmr_prompt.format(adjective=\"worried\"))"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "cf82956b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Give the antonym of every input\n",
"\n",
"Input: happy\n",
"Output: sad\n",
"\n",
"Input: enthusiastic\n",
"Output: apathetic\n",
"\n",
"Input: worried\n",
"Output:\n"
]
}
],
"source": [
"# Let's compare this to what we would just get if we went solely off of similarity\n",
"similar_prompt.example_selector.k = 2\n",
"print(similar_prompt.format(adjective=\"worried\"))"
]
},
{
"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.10.9"
},
"vscode": {
"interpreter": {
"hash": "b1677b440931f40d89ef8be7bf03acb108ce003de0ac9b18e8d43753ea2e7103"
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,539 @@
{
"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",
"\n",
"1. Both JSON and YAML are supported. We want to support serialization methods that 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",
"\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.\n",
"\n",
"There is also a single entry point to load prompts from disk, making it easy to load any type of prompt."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "2c8d7587",
"metadata": {},
"outputs": [],
"source": [
"# All prompts are loaded through the `load_prompt` function.\n",
"from langchain.prompts 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": 2,
"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": 3,
"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": 4,
"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": 5,
"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": 6,
"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": 7,
"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": 8,
"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": 9,
"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": 10,
"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": 11,
"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": 12,
"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": 13,
"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": 14,
"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": 15,
"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": 16,
"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": 17,
"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\"))"
]
}
],
"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.10.9"
},
"vscode": {
"interpreter": {
"hash": "b1677b440931f40d89ef8be7bf03acb108ce003de0ac9b18e8d43753ea2e7103"
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,4 @@
{
"input_variables": ["adjective", "content"],
"template": "Tell me a {adjective} joke about {content}."
}

View File

@@ -0,0 +1,4 @@
input_variables:
["adjective", "content"]
template:
Tell me a {adjective} joke about {content}.

View File

@@ -0,0 +1,4 @@
{
"input_variables": ["adjective", "content"],
"template_path": "simple_template.txt"
}

View File

@@ -0,0 +1 @@
Tell me a {adjective} joke about {content}.

View File

@@ -0,0 +1,227 @@
# Getting Started
In this tutorial, we will learn about:
- what a prompt template is, and why it is needed,
- how to create a prompt template,
- how to pass few shot examples to a prompt template,
- how to select examples for a prompt template.
## What is a prompt template?
A prompt template refers to a reproducible way to generate a prompt. It contains a text string ("the template"), that can can take in a set of parameters from the end user and generate a prompt.
The prompt template may contain:
- instructions to the language model,
- a set of few shot examples to help the language model generate a better response,
- a question to the language model.
The following code snippet contains an example of a prompt template:
```python
from langchain import PromptTemplate
template = """
I want you to act as a naming consultant for new companies.
Here are some examples of good company names:
- search engine, Google
- social media, Facebook
- video sharing, YouTube
The name should be short, catchy and easy to remember.
What is a good name for a company that makes {product}?
"""
prompt = PromptTemplate(
input_variables=["product"],
template=template,
)
```
## Create a prompt template
You can create simple hardcoded prompts using the `PromptTemplate` class. Prompt templates can take any number of input variables, and can be formatted to generate a prompt.
```python
from langchain import PromptTemplate
# An example prompt with no input variables
no_input_prompt = PromptTemplate(input_variables=[], template="Tell me a joke.")
no_input_prompt.format()
# -> "Tell me a joke."
# An example prompt with one input variable
one_input_prompt = PromptTemplate(input_variables=["adjective"], template="Tell me a {adjective} joke.")
one_input_prompt.format(adjective="funny")
# -> "Tell me a funny joke."
# An example prompt with multiple input variables
multiple_input_prompt = PromptTemplate(
input_variables=["adjective", "content"],
template="Tell me a {adjective} joke about {content}."
)
multiple_input_prompt.format(adjective="funny", content="chickens")
# -> "Tell me a funny joke about chickens."
```
You can create custom prompt templates that format the prompt in any way you want. For more information, see [Custom Prompt Templates](examples/custom_prompt_template.ipynb).
<!-- TODO(shreya): Add link to Jinja -->
:::{note}
Currently, the template should be formatted as a Python f-string. We also support Jinja2 templates (see [Using Jinja templates](examples/custom_prompt_template.ipynb)). In the future, we will support more templating languages such as Mako.
:::
## Pass few shot examples to a prompt template
Few shot examples are a set of examples that can be used to help the language model generate a better response.
To generate a prompt with few shot examples, you can use the `FewShotPromptTemplate`. This class takes in a `PromptTemplate` and a list of few shot examples. It then formats the prompt template with the few shot examples.
In this example, we'll create a prompt to generate word antonyms.
```python
from langchain import PromptTemplate, FewShotPromptTemplate
# First, create the list of few shot examples.
examples = [
{"word": "happy", "antonym": "sad"},
{"word": "tall", "antonym": "short"},
]
# Next, we specify the template to format the examples we have provided.
# We use the `PromptTemplate` class for this.
example_formatter_template = """
Word: {word}
Antonym: {antonym}\n
"""
example_prompt = PromptTemplate(
input_variables=["word", "antonym"],
template=example_formatter_template,
)
# Finally, we create the `FewShotPromptTemplate` object.
few_shot_prompt = FewShotPromptTemplate(
# These are the examples we want to insert into the prompt.
examples=examples,
# This is how we want to format the examples when we insert them into the prompt.
example_prompt=example_prompt,
# The prefix is some text that goes before the examples in the prompt.
# Usually, this consists of intructions.
prefix="Give the antonym of every input",
# The suffix is some text that goes after the examples in the prompt.
# Usually, this is where the user input will go
suffix="Word: {input}\nAntonym:",
# The input variables are the variables that the overall prompt expects.
input_variables=["input"],
# The example_separator is the string we will use to join the prefix, examples, and suffix together with.
example_separator="\n\n",
)
# We can now generate a prompt using the `format` method.
print(few_shot_prompt.format(input="big"))
# -> Give the antonym of every input
# ->
# -> Word: happy
# -> Antonym: sad
# ->
# -> Word: tall
# -> Antonym: short
# ->
# -> Word: big
# -> Antonym:
```
## Select examples for a prompt template
If you have a large number of examples, you can use the `ExampleSelector` to select a subset of examples that will be most informative for the Language Model. This will help you generate a prompt that is more likely to generate a good response.
Below, we'll use the `LengthBasedExampleSelector`, which selects examples based on the length of the input. 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.
We'll continue with the example from the previous section, but this time we'll use the `LengthBasedExampleSelector` to select the examples.
```python
from langchain.prompts.example_selector import LengthBasedExampleSelector
# These are a lot of examples of a pretend task of creating antonyms.
examples = [
{"input": "happy", "output": "sad"},
{"input": "tall", "output": "short"},
{"input": "energetic", "output": "lethargic"},
{"input": "sunny", "output": "gloomy"},
{"input": "windy", "output": "calm"},
]
# We'll use the `LengthBasedExampleSelector` to select the examples.
example_selector = LengthBasedExampleSelector(
# These are the examples is has available to choose from.
examples=examples,
# This is the PromptTemplate being used to format the examples.
example_prompt=example_prompt,
# This is the maximum length that the formatted examples should be.
# Length is measured by the get_text_length function below.
max_length=25,
)
# We can now use the `example_selector` to create a `FewShotPromptTemplate`.
few_shot_prompt = FewShotPromptTemplate(
# We provide an ExampleSelector instead of examples.
example_selector=example_selector,
example_prompt=example_prompt,
prefix="Give the antonym of every input",
suffix="Word: {input}\nAntonym:",
input_variables=["input"],
example_separator="\n\n",
)
# We can now generate a prompt using the `format` method.
print(few_shot_prompt.format(input="big"))
# -> Give the antonym of every input
# ->
# -> Word: happy
# -> Antonym: sad
# ->
# -> Word: tall
# -> Antonym: short
# ->
# -> Word: energetic
# -> Antonym: lethargic
# ->
# -> Word: sunny
# -> Antonym: gloomy
# ->
# -> Word: windy
# -> Antonym: calm
# ->
# -> Word: big
# -> Antonym:
```
In contrast, if we provide a very long input, the `LengthBasedExampleSelector` will select fewer examples to include in the prompt.
```python
long_string = "big and huge and massive and large and gigantic and tall and much much much much much bigger than everything else"
print(dynamic_prompt.format(adjective=long_string))
# -> Give the antonym of every input
# -> Word: happy
# -> Antonym: sad
# ->
# -> Word: big and huge and massive and large and gigantic and tall and much much much much much bigger than everything else
# -> Antonym:
```
<!-- TODO(shreya): Add correct link here. -->
LangChain comes with a few example selectors that you can use. For more details on how to use them, see [Example Selectors](examples/example_selectors.ipynb).
You can create custom example selectors that select examples based on any criteria you want. For more details on how to do this, see [Creating a custom example selector](examples/custom_example_selector.ipynb).

View File

@@ -0,0 +1,34 @@
How-To Guides
=============
If you're new to the library, you may want to start with the `Quickstart <getting_started.html>`_.
The user guide here shows more advanced workflows and how to use the library in different ways.
`Custom Prompt Template <examples/custom_prompt_template.html>`_: How to create and use a custom PromptTemplate, the logic that decides how input variables get formatted into a prompt.
`Custom Example Selector <examples/custom_example_selector.html>`_: How to create and use a custom ExampleSelector (the class responsible for choosing which examples to use in a prompt).
`Few Shot Prompt Templates <examples/few_shot_examples.html>`_: How to include examples in the prompt.
`Prompt Serialization <examples/prompt_serialization.html>`_: A walkthrough of how to serialize prompts to and from disk.
`Few Shot Prompt Examples <examples/few_shot_examples.html>`_: Examples of Few Shot Prompt Templates.
.. toctree::
:maxdepth: 1
:glob:
:hidden:
examples/custom_prompt_template.md
examples/custom_example_selector.md
examples/few_shot_examples.ipynb
examples/prompt_serialization.ipynb
examples/few_shot_examples_data.ipynb

View File

@@ -0,0 +1,76 @@
# Key Concepts
## Prompts
A prompt is the input to a language model. It is a string of text that is used to generate a response from the language model.
## Prompt Templates
`PromptTemplates` are a way to create prompts in a reproducible way. They contain a template string, and a set of input variables. The template string can be formatted with the input variables to generate a prompt. The template string often contains instructions to the language model, a few shot examples, and a question to the language model.
`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.
To learn more about `PromptTemplates`, see [Prompt Templates](getting_started.md).
As an example, consider the following template string:
```python
"""
Predict the capital of a country.
Country: {country}
Capital:
"""
```
### Input Variables
Input variables are the variables that are used to fill in the template string. In the example above, the input variable is `country`.
Given an input variable, the `PromptTemplate` can generate a prompt by filling in the template string with the input variable. For example, if the input variable is `United States`, the template string can be formatted to generate the following prompt:
```python
"""
Predict the capital of a country.
Country: United States
Capital:
"""
```
## Few Shot Examples
Few shot examples refer to in-context examples that are provided to a language model as part of a prompt. The examples can be used to help the language model understand the context of the prompt, and as a result generate a better response. Few shot examples can contain both positive and negative examples about the expected response.
Below, we list out some few shot examples that may be relevant for the task of predicting the capital of a country.
```
Country: United States
Capital: Washington, D.C.
Country: Canada
Capital: Ottawa
```
To learn more about how to provide few shot examples, see [Few Shot Examples](examples/few_shot_examples.ipynb).
<!-- TODO(shreya): Add correct link here. -->
## Example selection
If there are multiple examples that are relevant to a prompt, it is important to select the most relevant examples. Generally, the quality of the response from the LLM can be significantly improved by selecting the most relevant examples. This is because the language model will be able to better understand the context of the prompt, and also potentially learn failure modes to avoid.
To help the user with selecting the most relevant examples, we provide example selectors that select the most relevant based on different criteria, such as length, semantic similarity, etc. The example selector takes in a list of examples and returns a list of selected examples, formatted as a string. The user can also provide their own example selector. To learn more about example selectors, see [Example Selection](example_selection.md).
<!-- TODO(shreya): Add correct link here. -->
## Serialization
To make it easy to share `PromptTemplates`, we provide a `serialize` method that returns a JSON string. The JSON string can be saved to a file, and then loaded back into a `PromptTemplate` using the `deserialize` method. This allows users to share `PromptTemplates` with others, and also to save them for later use.
To learn more about serialization, see [Serialization](examples/prompt_serialization.ipynb).
<!-- TODO(shreya): Provide correct link. -->