Spaces:
Runtime error
Runtime error
远兮
commited on
Commit
·
e27205e
1
Parent(s):
1c6d1f6
add 最大边际相关性的MaxMarginalRelevanceExampleSelector
Browse files
prompts_relevance_example_selector.ipynb
ADDED
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 16,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [],
|
8 |
+
"source": [
|
9 |
+
"from langchain.prompts.example_selector import MaxMarginalRelevanceExampleSelector\n",
|
10 |
+
"from langchain.vectorstores import FAISS\n",
|
11 |
+
"from langchain.embeddings import OpenAIEmbeddings\n",
|
12 |
+
"from langchain.prompts import FewShotPromptTemplate, PromptTemplate\n",
|
13 |
+
"\n",
|
14 |
+
"example_prompt = PromptTemplate(\n",
|
15 |
+
" input_variables=[\"input\", \"output\"],\n",
|
16 |
+
" template=\"Input: {input}\\nOutput: {output}\",\n",
|
17 |
+
")\n",
|
18 |
+
"\n",
|
19 |
+
"# These are a lot of examples of a pretend task of creating antonyms.\n",
|
20 |
+
"examples = [\n",
|
21 |
+
" {\"input\": \"高兴\", \"output\": \"悲伤\"},\n",
|
22 |
+
" {\"input\": \"个子高\", \"output\": \"个子矮\"},\n",
|
23 |
+
" {\"input\": \"精力充沛\", \"output\": \"昏昏欲睡\"},\n",
|
24 |
+
" {\"input\": \"晴朗\", \"output\": \"阴暗的阴暗的\"},\n",
|
25 |
+
" {\"input\": \"多风\", \"output\": \"平静的\"},\n",
|
26 |
+
" {\"input\": \"经济下滑\", \"output\": \"业绩增长\"},\n",
|
27 |
+
" {\"input\": \"飞翔\", \"output\": \"天空\"},\n",
|
28 |
+
" {\"input\": \"教育\", \"output\": \"平静的\"},\n",
|
29 |
+
" {\"input\": \"小孩儿\", \"output\": \"平静的\"},\n",
|
30 |
+
" {\"input\": \"开心\", \"output\": \"平静的\"},\n",
|
31 |
+
" {\"input\": \"消防员\", \"output\": \"平静的\"},\n",
|
32 |
+
" {\"input\": \"程序员\", \"output\": \"平静的\"},\n",
|
33 |
+
" {\"input\": \"理财师\", \"output\": \"平静的\"},\n",
|
34 |
+
" {\"input\": \"学生\", \"output\": \"平静的\"},\n",
|
35 |
+
" {\"input\": \"计算机\", \"output\": \"平静的\"},\n",
|
36 |
+
"]"
|
37 |
+
]
|
38 |
+
},
|
39 |
+
{
|
40 |
+
"cell_type": "code",
|
41 |
+
"execution_count": 17,
|
42 |
+
"metadata": {},
|
43 |
+
"outputs": [],
|
44 |
+
"source": [
|
45 |
+
"example_selector = MaxMarginalRelevanceExampleSelector.from_examples(\n",
|
46 |
+
" # This is the list of examples available to select from.\n",
|
47 |
+
" examples, \n",
|
48 |
+
" # This is the embedding class used to produce embeddings which are used to measure semantic similarity.\n",
|
49 |
+
" OpenAIEmbeddings(), \n",
|
50 |
+
" # This is the VectorStore class that is used to store the embeddings and do a similarity search over.\n",
|
51 |
+
" FAISS, \n",
|
52 |
+
" # This is the number of examples to produce.\n",
|
53 |
+
" k=2\n",
|
54 |
+
")\n",
|
55 |
+
"mmr_prompt = FewShotPromptTemplate(\n",
|
56 |
+
" # We provide an ExampleSelector instead of examples.\n",
|
57 |
+
" example_selector=example_selector,\n",
|
58 |
+
" example_prompt=example_prompt,\n",
|
59 |
+
" prefix=\"Give the antonym of every input\",\n",
|
60 |
+
" suffix=\"Input: {adjective}\\nOutput:\", \n",
|
61 |
+
" input_variables=[\"adjective\"],\n",
|
62 |
+
")"
|
63 |
+
]
|
64 |
+
},
|
65 |
+
{
|
66 |
+
"cell_type": "code",
|
67 |
+
"execution_count": 18,
|
68 |
+
"metadata": {},
|
69 |
+
"outputs": [
|
70 |
+
{
|
71 |
+
"name": "stdout",
|
72 |
+
"output_type": "stream",
|
73 |
+
"text": [
|
74 |
+
"Give the antonym of every input\n",
|
75 |
+
"\n",
|
76 |
+
"Input: 理财师\n",
|
77 |
+
"Output: 平静的\n",
|
78 |
+
"\n",
|
79 |
+
"Input: 经济下滑\n",
|
80 |
+
"Output: 业绩增长\n",
|
81 |
+
"\n",
|
82 |
+
"Input: 投资\n",
|
83 |
+
"Output:\n"
|
84 |
+
]
|
85 |
+
}
|
86 |
+
],
|
87 |
+
"source": [
|
88 |
+
"# Input is a feeling, so should select the happy/sad example as the first one\n",
|
89 |
+
"print(mmr_prompt.format(adjective=\"投资\"))"
|
90 |
+
]
|
91 |
+
}
|
92 |
+
],
|
93 |
+
"metadata": {
|
94 |
+
"kernelspec": {
|
95 |
+
"display_name": "base",
|
96 |
+
"language": "python",
|
97 |
+
"name": "python3"
|
98 |
+
},
|
99 |
+
"language_info": {
|
100 |
+
"codemirror_mode": {
|
101 |
+
"name": "ipython",
|
102 |
+
"version": 3
|
103 |
+
},
|
104 |
+
"file_extension": ".py",
|
105 |
+
"mimetype": "text/x-python",
|
106 |
+
"name": "python",
|
107 |
+
"nbconvert_exporter": "python",
|
108 |
+
"pygments_lexer": "ipython3",
|
109 |
+
"version": "3.10.10"
|
110 |
+
},
|
111 |
+
"orig_nbformat": 4
|
112 |
+
},
|
113 |
+
"nbformat": 4,
|
114 |
+
"nbformat_minor": 2
|
115 |
+
}
|