mirror of https://github.com/hwchase17/langchain
add few shot example (#148)
parent
8869b0ab0e
commit
c02eb199b6
@ -0,0 +1,4 @@
|
||||
{
|
||||
"input_variables": ["input", "output"],
|
||||
"template": "Input: {input}\nOutput: {output}"
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
[
|
||||
{"input": "happy", "output": "sad"},
|
||||
{"input": "tall", "output": "short"}
|
||||
]
|
@ -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:"
|
||||
}
|
@ -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:"
|
||||
}
|
@ -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:"
|
||||
}
|
@ -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
|
||||
}
|
@ -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
|
||||
}
|
@ -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:
|
||||
Tell me a {adjective} joke about {content}.
|
@ -0,0 +1,4 @@
|
||||
{
|
||||
"input_variables": ["adjective", "content"],
|
||||
"template_path": "simple_template.txt"
|
||||
}
|
@ -0,0 +1 @@
|
||||
Tell me a {adjective} joke about {content}.
|
@ -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"`
|
@ -0,0 +1,5 @@
|
||||
:mod:`langchain.prompts.example_selector`
|
||||
=========================================
|
||||
|
||||
.. automodule:: langchain.prompts.example_selector
|
||||
:members:
|
@ -1,6 +1,12 @@
|
||||
"""Prompt template classes."""
|
||||
from langchain.prompts.base import BasePrompt
|
||||
from langchain.prompts.dynamic import DynamicPrompt
|
||||
from langchain.prompts.prompt import Prompt
|
||||
from langchain.prompts.base import BasePromptTemplate
|
||||
from langchain.prompts.few_shot import FewShotPromptTemplate
|
||||
from langchain.prompts.loading import load_prompt
|
||||
from langchain.prompts.prompt import PromptTemplate
|
||||
|
||||
__all__ = ["BasePrompt", "Prompt", "DynamicPrompt"]
|
||||
__all__ = [
|
||||
"BasePromptTemplate",
|
||||
"load_prompt",
|
||||
"PromptTemplate",
|
||||
"FewShotPromptTemplate",
|
||||
]
|
||||
|
@ -1,112 +0,0 @@
|
||||
"""Dynamic prompt schema definition."""
|
||||
import re
|
||||
from typing import Any, Callable, Dict, List
|
||||
|
||||
from pydantic import BaseModel, Extra, root_validator
|
||||
|
||||
from langchain.prompts.base import DEFAULT_FORMATTER_MAPPING, BasePrompt
|
||||
|
||||
|
||||
class DynamicPrompt(BaseModel, BasePrompt):
|
||||
r"""Schema to represent a dynamic prompt for an LLM.
|
||||
|
||||
Example:
|
||||
.. code-block:: python
|
||||
|
||||
from langchain import DynamicPrompt
|
||||
dynamic_prompt = DynamicPrompt(
|
||||
examples=["Say hi. Hi", "Say ho. Ho"],
|
||||
example_separator="\n\n",
|
||||
prefix="",
|
||||
suffix="\n\nSay {foo}"
|
||||
input_variables=["foo"],
|
||||
max_length=200,
|
||||
get_text_length=word_count
|
||||
)
|
||||
"""
|
||||
|
||||
examples: List[str]
|
||||
"""A list of the examples that the prompt template expects."""
|
||||
|
||||
example_separator: str = "\n\n"
|
||||
"""Example separator, e.g. \n\n, for the dynamic prompt creation."""
|
||||
|
||||
input_variables: List[str] = []
|
||||
"""A list of the names of the variables the prompt template expects."""
|
||||
|
||||
prefix: str = ""
|
||||
"""Prefix for the prompt."""
|
||||
|
||||
suffix: str = ""
|
||||
"""Suffix for the prompt."""
|
||||
|
||||
template_format: str = "f-string"
|
||||
"""The format of the prompt template. Options are: 'f-string'."""
|
||||
|
||||
get_text_length: Callable[[str], int] = lambda x: len(re.split("\n| ", x))
|
||||
"""Function to measure prompt length. Defaults to word count."""
|
||||
|
||||
max_length: int = 2048
|
||||
"""Max length for the prompt, beyond which examples are cut."""
|
||||
|
||||
class Config:
|
||||
"""Configuration for this pydantic object."""
|
||||
|
||||
extra = Extra.forbid
|
||||
|
||||
def template(self, example_list: List[str], **kwargs: Any) -> str:
|
||||
"""Return template given example list."""
|
||||
template = self.example_separator.join(
|
||||
[self.prefix, *example_list, self.suffix]
|
||||
)
|
||||
return DEFAULT_FORMATTER_MAPPING[self.template_format](template, **kwargs)
|
||||
|
||||
def format(self, **kwargs: Any) -> str:
|
||||
"""Dynamically format the prompt with the inputs.
|
||||
|
||||
Args:
|
||||
kwargs: Any arguments to be passed to the prompt template.
|
||||
|
||||
Returns:
|
||||
A formatted string.
|
||||
|
||||
Example:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
prompt.format(variable1="foo")
|
||||
"""
|
||||
curr_examples = self.examples
|
||||
template = self.template(curr_examples, **kwargs)
|
||||
while self.get_text_length(template) > self.max_length and curr_examples:
|
||||
curr_examples = curr_examples[:-1]
|
||||
template = self.template(curr_examples, **kwargs)
|
||||
return template
|
||||
|
||||
@root_validator()
|
||||
def template_is_valid(cls, values: Dict) -> Dict:
|
||||
"""Check that prefix, suffix and input variables are consistent."""
|
||||
input_variables = values["input_variables"]
|
||||
prefix = values["prefix"]
|
||||
suffix = values["suffix"]
|
||||
template_format = values["template_format"]
|
||||
if template_format not in DEFAULT_FORMATTER_MAPPING:
|
||||
valid_formats = list(DEFAULT_FORMATTER_MAPPING)
|
||||
raise ValueError(
|
||||
f"Invalid template format. Got `{template_format}`;"
|
||||
f" should be one of {valid_formats}"
|
||||
)
|
||||
try:
|
||||
result = values["get_text_length"]("foo")
|
||||
assert isinstance(result, int)
|
||||
except AssertionError:
|
||||
raise ValueError(
|
||||
"Invalid text length callable, must take string & return int;"
|
||||
)
|
||||
dummy_inputs = {input_variable: "foo" for input_variable in input_variables}
|
||||
try:
|
||||
formatter_func = DEFAULT_FORMATTER_MAPPING[template_format]
|
||||
formatter_func(prefix + suffix, **dummy_inputs)
|
||||
except KeyError:
|
||||
raise ValueError("Invalid prompt schema.")
|
||||
return values
|
@ -0,0 +1,7 @@
|
||||
"""Logic for selecting examples to include in prompts."""
|
||||
from langchain.prompts.example_selector.length_based import LengthBasedExampleSelector
|
||||
from langchain.prompts.example_selector.semantic_similarity import (
|
||||
SemanticSimilarityExampleSelector,
|
||||
)
|
||||
|
||||
__all__ = ["LengthBasedExampleSelector", "SemanticSimilarityExampleSelector"]
|
@ -0,0 +1,11 @@
|
||||
"""Interface for selecting examples to include in prompts."""
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Dict, List
|
||||
|
||||
|
||||
class BaseExampleSelector(ABC):
|
||||
"""Interface for selecting examples to include in prompts."""
|
||||
|
||||
@abstractmethod
|
||||
def select_examples(self, input_variables: Dict[str, str]) -> List[dict]:
|
||||
"""Select which examples to use based on the inputs."""
|
@ -0,0 +1,54 @@
|
||||
"""Select examples based on length."""
|
||||
import re
|
||||
from typing import Callable, Dict, List
|
||||
|
||||
from pydantic import BaseModel, validator
|
||||
|
||||
from langchain.prompts.example_selector.base import BaseExampleSelector
|
||||
from langchain.prompts.prompt import PromptTemplate
|
||||
|
||||
|
||||
class LengthBasedExampleSelector(BaseExampleSelector, BaseModel):
|
||||
"""Select examples based on length."""
|
||||
|
||||
examples: List[dict]
|
||||
"""A list of the examples that the prompt template expects."""
|
||||
|
||||
example_prompt: PromptTemplate
|
||||
"""Prompt template used to format the examples."""
|
||||
|
||||
get_text_length: Callable[[str], int] = lambda x: len(re.split("\n| ", x))
|
||||
"""Function to measure prompt length. Defaults to word count."""
|
||||
|
||||
max_length: int = 2048
|
||||
"""Max length for the prompt, beyond which examples are cut."""
|
||||
|
||||
example_text_lengths: List[int] = [] #: :meta private:
|
||||
|
||||
@validator("example_text_lengths", always=True)
|
||||
def calculate_example_text_lengths(cls, v: List[int], values: Dict) -> List[int]:
|
||||
"""Calculate text lengths if they don't exist."""
|
||||
# Check if text lengths were passed in
|
||||
if v:
|
||||
return v
|
||||
# If they were not, calculate them
|
||||
example_prompt = values["example_prompt"]
|
||||
get_text_length = values["get_text_length"]
|
||||
string_examples = [example_prompt.format(**eg) for eg in values["examples"]]
|
||||
return [get_text_length(eg) for eg in string_examples]
|
||||
|
||||
def select_examples(self, input_variables: Dict[str, str]) -> List[dict]:
|
||||
"""Select which examples to use based on the input lengths."""
|
||||
inputs = " ".join(input_variables.values())
|
||||
remaining_length = self.max_length - self.get_text_length(inputs)
|
||||
i = 0
|
||||
examples = []
|
||||
while remaining_length > 0 and i < len(self.examples):
|
||||
new_length = remaining_length - self.example_text_lengths[i]
|
||||
if i < 0:
|
||||
break
|
||||
else:
|
||||
examples.append(self.examples[i])
|
||||
remaining_length = new_length
|
||||
i += 1
|
||||
return examples
|
@ -0,0 +1,75 @@
|
||||
"""Example selector that selects examples based on SemanticSimilarity."""
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from pydantic import BaseModel, Extra
|
||||
|
||||
from langchain.embeddings.base import Embeddings
|
||||
from langchain.prompts.example_selector.base import BaseExampleSelector
|
||||
from langchain.vectorstores.base import VectorStore
|
||||
|
||||
|
||||
class SemanticSimilarityExampleSelector(BaseExampleSelector, BaseModel):
|
||||
"""Example selector that selects examples based on SemanticSimilarity."""
|
||||
|
||||
vectorstore: VectorStore
|
||||
"""VectorStore than contains information about examples."""
|
||||
k: int = 4
|
||||
"""Number of examples to select."""
|
||||
example_keys: Optional[List[str]] = None
|
||||
"""Optional keys to filter examples to."""
|
||||
|
||||
class Config:
|
||||
"""Configuration for this pydantic object."""
|
||||
|
||||
extra = Extra.forbid
|
||||
arbitrary_types_allowed = True
|
||||
|
||||
def select_examples(self, input_variables: Dict[str, str]) -> List[dict]:
|
||||
"""Select which examples to use based on semantic similarity."""
|
||||
# Get the docs with the highest similarity.
|
||||
query = " ".join(input_variables.values())
|
||||
example_docs = self.vectorstore.similarity_search(query, k=self.k)
|
||||
# Get the examples from the metadata.
|
||||
# This assumes that examples are stored in metadata.
|
||||
examples = [dict(e.metadata) for e in example_docs]
|
||||
# If example keys are provided, filter examples to those keys.
|
||||
if self.example_keys:
|
||||
examples = [{k: eg[k] for k in self.example_keys} for eg in examples]
|
||||
return examples
|
||||
|
||||
@classmethod
|
||||
def from_examples(
|
||||
cls,
|
||||
examples: List[dict],
|
||||
embeddings: Embeddings,
|
||||
vectorstore_cls: VectorStore,
|
||||
k: int = 4,
|
||||
**vectorstore_cls_kwargs: Any,
|
||||
) -> "SemanticSimilarityExampleSelector":
|
||||
"""Create k-shot example selector using example list and embeddings.
|
||||
|
||||
Reshuffles examples dynamically based on query similarity.
|
||||
|
||||
Args:
|
||||
examples: List of examples to use in the prompt.
|
||||
suffix: String to go after the list of examples. Should generally
|
||||
set up the user's input.
|
||||
input_variables: A list of variable names the final prompt template
|
||||
will expect.
|
||||
embeddings: An iniialized embedding API interface, e.g. OpenAIEmbeddings().
|
||||
vectorstore_cls: A vector store DB interface class, e.g. FAISS.
|
||||
example_separator: The seperator to use in between examples. Defaults
|
||||
to two new line characters.
|
||||
prefix: String that should go before any examples. Generally includes
|
||||
examples. Default to an empty string.
|
||||
k: Number of examples to select
|
||||
vectorstore_cls_kwargs: optional kwargs containing url for vector store
|
||||
|
||||
Returns:
|
||||
The ExampleSelector instantiated, backed by a vector store.
|
||||
"""
|
||||
string_examples = [" ".join(eg.values()) for eg in examples]
|
||||
vectorstore = vectorstore_cls.from_texts(
|
||||
string_examples, embeddings, metadatas=examples, **vectorstore_cls_kwargs
|
||||
)
|
||||
return cls(vectorstore=vectorstore, k=k)
|
@ -0,0 +1,110 @@
|
||||
"""Prompt template that contains few shot examples."""
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from pydantic import BaseModel, Extra, root_validator
|
||||
|
||||
from langchain.prompts.base import (
|
||||
DEFAULT_FORMATTER_MAPPING,
|
||||
BasePromptTemplate,
|
||||
check_valid_template,
|
||||
)
|
||||
from langchain.prompts.example_selector.base import BaseExampleSelector
|
||||
from langchain.prompts.prompt import PromptTemplate
|
||||
|
||||
|
||||
class FewShotPromptTemplate(BasePromptTemplate, BaseModel):
|
||||
"""Prompt template that contains few shot examples."""
|
||||
|
||||
examples: Optional[List[dict]] = None
|
||||
"""Examples to format into the prompt.
|
||||
Either this or example_selector should be provided."""
|
||||
|
||||
example_selector: Optional[BaseExampleSelector] = None
|
||||
"""ExampleSelector to choose the examples to format into the prompt.
|
||||
Either this or examples should be provided."""
|
||||
|
||||
example_prompt: PromptTemplate
|
||||
"""PromptTemplate used to format an individual example."""
|
||||
|
||||
suffix: str
|
||||
"""A prompt template string to put after the examples."""
|
||||
|
||||
input_variables: List[str]
|
||||
"""A list of the names of the variables the prompt template expects."""
|
||||
|
||||
example_separator: str = "\n\n"
|
||||
"""String separator used to join the prefix, the examples, and suffix."""
|
||||
|
||||
prefix: str = ""
|
||||
"""A prompt template string to put before the examples."""
|
||||
|
||||
template_format: str = "f-string"
|
||||
"""The format of the prompt template. Options are: 'f-string'."""
|
||||
|
||||
@root_validator(pre=True)
|
||||
def check_examples_and_selector(cls, values: Dict) -> Dict:
|
||||
"""Check that one and only one of examples/example_selector are provided."""
|
||||
examples = values.get("examples", None)
|
||||
example_selector = values.get("example_selector", None)
|
||||
if examples and example_selector:
|
||||
raise ValueError(
|
||||
"Only one of 'examples' and 'example_selector' should be provided"
|
||||
)
|
||||
|
||||
if examples is None and example_selector is None:
|
||||
raise ValueError(
|
||||
"One of 'examples' and 'example_selector' should be provided"
|
||||
)
|
||||
|
||||
return values
|
||||
|
||||
@root_validator()
|
||||
def template_is_valid(cls, values: Dict) -> Dict:
|
||||
"""Check that prefix, suffix and input variables are consistent."""
|
||||
check_valid_template(
|
||||
values["prefix"] + values["suffix"],
|
||||
values["template_format"],
|
||||
values["input_variables"],
|
||||
)
|
||||
return values
|
||||
|
||||
class Config:
|
||||
"""Configuration for this pydantic object."""
|
||||
|
||||
extra = Extra.forbid
|
||||
arbitrary_types_allowed = True
|
||||
|
||||
def _get_examples(self, **kwargs: Any) -> List[dict]:
|
||||
if self.examples is not None:
|
||||
return self.examples
|
||||
elif self.example_selector is not None:
|
||||
return self.example_selector.select_examples(kwargs)
|
||||
else:
|
||||
raise ValueError
|
||||
|
||||
def format(self, **kwargs: Any) -> str:
|
||||
"""Format the prompt with the inputs.
|
||||
|
||||
Args:
|
||||
kwargs: Any arguments to be passed to the prompt template.
|
||||
|
||||
Returns:
|
||||
A formatted string.
|
||||
|
||||
Example:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
prompt.format(variable1="foo")
|
||||
"""
|
||||
# Get the examples to use.
|
||||
examples = self._get_examples(**kwargs)
|
||||
# Format the examples.
|
||||
example_strings = [
|
||||
self.example_prompt.format(**example) for example in examples
|
||||
]
|
||||
# Create the overall template.
|
||||
pieces = [self.prefix, *example_strings, self.suffix]
|
||||
template = self.example_separator.join([piece for piece in pieces if piece])
|
||||
# Format the template with the input variables.
|
||||
return DEFAULT_FORMATTER_MAPPING[self.template_format](template, **kwargs)
|
@ -0,0 +1,103 @@
|
||||
"""Load prompts from disk."""
|
||||
import json
|
||||
from pathlib import Path
|
||||
from typing import Union
|
||||
|
||||
import yaml
|
||||
|
||||
from langchain.prompts.base import BasePromptTemplate
|
||||
from langchain.prompts.few_shot import FewShotPromptTemplate
|
||||
from langchain.prompts.prompt import PromptTemplate
|
||||
|
||||
|
||||
def load_prompt_from_config(config: dict) -> BasePromptTemplate:
|
||||
"""Get the right type from the config and load it accordingly."""
|
||||
prompt_type = config.pop("_type", "prompt")
|
||||
if prompt_type == "prompt":
|
||||
return _load_prompt(config)
|
||||
elif prompt_type == "few_shot":
|
||||
return _load_few_shot_prompt(config)
|
||||
else:
|
||||
raise ValueError
|
||||
|
||||
|
||||
def _load_template(var_name: str, config: dict) -> dict:
|
||||
"""Load template from disk if applicable."""
|
||||
# Check if template_path exists in config.
|
||||
if f"{var_name}_path" in config:
|
||||
# If it does, make sure template variable doesn't also exist.
|
||||
if var_name in config:
|
||||
raise ValueError(
|
||||
f"Both `{var_name}_path` and `{var_name}` cannot be provided."
|
||||
)
|
||||
# Pop the template path from the config.
|
||||
template_path = Path(config.pop(f"{var_name}_path"))
|
||||
# Load the template.
|
||||
if template_path.suffix == ".txt":
|
||||
with open(template_path) as f:
|
||||
template = f.read()
|
||||
else:
|
||||
raise ValueError
|
||||
# Set the template variable to the extracted variable.
|
||||
config[var_name] = template
|
||||
return config
|
||||
|
||||
|
||||
def _load_examples(config: dict) -> dict:
|
||||
"""Load examples if necessary."""
|
||||
if isinstance(config["examples"], list):
|
||||
pass
|
||||
elif isinstance(config["examples"], str):
|
||||
with open(config["examples"]) as f:
|
||||
examples = json.load(f)
|
||||
config["examples"] = examples
|
||||
else:
|
||||
raise ValueError
|
||||
return config
|
||||
|
||||
|
||||
def _load_few_shot_prompt(config: dict) -> FewShotPromptTemplate:
|
||||
"""Load the few shot prompt from the config."""
|
||||
# Load the suffix and prefix templates.
|
||||
config = _load_template("suffix", config)
|
||||
config = _load_template("prefix", config)
|
||||
# Load the example prompt.
|
||||
if "example_prompt_path" in config:
|
||||
if "example_prompt" in config:
|
||||
raise ValueError(
|
||||
"Only one of example_prompt and example_prompt_path should "
|
||||
"be specified."
|
||||
)
|
||||
config["example_prompt"] = load_prompt(config.pop("example_prompt_path"))
|
||||
else:
|
||||
config["example_prompt"] = _load_prompt(config["example_prompt"])
|
||||
# Load the examples.
|
||||
config = _load_examples(config)
|
||||
return FewShotPromptTemplate(**config)
|
||||
|
||||
|
||||
def _load_prompt(config: dict) -> PromptTemplate:
|
||||
"""Load the prompt template from config."""
|
||||
# Load the template from disk if necessary.
|
||||
config = _load_template("template", config)
|
||||
return PromptTemplate(**config)
|
||||
|
||||
|
||||
def load_prompt(file: Union[str, Path]) -> BasePromptTemplate:
|
||||
"""Load prompt from file."""
|
||||
# Convert file to Path object.
|
||||
if isinstance(file, str):
|
||||
file_path = Path(file)
|
||||
else:
|
||||
file_path = file
|
||||
# Load from either json or yaml.
|
||||
if file_path.suffix == ".json":
|
||||
with open(file_path) as f:
|
||||
config = json.load(f)
|
||||
elif file_path.suffix == ".yaml":
|
||||
with open(file_path, "r") as f:
|
||||
config = yaml.safe_load(f)
|
||||
else:
|
||||
raise ValueError
|
||||
# Load the prompt from the config now.
|
||||
return load_prompt_from_config(config)
|
@ -1,166 +0,0 @@
|
||||
"""Optimized prompt schema definition."""
|
||||
import re
|
||||
from typing import Any, Callable, Dict, List
|
||||
|
||||
from pydantic import BaseModel, Extra, root_validator
|
||||
|
||||
from langchain.embeddings.base import Embeddings
|
||||
from langchain.prompts.base import DEFAULT_FORMATTER_MAPPING
|
||||
from langchain.vectorstores.base import VectorStore
|
||||
|
||||
|
||||
class OptimizedPrompt(BaseModel):
|
||||
r"""Schema to represent an optimized prompt for an LLM.
|
||||
|
||||
Example:
|
||||
.. code-block:: python
|
||||
|
||||
from langchain import DynamicPrompt
|
||||
vectorstore = FAISS.from_texts(examples, OpenAIEmbeddings()
|
||||
optimized_prompt = OptimizedPrompt(
|
||||
example_separator="\n\n",
|
||||
prefix="",
|
||||
suffix="\n\nSay {foo}"
|
||||
input_variables=["foo"],
|
||||
max_length=200,
|
||||
get_text_length=word_count,
|
||||
vectorstore=vectorstore)
|
||||
)
|
||||
"""
|
||||
|
||||
example_separator: str = "\n\n"
|
||||
"""Example separator, e.g. \n\n, for the dynamic prompt creation."""
|
||||
|
||||
input_variables: List[str] = []
|
||||
"""A list of the names of the variables the prompt template expects."""
|
||||
|
||||
prefix: str = ""
|
||||
"""Prefix for the prompt."""
|
||||
|
||||
suffix: str = ""
|
||||
"""Suffix for the prompt."""
|
||||
|
||||
template_format: str = "f-string"
|
||||
"""The format of the prompt template. Options are: 'f-string'."""
|
||||
|
||||
get_text_length: Callable[[str], int] = lambda x: len(re.split("\n| ", x))
|
||||
"""Function to measure prompt length. Defaults to word count."""
|
||||
|
||||
max_length: int = 2048
|
||||
"""Max length for the prompt, beyond which examples are cut."""
|
||||
|
||||
vectorstore: VectorStore
|
||||
"""Vectorstore to use for storing the embeddings."""
|
||||
|
||||
class Config:
|
||||
"""Configuration for this pydantic object."""
|
||||
|
||||
arbitrary_types_allowed = True
|
||||
|
||||
extra = Extra.forbid
|
||||
|
||||
def template(self, example_list: List[str], **kwargs: Any) -> str:
|
||||
"""Return template given full example list."""
|
||||
template = self.example_separator.join(
|
||||
[self.prefix, *example_list, self.suffix]
|
||||
)
|
||||
return DEFAULT_FORMATTER_MAPPING[self.template_format](template, **kwargs)
|
||||
|
||||
def format(self, k: int = 4, **kwargs: Any) -> str:
|
||||
"""Optimize the examples in the prompt for the given inputs.
|
||||
|
||||
Args:
|
||||
k: Number of examples to aim for (may be trimmed by optimizer afterwards)
|
||||
kwargs: Any arguments to be passed to the prompt template.
|
||||
|
||||
Returns:
|
||||
A formatted string.
|
||||
|
||||
Example:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
prompt.format(variable1="foo")
|
||||
"""
|
||||
query = " ".join([v for k, v in kwargs.items()])
|
||||
example_docs = self.vectorstore.similarity_search(query, k=k)
|
||||
curr_examples = [str(e.page_content) for e in example_docs]
|
||||
template = self.template(curr_examples, **kwargs)
|
||||
while self.get_text_length(template) > self.max_length and curr_examples:
|
||||
curr_examples = curr_examples[:-1]
|
||||
template = self.template(curr_examples, **kwargs)
|
||||
return template
|
||||
|
||||
@root_validator()
|
||||
def template_is_valid(cls, values: Dict) -> Dict:
|
||||
"""Check that prefix, suffix and input variables are consistent."""
|
||||
input_variables = values["input_variables"]
|
||||
if len(input_variables) > 1:
|
||||
raise ValueError("Only one input variable allowed for optimized prompt;")
|
||||
prefix = values["prefix"]
|
||||
suffix = values["suffix"]
|
||||
template_format = values["template_format"]
|
||||
if template_format not in DEFAULT_FORMATTER_MAPPING:
|
||||
valid_formats = list(DEFAULT_FORMATTER_MAPPING)
|
||||
raise ValueError(
|
||||
f"Invalid template format. Got `{template_format}`;"
|
||||
f" should be one of {valid_formats}"
|
||||
)
|
||||
try:
|
||||
result = values["get_text_length"]("foo")
|
||||
assert isinstance(result, int)
|
||||
except AssertionError:
|
||||
raise ValueError(
|
||||
"Invalid text length callable, must take string & return int;"
|
||||
)
|
||||
dummy_inputs = {input_variable: "foo" for input_variable in input_variables}
|
||||
try:
|
||||
formatter_func = DEFAULT_FORMATTER_MAPPING[template_format]
|
||||
formatter_func(prefix + suffix, **dummy_inputs)
|
||||
except KeyError:
|
||||
raise ValueError("Invalid prompt schema.")
|
||||
return values
|
||||
|
||||
@classmethod
|
||||
def from_examples(
|
||||
cls,
|
||||
examples: List[str],
|
||||
suffix: str,
|
||||
input_variables: List[str],
|
||||
embeddings: Embeddings,
|
||||
vectorstore_cls: VectorStore,
|
||||
example_separator: str = "\n\n",
|
||||
prefix: str = "",
|
||||
**vectorstore_cls_kwargs: Any,
|
||||
) -> "OptimizedPrompt":
|
||||
"""Create k-shot prompt optimizer using example list and embeddings.
|
||||
|
||||
Reshuffles examples for the prompt dynamically based on query similarity.
|
||||
|
||||
Args:
|
||||
examples: List of examples to use in the prompt.
|
||||
suffix: String to go after the list of examples. Should generally
|
||||
set up the user's input.
|
||||
input_variables: A list of variable names the final prompt template
|
||||
will expect.
|
||||
embeddings: An iniialized embedding API interface, e.g. OpenAIEmbeddings().
|
||||
vectorstore_cls: A vector store DB interface class, e.g. FAISS.
|
||||
example_separator: The seperator to use in between examples. Defaults
|
||||
to two new line characters.
|
||||
prefix: String that should go before any examples. Generally includes
|
||||
examples. Default to an empty string.
|
||||
vectorstore_cls_kwargs: optional kwargs containing url for vector store
|
||||
|
||||
Returns:
|
||||
The OptimizedPrompt instantiated, backed by a vector store.
|
||||
"""
|
||||
vectorstore = vectorstore_cls.from_texts(
|
||||
examples, embeddings, **vectorstore_cls_kwargs
|
||||
)
|
||||
return cls(
|
||||
suffix=suffix,
|
||||
input_variables=input_variables,
|
||||
example_separator=example_separator,
|
||||
prefix=prefix,
|
||||
vectorstore=vectorstore,
|
||||
)
|
@ -0,0 +1 @@
|
||||
"""Test vectorstores."""
|
@ -0,0 +1,42 @@
|
||||
"""Test ElasticSearch functionality."""
|
||||
from typing import List
|
||||
|
||||
from langchain.docstore.document import Document
|
||||
from langchain.embeddings.base import Embeddings
|
||||
from langchain.vectorstores.elastic_vector_search import ElasticVectorSearch
|
||||
|
||||
|
||||
class FakeEmbeddings(Embeddings):
|
||||
"""Fake embeddings functionality for testing."""
|
||||
|
||||
def embed_documents(self, texts: List[str]) -> List[List[float]]:
|
||||
"""Return simple embeddings."""
|
||||
return [[1.0] * 9 + [i] for i in range(len(texts))]
|
||||
|
||||
def embed_query(self, text: str) -> List[float]:
|
||||
"""Return simple embeddings."""
|
||||
return [1.0] * 9 + [0.0]
|
||||
|
||||
|
||||
def test_elasticsearch() -> None:
|
||||
"""Test end to end construction and search."""
|
||||
texts = ["foo", "bar", "baz"]
|
||||
docsearch = ElasticVectorSearch.from_texts(
|
||||
texts, FakeEmbeddings(), elasticsearch_url="http://localhost:9200"
|
||||
)
|
||||
output = docsearch.similarity_search("foo", k=1)
|
||||
assert output == [Document(page_content="foo")]
|
||||
|
||||
|
||||
def test_elasticsearch_with_metadatas() -> None:
|
||||
"""Test end to end construction and search."""
|
||||
texts = ["foo", "bar", "baz"]
|
||||
metadatas = [{"page": i} for i in range(len(texts))]
|
||||
docsearch = ElasticVectorSearch.from_texts(
|
||||
texts,
|
||||
FakeEmbeddings(),
|
||||
metadatas=metadatas,
|
||||
elasticsearch_url="http://localhost:9200",
|
||||
)
|
||||
output = docsearch.similarity_search("foo", k=1)
|
||||
assert output == [Document(page_content="foo", metadata={"page": 0})]
|
@ -0,0 +1 @@
|
||||
"""Test prompt functionality."""
|
@ -0,0 +1,87 @@
|
||||
"""Test few shot prompt template."""
|
||||
import pytest
|
||||
|
||||
from langchain.prompts.few_shot import FewShotPromptTemplate
|
||||
from langchain.prompts.prompt import PromptTemplate
|
||||
|
||||
EXAMPLE_PROMPT = PromptTemplate(
|
||||
input_variables=["question", "answer"], template="{question}: {answer}"
|
||||
)
|
||||
|
||||
|
||||
def test_suffix_only() -> None:
|
||||
"""Test prompt works with just a suffix."""
|
||||
suffix = "This is a {foo} test."
|
||||
input_variables = ["foo"]
|
||||
prompt = FewShotPromptTemplate(
|
||||
input_variables=input_variables,
|
||||
suffix=suffix,
|
||||
examples=[],
|
||||
example_prompt=EXAMPLE_PROMPT,
|
||||
)
|
||||
output = prompt.format(foo="bar")
|
||||
expected_output = "This is a bar test."
|
||||
assert output == expected_output
|
||||
|
||||
|
||||
def test_prompt_missing_input_variables() -> None:
|
||||
"""Test error is raised when input variables are not provided."""
|
||||
# Test when missing in suffix
|
||||
template = "This is a {foo} test."
|
||||
with pytest.raises(ValueError):
|
||||
FewShotPromptTemplate(
|
||||
input_variables=[],
|
||||
suffix=template,
|
||||
examples=[],
|
||||
example_prompt=EXAMPLE_PROMPT,
|
||||
)
|
||||
|
||||
# Test when missing in prefix
|
||||
template = "This is a {foo} test."
|
||||
with pytest.raises(ValueError):
|
||||
FewShotPromptTemplate(
|
||||
input_variables=[],
|
||||
suffix="foo",
|
||||
examples=[],
|
||||
prefix=template,
|
||||
example_prompt=EXAMPLE_PROMPT,
|
||||
)
|
||||
|
||||
|
||||
def test_prompt_extra_input_variables() -> None:
|
||||
"""Test error is raised when there are too many input variables."""
|
||||
template = "This is a {foo} test."
|
||||
input_variables = ["foo", "bar"]
|
||||
with pytest.raises(ValueError):
|
||||
FewShotPromptTemplate(
|
||||
input_variables=input_variables,
|
||||
suffix=template,
|
||||
examples=[],
|
||||
example_prompt=EXAMPLE_PROMPT,
|
||||
)
|
||||
|
||||
|
||||
def test_few_shot_functionality() -> None:
|
||||
"""Test that few shot works with examples."""
|
||||
prefix = "This is a test about {content}."
|
||||
suffix = "Now you try to talk about {new_content}."
|
||||
examples = [
|
||||
{"question": "foo", "answer": "bar"},
|
||||
{"question": "baz", "answer": "foo"},
|
||||
]
|
||||
prompt = FewShotPromptTemplate(
|
||||
suffix=suffix,
|
||||
prefix=prefix,
|
||||
input_variables=["content", "new_content"],
|
||||
examples=examples,
|
||||
example_prompt=EXAMPLE_PROMPT,
|
||||
example_separator="\n",
|
||||
)
|
||||
output = prompt.format(content="animals", new_content="party")
|
||||
expected_output = (
|
||||
"This is a test about animals.\n"
|
||||
"foo: bar\n"
|
||||
"baz: foo\n"
|
||||
"Now you try to talk about party."
|
||||
)
|
||||
assert output == expected_output
|
@ -0,0 +1,48 @@
|
||||
"""Test functionality related to dynamic prompts."""
|
||||
import pytest
|
||||
|
||||
from langchain.prompts.example_selector.length_based import LengthBasedExampleSelector
|
||||
from langchain.prompts.prompt import PromptTemplate
|
||||
|
||||
EXAMPLES = [
|
||||
{"question": "Question: who are you?\nAnswer: foo"},
|
||||
{"question": "Question: who are you?\nAnswer: foo"},
|
||||
]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def selector() -> LengthBasedExampleSelector:
|
||||
"""Get length based selector to use in tests."""
|
||||
prompts = PromptTemplate(input_variables=["question"], template="{question}")
|
||||
selector = LengthBasedExampleSelector(
|
||||
examples=EXAMPLES,
|
||||
example_prompt=prompts,
|
||||
max_length=25,
|
||||
)
|
||||
return selector
|
||||
|
||||
|
||||
def test_dynamic_prompt_valid(selector: LengthBasedExampleSelector) -> None:
|
||||
"""Test dynamic prompt can be successfully constructed from examples."""
|
||||
short_question = "Short question?"
|
||||
output = selector.select_examples({"question": short_question})
|
||||
assert output == EXAMPLES
|
||||
|
||||
|
||||
def test_dynamic_prompt_trims_one_example(selector: LengthBasedExampleSelector) -> None:
|
||||
"""Test dynamic prompt can trim one example."""
|
||||
long_question = """I am writing a really long question,
|
||||
this probably is going to affect the example right?"""
|
||||
output = selector.select_examples({"question": long_question})
|
||||
assert output == EXAMPLES[:1]
|
||||
|
||||
|
||||
def test_dynamic_prompt_trims_all_examples(
|
||||
selector: LengthBasedExampleSelector,
|
||||
) -> None:
|
||||
"""Test dynamic prompt can trim all examples."""
|
||||
longest_question = """This question is super super super,
|
||||
super super super super super super super super super super super,
|
||||
super super super super long, this will affect the example right?"""
|
||||
output = selector.select_examples({"question": longest_question})
|
||||
assert output == []
|
@ -0,0 +1,134 @@
|
||||
"""Test loading functionality."""
|
||||
|
||||
import os
|
||||
from contextlib import contextmanager
|
||||
from pathlib import Path
|
||||
from typing import Iterator
|
||||
|
||||
from langchain.prompts.few_shot import FewShotPromptTemplate
|
||||
from langchain.prompts.loading import load_prompt
|
||||
from langchain.prompts.prompt import PromptTemplate
|
||||
|
||||
|
||||
@contextmanager
|
||||
def change_directory() -> Iterator:
|
||||
"""Change the working directory to the right folder."""
|
||||
origin = Path().absolute()
|
||||
try:
|
||||
os.chdir("docs/examples/prompts")
|
||||
yield
|
||||
finally:
|
||||
os.chdir(origin)
|
||||
|
||||
|
||||
def test_loading_from_YAML() -> None:
|
||||
"""Test loading from yaml file."""
|
||||
with change_directory():
|
||||
prompt = load_prompt("simple_prompt.yaml")
|
||||
expected_prompt = PromptTemplate(
|
||||
input_variables=["adjective", "content"],
|
||||
template="Tell me a {adjective} joke about {content}.",
|
||||
)
|
||||
assert prompt == expected_prompt
|
||||
|
||||
|
||||
def test_loading_from_JSON() -> None:
|
||||
"""Test loading from json file."""
|
||||
with change_directory():
|
||||
prompt = load_prompt("simple_prompt.json")
|
||||
expected_prompt = PromptTemplate(
|
||||
input_variables=["adjective", "content"],
|
||||
template="Tell me a {adjective} joke about {content}.",
|
||||
)
|
||||
assert prompt == expected_prompt
|
||||
|
||||
|
||||
def test_loading_with_template_as_file() -> None:
|
||||
"""Test loading when the template is a file."""
|
||||
with change_directory():
|
||||
prompt = load_prompt("simple_prompt_with_template_file.json")
|
||||
expected_prompt = PromptTemplate(
|
||||
input_variables=["adjective", "content"],
|
||||
template="Tell me a {adjective} joke about {content}.",
|
||||
)
|
||||
assert prompt == expected_prompt
|
||||
|
||||
|
||||
def test_loading_few_shot_prompt_from_yaml() -> None:
|
||||
"""Test loading few shot prompt from yaml."""
|
||||
with change_directory():
|
||||
prompt = load_prompt("few_shot_prompt.yaml")
|
||||
expected_prompt = FewShotPromptTemplate(
|
||||
input_variables=["adjective"],
|
||||
prefix="Write antonyms for the following words.",
|
||||
example_prompt=PromptTemplate(
|
||||
input_variables=["input", "output"],
|
||||
template="Input: {input}\nOutput: {output}",
|
||||
),
|
||||
examples=[
|
||||
{"input": "happy", "output": "sad"},
|
||||
{"input": "tall", "output": "short"},
|
||||
],
|
||||
suffix="Input: {adjective}\nOutput:",
|
||||
)
|
||||
assert prompt == expected_prompt
|
||||
|
||||
|
||||
def test_loading_few_shot_prompt_from_json() -> None:
|
||||
"""Test loading few shot prompt from json."""
|
||||
with change_directory():
|
||||
prompt = load_prompt("few_shot_prompt.json")
|
||||
expected_prompt = FewShotPromptTemplate(
|
||||
input_variables=["adjective"],
|
||||
prefix="Write antonyms for the following words.",
|
||||
example_prompt=PromptTemplate(
|
||||
input_variables=["input", "output"],
|
||||
template="Input: {input}\nOutput: {output}",
|
||||
),
|
||||
examples=[
|
||||
{"input": "happy", "output": "sad"},
|
||||
{"input": "tall", "output": "short"},
|
||||
],
|
||||
suffix="Input: {adjective}\nOutput:",
|
||||
)
|
||||
assert prompt == expected_prompt
|
||||
|
||||
|
||||
def test_loading_few_shot_prompt_when_examples_in_config() -> None:
|
||||
"""Test loading few shot prompt when the examples are in the config."""
|
||||
with change_directory():
|
||||
prompt = load_prompt("few_shot_prompt_examples_in.json")
|
||||
expected_prompt = FewShotPromptTemplate(
|
||||
input_variables=["adjective"],
|
||||
prefix="Write antonyms for the following words.",
|
||||
example_prompt=PromptTemplate(
|
||||
input_variables=["input", "output"],
|
||||
template="Input: {input}\nOutput: {output}",
|
||||
),
|
||||
examples=[
|
||||
{"input": "happy", "output": "sad"},
|
||||
{"input": "tall", "output": "short"},
|
||||
],
|
||||
suffix="Input: {adjective}\nOutput:",
|
||||
)
|
||||
assert prompt == expected_prompt
|
||||
|
||||
|
||||
def test_loading_few_shot_prompt_example_prompt() -> None:
|
||||
"""Test loading few shot when the example prompt is in its own file."""
|
||||
with change_directory():
|
||||
prompt = load_prompt("few_shot_prompt_example_prompt.json")
|
||||
expected_prompt = FewShotPromptTemplate(
|
||||
input_variables=["adjective"],
|
||||
prefix="Write antonyms for the following words.",
|
||||
example_prompt=PromptTemplate(
|
||||
input_variables=["input", "output"],
|
||||
template="Input: {input}\nOutput: {output}",
|
||||
),
|
||||
examples=[
|
||||
{"input": "happy", "output": "sad"},
|
||||
{"input": "tall", "output": "short"},
|
||||
],
|
||||
suffix="Input: {adjective}\nOutput:",
|
||||
)
|
||||
assert prompt == expected_prompt
|
@ -1,111 +0,0 @@
|
||||
"""Test functionality related to dynamic prompts."""
|
||||
from langchain.prompts.dynamic import DynamicPrompt
|
||||
from langchain.prompts.prompt import Prompt
|
||||
|
||||
# FULL TEMPLATES
|
||||
LONGER_TEMPLATE = """Test Prompt:
|
||||
|
||||
Question: who are you?
|
||||
Answer: foo
|
||||
|
||||
Question: what are you?
|
||||
Answer: bar
|
||||
|
||||
Question: {question}
|
||||
Answer:"""
|
||||
SHORTER_TEMPLATE = """Test Prompt:
|
||||
|
||||
Question: who are you?
|
||||
Answer: foo
|
||||
|
||||
Question: {question}
|
||||
Answer:"""
|
||||
SHORTEST_TEMPLATE = """Test Prompt:
|
||||
|
||||
Question: {question}
|
||||
Answer:"""
|
||||
|
||||
# DYNAMIC PROMPT COMPONENTS
|
||||
PREFIX = """Test Prompt:"""
|
||||
SUFFIX = """Question: {question}\nAnswer:"""
|
||||
EXAMPLES = [
|
||||
"""Question: who are you?\nAnswer: foo""",
|
||||
"""Question: what are you?\nAnswer: bar""",
|
||||
]
|
||||
|
||||
# INPUTS
|
||||
TEST_LONG_QUESTION = """I am writing a really long question,
|
||||
this probably is going to affect the example right?"""
|
||||
TEST_LONGEST_QUESTION = """This question is super super super,
|
||||
super super super super super super super super super super super,
|
||||
super super super super long, this will affect the example right?"""
|
||||
TEST_SHORT_QUESTION = "Short question?"
|
||||
|
||||
|
||||
def test_dynamic_prompt_valid() -> None:
|
||||
"""Test dynamic prompt can be successfully constructed from examples."""
|
||||
input_variables = ["question"]
|
||||
example_separator = "\n\n"
|
||||
dynamic_prompt_cls = DynamicPrompt(
|
||||
examples=EXAMPLES,
|
||||
suffix=SUFFIX,
|
||||
input_variables=input_variables,
|
||||
example_separator=example_separator,
|
||||
prefix=PREFIX,
|
||||
)
|
||||
prompt_cls = Prompt(input_variables=input_variables, template=LONGER_TEMPLATE)
|
||||
dynamic_prompt_template = dynamic_prompt_cls.format(question="foo?")
|
||||
prompt_template = prompt_cls.format(question="foo?")
|
||||
assert dynamic_prompt_template == prompt_template
|
||||
assert dynamic_prompt_cls.input_variables == prompt_cls.input_variables
|
||||
|
||||
|
||||
def test_dynamic_prompt_trims_one_example() -> None:
|
||||
"""Test dynamic prompt can trim one example."""
|
||||
input_variables = ["question"]
|
||||
example_separator = "\n\n"
|
||||
dynamic_prompt_cls = DynamicPrompt(
|
||||
examples=EXAMPLES,
|
||||
suffix=SUFFIX,
|
||||
input_variables=input_variables,
|
||||
example_separator=example_separator,
|
||||
prefix=PREFIX,
|
||||
max_length=30,
|
||||
)
|
||||
dynamic_prompt = dynamic_prompt_cls.format(question=TEST_LONG_QUESTION)
|
||||
shorter_prompt = SHORTER_TEMPLATE.format(question=TEST_LONG_QUESTION)
|
||||
assert dynamic_prompt == shorter_prompt
|
||||
|
||||
|
||||
def test_dynamic_prompt_trims_no_examples() -> None:
|
||||
"""Test dynamic prompt can trim no examples."""
|
||||
input_variables = ["question"]
|
||||
example_separator = "\n\n"
|
||||
dynamic_prompt_cls = DynamicPrompt(
|
||||
examples=EXAMPLES,
|
||||
suffix=SUFFIX,
|
||||
input_variables=input_variables,
|
||||
example_separator=example_separator,
|
||||
prefix=PREFIX,
|
||||
max_length=30,
|
||||
)
|
||||
dynamic_prompt = dynamic_prompt_cls.format(question=TEST_SHORT_QUESTION)
|
||||
full_prompt = LONGER_TEMPLATE.format(question=TEST_SHORT_QUESTION)
|
||||
assert dynamic_prompt == full_prompt
|
||||
|
||||
|
||||
def test_dynamic_prompt_trims_all_examples() -> None:
|
||||
"""Test dynamic prompt can trim all examples."""
|
||||
input_variables = ["question"]
|
||||
example_separator = "\n\n"
|
||||
dynamic_prompt_cls = DynamicPrompt(
|
||||
examples=EXAMPLES,
|
||||
suffix=SUFFIX,
|
||||
input_variables=input_variables,
|
||||
example_separator=example_separator,
|
||||
prefix=PREFIX,
|
||||
max_length=30,
|
||||
)
|
||||
dynamic_prompt = dynamic_prompt_cls.format(question=TEST_LONGEST_QUESTION)
|
||||
full_prompt = SHORTEST_TEMPLATE.format(question=TEST_LONGEST_QUESTION)
|
||||
assert dynamic_prompt == full_prompt
|
Loading…
Reference in New Issue