|
43 | 43 | "source": [ |
44 | 44 | "from langchain_core.output_parsers import CommaSeparatedListOutputParser\n", |
45 | 45 | "\n", |
46 | | - "output_parser = CommaSeparatedListOutputParser()" |
| 46 | + "csv_output_parser = CommaSeparatedListOutputParser()" |
47 | 47 | ] |
48 | 48 | }, |
49 | 49 | { |
|
66 | 66 | "source": [ |
67 | 67 | "# As discussed above, lets experiment with get_format_instructions()\n", |
68 | 68 | "\n", |
69 | | - "output_parser.get_format_instructions()" |
| 69 | + "csv_output_parser.get_format_instructions()" |
70 | 70 | ] |
71 | 71 | }, |
72 | 72 | { |
|
90 | 90 | "example_input = \"Python, DA, SQL, ML, DL\"\n", |
91 | 91 | "\n", |
92 | 92 | "# using parse() method\n", |
93 | | - "output_parser.parse(example_input)" |
| 93 | + "csv_output_parser.parse(example_input)" |
94 | 94 | ] |
95 | 95 | }, |
96 | 96 | { |
97 | 97 | "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, |
99 | 116 | "id": "67f5242f-1517-4b77-bb01-2507894a6fbe", |
100 | 117 | "metadata": {}, |
101 | 118 | "outputs": [ |
102 | 119 | { |
103 | 120 | "data": { |
104 | 121 | "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}'))])" |
107 | 123 | ] |
108 | 124 | }, |
109 | | - "execution_count": 17, |
| 125 | + "execution_count": 5, |
110 | 126 | "metadata": {}, |
111 | 127 | "output_type": "execute_result" |
112 | 128 | } |
|
119 | 135 | " (\"human\", \"Give me the ingredients for cooking {dish_name}. {output_format_instructions}\"),\n", |
120 | 136 | "])\n", |
121 | 137 | "\n", |
122 | | - "prompt_template.messages" |
| 138 | + "prompt_template" |
123 | 139 | ] |
124 | 140 | }, |
125 | 141 | { |
126 | 142 | "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", |
129 | 145 | "metadata": {}, |
130 | 146 | "outputs": [], |
131 | 147 | "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", |
135 | 187 | "\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**" |
137 | 197 | ] |
138 | 198 | }, |
139 | 199 | { |
140 | 200 | "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", |
143 | 203 | "metadata": {}, |
144 | 204 | "outputs": [], |
145 | 205 | "source": [ |
146 | | - "from langchain_openai import ChatOpenAI\n", |
| 206 | + "from langchain_core.output_parsers import JsonOutputParser\n", |
147 | 207 | "\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()" |
150 | 209 | ] |
151 | 210 | }, |
152 | 211 | { |
153 | 212 | "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", |
156 | 215 | "metadata": {}, |
157 | 216 | "outputs": [], |
158 | 217 | "source": [ |
159 | | - "chain = prompt_template | chat_model | output_parser" |
| 218 | + "json_chain = chain = prompt_template | chat_model | json_output_parser" |
160 | 219 | ] |
161 | 220 | }, |
162 | 221 | { |
163 | 222 | "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", |
166 | 225 | "metadata": {}, |
167 | 226 | "outputs": [ |
168 | 227 | { |
169 | 228 | "data": { |
170 | 229 | "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']}}" |
188 | 247 | ] |
189 | 248 | }, |
190 | | - "execution_count": 21, |
| 249 | + "execution_count": 10, |
191 | 250 | "metadata": {}, |
192 | 251 | "output_type": "execute_result" |
193 | 252 | } |
194 | 253 | ], |
195 | 254 | "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", |
197 | 256 | "\n", |
198 | 257 | "chain.invoke(input)" |
199 | 258 | ] |
|
210 | 269 | }, |
211 | 270 | { |
212 | 271 | "cell_type": "code", |
213 | | - "execution_count": 22, |
| 272 | + "execution_count": 11, |
214 | 273 | "id": "072bd02b-c06d-4d05-97b2-f62862e8dcc2", |
215 | 274 | "metadata": {}, |
216 | 275 | "outputs": [], |
|
222 | 281 | }, |
223 | 282 | { |
224 | 283 | "cell_type": "code", |
225 | | - "execution_count": 23, |
| 284 | + "execution_count": 12, |
226 | 285 | "id": "00755d45-e38a-4c09-8412-c12eb4540175", |
227 | 286 | "metadata": {}, |
228 | 287 | "outputs": [ |
229 | 288 | { |
230 | 289 | "data": { |
231 | 290 | "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!\"" |
233 | 292 | ] |
234 | 293 | }, |
235 | | - "execution_count": 23, |
| 294 | + "execution_count": 12, |
236 | 295 | "metadata": {}, |
237 | 296 | "output_type": "execute_result" |
238 | 297 | } |
|
245 | 304 | }, |
246 | 305 | { |
247 | 306 | "cell_type": "code", |
248 | | - "execution_count": 24, |
| 307 | + "execution_count": 13, |
249 | 308 | "id": "3bd3acd4-2cda-4776-a90c-968e87133d9f", |
250 | 309 | "metadata": {}, |
251 | 310 | "outputs": [], |
|
263 | 322 | }, |
264 | 323 | { |
265 | 324 | "cell_type": "code", |
266 | | - "execution_count": 25, |
| 325 | + "execution_count": 14, |
267 | 326 | "id": "c18efe6f-b8d4-4839-b8d0-8cfc01fc3587", |
268 | 327 | "metadata": {}, |
269 | 328 | "outputs": [], |
|
276 | 335 | }, |
277 | 336 | { |
278 | 337 | "cell_type": "code", |
279 | | - "execution_count": 26, |
| 338 | + "execution_count": 15, |
280 | 339 | "id": "69182c25-2ba5-4ad4-9287-41b31011880f", |
281 | 340 | "metadata": {}, |
282 | 341 | "outputs": [ |
|
286 | 345 | "datetime.datetime(1947, 8, 15, 0, 0)" |
287 | 346 | ] |
288 | 347 | }, |
289 | | - "execution_count": 26, |
| 348 | + "execution_count": 15, |
290 | 349 | "metadata": {}, |
291 | 350 | "output_type": "execute_result" |
292 | 351 | } |
|
0 commit comments