Skip to content

Commit 76ed393

Browse files
committed
Update output_parsing.ipynb
1 parent 962b9dc commit 76ed393

File tree

1 file changed

+110
-51
lines changed
  • Module 9 - GenAI (LLMs and Prompt Engineering)/6. Building Apps Powered by GenAI using LangChain/1. Introduction to LangChain/4. Output Parsing

1 file changed

+110
-51
lines changed

Module 9 - GenAI (LLMs and Prompt Engineering)/6. Building Apps Powered by GenAI using LangChain/1. Introduction to LangChain/4. Output Parsing/output_parsing.ipynb

Lines changed: 110 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"source": [
4444
"from langchain_core.output_parsers import CommaSeparatedListOutputParser\n",
4545
"\n",
46-
"output_parser = CommaSeparatedListOutputParser()"
46+
"csv_output_parser = CommaSeparatedListOutputParser()"
4747
]
4848
},
4949
{
@@ -66,7 +66,7 @@
6666
"source": [
6767
"# As discussed above, lets experiment with get_format_instructions()\n",
6868
"\n",
69-
"output_parser.get_format_instructions()"
69+
"csv_output_parser.get_format_instructions()"
7070
]
7171
},
7272
{
@@ -90,23 +90,39 @@
9090
"example_input = \"Python, DA, SQL, ML, DL\"\n",
9191
"\n",
9292
"# using parse() method\n",
93-
"output_parser.parse(example_input)"
93+
"csv_output_parser.parse(example_input)"
9494
]
9595
},
9696
{
9797
"cell_type": "code",
98-
"execution_count": 17,
98+
"execution_count": 4,
99+
"id": "653b4ff2-bde5-408b-afd6-14fd804399d0",
100+
"metadata": {},
101+
"outputs": [],
102+
"source": [
103+
"from langchain_openai import ChatOpenAI\n",
104+
"\n",
105+
"# Read the API Key\n",
106+
"f = open('keys/.openai_api_key.txt')\n",
107+
"OPENAI_API_KEY = f.read()\n",
108+
"\n",
109+
"# Set the OpenAI Key and initialize a ChatModel\n",
110+
"chat_model = ChatOpenAI(openai_api_key=OPENAI_API_KEY)"
111+
]
112+
},
113+
{
114+
"cell_type": "code",
115+
"execution_count": 5,
99116
"id": "67f5242f-1517-4b77-bb01-2507894a6fbe",
100117
"metadata": {},
101118
"outputs": [
102119
{
103120
"data": {
104121
"text/plain": [
105-
"[SystemMessagePromptTemplate(prompt=PromptTemplate(input_variables=[], template='You are a helpful AI Chef.')),\n",
106-
" HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=['dish_name', 'output_format_instructions'], template='Give me the ingredients for cooking {dish_name}. {output_format_instructions}'))]"
122+
"ChatPromptTemplate(input_variables=['dish_name', 'output_format_instructions'], messages=[SystemMessagePromptTemplate(prompt=PromptTemplate(input_variables=[], template='You are a helpful AI Chef.')), HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=['dish_name', 'output_format_instructions'], template='Give me the ingredients for cooking {dish_name}. {output_format_instructions}'))])"
107123
]
108124
},
109-
"execution_count": 17,
125+
"execution_count": 5,
110126
"metadata": {},
111127
"output_type": "execute_result"
112128
}
@@ -119,81 +135,124 @@
119135
" (\"human\", \"Give me the ingredients for cooking {dish_name}. {output_format_instructions}\"),\n",
120136
"])\n",
121137
"\n",
122-
"prompt_template.messages"
138+
"prompt_template"
123139
]
124140
},
125141
{
126142
"cell_type": "code",
127-
"execution_count": 18,
128-
"id": "653b4ff2-bde5-408b-afd6-14fd804399d0",
143+
"execution_count": 6,
144+
"id": "d5a6a423-ae97-4536-a898-ae9322478d22",
129145
"metadata": {},
130146
"outputs": [],
131147
"source": [
132-
"# Setup API Key\n",
133-
"\n",
134-
"f = open('keys/.openai_api_key.txt')\n",
148+
"chain = prompt_template | chat_model | csv_output_parser"
149+
]
150+
},
151+
{
152+
"cell_type": "code",
153+
"execution_count": 7,
154+
"id": "f7fe8e3d-f2b8-4008-ac02-bc00f6bff39d",
155+
"metadata": {},
156+
"outputs": [
157+
{
158+
"data": {
159+
"text/plain": [
160+
"['paneer',\n",
161+
" 'basmati rice',\n",
162+
" 'onions',\n",
163+
" 'tomatoes',\n",
164+
" 'yogurt',\n",
165+
" 'ginger',\n",
166+
" 'garlic',\n",
167+
" 'green chilies',\n",
168+
" 'spices (such as cumin',\n",
169+
" 'coriander',\n",
170+
" 'turmeric',\n",
171+
" 'garam masala)',\n",
172+
" 'ghee',\n",
173+
" 'vegetable oil',\n",
174+
" 'fresh coriander leaves',\n",
175+
" 'mint leaves',\n",
176+
" 'saffron',\n",
177+
" 'water']"
178+
]
179+
},
180+
"execution_count": 7,
181+
"metadata": {},
182+
"output_type": "execute_result"
183+
}
184+
],
185+
"source": [
186+
"input = {\"dish_name\": \"paneer biryani\", \"output_format_instructions\": csv_output_parser.get_format_instructions()}\n",
135187
"\n",
136-
"OPENAI_API_KEY = f.read()"
188+
"chain.invoke(input)"
189+
]
190+
},
191+
{
192+
"cell_type": "markdown",
193+
"id": "14516d5e-9cce-46aa-8c3d-5ba65d12f4ef",
194+
"metadata": {},
195+
"source": [
196+
"## **JSONParser**"
137197
]
138198
},
139199
{
140200
"cell_type": "code",
141-
"execution_count": 19,
142-
"id": "b734ae2c-bb0c-40a7-9e67-586eede34f7a",
201+
"execution_count": 8,
202+
"id": "f999b0d3-839b-4b60-ae6a-ebf8390008c7",
143203
"metadata": {},
144204
"outputs": [],
145205
"source": [
146-
"from langchain_openai import ChatOpenAI\n",
206+
"from langchain_core.output_parsers import JsonOutputParser\n",
147207
"\n",
148-
"# Set the OpenAI Key and initialize a ChatModel\n",
149-
"chat_model = ChatOpenAI(openai_api_key=OPENAI_API_KEY)"
208+
"json_output_parser = JsonOutputParser()"
150209
]
151210
},
152211
{
153212
"cell_type": "code",
154-
"execution_count": 20,
155-
"id": "d5a6a423-ae97-4536-a898-ae9322478d22",
213+
"execution_count": 9,
214+
"id": "2c3e7a50-9db7-4941-9c94-da1ac833cda1",
156215
"metadata": {},
157216
"outputs": [],
158217
"source": [
159-
"chain = prompt_template | chat_model | output_parser"
218+
"json_chain = chain = prompt_template | chat_model | json_output_parser"
160219
]
161220
},
162221
{
163222
"cell_type": "code",
164-
"execution_count": 21,
165-
"id": "f7fe8e3d-f2b8-4008-ac02-bc00f6bff39d",
223+
"execution_count": 10,
224+
"id": "7ef84474-2fa0-42b4-88b7-990662dad134",
166225
"metadata": {},
167226
"outputs": [
168227
{
169228
"data": {
170229
"text/plain": [
171-
"['paneer',\n",
172-
" 'basmati rice',\n",
173-
" 'yogurt',\n",
174-
" 'onions',\n",
175-
" 'tomatoes',\n",
176-
" 'ginger',\n",
177-
" 'garlic',\n",
178-
" 'green chilies',\n",
179-
" 'biryani masala',\n",
180-
" 'ghee',\n",
181-
" 'mint leaves',\n",
182-
" 'coriander leaves',\n",
183-
" 'saffron',\n",
184-
" 'milk',\n",
185-
" 'salt',\n",
186-
" 'oil',\n",
187-
" 'spices']"
230+
"{'paneerBiryani': {'ingredients': ['1 cup basmati rice',\n",
231+
" '200g paneer, cut into cubes',\n",
232+
" '1 onion, thinly sliced',\n",
233+
" '1 tomato, chopped',\n",
234+
" '1/2 cup yogurt',\n",
235+
" '1/2 cup fried onions',\n",
236+
" '1/4 cup chopped coriander leaves',\n",
237+
" '1/4 cup chopped mint leaves',\n",
238+
" '2 tablespoons biryani masala',\n",
239+
" '1 tablespoon ginger-garlic paste',\n",
240+
" '1 teaspoon red chili powder',\n",
241+
" '1/2 teaspoon turmeric powder',\n",
242+
" '1/2 teaspoon garam masala',\n",
243+
" '2 tablespoons ghee',\n",
244+
" '2 tablespoons oil',\n",
245+
" 'Salt to taste',\n",
246+
" 'Water']}}"
188247
]
189248
},
190-
"execution_count": 21,
249+
"execution_count": 10,
191250
"metadata": {},
192251
"output_type": "execute_result"
193252
}
194253
],
195254
"source": [
196-
"input = {\"dish_name\": \"paneer biryani\", \"output_format_instructions\": output_parser.get_format_instructions()}\n",
255+
"input = {\"dish_name\": \"paneer biryani\", \"output_format_instructions\": json_output_parser.get_format_instructions()}\n",
197256
"\n",
198257
"chain.invoke(input)"
199258
]
@@ -210,7 +269,7 @@
210269
},
211270
{
212271
"cell_type": "code",
213-
"execution_count": 22,
272+
"execution_count": 11,
214273
"id": "072bd02b-c06d-4d05-97b2-f62862e8dcc2",
215274
"metadata": {},
216275
"outputs": [],
@@ -222,17 +281,17 @@
222281
},
223282
{
224283
"cell_type": "code",
225-
"execution_count": 23,
284+
"execution_count": 12,
226285
"id": "00755d45-e38a-4c09-8412-c12eb4540175",
227286
"metadata": {},
228287
"outputs": [
229288
{
230289
"data": {
231290
"text/plain": [
232-
"\"Write a datetime string that matches the following pattern: '%Y-%m-%dT%H:%M:%S.%fZ'.\\n\\nExamples: 0789-09-27T20:15:58.070587Z, 0146-06-12T19:06:59.697461Z, 1178-01-28T21:50:31.033531Z\\n\\nReturn ONLY this string, no other words!\""
291+
"\"Write a datetime string that matches the following pattern: '%Y-%m-%dT%H:%M:%S.%fZ'.\\n\\nExamples: 1892-01-30T12:13:48.068611Z, 0561-05-13T02:27:55.160011Z, 1699-05-11T04:40:29.696518Z\\n\\nReturn ONLY this string, no other words!\""
233292
]
234293
},
235-
"execution_count": 23,
294+
"execution_count": 12,
236295
"metadata": {},
237296
"output_type": "execute_result"
238297
}
@@ -245,7 +304,7 @@
245304
},
246305
{
247306
"cell_type": "code",
248-
"execution_count": 24,
307+
"execution_count": 13,
249308
"id": "3bd3acd4-2cda-4776-a90c-968e87133d9f",
250309
"metadata": {},
251310
"outputs": [],
@@ -263,7 +322,7 @@
263322
},
264323
{
265324
"cell_type": "code",
266-
"execution_count": 25,
325+
"execution_count": 14,
267326
"id": "c18efe6f-b8d4-4839-b8d0-8cfc01fc3587",
268327
"metadata": {},
269328
"outputs": [],
@@ -276,7 +335,7 @@
276335
},
277336
{
278337
"cell_type": "code",
279-
"execution_count": 26,
338+
"execution_count": 15,
280339
"id": "69182c25-2ba5-4ad4-9287-41b31011880f",
281340
"metadata": {},
282341
"outputs": [
@@ -286,7 +345,7 @@
286345
"datetime.datetime(1947, 8, 15, 0, 0)"
287346
]
288347
},
289-
"execution_count": 26,
348+
"execution_count": 15,
290349
"metadata": {},
291350
"output_type": "execute_result"
292351
}

0 commit comments

Comments
 (0)