远兮 commited on
Commit
9acaed2
·
1 Parent(s): e27205e

add PydanticOutputParser

Browse files
Files changed (1) hide show
  1. parser_pydantic_output.ipynb +151 -0
parser_pydantic_output.ipynb ADDED
@@ -0,0 +1,151 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 51,
6
+ "metadata": {},
7
+ "outputs": [],
8
+ "source": [
9
+ "from langchain.prompts import PromptTemplate, ChatPromptTemplate, HumanMessagePromptTemplate\n",
10
+ "from langchain.llms import OpenAI\n",
11
+ "from langchain.chat_models import ChatOpenAI\n",
12
+ "\n",
13
+ "from langchain.output_parsers import PydanticOutputParser\n",
14
+ "from pydantic import BaseModel, Field, validator\n",
15
+ "from typing import List"
16
+ ]
17
+ },
18
+ {
19
+ "cell_type": "code",
20
+ "execution_count": 52,
21
+ "metadata": {},
22
+ "outputs": [],
23
+ "source": [
24
+ "model_name = 'text-davinci-003'\n",
25
+ "temperature = 0.7\n",
26
+ "model = OpenAI(model_name=model_name, temperature=temperature)"
27
+ ]
28
+ },
29
+ {
30
+ "cell_type": "code",
31
+ "execution_count": 53,
32
+ "metadata": {},
33
+ "outputs": [],
34
+ "source": [
35
+ "# Define your desired data structure.\n",
36
+ "class Joke(BaseModel):\n",
37
+ " setup: str = Field(description=\"question to set up a joke\")\n",
38
+ " punchline: str = Field(description=\"answer to resolve the joke\")\n",
39
+ " \n",
40
+ " # You can add custom validation logic easily with Pydantic.\n",
41
+ " @validator('setup')\n",
42
+ " def question_ends_with_question_mark(cls, field):\n",
43
+ " if field[-1] != '?' and field[-1] != '?':\n",
44
+ " raise ValueError(\"Badly formed question!\")\n",
45
+ " return field"
46
+ ]
47
+ },
48
+ {
49
+ "cell_type": "code",
50
+ "execution_count": 54,
51
+ "metadata": {},
52
+ "outputs": [],
53
+ "source": [
54
+ "# Set up a parser + inject instructions into the prompt template.\n",
55
+ "parser = PydanticOutputParser(pydantic_object=Joke)"
56
+ ]
57
+ },
58
+ {
59
+ "cell_type": "code",
60
+ "execution_count": 55,
61
+ "metadata": {},
62
+ "outputs": [],
63
+ "source": [
64
+ "prompt = PromptTemplate(\n",
65
+ " template=\"Answer the user query.\\n{format_instructions}\\n{query}\\n\",\n",
66
+ " input_variables=[\"query\"],\n",
67
+ " partial_variables={\"format_instructions\": parser.get_format_instructions()}\n",
68
+ ")"
69
+ ]
70
+ },
71
+ {
72
+ "cell_type": "code",
73
+ "execution_count": 56,
74
+ "metadata": {},
75
+ "outputs": [
76
+ {
77
+ "name": "stdout",
78
+ "output_type": "stream",
79
+ "text": [
80
+ "prompt: text='Answer the user query.\\nThe output should be formatted as a JSON instance that conforms to the JSON schema below.\\n\\nAs an example, for the schema {\"properties\": {\"foo\": {\"title\": \"Foo\", \"description\": \"a list of strings\", \"type\": \"array\", \"items\": {\"type\": \"string\"}}}, \"required\": [\"foo\"]}}\\nthe object {\"foo\": [\"bar\", \"baz\"]} is a well-formatted instance of the schema. The object {\"properties\": {\"foo\": [\"bar\", \"baz\"]}} is not well-formatted.\\n\\nHere is the output schema:\\n```\\n{\"properties\": {\"setup\": {\"title\": \"Setup\", \"description\": \"question to set up a joke\", \"type\": \"string\"}, \"punchline\": {\"title\": \"Punchline\", \"description\": \"answer to resolve the joke\", \"type\": \"string\"}}, \"required\": [\"setup\", \"punchline\"]}\\n```\\n用中文回答,讲个笑话。\\n'\n"
81
+ ]
82
+ }
83
+ ],
84
+ "source": [
85
+ "# And a query intented to prompt a language model to populate the data structure.\n",
86
+ "joke_query = \"用中文回答,讲个笑话。\"\n",
87
+ "_input = prompt.format_prompt(query=joke_query)\n",
88
+ "print(\"prompt:\", _input)"
89
+ ]
90
+ },
91
+ {
92
+ "cell_type": "code",
93
+ "execution_count": 57,
94
+ "metadata": {},
95
+ "outputs": [],
96
+ "source": [
97
+ "output = model(_input.to_string())"
98
+ ]
99
+ },
100
+ {
101
+ "cell_type": "code",
102
+ "execution_count": 80,
103
+ "metadata": {},
104
+ "outputs": [
105
+ {
106
+ "name": "stdout",
107
+ "output_type": "stream",
108
+ "text": [
109
+ "0 setup='为什么大象不喜欢穿衣服?' punchline='因为衣服太小!'\n",
110
+ "1 setup='为什么乌龟不能唱歌?' punchline='因为它没有嗓子!'\n",
111
+ "2 setup='什么东西可以排列成一半?' punchline='一个鸡蛋!'\n",
112
+ "3 setup='为什么老虎不吃素?' punchline='因为他不能把萝卜拔得出土。'\n",
113
+ "4 setup='什么植物可以在水里生长?' punchline='水芹菜!'\n",
114
+ "5 setup='什么可以把一只老虎变成猫?' punchline='把它放进冰箱!'\n",
115
+ "6 setup='为什么恐龙不能坐飞机?' punchline='因为它没有票!'\n",
116
+ "7 setup='为什么熊总喜欢去森林游玩?' punchline='因为他想要一个熊朋友!'\n",
117
+ "8 setup='什么东西可以把一座山搬走?' punchline='风'\n",
118
+ "9 setup='为什么有些人把面包放进微波炉里?' punchline='因为他们想把它变成“烤面包”。'\n"
119
+ ]
120
+ }
121
+ ],
122
+ "source": [
123
+ "for i in range(10):\n",
124
+ " output = model(_input.to_string())\n",
125
+ " print(i, parser.parse(output))"
126
+ ]
127
+ }
128
+ ],
129
+ "metadata": {
130
+ "kernelspec": {
131
+ "display_name": "base",
132
+ "language": "python",
133
+ "name": "python3"
134
+ },
135
+ "language_info": {
136
+ "codemirror_mode": {
137
+ "name": "ipython",
138
+ "version": 3
139
+ },
140
+ "file_extension": ".py",
141
+ "mimetype": "text/x-python",
142
+ "name": "python",
143
+ "nbconvert_exporter": "python",
144
+ "pygments_lexer": "ipython3",
145
+ "version": "3.10.10"
146
+ },
147
+ "orig_nbformat": 4
148
+ },
149
+ "nbformat": 4,
150
+ "nbformat_minor": 2
151
+ }