Got working in Interface gradio with simple quest.
Browse files- demo-tools-1.ipynb +126 -108
demo-tools-1.ipynb
CHANGED
@@ -2,7 +2,7 @@
|
|
2 |
"cells": [
|
3 |
{
|
4 |
"cell_type": "code",
|
5 |
-
"execution_count":
|
6 |
"metadata": {},
|
7 |
"outputs": [],
|
8 |
"source": [
|
@@ -35,13 +35,14 @@
|
|
35 |
},
|
36 |
{
|
37 |
"cell_type": "code",
|
38 |
-
"execution_count":
|
39 |
"metadata": {},
|
40 |
"outputs": [],
|
41 |
"source": [
|
42 |
"import requests\n",
|
43 |
"from bs4 import BeautifulSoup\n",
|
44 |
"import json\n",
|
|
|
45 |
"\n",
|
46 |
"def remove_keys(dictionary: dict, keyList: list):\n",
|
47 |
" for key in keyList: \n",
|
@@ -49,22 +50,25 @@
|
|
49 |
" del dictionary[key] \n",
|
50 |
"\n",
|
51 |
"def get_recipe_as_json(url: str) -> dict: \n",
|
|
|
52 |
" url = url.replace(\"'\", \"\").replace('\"', '')\n",
|
|
|
|
|
|
|
|
|
53 |
" html = requests.get(url).text \n",
|
54 |
" soup = BeautifulSoup(html)\n",
|
55 |
" script = soup.find_all(\"script\", {\"id\": \"allrecipes-schema_1-0\"})\n",
|
56 |
"\n",
|
57 |
" recipeDict = json.loads(script[0].text)[0]\n",
|
58 |
" remove_keys(recipeDict, ['review', 'image', 'mainEntityOfPage', 'publisher'])\n",
|
59 |
-
" # del recipeDict['review']\n",
|
60 |
-
" # del recipeDict['image']\n",
|
61 |
-
" # del recipeDict['mainEntityOfPage']\n",
|
62 |
-
" # del recipeDict['publisher']\n",
|
63 |
" \n",
|
64 |
" return recipeDict\n",
|
65 |
"\n",
|
66 |
"# url = \"https://www.allrecipes.com/recipe/212498/easy-chicken-and-broccoli-alfredo/\"\n",
|
67 |
-
"# obj = get_recipe_as_json(url)"
|
|
|
|
|
68 |
]
|
69 |
},
|
70 |
{
|
@@ -76,7 +80,7 @@
|
|
76 |
},
|
77 |
{
|
78 |
"cell_type": "code",
|
79 |
-
"execution_count":
|
80 |
"metadata": {},
|
81 |
"outputs": [],
|
82 |
"source": [
|
@@ -177,16 +181,16 @@
|
|
177 |
},
|
178 |
{
|
179 |
"cell_type": "code",
|
180 |
-
"execution_count":
|
181 |
"metadata": {},
|
182 |
"outputs": [],
|
183 |
"source": [
|
184 |
"# Tools \n",
|
185 |
"from langchain.agents import Tool\n",
|
186 |
-
"
|
187 |
"\n",
|
188 |
"# Chicken functions\n",
|
189 |
-
"def list_chicken_recipes(query: str):
|
190 |
" return chickenDishList\n",
|
191 |
"\n",
|
192 |
"list_chicken_recipes_tool = Tool(name='Chicken Recipes tool', func= list_chicken_recipes, description=\"This tools lists the available Chicken Recipes it returns a list of recipes with a TITLE and a URL where you can fetch the recipe.\")\n",
|
@@ -199,7 +203,7 @@
|
|
199 |
" description=\"This tools lists the available Dessert Recipes it returns a list of recipes with a TITLE and a URL where you can fetch the recipe.\")\n",
|
200 |
"\n",
|
201 |
"# Recipe fetcher functions\n",
|
202 |
-
"def get_recipe(url: str):
|
203 |
" return get_recipe_as_json(url)\n",
|
204 |
"\n",
|
205 |
"get_recipe_as_json_tool = Tool(name='Get a Recipe tool', func=get_recipe, description=\"\"\"\n",
|
@@ -213,53 +217,34 @@
|
|
213 |
},
|
214 |
{
|
215 |
"cell_type": "code",
|
216 |
-
"execution_count":
|
217 |
"metadata": {},
|
218 |
-
"outputs": [
|
219 |
-
{
|
220 |
-
"name": "stdout",
|
221 |
-
"output_type": "stream",
|
222 |
-
"text": [
|
223 |
-
"Chicken dishes:\n",
|
224 |
-
"Crispy Roasted Chicken\n",
|
225 |
-
"Roasted Spatchcocked Chicken With Potatoes\n",
|
226 |
-
"Easy Baked Chicken Thighs\n",
|
227 |
-
"Crispy Baked Chicken Thighs\n",
|
228 |
-
"Crispy and Tender Baked Chicken Thighs\n",
|
229 |
-
"Million Dollar Chicken\n",
|
230 |
-
"Simple Whole Roasted Chicken\n",
|
231 |
-
"Beer Can Chicken\n",
|
232 |
-
"Air Fryer Chicken Thighs\n",
|
233 |
-
"Happy Roast Chicken\n",
|
234 |
-
"\n",
|
235 |
-
"Desserts:\n",
|
236 |
-
"Chocolate Snack Cake\n",
|
237 |
-
"Charred Spiced Pears with Smoky Vanilla Cherry Sauce\n",
|
238 |
-
"Meringue Topped Banana Pudding\n",
|
239 |
-
"White Chocolate Cinnamon Toast Crunch Bars\n",
|
240 |
-
"Plum Cobbler for Two\n",
|
241 |
-
"Pumpkin Cheesecake Cookies\n",
|
242 |
-
"Chocolate Whipped Cottage Cheese\n",
|
243 |
-
"Nutella Ice Cream\n",
|
244 |
-
"3-Ingredient Banana Oatmeal Cookies\n",
|
245 |
-
"Caramel Apple Pie Cookies\n"
|
246 |
-
]
|
247 |
-
}
|
248 |
-
],
|
249 |
"source": [
|
250 |
"print('Chicken dishes:')\n",
|
251 |
"for i in chickenDishList:\n",
|
252 |
-
"
|
253 |
" \n",
|
254 |
-
"
|
255 |
"print('Desserts:')\n",
|
256 |
"for i in dessertList:\n",
|
257 |
" print(i['title'])"
|
258 |
]
|
259 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
260 |
{
|
261 |
"cell_type": "code",
|
262 |
-
"execution_count":
|
263 |
"metadata": {},
|
264 |
"outputs": [
|
265 |
{
|
@@ -269,8 +254,16 @@
|
|
269 |
"\n",
|
270 |
"\n",
|
271 |
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
272 |
-
"\u001b[32;1m\u001b[1;
|
273 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
274 |
"\n",
|
275 |
"\u001b[1m> Finished chain.\u001b[0m\n"
|
276 |
]
|
@@ -281,6 +274,7 @@
|
|
281 |
"from langchain.agents import initialize_agent\n",
|
282 |
"from langchain.llms import OpenAI\n",
|
283 |
"from langchain.chat_models import ChatOpenAI\n",
|
|
|
284 |
"\n",
|
285 |
"from langchain.agents import initialize_agent\n",
|
286 |
"llm = ChatOpenAI(temperature=0, model_name='gpt-4') # 'gpt-3.5-turbo'\n",
|
@@ -293,58 +287,97 @@
|
|
293 |
" max_iterations=7, \n",
|
294 |
" return_intermediate_steps=True\n",
|
295 |
")\n",
|
296 |
-
"system = \"SYSTEM: If the answer is not in the tools or context passed to you then don't answer. If you don't know the answer then say so. QUESTION: \" \n",
|
297 |
-
"#agent.run(\"Can you show tell me what ingredients I need for the first baked chicken recipe? \")\n",
|
298 |
-
"#agent.run(\"Can you show tell me what ingredients I need for the last baked chicken recipe? \")\n",
|
299 |
-
"#agent.run(\"What is the best baked chicken recipe? Please look across all recipes with the word 'baked' in the title\") # There are 3 baked chicken recipes\n",
|
300 |
-
"#agent.run(\"Is there a Chicken recipe that's prepared with an alchohol? And if so how long does it take in total from start time to finish?\")\n",
|
301 |
-
"#agent.run(\"Which is healthier the Cheesecake or the beer chicken? Please explain how you got to your answer.\")\n",
|
302 |
-
"\n",
|
303 |
-
"#agent.run(\"What tools do I need for the Nutella Ice Cream?\")\n",
|
304 |
-
"#agent.run(\"My bowl is broken, can I still make Nutella Ice Cream?\")\n",
|
305 |
"\n",
|
306 |
-
"
|
307 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
308 |
"\n",
|
309 |
-
"
|
310 |
-
"#print(print(response[\"intermediate_steps\"]))\n",
|
311 |
-
"\n"
|
312 |
]
|
313 |
},
|
314 |
{
|
315 |
"cell_type": "code",
|
316 |
-
"execution_count":
|
317 |
"metadata": {},
|
318 |
"outputs": [
|
319 |
{
|
320 |
"name": "stdout",
|
321 |
"output_type": "stream",
|
322 |
"text": [
|
323 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
324 |
]
|
325 |
}
|
326 |
],
|
327 |
"source": [
|
328 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
329 |
]
|
330 |
},
|
331 |
{
|
332 |
"cell_type": "markdown",
|
333 |
"metadata": {},
|
334 |
"source": [
|
335 |
-
"# UI"
|
336 |
]
|
337 |
},
|
338 |
{
|
339 |
"cell_type": "code",
|
340 |
-
"execution_count":
|
341 |
"metadata": {},
|
342 |
"outputs": [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
343 |
{
|
344 |
"name": "stdout",
|
345 |
"output_type": "stream",
|
346 |
"text": [
|
347 |
-
"Running on local URL: http://127.0.0.1:
|
348 |
"\n",
|
349 |
"To create a public link, set `share=True` in `launch()`.\n"
|
350 |
]
|
@@ -352,7 +385,7 @@
|
|
352 |
{
|
353 |
"data": {
|
354 |
"text/html": [
|
355 |
-
"<div><iframe src=\"http://127.0.0.1:
|
356 |
],
|
357 |
"text/plain": [
|
358 |
"<IPython.core.display.HTML object>"
|
@@ -365,7 +398,7 @@
|
|
365 |
"data": {
|
366 |
"text/plain": []
|
367 |
},
|
368 |
-
"execution_count":
|
369 |
"metadata": {},
|
370 |
"output_type": "execute_result"
|
371 |
},
|
@@ -376,22 +409,8 @@
|
|
376 |
"\n",
|
377 |
"\n",
|
378 |
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
379 |
-
"\u001b[32;1m\u001b[1;
|
380 |
-
"
|
381 |
-
"Action Input: Ice cream\u001b[0m\n",
|
382 |
-
"Observation: \u001b[33;1m\u001b[1;3m[{'title': 'Chocolate Snack Cake', 'url': 'https://www.allrecipes.com/chocolate-snack-cake-recipe-8350343'}, {'title': 'Charred Spiced Pears with Smoky Vanilla Cherry Sauce', 'url': 'https://www.allrecipes.com/charred-spiced-pears-with-smoky-vanilla-cherry-sauce-recipe-8347080'}, {'title': 'Meringue Topped Banana Pudding', 'url': 'https://www.allrecipes.com/meringue-topped-banana-pudding-recipe-8347040'}, {'title': 'White Chocolate Cinnamon Toast Crunch Bars', 'url': 'https://www.allrecipes.com/white-chocolate-cinnamon-toast-crunch-bars-recipe-7556790'}, {'title': 'Plum Cobbler for Two', 'url': 'https://www.allrecipes.com/plum-cobbler-for-two-recipe-8304143'}, {'title': 'Pumpkin Cheesecake Cookies', 'url': 'https://www.allrecipes.com/pumpkin-cheesecake-cookies-recipe-7972485'}, {'title': 'Chocolate Whipped Cottage Cheese', 'url': 'https://www.allrecipes.com/chocolate-whipped-cottage-cheese-recipe-8303272'}, {'title': 'Nutella Ice Cream', 'url': 'https://www.allrecipes.com/nutella-ice-cream-recipe-7508716'}, {'title': '3-Ingredient Banana Oatmeal Cookies', 'url': 'https://www.allrecipes.com/3-ingredient-banana-oatmeal-cookies-recipe-7972686'}, {'title': 'Caramel Apple Pie Cookies', 'url': 'https://www.allrecipes.com/caramel-apple-pie-cookies-recipe-7642173'}]\u001b[0m\n",
|
383 |
-
"Thought:\u001b[32;1m\u001b[1;3mThe first ice cream recipe I found is for Nutella Ice Cream. I will use the Get a Recipe tool to fetch the recipe.\n",
|
384 |
-
"Action: Get a Recipe tool\n",
|
385 |
-
"Action Input: https://www.allrecipes.com/nutella-ice-cream-recipe-7508716\u001b[0m\n",
|
386 |
-
"Observation: \u001b[38;5;200m\u001b[1;3m{'@context': 'http://schema.org', '@type': ['Recipe'], 'headline': 'Nutella Ice Cream', 'datePublished': '2023-09-28T17:27:33.088-04:00', 'dateModified': '2023-10-12T11:27:16.725-04:00', 'author': [{'@type': 'Person', 'name': 'Laka kuharika - Easy Cook', 'url': 'https://www.allrecipes.com/cook/lakakuharica'}], 'description': 'This exquisite homemade Nutella ice cream is full of nutty flavor, and everyone will simply love it. Nutella gives this ice cream an unbelievable hazelnut taste.', 'name': 'Nutella Ice Cream', 'cookTime': 'PT15M', 'nutrition': {'@type': 'NutritionInformation', 'calories': '305 kcal', 'carbohydrateContent': '16 g', 'cholesterolContent': '156 mg', 'fiberContent': '1 g', 'proteinContent': '8 g', 'saturatedFatContent': '14 g', 'sodiumContent': '78 mg', 'sugarContent': '14 g', 'fatContent': '23 g', 'unsaturatedFatContent': '0 g'}, 'prepTime': 'PT10M', 'recipeCategory': ['Dessert'], 'recipeCuisine': ['American'], 'recipeIngredient': ['2 cups whole milk', '1 cup heavy cream, divided', '4 egg yolks', '1/2 teaspoon vanilla extract', '1/2\\xa0 cup cocoa hazelnut spread (such as Nutella®)', 'Hazelnuts, crushed, for garnish'], 'recipeInstructions': [{'@type': 'HowToStep', 'text': 'Combine milk, cream, and 1/2 cup sugar in a saucepan over medium heat. Cook until sugar dissolves, about 5 minutes.'}, {'@type': 'HowToStep', 'text': 'Beat egg yolks with the remaining sugar in a bowl with an electric mixer until eggs become thick and pale yellow, about 4 minutes.'}, {'@type': 'HowToStep', 'text': 'Pour 1/2 cup warm milk mixture into egg mixture and stir. Add this mixture back into the saucepan. Cook over very low heat, stirring constantly, until the mixture becomes thick enough to coat the back of a wooden spoon, 7 to 10 minutes.'}, {'@type': 'HowToStep', 'text': 'Place a strainer over a bowl and pour the warm custard mixture through the strainer. Stir in vanilla and hazelnut spread until it dissolves.'}, {'@type': 'HowToStep', 'text': 'Chill mixture completely, about 1 hour. Pour into an ice cream maker and churn until frozen according to manufacturer’s instructions, 15 to 20 minutes.'}, {'@type': 'HowToStep', 'text': 'Transfer to a plastic container and place in the freezer for an hour before serving. Top with crushed hazelnuts for serving.'}], 'recipeYield': ['8'], 'totalTime': 'PT160M', 'about': []}\u001b[0m\n",
|
387 |
-
"Thought:\u001b[32;1m\u001b[1;3mI now know the final answer\n",
|
388 |
-
"Final Answer: To make Nutella Ice Cream, follow these steps:\n",
|
389 |
-
"1. Combine 2 cups of whole milk, 1 cup of heavy cream, and 1/2 cup of sugar in a saucepan over medium heat. Cook until sugar dissolves, about 5 minutes.\n",
|
390 |
-
"2. Beat 4 egg yolks with the remaining sugar in a bowl with an electric mixer until eggs become thick and pale yellow, about 4 minutes.\n",
|
391 |
-
"3. Pour 1/2 cup warm milk mixture into egg mixture and stir. Add this mixture back into the saucepan. Cook over very low heat, stirring constantly, until the mixture becomes thick enough to coat the back of a wooden spoon, 7 to 10 minutes.\n",
|
392 |
-
"4. Place a strainer over a bowl and pour the warm custard mixture through the strainer. Stir in 1/2 teaspoon of vanilla and 1/2 cup of cocoa hazelnut spread (such as Nutella®) until it dissolves.\n",
|
393 |
-
"5. Chill mixture completely, about 1 hour. Pour into an ice cream maker and churn until frozen according to manufacturer’s instructions, 15 to 20 minutes.\n",
|
394 |
-
"6. Transfer to a plastic container and place in the freezer for an hour before serving. Top with crushed hazelnuts for serving.\u001b[0m\n",
|
395 |
"\n",
|
396 |
"\u001b[1m> Finished chain.\u001b[0m\n"
|
397 |
]
|
@@ -400,29 +419,28 @@
|
|
400 |
"source": [
|
401 |
"import gradio as gr\n",
|
402 |
"\n",
|
403 |
-
"
|
404 |
-
"
|
405 |
-
"
|
406 |
-
"\n",
|
407 |
-
"\n",
|
408 |
-
"
|
409 |
-
"
|
410 |
-
"
|
411 |
-
"
|
412 |
-
"\n",
|
413 |
-
"
|
414 |
-
"
|
415 |
-
"
|
416 |
-
"
|
417 |
-
" return \"Type a prompt to get a Wikipedia-like response!\"\n",
|
418 |
"\n",
|
419 |
-
"
|
420 |
-
"
|
421 |
-
"
|
422 |
-
" description=\"Generate Wikipedia-like responses using Langchain AI.\")\n",
|
423 |
"\n",
|
424 |
-
"
|
425 |
-
"
|
|
|
426 |
]
|
427 |
}
|
428 |
],
|
|
|
2 |
"cells": [
|
3 |
{
|
4 |
"cell_type": "code",
|
5 |
+
"execution_count": 7,
|
6 |
"metadata": {},
|
7 |
"outputs": [],
|
8 |
"source": [
|
|
|
35 |
},
|
36 |
{
|
37 |
"cell_type": "code",
|
38 |
+
"execution_count": 168,
|
39 |
"metadata": {},
|
40 |
"outputs": [],
|
41 |
"source": [
|
42 |
"import requests\n",
|
43 |
"from bs4 import BeautifulSoup\n",
|
44 |
"import json\n",
|
45 |
+
"import validators \n",
|
46 |
"\n",
|
47 |
"def remove_keys(dictionary: dict, keyList: list):\n",
|
48 |
" for key in keyList: \n",
|
|
|
50 |
" del dictionary[key] \n",
|
51 |
"\n",
|
52 |
"def get_recipe_as_json(url: str) -> dict: \n",
|
53 |
+
" \n",
|
54 |
" url = url.replace(\"'\", \"\").replace('\"', '')\n",
|
55 |
+
" \n",
|
56 |
+
" if not validators.url(url):\n",
|
57 |
+
" return {'error': 'Invalid url format', 'url': url }\n",
|
58 |
+
"\n",
|
59 |
" html = requests.get(url).text \n",
|
60 |
" soup = BeautifulSoup(html)\n",
|
61 |
" script = soup.find_all(\"script\", {\"id\": \"allrecipes-schema_1-0\"})\n",
|
62 |
"\n",
|
63 |
" recipeDict = json.loads(script[0].text)[0]\n",
|
64 |
" remove_keys(recipeDict, ['review', 'image', 'mainEntityOfPage', 'publisher'])\n",
|
|
|
|
|
|
|
|
|
65 |
" \n",
|
66 |
" return recipeDict\n",
|
67 |
"\n",
|
68 |
"# url = \"https://www.allrecipes.com/recipe/212498/easy-chicken-and-broccoli-alfredo/\"\n",
|
69 |
+
"# obj = get_recipe_as_json(url)\n",
|
70 |
+
"# x = get_recipe_as_json('https://www.allrecipes.com/recipe/235153/easy-baked-chicken-thighs/')\n",
|
71 |
+
"# print(x)"
|
72 |
]
|
73 |
},
|
74 |
{
|
|
|
80 |
},
|
81 |
{
|
82 |
"cell_type": "code",
|
83 |
+
"execution_count": 9,
|
84 |
"metadata": {},
|
85 |
"outputs": [],
|
86 |
"source": [
|
|
|
181 |
},
|
182 |
{
|
183 |
"cell_type": "code",
|
184 |
+
"execution_count": 169,
|
185 |
"metadata": {},
|
186 |
"outputs": [],
|
187 |
"source": [
|
188 |
"# Tools \n",
|
189 |
"from langchain.agents import Tool\n",
|
190 |
+
"\n",
|
191 |
"\n",
|
192 |
"# Chicken functions\n",
|
193 |
+
"def list_chicken_recipes(query: str): \n",
|
194 |
" return chickenDishList\n",
|
195 |
"\n",
|
196 |
"list_chicken_recipes_tool = Tool(name='Chicken Recipes tool', func= list_chicken_recipes, description=\"This tools lists the available Chicken Recipes it returns a list of recipes with a TITLE and a URL where you can fetch the recipe.\")\n",
|
|
|
203 |
" description=\"This tools lists the available Dessert Recipes it returns a list of recipes with a TITLE and a URL where you can fetch the recipe.\")\n",
|
204 |
"\n",
|
205 |
"# Recipe fetcher functions\n",
|
206 |
+
"def get_recipe(url: str): \n",
|
207 |
" return get_recipe_as_json(url)\n",
|
208 |
"\n",
|
209 |
"get_recipe_as_json_tool = Tool(name='Get a Recipe tool', func=get_recipe, description=\"\"\"\n",
|
|
|
217 |
},
|
218 |
{
|
219 |
"cell_type": "code",
|
220 |
+
"execution_count": null,
|
221 |
"metadata": {},
|
222 |
+
"outputs": [],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
223 |
"source": [
|
224 |
"print('Chicken dishes:')\n",
|
225 |
"for i in chickenDishList:\n",
|
226 |
+
" resp += i['title'])\n",
|
227 |
" \n",
|
228 |
+
"resp += )\n",
|
229 |
"print('Desserts:')\n",
|
230 |
"for i in dessertList:\n",
|
231 |
" print(i['title'])"
|
232 |
]
|
233 |
},
|
234 |
+
{
|
235 |
+
"cell_type": "markdown",
|
236 |
+
"metadata": {},
|
237 |
+
"source": [
|
238 |
+
"# LLM\n",
|
239 |
+
"Links\n",
|
240 |
+
"\n",
|
241 |
+
"\n",
|
242 |
+
"1 [tracking-inspecting-prompts-langchain-agents-weights-and-biases](https://kleiber.me/blog/2023/05/14/tracking-inspecting-prompts-langchain-agents-weights-and-biases/)"
|
243 |
+
]
|
244 |
+
},
|
245 |
{
|
246 |
"cell_type": "code",
|
247 |
+
"execution_count": 210,
|
248 |
"metadata": {},
|
249 |
"outputs": [
|
250 |
{
|
|
|
254 |
"\n",
|
255 |
"\n",
|
256 |
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
257 |
+
"\u001b[32;1m\u001b[1;3mI need to find the recipe for Nutella Ice Cream to know what tools are needed.\n",
|
258 |
+
"Action: Dessert Recipes tool\n",
|
259 |
+
"Action Input: Nutella Ice Cream\u001b[0m\n",
|
260 |
+
"Observation: \u001b[33;1m\u001b[1;3m[{'title': 'Chocolate Snack Cake', 'url': 'https://www.allrecipes.com/chocolate-snack-cake-recipe-8350343'}, {'title': 'Charred Spiced Pears with Smoky Vanilla Cherry Sauce', 'url': 'https://www.allrecipes.com/charred-spiced-pears-with-smoky-vanilla-cherry-sauce-recipe-8347080'}, {'title': 'Meringue Topped Banana Pudding', 'url': 'https://www.allrecipes.com/meringue-topped-banana-pudding-recipe-8347040'}, {'title': 'White Chocolate Cinnamon Toast Crunch Bars', 'url': 'https://www.allrecipes.com/white-chocolate-cinnamon-toast-crunch-bars-recipe-7556790'}, {'title': 'Plum Cobbler for Two', 'url': 'https://www.allrecipes.com/plum-cobbler-for-two-recipe-8304143'}, {'title': 'Pumpkin Cheesecake Cookies', 'url': 'https://www.allrecipes.com/pumpkin-cheesecake-cookies-recipe-7972485'}, {'title': 'Chocolate Whipped Cottage Cheese', 'url': 'https://www.allrecipes.com/chocolate-whipped-cottage-cheese-recipe-8303272'}, {'title': 'Nutella Ice Cream', 'url': 'https://www.allrecipes.com/nutella-ice-cream-recipe-7508716'}, {'title': '3-Ingredient Banana Oatmeal Cookies', 'url': 'https://www.allrecipes.com/3-ingredient-banana-oatmeal-cookies-recipe-7972686'}, {'title': 'Caramel Apple Pie Cookies', 'url': 'https://www.allrecipes.com/caramel-apple-pie-cookies-recipe-7642173'}]\u001b[0m\n",
|
261 |
+
"Thought:\u001b[32;1m\u001b[1;3mI found the recipe for Nutella Ice Cream. Now I need to fetch the recipe to find out what tools are needed.\n",
|
262 |
+
"Action: Get a Recipe tool\n",
|
263 |
+
"Action Input: https://www.allrecipes.com/nutella-ice-cream-recipe-7508716\u001b[0m\n",
|
264 |
+
"Observation: \u001b[38;5;200m\u001b[1;3m{'@context': 'http://schema.org', '@type': ['Recipe'], 'headline': 'Nutella Ice Cream', 'datePublished': '2023-09-28T17:27:33.088-04:00', 'dateModified': '2023-10-12T11:27:16.725-04:00', 'author': [{'@type': 'Person', 'name': 'Laka kuharika - Easy Cook', 'url': 'https://www.allrecipes.com/cook/lakakuharica'}], 'description': 'This exquisite homemade Nutella ice cream is full of nutty flavor, and everyone will simply love it. Nutella gives this ice cream an unbelievable hazelnut taste.', 'name': 'Nutella Ice Cream', 'cookTime': 'PT15M', 'nutrition': {'@type': 'NutritionInformation', 'calories': '305 kcal', 'carbohydrateContent': '16 g', 'cholesterolContent': '156 mg', 'fiberContent': '1 g', 'proteinContent': '8 g', 'saturatedFatContent': '14 g', 'sodiumContent': '78 mg', 'sugarContent': '14 g', 'fatContent': '23 g', 'unsaturatedFatContent': '0 g'}, 'prepTime': 'PT10M', 'recipeCategory': ['Dessert'], 'recipeCuisine': ['American'], 'recipeIngredient': ['2 cups whole milk', '1 cup heavy cream, divided', '4 egg yolks', '1/2 teaspoon vanilla extract', '1/2\\xa0 cup cocoa hazelnut spread (such as Nutella®)', 'Hazelnuts, crushed, for garnish'], 'recipeInstructions': [{'@type': 'HowToStep', 'text': 'Combine milk, cream, and 1/2 cup sugar in a saucepan over medium heat. Cook until sugar dissolves, about 5 minutes.'}, {'@type': 'HowToStep', 'text': 'Beat egg yolks with the remaining sugar in a bowl with an electric mixer until eggs become thick and pale yellow, about 4 minutes.'}, {'@type': 'HowToStep', 'text': 'Pour 1/2 cup warm milk mixture into egg mixture and stir. Add this mixture back into the saucepan. Cook over very low heat, stirring constantly, until the mixture becomes thick enough to coat the back of a wooden spoon, 7 to 10 minutes.'}, {'@type': 'HowToStep', 'text': 'Place a strainer over a bowl and pour the warm custard mixture through the strainer. Stir in vanilla and hazelnut spread until it dissolves.'}, {'@type': 'HowToStep', 'text': 'Chill mixture completely, about 1 hour. Pour into an ice cream maker and churn until frozen according to manufacturer’s instructions, 15 to 20 minutes.'}, {'@type': 'HowToStep', 'text': 'Transfer to a plastic container and place in the freezer for an hour before serving. Top with crushed hazelnuts for serving.'}], 'recipeYield': ['8'], 'totalTime': 'PT160M', 'about': []}\u001b[0m\n",
|
265 |
+
"Thought:\u001b[32;1m\u001b[1;3mI now have the recipe and can identify the tools needed.\n",
|
266 |
+
"Final Answer: The tools needed for the Nutella Ice Cream recipe are a saucepan, an electric mixer, a bowl, a strainer, and an ice cream maker.\u001b[0m\n",
|
267 |
"\n",
|
268 |
"\u001b[1m> Finished chain.\u001b[0m\n"
|
269 |
]
|
|
|
274 |
"from langchain.agents import initialize_agent\n",
|
275 |
"from langchain.llms import OpenAI\n",
|
276 |
"from langchain.chat_models import ChatOpenAI\n",
|
277 |
+
"from langchain.callbacks import StdOutCallbackHandler\n",
|
278 |
"\n",
|
279 |
"from langchain.agents import initialize_agent\n",
|
280 |
"llm = ChatOpenAI(temperature=0, model_name='gpt-4') # 'gpt-3.5-turbo'\n",
|
|
|
287 |
" max_iterations=7, \n",
|
288 |
" return_intermediate_steps=True\n",
|
289 |
")\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
290 |
"\n",
|
291 |
+
"system = \"If the answer is not in the tools or context passed to you then don't answer. \\nIf you don't know the answer then say so.\" \n",
|
292 |
+
"#query = \"Can you show tell me what ingredients I need for the first baked chicken recipe?\"\n",
|
293 |
+
"#query = \"Can you show tell me what ingredients I need for the last baked chicken recipe? \"\n",
|
294 |
+
"#query = \"What is the best baked chicken recipe? Please look across all recipes with the word 'baked' in the title\" # There are 3 baked chicken recipes\n",
|
295 |
+
"#query = \"Is there a Chicken recipe that's prepared with an alchohol? And if so how long does it take in total from start time to finish?\"\n",
|
296 |
+
"#query = \"Which is healthier the Caramel Apple Pie Cookies or the beer chicken? Please explain how you got to your answer.\"\n",
|
297 |
+
"#query = \"Is the moon closer to earth or the sun?\"\n",
|
298 |
+
"query = \"What tools do I need for the Nutella Ice Cream?\"\n",
|
299 |
+
"#query = \"My bowl is broken, can I still make Nutella Ice Cream? Answer as a yes/no.\"\n",
|
300 |
"\n",
|
301 |
+
"response = agent({\"input\": f\"{system} {query}\"})\n"
|
|
|
|
|
302 |
]
|
303 |
},
|
304 |
{
|
305 |
"cell_type": "code",
|
306 |
+
"execution_count": 211,
|
307 |
"metadata": {},
|
308 |
"outputs": [
|
309 |
{
|
310 |
"name": "stdout",
|
311 |
"output_type": "stream",
|
312 |
"text": [
|
313 |
+
"Step: 1 of 2\n",
|
314 |
+
"Thought: I need to find the recipe for Nutella Ice Cream to know what tools are needed.\n",
|
315 |
+
"Action: Dessert Recipes tool\n",
|
316 |
+
"Action Input: Nutella Ice Cream\n",
|
317 |
+
"\n",
|
318 |
+
"Action response: [{'title': 'Chocolate Snack Cake', 'url': 'https://www.allrecipes.com/chocolate-snack-cake-recipe-8350343'}, {'title': 'Charred Spiced Pears with Smoky Vanilla Cherry Sauce', 'url': 'https://www.allrecipes.com/charred-spiced-pears-with-smoky-vanilla-cherry-sauce-recipe-8347080'}, {'title': 'Meringue Topped Banana Pudding', 'url': 'https://www.allrecipes.com/meringue-topped-banana-pudding-recipe-8347040'}, {'title': 'White Chocolate Cinnamon Toast Crunch Bars', 'url': 'https://www.allrecipes.com/white-chocolate-cinnamon-toast-crunch-bars-recipe-7556790'}, {'title': 'Plum Cobbler for Two', 'url': 'https://www.allrecipes.com/plum-cobbler-for-two-recipe-8304143'}, {'title': 'Pumpkin Cheesecake Cookies', 'url': 'https://www.allrecipes.com/pumpkin-cheesecake-cookies-recipe-7972485'}, {'title': 'Chocolate Whipped Cottage Cheese', 'url': 'https://www.allrecipes.com/chocolate-whipped-cottage-cheese-recipe-8303272'}, {'title': 'Nutella Ice Cream', 'url': 'https://www.allrecipes.com/nutella-ice-cream-recipe-7508716'}, {'title': '3-Ingredient Banana Oatmeal Cookies', 'url': 'https://www.allrecipes.com/3-ingredient-banana-oatmeal-cookies-recipe-7972686'}, {'title': 'Caramel Apple Pie Cookies', 'url': 'https://www.allrecipes.com/caramel-apple-pie-cookies-recipe-7642173'}]\n",
|
319 |
+
"###########################\n",
|
320 |
+
"\n",
|
321 |
+
"Step: 2 of 2\n",
|
322 |
+
"Thought: I found the recipe for Nutella Ice Cream. Now I need to fetch the recipe to find out what tools are needed.\n",
|
323 |
+
"Action: Get a Recipe tool\n",
|
324 |
+
"Action Input: https://www.allrecipes.com/nutella-ice-cream-recipe-7508716\n",
|
325 |
+
"\n",
|
326 |
+
"Action response: {'@context': 'http://schema.org', '@type': ['Recipe'], 'headline': 'Nutella Ice Cream', 'datePublished': '2023-09-28T17:27:33.088-04:00', 'dateModified': '2023-10-12T11:27:16.725-04:00', 'author': [{'@type': 'Person', 'name': 'Laka kuharika - Easy Cook', 'url': 'https://www.allrecipes.com/cook/lakakuharica'}], 'description': 'This exquisite homemade Nutella ice cream is full of nutty flavor, and everyone will simply love it. Nutella gives this ice cream an unbelievable hazelnut taste.', 'name': 'Nutella Ice Cream', 'cookTime': 'PT15M', 'nutrition': {'@type': 'NutritionInformation', 'calories': '305 kcal', 'carbohydrateContent': '16 g', 'cholesterolContent': '156 mg', 'fiberContent': '1 g', 'proteinContent': '8 g', 'saturatedFatContent': '14 g', 'sodiumContent': '78 mg', 'sugarContent': '14 g', 'fatContent': '23 g', 'unsaturatedFatContent': '0 g'}, 'prepTime': 'PT10M', 'recipeCategory': ['Dessert'], 'recipeCuisine': ['American'], 'recipeIngredient': ['2 cups whole milk', '1 cup heavy cream, divided', '4 egg yolks', '1/2 teaspoon vanilla extract', '1/2\\xa0 cup cocoa hazelnut spread (such as Nutella®)', 'Hazelnuts, crushed, for garnish'], 'recipeInstructions': [{'@type': 'HowToStep', 'text': 'Combine milk, cream, and 1/2 cup sugar in a saucepan over medium heat. Cook until sugar dissolves, about 5 minutes.'}, {'@type': 'HowToStep', 'text': 'Beat egg yolks with the remaining sugar in a bowl with an electric mixer until eggs become thick and pale yellow, about 4 minutes.'}, {'@type': 'HowToStep', 'text': 'Pour 1/2 cup warm milk mixture into egg mixture and stir. Add this mixture back into the saucepan. Cook over very low heat, stirring constantly, until the mixture becomes thick enough to coat the back of a wooden spoon, 7 to 10 minutes.'}, {'@type': 'HowToStep', 'text': 'Place a strainer over a bowl and pour the warm custard mixture through the strainer. Stir in vanilla and hazelnut spread until it dissolves.'}, {'@type': 'HowToStep', 'text': 'Chill mixture completely, about 1 hour. Pour into an ice cream maker and churn until frozen according to manufacturer’s instructions, 15 to 20 minutes.'}, {'@type': 'HowToStep', 'text': 'Transfer to a plastic container and place in the freezer for an hour before serving. Top with crushed hazelnuts for serving.'}], 'recipeYield': ['8'], 'totalTime': 'PT160M', 'about': []}\n",
|
327 |
+
"###########################\n",
|
328 |
+
"\n",
|
329 |
+
"\n",
|
330 |
+
"Final Thought: The tools needed for the Nutella Ice Cream recipe are a saucepan, an electric mixer, a bowl, a strainer, and an ice cream maker.\n"
|
331 |
]
|
332 |
}
|
333 |
],
|
334 |
"source": [
|
335 |
+
"from langchain.load.dump import dumps\n",
|
336 |
+
"\n",
|
337 |
+
"# for k in response:\n",
|
338 |
+
"# print(k)\n",
|
339 |
+
"\n",
|
340 |
+
"# print(response['output'])\n",
|
341 |
+
"\n",
|
342 |
+
"stepsDict = json.loads(dumps(response[\"intermediate_steps\"], pretty=True))\n",
|
343 |
+
"for i in range(len(stepsDict)):\n",
|
344 |
+
" print(f'Step: {i+1} of {len(stepsDict)}')\n",
|
345 |
+
" print(f\"Thought: {stepsDict[i][0]['kwargs']['log']}\")\n",
|
346 |
+
" print('')\n",
|
347 |
+
" print(f\"Action response: {stepsDict[i][1]}\")\n",
|
348 |
+
" print('###########################')\n",
|
349 |
+
" print('')\n",
|
350 |
+
"\n",
|
351 |
+
"print('')\n",
|
352 |
+
"print(f\"Final Thought: {response['output']}\")\n",
|
353 |
+
"\n"
|
354 |
]
|
355 |
},
|
356 |
{
|
357 |
"cell_type": "markdown",
|
358 |
"metadata": {},
|
359 |
"source": [
|
360 |
+
"# UI - Simple UI"
|
361 |
]
|
362 |
},
|
363 |
{
|
364 |
"cell_type": "code",
|
365 |
+
"execution_count": 221,
|
366 |
"metadata": {},
|
367 |
"outputs": [
|
368 |
+
{
|
369 |
+
"name": "stderr",
|
370 |
+
"output_type": "stream",
|
371 |
+
"text": [
|
372 |
+
"c:\\sc\\ai\\rag-demo-1\\.venv\\lib\\site-packages\\gradio\\interface.py:330: UserWarning: The `allow_flagging` parameter in `Interface` nowtakes a string value ('auto', 'manual', or 'never'), not a boolean. Setting parameter to: 'never'.\n",
|
373 |
+
" warnings.warn(\n"
|
374 |
+
]
|
375 |
+
},
|
376 |
{
|
377 |
"name": "stdout",
|
378 |
"output_type": "stream",
|
379 |
"text": [
|
380 |
+
"Running on local URL: http://127.0.0.1:7897\n",
|
381 |
"\n",
|
382 |
"To create a public link, set `share=True` in `launch()`.\n"
|
383 |
]
|
|
|
385 |
{
|
386 |
"data": {
|
387 |
"text/html": [
|
388 |
+
"<div><iframe src=\"http://127.0.0.1:7897/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
389 |
],
|
390 |
"text/plain": [
|
391 |
"<IPython.core.display.HTML object>"
|
|
|
398 |
"data": {
|
399 |
"text/plain": []
|
400 |
},
|
401 |
+
"execution_count": 221,
|
402 |
"metadata": {},
|
403 |
"output_type": "execute_result"
|
404 |
},
|
|
|
409 |
"\n",
|
410 |
"\n",
|
411 |
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
412 |
+
"\u001b[32;1m\u001b[1;3mThe question is asking for the location of a watch, which is not information that can be obtained from the available tools. \n",
|
413 |
+
"Final Answer: I'm sorry, I don't have the ability to locate physical objects.\u001b[0m\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
414 |
"\n",
|
415 |
"\u001b[1m> Finished chain.\u001b[0m\n"
|
416 |
]
|
|
|
419 |
"source": [
|
420 |
"import gradio as gr\n",
|
421 |
"\n",
|
422 |
+
"def ask_query(query):\n",
|
423 |
+
" response = agent({\"input\": f\"{system} {query}\"})\n",
|
424 |
+
" \n",
|
425 |
+
" stepsDict = json.loads(dumps(response[\"intermediate_steps\"], pretty=True))\n",
|
426 |
+
" resp = 'Below are the steps the agent took to get to the Final Answer. \\n\"Thought\" is the LLMs internal dialogue, \\n\"Action\" is the tool it will use to fetch the next piece of information. \\n\"Action Input\" is the input it passes the tool to fetch this information. \\n\"Action Response\" is what was returned from the tool to the LLM at that given step. '\n",
|
427 |
+
" resp += '\\n\\n'\n",
|
428 |
+
" resp += 'Steps to solve answer using ReAct\\n'\n",
|
429 |
+
" for i in range(len(stepsDict)):\n",
|
430 |
+
" resp += '##########################################\\n'\n",
|
431 |
+
" resp += f'Step: {i+1} of {len(stepsDict)}\\n'\n",
|
432 |
+
" resp += f\"Thought: {stepsDict[i][0]['kwargs']['log']}\\n\"\n",
|
433 |
+
" resp += 'Below is what the tool returned...\\n'\n",
|
434 |
+
" resp += f\"Action response: {stepsDict[i][1]}\\n\" \n",
|
435 |
+
" resp += '\\n'\n",
|
|
|
436 |
"\n",
|
437 |
+
" resp += '\\nFinal Thought:\\n'\n",
|
438 |
+
" resp += response['output']\n",
|
439 |
+
" return resp\n",
|
|
|
440 |
"\n",
|
441 |
+
"demo = gr.Interface(fn=ask_query, inputs=\"text\", outputs=\"text\", allow_flagging=False)\n",
|
442 |
+
" \n",
|
443 |
+
"demo.launch(show_error=True)"
|
444 |
]
|
445 |
}
|
446 |
],
|