Skip to content

Commit e45f7e4

Browse files
Harrison/few shot yaml (#682)
Co-authored-by: vintro <[email protected]>
1 parent a2eeaf3 commit e45f7e4

File tree

4 files changed

+133
-12
lines changed

4 files changed

+133
-12
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- input: happy
2+
output: sad
3+
- input: tall
4+
output: short
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
_type: few_shot
2+
input_variables:
3+
["adjective"]
4+
prefix:
5+
Write antonyms for the following words.
6+
example_prompt:
7+
input_variables:
8+
["input", "output"]
9+
template:
10+
"Input: {input}\nOutput: {output}"
11+
examples:
12+
examples.yaml
13+
suffix:
14+
"Input: {adjective}\nOutput:"

docs/modules/prompts/examples/prompt_serialization.ipynb

Lines changed: 106 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,35 @@
225225
"!cat examples.json"
226226
]
227227
},
228+
{
229+
"cell_type": "markdown",
230+
"id": "d3052850",
231+
"metadata": {},
232+
"source": [
233+
"And here is what the same examples stored as yaml might look like."
234+
]
235+
},
236+
{
237+
"cell_type": "code",
238+
"execution_count": 9,
239+
"id": "901385d1",
240+
"metadata": {},
241+
"outputs": [
242+
{
243+
"name": "stdout",
244+
"output_type": "stream",
245+
"text": [
246+
"- input: happy\r\n",
247+
" output: sad\r\n",
248+
"- input: tall\r\n",
249+
" output: short\r\n"
250+
]
251+
}
252+
],
253+
"source": [
254+
"!cat examples.yaml"
255+
]
256+
},
228257
{
229258
"cell_type": "markdown",
230259
"id": "8e300335",
@@ -236,7 +265,7 @@
236265
},
237266
{
238267
"cell_type": "code",
239-
"execution_count": 9,
268+
"execution_count": 10,
240269
"id": "e2bec0fc",
241270
"metadata": {},
242271
"outputs": [
@@ -267,7 +296,7 @@
267296
},
268297
{
269298
"cell_type": "code",
270-
"execution_count": 10,
299+
"execution_count": 11,
271300
"id": "98c8f356",
272301
"metadata": {},
273302
"outputs": [
@@ -293,6 +322,73 @@
293322
"print(prompt.format(adjective=\"funny\"))"
294323
]
295324
},
325+
{
326+
"cell_type": "markdown",
327+
"id": "13620324",
328+
"metadata": {},
329+
"source": [
330+
"The same would work if you loaded examples from the yaml file."
331+
]
332+
},
333+
{
334+
"cell_type": "code",
335+
"execution_count": 12,
336+
"id": "831e5e4a",
337+
"metadata": {},
338+
"outputs": [
339+
{
340+
"name": "stdout",
341+
"output_type": "stream",
342+
"text": [
343+
"_type: few_shot\r\n",
344+
"input_variables:\r\n",
345+
" [\"adjective\"]\r\n",
346+
"prefix: \r\n",
347+
" Write antonyms for the following words.\r\n",
348+
"example_prompt:\r\n",
349+
" input_variables:\r\n",
350+
" [\"input\", \"output\"]\r\n",
351+
" template:\r\n",
352+
" \"Input: {input}\\nOutput: {output}\"\r\n",
353+
"examples:\r\n",
354+
" examples.yaml\r\n",
355+
"suffix:\r\n",
356+
" \"Input: {adjective}\\nOutput:\"\r\n"
357+
]
358+
}
359+
],
360+
"source": [
361+
"!cat few_shot_prompt_yaml_examples.yaml"
362+
]
363+
},
364+
{
365+
"cell_type": "code",
366+
"execution_count": 13,
367+
"id": "6f0a7eaa",
368+
"metadata": {},
369+
"outputs": [
370+
{
371+
"name": "stdout",
372+
"output_type": "stream",
373+
"text": [
374+
"Write antonyms for the following words.\n",
375+
"\n",
376+
"Input: happy\n",
377+
"Output: sad\n",
378+
"\n",
379+
"Input: tall\n",
380+
"Output: short\n",
381+
"\n",
382+
"Input: funny\n",
383+
"Output:\n"
384+
]
385+
}
386+
],
387+
"source": [
388+
"prompt = load_prompt(\"few_shot_prompt_yaml_examples.yaml\")\n",
389+
"print(prompt.format(adjective=\"funny\"))"
390+
]
391+
},
296392
{
297393
"cell_type": "markdown",
298394
"id": "4870aa9d",
@@ -304,7 +400,7 @@
304400
},
305401
{
306402
"cell_type": "code",
307-
"execution_count": 11,
403+
"execution_count": 14,
308404
"id": "9d996a86",
309405
"metadata": {},
310406
"outputs": [
@@ -332,7 +428,7 @@
332428
},
333429
{
334430
"cell_type": "code",
335-
"execution_count": 12,
431+
"execution_count": 15,
336432
"id": "dd2c10bb",
337433
"metadata": {},
338434
"outputs": [
@@ -369,7 +465,7 @@
369465
},
370466
{
371467
"cell_type": "code",
372-
"execution_count": 13,
468+
"execution_count": 16,
373469
"id": "6cd781ef",
374470
"metadata": {},
375471
"outputs": [
@@ -400,7 +496,7 @@
400496
},
401497
{
402498
"cell_type": "code",
403-
"execution_count": 14,
499+
"execution_count": 17,
404500
"id": "533ab8a7",
405501
"metadata": {},
406502
"outputs": [
@@ -437,7 +533,7 @@
437533
},
438534
{
439535
"cell_type": "code",
440-
"execution_count": 15,
536+
"execution_count": 18,
441537
"id": "0b6dd7b8",
442538
"metadata": {},
443539
"outputs": [
@@ -458,7 +554,7 @@
458554
},
459555
{
460556
"cell_type": "code",
461-
"execution_count": 16,
557+
"execution_count": 19,
462558
"id": "76a1065d",
463559
"metadata": {},
464560
"outputs": [
@@ -483,7 +579,7 @@
483579
},
484580
{
485581
"cell_type": "code",
486-
"execution_count": 17,
582+
"execution_count": 20,
487583
"id": "744d275d",
488584
"metadata": {},
489585
"outputs": [
@@ -530,7 +626,7 @@
530626
},
531627
"vscode": {
532628
"interpreter": {
533-
"hash": "b1677b440931f40d89ef8be7bf03acb108ce003de0ac9b18e8d43753ea2e7103"
629+
"hash": "8eb71adebe840dca1185e9603533462bc47eb1b1a73bf7dab2d0a8a4c932882e"
534630
}
535631
}
536632
},

langchain/prompts/loading.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,17 @@ def _load_examples(config: dict) -> dict:
5252
pass
5353
elif isinstance(config["examples"], str):
5454
with open(config["examples"]) as f:
55-
examples = json.load(f)
55+
if config["examples"].endswith(".json"):
56+
examples = json.load(f)
57+
elif config["examples"].endswith((".yaml", ".yml")):
58+
examples = yaml.safe_load(f)
59+
else:
60+
raise ValueError(
61+
"Invalid file format. Only json or yaml formats are supported."
62+
)
5663
config["examples"] = examples
5764
else:
58-
raise ValueError
65+
raise ValueError("Invalid examples format. Only list or string are supported.")
5966
return config
6067

6168

0 commit comments

Comments
 (0)