Spaces:
Runtime error
Runtime error
远兮
commited on
Commit
·
d7ed536
1
Parent(s):
a9098a9
add ResponseSchema and fix output。
Browse files- parser_fix_output.ipynb +105 -0
- parser_reponse_schema.ipynb +116 -0
parser_fix_output.ipynb
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 11,
|
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 |
+
"from langchain.output_parsers import PydanticOutputParser\n",
|
13 |
+
"from pydantic import BaseModel, Field, validator\n",
|
14 |
+
"from typing import List"
|
15 |
+
]
|
16 |
+
},
|
17 |
+
{
|
18 |
+
"cell_type": "code",
|
19 |
+
"execution_count": 12,
|
20 |
+
"metadata": {},
|
21 |
+
"outputs": [],
|
22 |
+
"source": [
|
23 |
+
"class Actor(BaseModel):\n",
|
24 |
+
" name: str = Field(description=\"name of an actor\")\n",
|
25 |
+
" film_names: List[str] = Field(description=\"list of names of films they starred in\")\n",
|
26 |
+
" \n",
|
27 |
+
"actor_query = \"Generate the filmography for a random actor.\"\n",
|
28 |
+
"\n",
|
29 |
+
"parser = PydanticOutputParser(pydantic_object=Actor)"
|
30 |
+
]
|
31 |
+
},
|
32 |
+
{
|
33 |
+
"cell_type": "code",
|
34 |
+
"execution_count": 13,
|
35 |
+
"metadata": {},
|
36 |
+
"outputs": [],
|
37 |
+
"source": [
|
38 |
+
"misformatted = \"{'name': \\\"Tom Hanks\\\", \\\"film_names\\\": [\\\"Forrest Gump\\\"]}\""
|
39 |
+
]
|
40 |
+
},
|
41 |
+
{
|
42 |
+
"cell_type": "code",
|
43 |
+
"execution_count": 14,
|
44 |
+
"metadata": {},
|
45 |
+
"outputs": [],
|
46 |
+
"source": [
|
47 |
+
"# 将报解析错误。\n",
|
48 |
+
"# parser.parse(misformatted)"
|
49 |
+
]
|
50 |
+
},
|
51 |
+
{
|
52 |
+
"cell_type": "code",
|
53 |
+
"execution_count": 15,
|
54 |
+
"metadata": {},
|
55 |
+
"outputs": [],
|
56 |
+
"source": [
|
57 |
+
"from langchain.output_parsers import OutputFixingParser\n",
|
58 |
+
"\n",
|
59 |
+
"new_parser = OutputFixingParser.from_llm(parser=parser, llm=OpenAI())"
|
60 |
+
]
|
61 |
+
},
|
62 |
+
{
|
63 |
+
"cell_type": "code",
|
64 |
+
"execution_count": 16,
|
65 |
+
"metadata": {},
|
66 |
+
"outputs": [
|
67 |
+
{
|
68 |
+
"data": {
|
69 |
+
"text/plain": [
|
70 |
+
"Actor(name='Tom Hanks', film_names=['Forrest Gump'])"
|
71 |
+
]
|
72 |
+
},
|
73 |
+
"execution_count": 16,
|
74 |
+
"metadata": {},
|
75 |
+
"output_type": "execute_result"
|
76 |
+
}
|
77 |
+
],
|
78 |
+
"source": [
|
79 |
+
"new_parser.parse(misformatted)"
|
80 |
+
]
|
81 |
+
}
|
82 |
+
],
|
83 |
+
"metadata": {
|
84 |
+
"kernelspec": {
|
85 |
+
"display_name": "base",
|
86 |
+
"language": "python",
|
87 |
+
"name": "python3"
|
88 |
+
},
|
89 |
+
"language_info": {
|
90 |
+
"codemirror_mode": {
|
91 |
+
"name": "ipython",
|
92 |
+
"version": 3
|
93 |
+
},
|
94 |
+
"file_extension": ".py",
|
95 |
+
"mimetype": "text/x-python",
|
96 |
+
"name": "python",
|
97 |
+
"nbconvert_exporter": "python",
|
98 |
+
"pygments_lexer": "ipython3",
|
99 |
+
"version": "3.10.10"
|
100 |
+
},
|
101 |
+
"orig_nbformat": 4
|
102 |
+
},
|
103 |
+
"nbformat": 4,
|
104 |
+
"nbformat_minor": 2
|
105 |
+
}
|
parser_reponse_schema.ipynb
ADDED
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 52,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [],
|
8 |
+
"source": [
|
9 |
+
"from langchain.output_parsers import StructuredOutputParser, ResponseSchema\n",
|
10 |
+
"from langchain.prompts import PromptTemplate, ChatPromptTemplate, HumanMessagePromptTemplate\n",
|
11 |
+
"from langchain.llms import OpenAI\n",
|
12 |
+
"from langchain.chat_models import ChatOpenAI"
|
13 |
+
]
|
14 |
+
},
|
15 |
+
{
|
16 |
+
"cell_type": "code",
|
17 |
+
"execution_count": 53,
|
18 |
+
"metadata": {},
|
19 |
+
"outputs": [],
|
20 |
+
"source": [
|
21 |
+
"response_schemas = [\n",
|
22 |
+
" ResponseSchema(name=\"answer\", description=\"answer to the user's question\"),\n",
|
23 |
+
" ResponseSchema(name=\"source\", description=\"source used to answer the user's question, should be a website.\")\n",
|
24 |
+
"]\n",
|
25 |
+
"output_parser = StructuredOutputParser.from_response_schemas(response_schemas)"
|
26 |
+
]
|
27 |
+
},
|
28 |
+
{
|
29 |
+
"cell_type": "code",
|
30 |
+
"execution_count": 54,
|
31 |
+
"metadata": {},
|
32 |
+
"outputs": [],
|
33 |
+
"source": [
|
34 |
+
"format_instructions = output_parser.get_format_instructions()\n",
|
35 |
+
"prompt = PromptTemplate(\n",
|
36 |
+
" template=\"answer the users question as best as possible.\\n{format_instructions}\\n{question}\",\n",
|
37 |
+
" input_variables=[\"question\"],\n",
|
38 |
+
" partial_variables={\"format_instructions\": format_instructions}\n",
|
39 |
+
")"
|
40 |
+
]
|
41 |
+
},
|
42 |
+
{
|
43 |
+
"cell_type": "code",
|
44 |
+
"execution_count": 55,
|
45 |
+
"metadata": {},
|
46 |
+
"outputs": [],
|
47 |
+
"source": [
|
48 |
+
"model = OpenAI(temperature=0, n=2, best_of=2)"
|
49 |
+
]
|
50 |
+
},
|
51 |
+
{
|
52 |
+
"cell_type": "code",
|
53 |
+
"execution_count": 56,
|
54 |
+
"metadata": {},
|
55 |
+
"outputs": [],
|
56 |
+
"source": [
|
57 |
+
"_input = prompt.format_prompt(question=\"周杰伦有哪些歌?\")\n",
|
58 |
+
"output = model(_input.to_string())"
|
59 |
+
]
|
60 |
+
},
|
61 |
+
{
|
62 |
+
"cell_type": "code",
|
63 |
+
"execution_count": 57,
|
64 |
+
"metadata": {},
|
65 |
+
"outputs": [],
|
66 |
+
"source": [
|
67 |
+
"# output_parser.parse(output)"
|
68 |
+
]
|
69 |
+
},
|
70 |
+
{
|
71 |
+
"cell_type": "code",
|
72 |
+
"execution_count": 58,
|
73 |
+
"metadata": {},
|
74 |
+
"outputs": [
|
75 |
+
{
|
76 |
+
"data": {
|
77 |
+
"text/plain": [
|
78 |
+
"{'answer': '周杰伦的歌曲有《稻香》、《发如雪》、《青花瓷》、《七里香》、《简单爱》、《等你下课》、《菊花台》、《夜曲》、《不能说的秘密》、《回到过去》等。',\n",
|
79 |
+
" 'source': '百度百科 https://baike.baidu.com/item/%E5%91%A8%E6%9D%B0%E4%BC%A6/109983'}"
|
80 |
+
]
|
81 |
+
},
|
82 |
+
"execution_count": 58,
|
83 |
+
"metadata": {},
|
84 |
+
"output_type": "execute_result"
|
85 |
+
}
|
86 |
+
],
|
87 |
+
"source": [
|
88 |
+
"from langchain.output_parsers import OutputFixingParser\n",
|
89 |
+
"new_parser = OutputFixingParser.from_llm(parser=output_parser, llm=OpenAI())\n",
|
90 |
+
"new_parser.parse(output)"
|
91 |
+
]
|
92 |
+
}
|
93 |
+
],
|
94 |
+
"metadata": {
|
95 |
+
"kernelspec": {
|
96 |
+
"display_name": "base",
|
97 |
+
"language": "python",
|
98 |
+
"name": "python3"
|
99 |
+
},
|
100 |
+
"language_info": {
|
101 |
+
"codemirror_mode": {
|
102 |
+
"name": "ipython",
|
103 |
+
"version": 3
|
104 |
+
},
|
105 |
+
"file_extension": ".py",
|
106 |
+
"mimetype": "text/x-python",
|
107 |
+
"name": "python",
|
108 |
+
"nbconvert_exporter": "python",
|
109 |
+
"pygments_lexer": "ipython3",
|
110 |
+
"version": "3.10.10"
|
111 |
+
},
|
112 |
+
"orig_nbformat": 4
|
113 |
+
},
|
114 |
+
"nbformat": 4,
|
115 |
+
"nbformat_minor": 2
|
116 |
+
}
|