{ "cells": [ { "cell_type": "markdown", "metadata": { "jukit_cell_id": "IagIXdFJ76" }, "source": [ "# Prompt templates\n", "[see](https://langchain.readthedocs.io/en/latest/modules/prompts/getting_started.html)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "jukit_cell_id": "5rNpKSE97a" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('\\n'\n", " 'I want you to act as a naming consultant for new companies.\\n'\n", " '\\n'\n", " 'Here are some examples of good company names:\\n'\n", " '\\n'\n", " '- search engine, Google\\n'\n", " '- social media, Facebook\\n'\n", " '- video sharing, YouTube\\n'\n", " '\\n'\n", " 'The name should be short, catchy and easy to remember.\\n'\n", " '\\n'\n", " 'What is a good name for a company that makes cookie?\\n')\n" ] } ], "source": [ "from langchain import PromptTemplate\n", "import pprint as pp\n", "\n", "template = \"\"\"\n", "I want you to act as a naming consultant for new companies.\n", "\n", "Here are some examples of good company names:\n", "\n", "- search engine, Google\n", "- social media, Facebook\n", "- video sharing, YouTube\n", "\n", "The name should be short, catchy and easy to remember.\n", "\n", "What is a good name for a company that makes {product}?\n", "\"\"\"\n", "\n", "prompt = PromptTemplate(\n", " input_variables=[\"product\"],\n", " template=template,\n", " )\n", "\n", "pp.pp(prompt.format(product='cookie'))" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "jukit_cell_id": "UH7UDNwwOT" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] }, { "data": { "text/plain": [ "'tell me a funny joke about bats.'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# without inputs\n", "no_input_prompt = PromptTemplate(input_variables=[],\n", " template=\"tell me a joke.\")\n", "no_input_prompt.format()\n", "\n", "# with inputs\n", "multi_input_prompt = PromptTemplate(\n", " input_variables=[\"adjective\", \"content\"],\n", " template=\"tell me a {adjective} joke about {content}.\" \n", " )\n", "multi_input_prompt.format(adjective=\"funny\", content=\"bats\")" ] }, { "cell_type": "markdown", "metadata": { "jukit_cell_id": "m4HfAhbN4T" }, "source": [ "## Loading prompt templates from LangChainHub " ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "jukit_cell_id": "6sAHvM0Vrt" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.\n", "\n", "Current conversation:\n", "\n", "Human: what is 1 + 1?\n", "AI:\n" ] } ], "source": [ "from langchain.prompts import load_prompt\n", "\n", "prompt=load_prompt(\"lc://prompts/conversation/prompt.json\")\n", "#NOTE: is there a helper to quickly build a history ? \n", "print(prompt.format(history=\"\", input=\"what is 1 + 1?\"))" ] }, { "cell_type": "markdown", "metadata": { "jukit_cell_id": "u2xOTHeA5E" }, "source": [ "## Pass few shot examples to prompt template" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "jukit_cell_id": "KO5IXCuzjw" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Give the antonym of every input\n", "\n", "\n", "Word: happy\n", "Antonym: sad\n", "\n", "\n", "\n", "Word: tall\n", "Antonym: short\n", "\n", "\n", "Word: fast\n", "Antonym:\n" ] } ], "source": [ "from langchain import FewShotPromptTemplate\n", "\n", "# create a list of few shot examples\n", "examples = [\n", " {\"word\": \"happy\", \"antonym\": \"sad\"},\n", " {\"word\": \"tall\", \"antonym\": \"short\"},\n", " ]\n", "\n", "# next e specify a template for format the examples\n", "# we use PromptTemplate class\n", "example_formatter_template = \"\"\"\n", "Word: {word}\n", "Antonym: {antonym}\n", "\"\"\"\n", "example_pr = PromptTemplate(\n", " input_variables=[\"word\", \"antonym\"],\n", " template=example_formatter_template,\n", " )\n", "\n", "# now we can use FewShotPromptTemplate\n", "few_shot_prompt = FewShotPromptTemplate(\n", " # examples we want to insert in prompt\n", " examples=examples,\n", " # how we want examples to be formatted in prompt\n", " example_prompt=example_pr,\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", " suffix=\"Word: {input}\\nAntonym:\",\n", " # The input variables are the variables that the overall prompt expects.\n", " input_variables=[\"input\"],\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", "# generate few shot prompt using input\n", "print(few_shot_prompt.format(input=\"fast\"))" ] }, { "cell_type": "markdown", "metadata": { "jukit_cell_id": "fV1qxeDncc" }, "source": [ "## Select examples from prompt template\n", "\n", "- for a large number of exaamples use ExampleSelector to select a subset of\n", " most informative ones for language model.\n", "- LengthBasedExampleSelector selects examples based on length of input.\n", " practical to to construct prompt that do not extend over context window\n", " based on input length" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "jukit_cell_id": "uuIdYaJ4wD" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Give the antonym of every input\n", "\n", "\n", "Word: happy\n", "Antonym: sad\n", "\n", "\n", "\n", "Word: tall\n", "Antonym: short\n", "\n", "\n", "\n", "Word: energetic\n", "Antonym: lethargic\n", "\n", "\n", "\n", "Word: sunny\n", "Antonym: gloomy\n", "\n", "\n", "Word: big\n", "Antonym:\n", "----------\n", "Give the antonym of every input\n", "\n", "\n", "Word: happy\n", "Antonym: sad\n", "\n", "\n", "Word: big and huge and massive and large and gigantic and tall and much much much much much bigger than everything else\n", "Antonym:\n" ] } ], "source": [ "from langchain.prompts.example_selector import LengthBasedExampleSelector\n", "\n", "#These are a lot of examples of a pretend task of creating antonyms.\n", "examples = [\n", " {\"word\": \"happy\", \"antonym\": \"sad\"},\n", " {\"word\": \"tall\", \"antonym\": \"short\"},\n", " {\"word\": \"energetic\", \"antonym\": \"lethargic\"},\n", " {\"word\": \"sunny\", \"antonym\": \"gloomy\"},\n", " {\"word\": \"windy\", \"antonym\": \"calm\"},\n", "]\n", "\n", "example_selector = LengthBasedExampleSelector(\n", " examples=examples,\n", " # This is the PromptTemplate being used to format the examples.\n", " example_prompt=example_pr,\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=30,\n", " )\n", "\n", "# We can now use the `example_selector` to create a `FewShotPromptTemplate`.\n", "dynamic_prompt = FewShotPromptTemplate(\n", " # We provide an ExampleSelector instead of examples.\n", " example_selector=example_selector,\n", " example_prompt=example_pr,\n", " prefix=\"Give the antonym of every input\",\n", " suffix=\"Word: {input}\\nAntonym:\",\n", " input_variables=[\"input\"],\n", " example_separator=\"\\n\\n\",\n", ")\n", "\n", "# We can now generate a prompt using the `format` method.\n", "print(dynamic_prompt.format(input=\"big\"))\n", "\n", "print(\"----------\")\n", "\n", "# In contrast, if we provide a very long input, the LengthBasedExampleSelector\n", "# will select fewer examples to include in the prompt.\n", "long_string = \"big and huge and massive and large and gigantic and tall and much much much much much bigger than everything else\"\n", "print(dynamic_prompt.format(input=long_string))" ] } ], "metadata": { "anaconda-cloud": {}, "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.9.11" } }, "nbformat": 4, "nbformat_minor": 4 }