{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Reading settings from ../../env/ai.json\n" ] } ], "source": [ "import os\n", "import json\n", "\n", "pathToSettings = '../../env/ai.json'\n", "if os.path.exists(pathToSettings):\n", " # Load setting from Json outside of project.\n", " print(f'Reading settings from {pathToSettings}')\n", " f = open(pathToSettings)\n", " settingsJson = json.load(f)\n", " del f\n", "\n", " for key in settingsJson:\n", " os.environ[key] = settingsJson[key]\n", " \n", " del settingsJson" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Setup Web Crawler and Lookup functions" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import requests\n", "from bs4 import BeautifulSoup\n", "import json\n", "import validators \n", "\n", "pageCache = {}\n", "\n", "def json_safe_loads(jsonString):\n", " try:\n", " obj = json.loads(jsonString)\n", " except ValueError as e:\n", " return None\n", " return obj\n", "\n", "def remove_keys(dictionary: dict, keyList: list):\n", " for key in keyList: \n", " if key in dictionary:\n", " del dictionary[key] \n", "\n", "def get_recipe_as_json(url: str) -> dict: \n", " \n", " obj = json_safe_loads(url)\n", " if obj is not None: \n", " if 'url' in obj:\n", " url = obj['url']\n", " \n", " url = url.replace(\"'\", \"\").replace('\"', '')\n", " \n", " if not validators.url(url):\n", " return {'error': 'Invalid url format', 'url': url }\n", " \n", " if url in pageCache:\n", " return pageCache[url] \n", "\n", " html = requests.get(url).text \n", " soup = BeautifulSoup(html)\n", " script = soup.find_all(\"script\", {\"id\": \"allrecipes-schema_1-0\"})\n", "\n", " recipeDict = json.loads(script[0].text)[0]\n", " remove_keys(recipeDict, ['review', 'image', 'mainEntityOfPage', 'publisher'])\n", " \n", " pageCache[url] = recipeDict\n", " \n", " return recipeDict\n", "\n", "# url = \"https://www.allrecipes.com/recipe/212498/easy-chicken-and-broccoli-alfredo/\"\n", "# obj = get_recipe_as_json(url)\n", "#x = get_recipe_as_json('{ \"url\": \"https://www.allrecipes.com/recipe/235153/easy-baked-chicken-thighs/\" }')\n", "# x = get_recipe_as_json('\"https://www.allrecipes.com/recipe/235153/easy-baked-chicken-thighs/\"')\n", "# print(x)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "x = json.loads('{ \"url\": \"https://www.allrecipes.com/recipe/235153/easy-baked-chicken-thighs/\"}')\n", "#x = json.loads('{\"param1\": 123}')\n", "print(type(x))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Static recipe lists" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "dessertList = [\n", " {\n", " \"title\": \"Chocolate Snack Cake\",\n", " \"url\": \"https://www.allrecipes.com/chocolate-snack-cake-recipe-8350343\"\n", " },\n", " {\n", " \"title\": \"Charred Spiced Pears with Smoky Vanilla Cherry Sauce\",\n", " \"url\": \"https://www.allrecipes.com/charred-spiced-pears-with-smoky-vanilla-cherry-sauce-recipe-8347080\"\n", " },\n", " {\n", " \"title\": \"Meringue Topped Banana Pudding\",\n", " \"url\": \"https://www.allrecipes.com/meringue-topped-banana-pudding-recipe-8347040\"\n", " },\n", " {\n", " \"title\": \"White Chocolate Cinnamon Toast Crunch Bars\",\n", " \"url\": \"https://www.allrecipes.com/white-chocolate-cinnamon-toast-crunch-bars-recipe-7556790\"\n", " },\n", " {\n", " \"title\": \"Plum Cobbler for Two\",\n", " \"url\": \"https://www.allrecipes.com/plum-cobbler-for-two-recipe-8304143\"\n", " },\n", " {\n", " \"title\": \"Pumpkin Cheesecake Cookies\",\n", " \"url\": \"https://www.allrecipes.com/pumpkin-cheesecake-cookies-recipe-7972485\"\n", " },\n", " {\n", " \"title\": \"Chocolate Whipped Cottage Cheese\",\n", " \"url\": \"https://www.allrecipes.com/chocolate-whipped-cottage-cheese-recipe-8303272\"\n", " },\n", " {\n", " \"title\": \"Nutella Ice Cream\",\n", " \"url\": \"https://www.allrecipes.com/nutella-ice-cream-recipe-7508716\"\n", " },\n", " {\n", " \"title\": \"3-Ingredient Banana Oatmeal Cookies\",\n", " \"url\": \"https://www.allrecipes.com/3-ingredient-banana-oatmeal-cookies-recipe-7972686\"\n", " },\n", " {\n", " \"title\": \"Caramel Apple Pie Cookies\",\n", " \"url\": \"https://www.allrecipes.com/caramel-apple-pie-cookies-recipe-7642173\"\n", " }\n", "]\n", "\n", "chickenDishList = [\n", " {\n", " \"title\": \"Crispy Roasted Chicken\",\n", " \"url\": \"https://www.allrecipes.com/recipe/228363/crispy-roasted-chicken/\"\n", " },\n", " {\n", " \"title\": \"Roasted Spatchcocked Chicken With Potatoes\",\n", " \"url\": \"https://www.allrecipes.com/recipe/254877/roasted-spatchcocked-chicken-with-potatoes/\"\n", " },\n", " {\n", " \"title\": \"Easy Baked Chicken Thighs\",\n", " \"url\": \"https://www.allrecipes.com/recipe/235153/easy-baked-chicken-thighs/\"\n", " },\n", " {\n", " \"title\": \"Crispy Baked Chicken Thighs\",\n", " \"url\": \"https://www.allrecipes.com/recipe/258878/crispy-baked-chicken-thighs/\"\n", " },\n", " {\n", " \"title\": \"Crispy and Tender Baked Chicken Thighs\",\n", " \"url\": \"https://www.allrecipes.com/recipe/235151/crispy-and-tender-baked-chicken-thighs/\"\n", " },\n", " {\n", " \"title\": \"Million Dollar Chicken\",\n", " \"url\": \"https://www.allrecipes.com/recipe/233953/million-dollar-chicken/\"\n", " },\n", " {\n", " \"title\": \"Simple Whole Roasted Chicken\",\n", " \"url\": \"https://www.allrecipes.com/recipe/70679/simple-whole-roasted-chicken/\"\n", " },\n", " {\n", " \"title\": \"Beer Can Chicken\",\n", " \"url\": \"https://www.allrecipes.com/recipe/214618/beer-can-chicken/\"\n", " },\n", " {\n", " \"title\": \"Air Fryer Chicken Thighs\",\n", " \"url\": \"https://www.allrecipes.com/recipe/272858/air-fryer-chicken-thighs/\"\n", " },\n", " {\n", " \"title\": \"Happy Roast Chicken\",\n", " \"url\": \"https://www.allrecipes.com/recipe/214478/happy-roast-chicken/\"\n", " }\n", "]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Setup Tools" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# Tools \n", "from langchain.agents import Tool\n", "\n", "\n", "# Chicken functions\n", "def list_chicken_recipes(query: str): \n", " return chickenDishList\n", "\n", "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", "\n", "# Dessert functions\n", "def list_dessert_recipes(query: str): \n", " return dessertList\n", "\n", "list_dessert_recipes_tool = Tool(name='Dessert Recipes tool', func=list_dessert_recipes, \n", " 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", "\n", "# Recipe fetcher functions\n", "def get_recipe(url: str): \n", " return get_recipe_as_json(url)\n", "\n", "get_recipe_as_json_tool = Tool(name='Get a Recipe tool', func=get_recipe, description=\"\"\"\n", " Useful for fetching a particular recipe, you can only fetch a recipe with it's url, you must get that using another tool\n", " The tool uses the https://schema.org/Recipe format to store it's recipes. \n", " \"\"\")\n", "\n", "# Tool list\n", "tools = [list_chicken_recipes_tool, list_dessert_recipes_tool, get_recipe_as_json_tool]" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "# print('Chicken dishes:')\n", "# for i in chickenDishList:\n", "# print(i['title'])\n", " \n", "# print('')\n", "# print('Desserts:')\n", "# for i in dessertList:\n", "# print(i['title'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# LLM\n", "Links\n", "\n", "\n", "1 [tracking-inspecting-prompts-langchain-agents-weights-and-biases](https://kleiber.me/blog/2023/05/14/tracking-inspecting-prompts-langchain-agents-weights-and-biases/)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "# from langchain.agents import load_tools\n", "# from langchain.agents import initialize_agent\n", "# from langchain.llms import OpenAI\n", "# from langchain.chat_models import ChatOpenAI\n", "# from langchain.callbacks import StdOutCallbackHandler\n", "\n", "# from langchain.agents import initialize_agent\n", "# llm = ChatOpenAI(temperature=0, model_name='gpt-4') # 'gpt-3.5-turbo'\n", "\n", "# agent = initialize_agent(\n", "# agent=\"zero-shot-react-description\", \n", "# tools=tools, \n", "# llm=llm,\n", "# verbose=True,\n", "# max_iterations=7, \n", "# return_intermediate_steps=True\n", "# )\n", "# 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", "# #query = \"Can you show tell me what ingredients I need for the first baked chicken recipe?\"\n", "# #query = \"Can you show tell me what ingredients I need for the last baked chicken recipe? \"\n", "# #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", "# #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", "# #query = \"Which is healthier the Caramel Apple Pie Cookies or the beer chicken? Please explain how you got to your answer.\"\n", "# #query = \"Is the moon closer to earth or the sun?\"\n", "# #query = \"How good are the apple pie cookies?\"\n", "# query = \"What tools do I need for the Nutella Ice Cream?\"\n", "# #query = \"My bowl is broken, can I still make Nutella Ice Cream? Answer as a yes/no.\"\n", "# #response = agent({\"input\": f\"{system} {query}\"})\n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "from langchain.agents import load_tools\n", "from langchain.agents import initialize_agent\n", "from langchain.llms import OpenAI\n", "from langchain.chat_models import ChatOpenAI\n", "from langchain.load.dump import dumps\n", "\n", "def ask_query(model, apiKey, query):\n", " \n", " if len(apiKey) > 0:\n", " os.environ['OPENAI_API_KEY'] = apiKey\n", " \n", " print(model)\n", " \n", " # LLM \n", " llm = ChatOpenAI(temperature=0.2, model_name=model) # 'gpt-3.5-turbo' # gpt-4\n", " agent = initialize_agent(agent=\"zero-shot-react-description\", tools=tools, llm=llm, verbose=True, max_iterations=7, return_intermediate_steps=True)\n", " system = \"\"\"\n", " If the answer is not in the tools or context passed to you then don't answer. \\n\n", " If you don't know the answer then say so. \\n \n", " \"\"\" \n", " response = agent({\"input\": f\"{system} [[RECIPENAME]] {query}\"})\n", "\n", " # Show response \n", " stepsDict = json.loads(dumps(response[\"intermediate_steps\"], pretty=True))\n", " 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", " resp += '\\n\\n'\n", " resp += 'Steps to solve answer using ReAct\\n'\n", " for i in range(len(stepsDict)):\n", " resp += '##########################################\\n'\n", " resp += f'Step: {i+1} of {len(stepsDict)}\\n'\n", " resp += f\"Thought: {stepsDict[i][0]['kwargs']['log']}\\n\"\n", " resp += 'Below is what the tool returned...\\n'\n", " resp += f\"Action response: {stepsDict[i][1]}\\n\" \n", " resp += '\\n'\n", "\n", " resp += '\\nFinal Thought:\\n'\n", " resp += response['output']\n", " return resp" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# UI - Simple UI" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7861\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" }, { "name": "stdout", "output_type": "stream", "text": [ "gpt-3.5-turbo\n", "\n", "\n", "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", "\u001b[32;1m\u001b[1;3mI need to find the first baked chicken recipe and fetch its ingredients.\n", "Action: Chicken Recipes tool\n", "Action Input: None\u001b[0m\n", "Observation: \u001b[36;1m\u001b[1;3m['https://www.allrecipes.com/recipe/228363/crispy-roasted-chicken/', 'https://www.allrecipes.com/recipe/254877/roasted-spatchcocked-chicken-with-potatoes/', 'https://www.allrecipes.com/recipe/235153/easy-baked-chicken-thighs/', 'https://www.allrecipes.com/recipe/258878/crispy-baked-chicken-thighs/', 'https://www.allrecipes.com/recipe/235151/crispy-and-tender-baked-chicken-thighs/', 'https://www.allrecipes.com/recipe/233953/million-dollar-chicken/', 'https://www.allrecipes.com/recipe/70679/simple-whole-roasted-chicken/', 'https://www.allrecipes.com/recipe/214618/beer-can-chicken/', 'https://www.allrecipes.com/recipe/272858/air-fryer-chicken-thighs/', 'https://www.allrecipes.com/recipe/214478/happy-roast-chicken/']\u001b[0m\n", "Thought:\u001b[32;1m\u001b[1;3mI need to fetch the first baked chicken recipe and get its ingredients.\n", "Action: Get a Recipe tool\n", "Action Input: 'https://www.allrecipes.com/recipe/228363/crispy-roasted-chicken/'\u001b[0m\n", "Observation: \u001b[38;5;200m\u001b[1;3m{'@context': 'http://schema.org', '@type': ['Recipe'], 'headline': 'Crispy Roasted Chicken', 'datePublished': '2014-08-26T18:22:55.000-04:00', 'dateModified': '2023-01-30T04:44:54.000-05:00', 'author': [{'@type': 'Person', 'name': 'Doc Simonson', 'url': 'https://www.allrecipes.com/cook/docsimonson'}], 'description': 'This roasted half chicken is prepared Bavarian-style with spices and herbs, including caraway, sage, fennel, garlic, rosemary, coriander, and paprika.', 'name': 'Crispy Roasted Chicken', 'aggregateRating': {'@type': 'AggregateRating', 'ratingValue': '4.7', 'ratingCount': '134'}, 'cookTime': 'PT60M', 'nutrition': {'@type': 'NutritionInformation', 'calories': '495 kcal', 'carbohydrateContent': '3 g', 'cholesterolContent': '129 mg', 'fiberContent': '1 g', 'proteinContent': '42 g', 'saturatedFatContent': '8 g', 'sodiumContent': '446 mg', 'sugarContent': '1 g', 'fatContent': '35 g', 'unsaturatedFatContent': '0 g'}, 'prepTime': 'PT20M', 'recipeCategory': ['Dinner', 'Drink'], 'recipeCuisine': ['German'], 'recipeIngredient': ['1 teaspoon kosher salt', '0.5 teaspoon caraway seeds', '0.5 teaspoon dried sage', '0.25 teaspoon fennel seeds', '0.25 teaspoon coriander seeds', '0.25 teaspoon dried rosemary', '2 tablespoons paprika', '2 teaspoons garlic powder', '2 teaspoons all-purpose flour', '1 teaspoon onion powder', '5 tablespoons vegetable oil', '1 (4 pound) broiler-fryer chicken, cut in half lengthwise'], 'recipeInstructions': [{'@type': 'HowToStep', 'text': 'Preheat the oven to 425 degrees F (220 degrees C).'}, {'@type': 'HowToStep', 'text': 'Combine kosher salt, caraway, sage, fennel, coriander, and rosemary in a spice grinder or mortar. Grind to a coarse powder. Transfer spice mixture to a bowl; stir in paprika, garlic powder, flour, and onion powder. Mix in vegetable oil to make a smooth paste.'}, {'@type': 'HowToStep', 'text': 'Pat chicken halves dry with paper towels and tuck wing tips behind the back. Brush spice paste onto chicken halves, coating all sides and seasoning under wings and legs. Place chicken halves, skin-sides up, in a baking dish or roasting pan, leaving space around chickens so halves aren't touching.'}, {'@type': 'HowToStep', 'text': 'Roast in the preheated oven until no longer pink at the bone and the juices run clear, about 1 hour. An instant-read thermometer inserted into the thickest part of the thigh, near the bone, should read 165 degrees F (74 degrees C). Remove from the oven and let rest for 10 minutes before slicing.'}], 'recipeYield': ['6'], 'totalTime': 'PT90M', 'about': []}\u001b[0m\n", "Thought:\u001b[32;1m\u001b[1;3mI have fetched the first baked chicken recipe and its ingredients are:\n", "- 1 teaspoon kosher salt\n", "- 0.5 teaspoon caraway seeds\n", "- 0.5 teaspoon dried sage\n", "- 0.25 teaspoon fennel seeds\n", "- 0.25 teaspoon coriander seeds\n", "- 0.25 teaspoon dried rosemary\n", "- 2 tablespoons paprika\n", "- 2 teaspoons garlic powder\n", "- 2 teaspoons all-purpose flour\n", "- 1 teaspoon onion powder\n", "- 5 tablespoons vegetable oil\n", "- 1 (4 pound) broiler-fryer chicken, cut in half lengthwise\n", "\n", "Final Answer: The ingredients needed for the first baked chicken recipe are listed above.\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n", "gpt-3.5-turbo\n", "\n", "\n", "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", "\u001b[32;1m\u001b[1;3mI need to use the Chicken Recipes tool to get a list of all the chicken recipes.\n", "Action: Chicken Recipes tool\n", "Action Input: None\u001b[0m\n", "Observation: \u001b[36;1m\u001b[1;3m['https://www.allrecipes.com/recipe/228363/crispy-roasted-chicken/', 'https://www.allrecipes.com/recipe/254877/roasted-spatchcocked-chicken-with-potatoes/', 'https://www.allrecipes.com/recipe/235153/easy-baked-chicken-thighs/', 'https://www.allrecipes.com/recipe/258878/crispy-baked-chicken-thighs/', 'https://www.allrecipes.com/recipe/235151/crispy-and-tender-baked-chicken-thighs/', 'https://www.allrecipes.com/recipe/233953/million-dollar-chicken/', 'https://www.allrecipes.com/recipe/70679/simple-whole-roasted-chicken/', 'https://www.allrecipes.com/recipe/214618/beer-can-chicken/', 'https://www.allrecipes.com/recipe/272858/air-fryer-chicken-thighs/', 'https://www.allrecipes.com/recipe/214478/happy-roast-chicken/']\u001b[0m\n", "Thought:\u001b[32;1m\u001b[1;3mI have a list of chicken recipes.\n", "Final Answer: ['https://www.allrecipes.com/recipe/228363/crispy-roasted-chicken/', 'https://www.allrecipes.com/recipe/254877/roasted-spatchcocked-chicken-with-potatoes/', 'https://www.allrecipes.com/recipe/235153/easy-baked-chicken-thighs/', 'https://www.allrecipes.com/recipe/258878/crispy-baked-chicken-thighs/', 'https://www.allrecipes.com/recipe/235151/crispy-and-tender-baked-chicken-thighs/', 'https://www.allrecipes.com/recipe/233953/million-dollar-chicken/', 'https://www.allrecipes.com/recipe/70679/simple-whole-roasted-chicken/', 'https://www.allrecipes.com/recipe/214618/beer-can-chicken/', 'https://www.allrecipes.com/recipe/272858/air-fryer-chicken-thighs/', 'https://www.allrecipes.com/recipe/214478/happy-roast-chicken/']\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] } ], "source": [ "import gradio as gr\n", "\n", "with gr.Blocks() as demo:\n", " gr.Markdown(\"Start typing below and then click **Run** to see the output.\")\n", " with gr.Row():\n", " with gr.Column():\n", " modelDD = gr.Dropdown(['gpt-3.5-turbo', 'gpt-4'], value='gpt-3.5-turbo')\n", " openAIKey = gr.Textbox(placeholder=\"Paste your OpenAI API Key Here\", label=\"OpenAI API Key\") \n", " inp = gr.Textbox(placeholder=\"Type your question here...\", label=\"Question\") \n", " btn = gr.Button(\"Run\") \n", " examples = [\"Can you show tell me what ingredients I need for the first baked chicken recipe?\",\n", " \"I have 15 guests coming to visit, if I make the 'Chocolate Snack Cake' recipe will there be enough? \",\n", " \"Can you show tell me what ingredients I need for the last baked chicken recipe? \",\n", " \"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", " \"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", " \"Which is healthier the Caramel Apple Pie Cookies or the beer chicken? Please explain how you got to your answer.\",\n", " \"Is the moon closer to earth or the sun?\",\n", " \"How good are the apple pie cookies?\",\n", " \"What tools do I need for the Nutella Ice Cream?\",\n", " \"My bowl is broken, can I still make Nutella Ice Cream? Answer as a yes/no.\"\n", " ] \n", " gr.Examples(examples, [inp])\n", " with gr.Column():\n", " out = gr.TextArea(label=\"ReAct Answer\", placeholder=\"The answer will go here...\")\n", " \n", " btn.click(fn=ask_query, inputs=[modelDD, openAIKey, inp], outputs=out)\n", " \n", "demo.launch(show_error=True)" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.6" } }, "nbformat": 4, "nbformat_minor": 2 }