You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
langchain/docs/examples/prompts/load_prompts.ipynb

580 lines
16 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "43fb16cb",
"metadata": {},
"source": [
"# Prompt Walkthrough\n",
"\n",
"An overview of the different types of prompts in LangChain and how to use them"
]
},
{
"cell_type": "code",
"execution_count": 53,
"id": "2c8d7587",
"metadata": {},
"outputs": [],
"source": [
"import yaml\n",
"\n",
"with open(\"simple_prompt.yaml\", \"r\") as stream:\n",
" config = yaml.safe_load(stream)"
]
},
{
"cell_type": "code",
"execution_count": 54,
"id": "1ab11b59",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'input_variables': ['adjective', 'content'],\n",
" 'template': 'Tell me a {adjective} joke about {content}.\\nLike what does it mean?'}"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"config"
]
},
{
"cell_type": "code",
"execution_count": 55,
"id": "78a2cf84",
"metadata": {},
"outputs": [],
"source": [
"from pathlib import Path\n",
"from typing import Union\n",
"import yaml\n",
"def load_file(file: Union[str, Path]):\n",
" if isinstance(file, str):\n",
" file_path = Path(file)\n",
" else:\n",
" file_path = file\n",
" if file_path.suffix == \".json\":\n",
" with open(file_path) as f:\n",
" config = json.load(f)\n",
" elif file_path.suffix == \".yaml\":\n",
" with open(file_path, \"r\") as f:\n",
" config = yaml.safe_load(f)\n",
" else:\n",
" raise ValueError\n",
" return load_prompt_from_config(config)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "6e1f9bcd",
"metadata": {},
"outputs": [],
"source": [
"from langchain.prompts import Prompt, DynamicPrompt\n",
"import json\n",
"def load_prompt_from_config(config):\n",
" if \"type\" in config:\n",
" prompt_type = config.pop(\"type\")\n",
" else:\n",
" prompt_type = \"prompt\"\n",
" if prompt_type == \"prompt\":\n",
" return _load_prompt(config)\n",
" elif prompt_type == \"dynamic_prompt\":\n",
" return _load_dynamic_prompt(config)\n",
" else:\n",
" raise ValueError\n",
" \n",
"def _load_template(var_name: str, config: dict) -> dict:\n",
" if f\"{var_name}_path\" in config:\n",
" if var_name in config:\n",
" raise ValueError(f\"Both `{var_name}_path` and `{var_name}` cannot be provided.\")\n",
" template_path = Path(config.pop(f\"{var_name}_path\"))\n",
" if template_path.suffix == \".txt\":\n",
" with open(template_path) as f:\n",
" template = f.read()\n",
" else:\n",
" raise ValueError\n",
" config[var_name] = template\n",
" return config\n",
" \n",
" \n",
"def _load_dynamic_prompt(config):\n",
" if \"loader\" in config:\n",
" prompt_type = config.pop(\"loader\")\n",
" else:\n",
" prompt_type = \"init\"\n",
" if prompt_type == \"init\":\n",
" config = _load_template(\"suffix\", config)\n",
" config = _load_template(\"prefix\", config)\n",
" return DynamicPrompt(**config)\n",
" elif prompt_type == \"from_structured_examples\":\n",
" config = _load_template(\"suffix\", config)\n",
" config = _load_template(\"prefix\", config)\n",
" config[\"example_prompt\"] = _load_prompt(config[\"example_prompt\"])\n",
" if isinstance(config[\"examples\"], list):\n",
" pass\n",
" elif isinstance(config[\"examples\"], str):\n",
" with open(config[\"examples\"]) as f:\n",
" examples = json.load(f)\n",
" config[\"examples\"] = examples\n",
" else:\n",
" raise ValueError\n",
" return DynamicPrompt.from_structured_examples(**config)\n",
" else:\n",
" raise ValueError\n",
"\n",
"def _load_prompt(config):\n",
" if \"loader\" in config:\n",
" prompt_type = config.pop(\"loader\")\n",
" else:\n",
" prompt_type = \"init\"\n",
" if prompt_type == \"init\":\n",
" config = _load_template(\"template\", config)\n",
" return Prompt(**config)\n",
" elif prompt_type == \"from_examples\":\n",
" config = _load_template(\"suffix\", config)\n",
" config = _load_template(\"prefix\", config)\n",
" if isinstance(config[\"examples\"], list):\n",
" pass\n",
" elif isinstance(config[\"examples\"], str):\n",
" with open(config[\"examples\"]) as f:\n",
" examples = json.load(f)\n",
" config[\"examples\"] = examples\n",
" else:\n",
" raise ValueError\n",
" return Prompt.from_examples(**config)\n",
" elif prompt_type == \"from_structured_examples\":\n",
" config = _load_template(\"suffix\", config)\n",
" config = _load_template(\"prefix\", config)\n",
" config[\"example_prompt\"] = _load_prompt(config[\"example_prompt\"])\n",
" if isinstance(config[\"examples\"], list):\n",
" pass\n",
" elif isinstance(config[\"examples\"], str):\n",
" with open(config[\"examples\"]) as f:\n",
" examples = json.load(f)\n",
" config[\"examples\"] = examples\n",
" else:\n",
" raise ValueError\n",
" return Prompt.from_structured_examples(**config)\n",
" else:\n",
" raise ValueError"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b045da0f",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "cddb465e",
"metadata": {},
"source": [
"### Basic Prompt\n",
"\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "ab46bd2a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Tellme a joke.'"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# An example prompt with no input variables\n",
"_config = {\n",
" \"input_variables\": [],\n",
" \"template\": \"Tell me a joke.\"\n",
"}\n",
"no_input_prompt = load_prompt_from_config(_config)\n",
"no_input_prompt.format()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "c3ad0fa8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Tell me a funny joke.'"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# An example prompt with one input variable\n",
"_config = {\n",
" \"input_variables\": [\"adjective\"],\n",
" \"template\": \"Tell me a {adjective} joke.\"\n",
"}\n",
"one_input_prompt = load_prompt_from_config(_config)\n",
"one_input_prompt.format(adjective=\"funny\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "ba577dcf",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Tell me a funny joke about chickens.'"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# An example prompt with multiple input variables\n",
"_config = {\n",
" \"input_variables\": [\"adjective\", \"content\"],\n",
" \"template\": \"Tell me a {adjective} joke about {content}.\"\n",
"}\n",
"multiple_input_prompt = load_prompt_from_config(_config)\n",
"multiple_input_prompt.format(adjective=\"funny\", content=\"chickens\")"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "e0ad7fb8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Tell me a funny joke about chickens.'"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"multiple_input_prompt = load_file(\"simple_prompt_with_template_file.json\")\n",
"multiple_input_prompt.format(adjective=\"funny\", content=\"chickens\")"
]
},
{
"cell_type": "markdown",
"id": "d27b1824",
"metadata": {},
"source": [
"### Examples\n",
"Examples are datapoints that can be used to show the model how to produce results. They can be either strings, or dictionaries that are then turned into strings by an example prompt itself."
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "2c00e965",
"metadata": {},
"outputs": [],
"source": [
"string_examples = [\"Input: happy\\nOutput: sad\", \"Input: tall\\nOutput: short\"]\n",
"dict_examples = [{\"input\": \"happy\", \"output\": \"sad\"}, {\"input\": \"tall\", \"output\": \"short\"}]\n",
"example_prompt_config = {\"input_variables\": [\"input\",\"output\"], \"template\": \"Input: {input}\\nOutput: {output}\"}"
]
},
{
"cell_type": "markdown",
"id": "1492b49d",
"metadata": {},
"source": [
"### Simple Prompt with examples\n",
"\n",
"We can then use these examples to construct prompts."
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "1a5a686d",
"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": [
"_config = {\n",
" \"loader\": \"from_examples\",\n",
" \"examples\": string_examples,\n",
" \"prefix\": \"Give the antonym of every input\",\n",
" \"suffix\": \"Input: {adjective}\\nOutput:\", \n",
" \"input_variables\": [\"adjective\"],\n",
"}\n",
"prompt_from_string_examples = load_prompt_from_config(_config)\n",
"print(prompt_from_string_examples.format(adjective=\"big\"))"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "08d43717",
"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": [
"_config = {\n",
" \"loader\": \"from_examples\",\n",
" \"examples\": \"string_examples.json\",\n",
" \"prefix\": \"Give the antonym of every input\",\n",
" \"suffix\": \"Input: {adjective}\\nOutput:\", \n",
" \"input_variables\": [\"adjective\"],\n",
"}\n",
"prompt_from_string_examples = load_prompt_from_config(_config)\n",
"print(prompt_from_string_examples.format(adjective=\"big\"))"
]
},
{
"cell_type": "code",
"execution_count": 17,
"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": [
"_config = {\n",
" \"loader\": \"from_structured_examples\",\n",
" \"examples\": dict_examples,\n",
" \"example_prompt\": example_prompt_config,\n",
" \"prefix\": \"Give the antonym of every input\",\n",
" \"suffix\": \"Input: {adjective}\\nOutput:\", \n",
" \"input_variables\": [\"adjective\"],\n",
"}\n",
"prompt_from_structured_examples = load_prompt_from_config(_config)\n",
"print(prompt_from_structured_examples.format(adjective=\"big\"))"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "738ff0a8",
"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": [
"_config = {\n",
" \"loader\": \"from_structured_examples\",\n",
" \"examples\": \"structured_examples.json\",\n",
" \"example_prompt\": example_prompt_config,\n",
" \"prefix\": \"Give the antonym of every input\",\n",
" \"suffix\": \"Input: {adjective}\\nOutput:\", \n",
" \"input_variables\": [\"adjective\"],\n",
"}\n",
"prompt_from_structured_examples = load_prompt_from_config(_config)\n",
"print(prompt_from_structured_examples.format(adjective=\"big\"))"
]
},
{
"cell_type": "markdown",
"id": "861a4d1f",
"metadata": {},
"source": [
"### Dynamic Prompt\n",
"\n",
"We also do more clever things with prompts - for example, only select a certain number of examples in order to limit the size of the text passed in. This will vary with the input text size."
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "207e55f7",
"metadata": {},
"outputs": [],
"source": [
"_config = {\n",
" \"type\": \"dynamic_prompt\",\n",
" \"loader\": \"from_structured_examples\",\n",
" \"examples\": \"structured_examples.json\",\n",
" \"example_prompt\": example_prompt_config,\n",
" \"prefix\": \"Give the antonym of every input\",\n",
" \"suffix\": \"Input: {adjective}\\nOutput:\", \n",
" \"input_variables\": [\"adjective\"],\n",
" \"max_length\": 20,\n",
"}\n",
"dynamic_prompt = load_prompt_from_config(_config)"
]
},
{
"cell_type": "code",
"execution_count": 30,
"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: big\n",
"Output:\n"
]
}
],
"source": [
"# An example with small input, so it selects both examples.\n",
"print(dynamic_prompt.format(adjective=\"big\"))"
]
},
{
"cell_type": "code",
"execution_count": 31,
"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\n",
"Output:\n"
]
}
],
"source": [
"# An example with long input, so it selects only one example.\n",
"print(dynamic_prompt.format(adjective=\"big and huge and massive\"))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "76a1065d",
"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
}