Spaces:
Runtime error
Runtime error
lizhen
commited on
Commit
·
5879dd0
1
Parent(s):
7d067f3
add custom example selector
Browse files
prompt_custom_example_selector.ipynb
ADDED
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 3,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [],
|
8 |
+
"source": [
|
9 |
+
"from langchain.prompts.example_selector.base import BaseExampleSelector\n",
|
10 |
+
"from typing import Dict, List\n",
|
11 |
+
"import numpy as np\n",
|
12 |
+
"\n",
|
13 |
+
"class CustomExampleSelector(BaseExampleSelector):\n",
|
14 |
+
"\n",
|
15 |
+
" def __init__(self, examples: List[Dict[str, str]]) -> None:\n",
|
16 |
+
" self.examples = examples\n",
|
17 |
+
"\n",
|
18 |
+
" def add_example(self, example: Dict[str, str]):\n",
|
19 |
+
" self.examples.append(example)\n",
|
20 |
+
"\n",
|
21 |
+
" def select_examples(self) -> List[dict]:\n",
|
22 |
+
" return np.random.choice(self.examples, size=2, replace=False)"
|
23 |
+
]
|
24 |
+
},
|
25 |
+
{
|
26 |
+
"cell_type": "code",
|
27 |
+
"execution_count": 4,
|
28 |
+
"metadata": {},
|
29 |
+
"outputs": [
|
30 |
+
{
|
31 |
+
"data": {
|
32 |
+
"text/plain": [
|
33 |
+
"array([{'name', '黄奕'}, {'王刚', 'name'}], dtype=object)"
|
34 |
+
]
|
35 |
+
},
|
36 |
+
"execution_count": 4,
|
37 |
+
"metadata": {},
|
38 |
+
"output_type": "execute_result"
|
39 |
+
}
|
40 |
+
],
|
41 |
+
"source": [
|
42 |
+
"examples = [\n",
|
43 |
+
" {\"name\", \"李振\"},\n",
|
44 |
+
" {\"name\", \"张三\"},\n",
|
45 |
+
" {\"name\", \"王刚\"},\n",
|
46 |
+
" {\"name\", \"黄奕\"},\n",
|
47 |
+
" {\"name\", \"赵军\"},\n",
|
48 |
+
"]\n",
|
49 |
+
"\n",
|
50 |
+
"example_selector = CustomExampleSelector(examples)\n",
|
51 |
+
"\n",
|
52 |
+
"example_selector.select_examples()"
|
53 |
+
]
|
54 |
+
},
|
55 |
+
{
|
56 |
+
"cell_type": "code",
|
57 |
+
"execution_count": 5,
|
58 |
+
"metadata": {},
|
59 |
+
"outputs": [
|
60 |
+
{
|
61 |
+
"data": {
|
62 |
+
"text/plain": [
|
63 |
+
"[{'name', '李振'},\n",
|
64 |
+
" {'name', '张三'},\n",
|
65 |
+
" {'name', '王刚'},\n",
|
66 |
+
" {'name', '黄奕'},\n",
|
67 |
+
" {'name', '赵军'},\n",
|
68 |
+
" {'name', '秦都'}]"
|
69 |
+
]
|
70 |
+
},
|
71 |
+
"execution_count": 5,
|
72 |
+
"metadata": {},
|
73 |
+
"output_type": "execute_result"
|
74 |
+
}
|
75 |
+
],
|
76 |
+
"source": [
|
77 |
+
"example_selector.add_example({\"name\", \"秦都\"})\n",
|
78 |
+
"example_selector.examples"
|
79 |
+
]
|
80 |
+
},
|
81 |
+
{
|
82 |
+
"cell_type": "code",
|
83 |
+
"execution_count": 9,
|
84 |
+
"metadata": {},
|
85 |
+
"outputs": [
|
86 |
+
{
|
87 |
+
"data": {
|
88 |
+
"text/plain": [
|
89 |
+
"array([{'秦都', 'name'}, {'张三', 'name'}], dtype=object)"
|
90 |
+
]
|
91 |
+
},
|
92 |
+
"execution_count": 9,
|
93 |
+
"metadata": {},
|
94 |
+
"output_type": "execute_result"
|
95 |
+
}
|
96 |
+
],
|
97 |
+
"source": [
|
98 |
+
"example_selector.select_examples()"
|
99 |
+
]
|
100 |
+
}
|
101 |
+
],
|
102 |
+
"metadata": {
|
103 |
+
"kernelspec": {
|
104 |
+
"display_name": "Python 3",
|
105 |
+
"language": "python",
|
106 |
+
"name": "python3"
|
107 |
+
},
|
108 |
+
"language_info": {
|
109 |
+
"codemirror_mode": {
|
110 |
+
"name": "ipython",
|
111 |
+
"version": 3
|
112 |
+
},
|
113 |
+
"file_extension": ".py",
|
114 |
+
"mimetype": "text/x-python",
|
115 |
+
"name": "python",
|
116 |
+
"nbconvert_exporter": "python",
|
117 |
+
"pygments_lexer": "ipython3",
|
118 |
+
"version": "3.11.3"
|
119 |
+
},
|
120 |
+
"orig_nbformat": 4
|
121 |
+
},
|
122 |
+
"nbformat": 4,
|
123 |
+
"nbformat_minor": 2
|
124 |
+
}
|