{ "cells": [ { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [], "source": [ "from langchain.prompts.example_selector import MaxMarginalRelevanceExampleSelector\n", "from langchain.vectorstores import FAISS\n", "from langchain.embeddings import OpenAIEmbeddings\n", "from langchain.prompts import FewShotPromptTemplate, PromptTemplate\n", "from langchain.prompts.example_selector.ngram_overlap import NGramOverlapExampleSelector\n", "\n", "example_prompt = PromptTemplate(\n", " input_variables=[\"input\", \"output\"],\n", " template=\"Input: {input}\\nOutput: {output}\",\n", ")\n", "\n", "# These are a lot of examples of a pretend task of creating antonyms.\n", "examples = [\n", " {\"input\": \"高兴\", \"output\": \"悲伤\"},\n", " {\"input\": \"个子高\", \"output\": \"个子矮\"},\n", " {\"input\": \"精力充沛\", \"output\": \"昏昏欲睡\"},\n", " {\"input\": \"晴朗\", \"output\": \"阴暗的阴暗的\"},\n", " {\"input\": \"多风\", \"output\": \"平静的\"},\n", " {\"input\": \"经济下滑一路到底\", \"output\": \"业绩增长\"},\n", " {\"input\": \"飞翔\", \"output\": \"天空\"},\n", " {\"input\": \"教育\", \"output\": \"平静的\"},\n", " {\"input\": \"小孩儿\", \"output\": \"平静的\"},\n", " {\"input\": \"开心\", \"output\": \"平静的\"},\n", " {\"input\": \"消防员\", \"output\": \"平静的\"},\n", " {\"input\": \"程序员\", \"output\": \"平静的\"},\n", " {\"input\": \"理财师\", \"output\": \"平静的\"},\n", " {\"input\": \"学生\", \"output\": \"平静的\"},\n", " {\"input\": \"计算机\", \"output\": \"平静的\"},\n", " {\"input\": \"See Spot run.\", \"output\": \"Ver correr a Spot.\"},\n", " {\"input\": \"My dog barks.\", \"output\": \"Mi perro ladra.\"},\n", " {\"input\": \"Spot can run.\", \"output\": \"Spot puede correr.\"},\n", "]" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [], "source": [ "example_selector = MaxMarginalRelevanceExampleSelector.from_examples(\n", " # This is the list of examples available to select from.\n", " examples, \n", " # This is the embedding class used to produce embeddings which are used to measure semantic similarity.\n", " OpenAIEmbeddings(), \n", " # This is the VectorStore class that is used to store the embeddings and do a similarity search over.\n", " FAISS, \n", " # This is the number of examples to produce.\n", " k=2\n", ")\n", "mmr_prompt = FewShotPromptTemplate(\n", " # We provide an ExampleSelector instead of examples.\n", " example_selector=example_selector,\n", " example_prompt=example_prompt,\n", " prefix=\"Give the antonym of every input\",\n", " suffix=\"Input: {adjective}\\nOutput:\", \n", " input_variables=[\"adjective\"],\n", ")" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Give the antonym of every input\n", "\n", "Input: 理财师\n", "Output: 平静的\n", "\n", "Input: See Spot run.\n", "Output: Ver correr a Spot.\n", "\n", "Input: 投资\n", "Output:\n" ] } ], "source": [ "# Input is a feeling, so should select the happy/sad example as the first one\n", "print(mmr_prompt.format(adjective=\"投资\"))" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [], "source": [ "example_selector = NGramOverlapExampleSelector(\n", " # These are the examples it 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 threshold, at which selector stops.\n", " # It is set to -1.0 by default.\n", " threshold=-1.0,\n", " # For negative threshold:\n", " # Selector sorts examples by ngram overlap score, and excludes none.\n", " # For threshold greater than 1.0:\n", " # Selector excludes all examples, and returns an empty list.\n", " # For threshold equal to 0.0:\n", " # Selector sorts examples by ngram overlap score,\n", " # and excludes those with no ngram overlap with input.\n", ")" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [], "source": [ "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 Spanish translation of every input\",\n", " suffix=\"Input: {sentence}\\nOutput:\", \n", " input_variables=[\"sentence\"],\n", ")" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Give the Spanish translation of every input\n", "\n", "Input: See Spot run.\n", "Output: Ver correr a Spot.\n", "\n", "Input: Spot can run.\n", "Output: Spot puede correr.\n", "\n", "Input: Spot run.\n", "Output:\n" ] } ], "source": [ "# 没看懂有什么作用,0~1,\n", "example_selector.threshold=0\n", "print(dynamic_prompt.format(sentence=\" Spot run.\"))" ] } ], "metadata": { "kernelspec": { "display_name": "base", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.10" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }