"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."
"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",
"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",
"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",
" \"\"\"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"
"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"
"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 :)"