远兮 commited on
Commit
04bafe4
·
1 Parent(s): 92ee09b

add SemanticSimilarityExampleSelector

Browse files
Files changed (2) hide show
  1. README.md +1 -0
  2. llms_semantic_similarity.ipynb +153 -0
README.md CHANGED
@@ -47,5 +47,6 @@ TODO:
47
  2.详细看一下gptcache
48
  3.看一下Hugging Face Hub上有哪些开源model,这些model大模型环境是跑在FuggingFace上的?如果要用的话,可以直接用还是自己搭server环境?
49
  4.PromptTemplate,可以有多个input/output吗,怎么使用?参见llms_sequential_chain.ipynb
 
50
 
51
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
47
  2.详细看一下gptcache
48
  3.看一下Hugging Face Hub上有哪些开源model,这些model大模型环境是跑在FuggingFace上的?如果要用的话,可以直接用还是自己搭server环境?
49
  4.PromptTemplate,可以有多个input/output吗,怎么使用?参见llms_sequential_chain.ipynb
50
+ 5.看一下OpenAI的Embding Vector。
51
 
52
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
llms_semantic_similarity.ipynb ADDED
@@ -0,0 +1,153 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 76,
6
+ "metadata": {},
7
+ "outputs": [],
8
+ "source": [
9
+ "from langchain.prompts.few_shot import FewShotPromptTemplate\n",
10
+ "from langchain.prompts.prompt import PromptTemplate\n",
11
+ "from langchain.prompts.example_selector import SemanticSimilarityExampleSelector\n",
12
+ "from langchain.vectorstores import Chroma\n",
13
+ "from langchain.embeddings import OpenAIEmbeddings\n",
14
+ "from langchain.llms import OpenAI"
15
+ ]
16
+ },
17
+ {
18
+ "cell_type": "code",
19
+ "execution_count": 77,
20
+ "metadata": {},
21
+ "outputs": [],
22
+ "source": [
23
+ "examples = [\n",
24
+ " {\"question\":\"天气怎么样\", \"answer\":\"晴\"},\n",
25
+ " {\"question\":\"北京天气怎么样\", \"answer\":\"加法\"},\n",
26
+ " {\"question\":\"咋样\", \"answer\":\"加法\"},\n",
27
+ " {\"question\":\"天儿咋样\", \"answer\":\"加法\"},\n",
28
+ " {\"question\":\"今天有雾\", \"answer\":\"加法\"},\n",
29
+ " {\"question\":\"吃得怎么样\", \"answer\":\"加法\"},\n",
30
+ " {\"question\":\"你叫什么\", \"answer\":\"加法\"},\n",
31
+ " {\"question\":\"你叫嘿嘿\", \"answer\":\"加法\"},\n",
32
+ " {\"question\":\"是吗\", \"answer\":\"加法\"},\n",
33
+ " {\"question\":\"10-1=9\", \"answer\":\"作业帮回答:减法\"},\n",
34
+ " {\"question\":\"9-1=8\", \"answer\":\"作业帮回答:减法\"},\n",
35
+ " {\"question\":\"8-1=7\", \"answer\":\"作业帮回答:减法\"},\n",
36
+ " {\"question\":\"7-1=6\", \"answer\":\"作业帮回答:减法\"},\n",
37
+ " {\"question\":\"7-2=5\", \"answer\":\"作业帮回答:减法\"},\n",
38
+ " {\"question\":\"10-3=7\", \"answer\":\"作业帮回答:减法\"},\n",
39
+ " {\"question\":\"10-4=6\", \"answer\":\"作业帮回答:减法\"},\n",
40
+ " {\"question\":\"10-5=5\", \"answer\":\"作业帮回答:减法\"},\n",
41
+ " {\"question\":\"10+5=15\", \"answer\":\"作业帮回答:加法\"},\n",
42
+ " {\"question\":\"10*5=50\", \"answer\":\"作业帮回答:乘法\"},\n",
43
+ " {\"question\":\"10/5=2\", \"answer\":\"作业帮回答:除法\"},\n",
44
+ "]"
45
+ ]
46
+ },
47
+ {
48
+ "cell_type": "code",
49
+ "execution_count": 78,
50
+ "metadata": {},
51
+ "outputs": [
52
+ {
53
+ "name": "stderr",
54
+ "output_type": "stream",
55
+ "text": [
56
+ "Using embedded DuckDB without persistence: data will be transient\n"
57
+ ]
58
+ }
59
+ ],
60
+ "source": [
61
+ "example_selector = SemanticSimilarityExampleSelector.from_examples(\n",
62
+ " # This is the list of examples available to select from.\n",
63
+ " examples,\n",
64
+ " # This is the embedding class used to produce embeddings which are used to measure semantic similarity.\n",
65
+ " OpenAIEmbeddings(),\n",
66
+ " # This is the VectorStore class that is used to store the embeddings and do a similarity search over.\n",
67
+ " Chroma,\n",
68
+ " # This is the number of examples to produce.\n",
69
+ " k=1\n",
70
+ ")"
71
+ ]
72
+ },
73
+ {
74
+ "cell_type": "code",
75
+ "execution_count": 79,
76
+ "metadata": {},
77
+ "outputs": [],
78
+ "source": [
79
+ "example_prompt = PromptTemplate(input_variables=[\"question\", \"answer\"], template=\"Question: {question}\\n{answer}\")"
80
+ ]
81
+ },
82
+ {
83
+ "cell_type": "code",
84
+ "execution_count": 80,
85
+ "metadata": {},
86
+ "outputs": [
87
+ {
88
+ "name": "stdout",
89
+ "output_type": "stream",
90
+ "text": [
91
+ "Question: 10+5=15\n",
92
+ "作业帮回答:加法\n",
93
+ "\n",
94
+ "Question: 1+2=?\n"
95
+ ]
96
+ }
97
+ ],
98
+ "source": [
99
+ "prompt = FewShotPromptTemplate(\n",
100
+ " example_selector=example_selector, \n",
101
+ " example_prompt=example_prompt, \n",
102
+ " suffix=\"Question: {input}\", \n",
103
+ " input_variables=[\"input\"]\n",
104
+ ")\n",
105
+ "\n",
106
+ "print(prompt.format(input=\"1+2=?\"))"
107
+ ]
108
+ },
109
+ {
110
+ "cell_type": "code",
111
+ "execution_count": 81,
112
+ "metadata": {},
113
+ "outputs": [
114
+ {
115
+ "data": {
116
+ "text/plain": [
117
+ "'\\n作业帮回答:除法'"
118
+ ]
119
+ },
120
+ "execution_count": 81,
121
+ "metadata": {},
122
+ "output_type": "execute_result"
123
+ }
124
+ ],
125
+ "source": [
126
+ "llm = OpenAI(temperature=0)\n",
127
+ "llm(prompt.format(input=\"66/2=34\"))"
128
+ ]
129
+ }
130
+ ],
131
+ "metadata": {
132
+ "kernelspec": {
133
+ "display_name": "base",
134
+ "language": "python",
135
+ "name": "python3"
136
+ },
137
+ "language_info": {
138
+ "codemirror_mode": {
139
+ "name": "ipython",
140
+ "version": 3
141
+ },
142
+ "file_extension": ".py",
143
+ "mimetype": "text/x-python",
144
+ "name": "python",
145
+ "nbconvert_exporter": "python",
146
+ "pygments_lexer": "ipython3",
147
+ "version": "3.10.10"
148
+ },
149
+ "orig_nbformat": 4
150
+ },
151
+ "nbformat": 4,
152
+ "nbformat_minor": 2
153
+ }