Playing with LLM and recipe lists.
Browse files
demo-tools-1.ipynb
ADDED
@@ -0,0 +1,309 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 1,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [],
|
8 |
+
"source": [
|
9 |
+
"import os\n",
|
10 |
+
"import json\n",
|
11 |
+
"\n",
|
12 |
+
"# Load setting from Json outside of project.\n",
|
13 |
+
"f = open('../../env/ai.json')\n",
|
14 |
+
"settingsJson = json.load(f)\n",
|
15 |
+
"del f\n",
|
16 |
+
"\n",
|
17 |
+
"for key in settingsJson:\n",
|
18 |
+
" os.environ[key] = settingsJson[key]\n",
|
19 |
+
" \n",
|
20 |
+
"del settingsJson\n",
|
21 |
+
" \n",
|
22 |
+
"# # OR manually set them\n",
|
23 |
+
"# os.environ['REQUESTS_CA_BUNDLE'] = '../../env/ZCert.pem'\n",
|
24 |
+
"# os.environ['HUGGING_FACE_API_KEY'] = 'Get here: https://huggingface.co/settings/tokens'\n",
|
25 |
+
"# os.environ['OPENAI_API_KEY'] = 'Get here: https://platform.openai.com/account/api-keys'\n",
|
26 |
+
"# os.environ[\"SERPAPI_API_KEY\"] = 'serpapi KEY, Get here: https://serpapi.com/manage-api-key'"
|
27 |
+
]
|
28 |
+
},
|
29 |
+
{
|
30 |
+
"cell_type": "markdown",
|
31 |
+
"metadata": {},
|
32 |
+
"source": [
|
33 |
+
"# Setup Web Crawler and Lookup functions"
|
34 |
+
]
|
35 |
+
},
|
36 |
+
{
|
37 |
+
"cell_type": "code",
|
38 |
+
"execution_count": 2,
|
39 |
+
"metadata": {},
|
40 |
+
"outputs": [],
|
41 |
+
"source": [
|
42 |
+
"import requests\n",
|
43 |
+
"from bs4 import BeautifulSoup\n",
|
44 |
+
"\n",
|
45 |
+
"import dateutil.parser\n",
|
46 |
+
"from datetime import datetime\n",
|
47 |
+
"import json\n",
|
48 |
+
"\n",
|
49 |
+
"def get_recipe_as_json(url: str) -> dict: \n",
|
50 |
+
" html = requests.get(url).text \n",
|
51 |
+
" soup = BeautifulSoup(html)\n",
|
52 |
+
" script = soup.find_all(\"script\", {\"id\": \"allrecipes-schema_1-0\"})\n",
|
53 |
+
"\n",
|
54 |
+
" recipeDict = json.loads(script[0].text)[0]\n",
|
55 |
+
" del recipeDict['review']\n",
|
56 |
+
" del recipeDict['image']\n",
|
57 |
+
" del recipeDict['mainEntityOfPage']\n",
|
58 |
+
" del recipeDict['publisher']\n",
|
59 |
+
" del recipeDict['thumbnailUrl']\n",
|
60 |
+
" \n",
|
61 |
+
" return recipeDict\n",
|
62 |
+
"\n",
|
63 |
+
"# url = \"https://www.allrecipes.com/recipe/212498/easy-chicken-and-broccoli-alfredo/\"\n",
|
64 |
+
"# obj = get_recipe_as_json(url)"
|
65 |
+
]
|
66 |
+
},
|
67 |
+
{
|
68 |
+
"cell_type": "markdown",
|
69 |
+
"metadata": {},
|
70 |
+
"source": [
|
71 |
+
"# Static recipe lists"
|
72 |
+
]
|
73 |
+
},
|
74 |
+
{
|
75 |
+
"cell_type": "code",
|
76 |
+
"execution_count": 3,
|
77 |
+
"metadata": {},
|
78 |
+
"outputs": [],
|
79 |
+
"source": [
|
80 |
+
"dessertList = [\n",
|
81 |
+
" {\n",
|
82 |
+
" \"title\": \"Chocolate Snack Cake\",\n",
|
83 |
+
" \"url\": \"https://www.allrecipes.com/chocolate-snack-cake-recipe-8350343\"\n",
|
84 |
+
" },\n",
|
85 |
+
" {\n",
|
86 |
+
" \"title\": \"Charred Spiced Pears with Smoky Vanilla Cherry Sauce\",\n",
|
87 |
+
" \"url\": \"https://www.allrecipes.com/charred-spiced-pears-with-smoky-vanilla-cherry-sauce-recipe-8347080\"\n",
|
88 |
+
" },\n",
|
89 |
+
" {\n",
|
90 |
+
" \"title\": \"Meringue Topped Banana Pudding\",\n",
|
91 |
+
" \"url\": \"https://www.allrecipes.com/meringue-topped-banana-pudding-recipe-8347040\"\n",
|
92 |
+
" },\n",
|
93 |
+
" {\n",
|
94 |
+
" \"title\": \"White Chocolate Cinnamon Toast Crunch Bars\",\n",
|
95 |
+
" \"url\": \"https://www.allrecipes.com/white-chocolate-cinnamon-toast-crunch-bars-recipe-7556790\"\n",
|
96 |
+
" },\n",
|
97 |
+
" {\n",
|
98 |
+
" \"title\": \"Plum Cobbler for Two\",\n",
|
99 |
+
" \"url\": \"https://www.allrecipes.com/plum-cobbler-for-two-recipe-8304143\"\n",
|
100 |
+
" },\n",
|
101 |
+
" {\n",
|
102 |
+
" \"title\": \"Pumpkin Cheesecake Cookies\",\n",
|
103 |
+
" \"url\": \"https://www.allrecipes.com/pumpkin-cheesecake-cookies-recipe-7972485\"\n",
|
104 |
+
" },\n",
|
105 |
+
" {\n",
|
106 |
+
" \"title\": \"Chocolate Whipped Cottage Cheese\",\n",
|
107 |
+
" \"url\": \"https://www.allrecipes.com/chocolate-whipped-cottage-cheese-recipe-8303272\"\n",
|
108 |
+
" },\n",
|
109 |
+
" {\n",
|
110 |
+
" \"title\": \"Nutella Ice Cream\",\n",
|
111 |
+
" \"url\": \"https://www.allrecipes.com/nutella-ice-cream-recipe-7508716\"\n",
|
112 |
+
" },\n",
|
113 |
+
" {\n",
|
114 |
+
" \"title\": \"3-Ingredient Banana Oatmeal Cookies\",\n",
|
115 |
+
" \"url\": \"https://www.allrecipes.com/3-ingredient-banana-oatmeal-cookies-recipe-7972686\"\n",
|
116 |
+
" },\n",
|
117 |
+
" {\n",
|
118 |
+
" \"title\": \"Caramel Apple Pie Cookies\",\n",
|
119 |
+
" \"url\": \"https://www.allrecipes.com/caramel-apple-pie-cookies-recipe-7642173\"\n",
|
120 |
+
" }\n",
|
121 |
+
"]\n",
|
122 |
+
"\n",
|
123 |
+
"chickenDishList = [\n",
|
124 |
+
" {\n",
|
125 |
+
" \"title\": \"Crispy Roasted Chicken\",\n",
|
126 |
+
" \"url\": \"https://www.allrecipes.com/recipe/228363/crispy-roasted-chicken/\"\n",
|
127 |
+
" },\n",
|
128 |
+
" {\n",
|
129 |
+
" \"title\": \"Roasted Spatchcocked Chicken With Potatoes\",\n",
|
130 |
+
" \"url\": \"https://www.allrecipes.com/recipe/254877/roasted-spatchcocked-chicken-with-potatoes/\"\n",
|
131 |
+
" },\n",
|
132 |
+
" {\n",
|
133 |
+
" \"title\": \"Easy Baked Chicken Thighs\",\n",
|
134 |
+
" \"url\": \"https://www.allrecipes.com/recipe/235153/easy-baked-chicken-thighs/\"\n",
|
135 |
+
" },\n",
|
136 |
+
" {\n",
|
137 |
+
" \"title\": \"Crispy Baked Chicken Thighs\",\n",
|
138 |
+
" \"url\": \"https://www.allrecipes.com/recipe/258878/crispy-baked-chicken-thighs/\"\n",
|
139 |
+
" },\n",
|
140 |
+
" {\n",
|
141 |
+
" \"title\": \"Crispy and Tender Baked Chicken Thighs\",\n",
|
142 |
+
" \"url\": \"https://www.allrecipes.com/recipe/235151/crispy-and-tender-baked-chicken-thighs/\"\n",
|
143 |
+
" },\n",
|
144 |
+
" {\n",
|
145 |
+
" \"title\": \"Million Dollar Chicken\",\n",
|
146 |
+
" \"url\": \"https://www.allrecipes.com/recipe/233953/million-dollar-chicken/\"\n",
|
147 |
+
" },\n",
|
148 |
+
" {\n",
|
149 |
+
" \"title\": \"Simple Whole Roasted Chicken\",\n",
|
150 |
+
" \"url\": \"https://www.allrecipes.com/recipe/70679/simple-whole-roasted-chicken/\"\n",
|
151 |
+
" },\n",
|
152 |
+
" {\n",
|
153 |
+
" \"title\": \"Beer Can Chicken\",\n",
|
154 |
+
" \"url\": \"https://www.allrecipes.com/recipe/214618/beer-can-chicken/\"\n",
|
155 |
+
" },\n",
|
156 |
+
" {\n",
|
157 |
+
" \"title\": \"Air Fryer Chicken Thighs\",\n",
|
158 |
+
" \"url\": \"https://www.allrecipes.com/recipe/272858/air-fryer-chicken-thighs/\"\n",
|
159 |
+
" },\n",
|
160 |
+
" {\n",
|
161 |
+
" \"title\": \"Happy Roast Chicken\",\n",
|
162 |
+
" \"url\": \"https://www.allrecipes.com/recipe/214478/happy-roast-chicken/\"\n",
|
163 |
+
" }\n",
|
164 |
+
"]\n",
|
165 |
+
"\n"
|
166 |
+
]
|
167 |
+
},
|
168 |
+
{
|
169 |
+
"cell_type": "markdown",
|
170 |
+
"metadata": {},
|
171 |
+
"source": [
|
172 |
+
"# Setup Tools"
|
173 |
+
]
|
174 |
+
},
|
175 |
+
{
|
176 |
+
"cell_type": "code",
|
177 |
+
"execution_count": 4,
|
178 |
+
"metadata": {},
|
179 |
+
"outputs": [],
|
180 |
+
"source": [
|
181 |
+
"# Tools \n",
|
182 |
+
"from langchain.agents import Tool\n",
|
183 |
+
"from langchain.tools import BaseTool\n",
|
184 |
+
"\n",
|
185 |
+
"# Chicken functions\n",
|
186 |
+
"def list_chicken_recipes(query: str): \n",
|
187 |
+
" return chickenDishList\n",
|
188 |
+
"\n",
|
189 |
+
"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",
|
190 |
+
"\n",
|
191 |
+
"# Dessert functions\n",
|
192 |
+
"def list_dessert_recipes(query: str): \n",
|
193 |
+
" return dessertList\n",
|
194 |
+
"\n",
|
195 |
+
"list_dessert_recipes_tool = Tool(name='Dessert Recipes tool', func=list_dessert_recipes, \n",
|
196 |
+
" 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",
|
197 |
+
"\n",
|
198 |
+
"# Recipe fetcher functions\n",
|
199 |
+
"def get_recipe(url: str): \n",
|
200 |
+
" return get_recipe_as_json(url)\n",
|
201 |
+
"\n",
|
202 |
+
"get_recipe_as_json_tool = Tool(name='Get a Recipe tool', func=get_recipe, description=\"\"\"\n",
|
203 |
+
" Useful for fetching a particular recipe. \n",
|
204 |
+
" It uses the https://schema.org/Recipe format to store it's recipes. \n",
|
205 |
+
" \"\"\")\n",
|
206 |
+
"\n",
|
207 |
+
"# Tool list\n",
|
208 |
+
"tools = [list_chicken_recipes_tool, list_dessert_recipes_tool, get_recipe_as_json_tool]"
|
209 |
+
]
|
210 |
+
},
|
211 |
+
{
|
212 |
+
"cell_type": "code",
|
213 |
+
"execution_count": 6,
|
214 |
+
"metadata": {},
|
215 |
+
"outputs": [
|
216 |
+
{
|
217 |
+
"name": "stdout",
|
218 |
+
"output_type": "stream",
|
219 |
+
"text": [
|
220 |
+
"\n",
|
221 |
+
"\n",
|
222 |
+
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
223 |
+
"\u001b[32;1m\u001b[1;3mI need to check the Chicken Recipes tool to see if there is a recipe that includes alcohol in its ingredients.\n",
|
224 |
+
"Action: Chicken Recipes tool\n",
|
225 |
+
"Action Input: None\u001b[0m\n",
|
226 |
+
"Observation: \u001b[36;1m\u001b[1;3m[{'title': 'Crispy Roasted Chicken', 'url': 'https://www.allrecipes.com/recipe/228363/crispy-roasted-chicken/'}, {'title': 'Roasted Spatchcocked Chicken With Potatoes', 'url': 'https://www.allrecipes.com/recipe/254877/roasted-spatchcocked-chicken-with-potatoes/'}, {'title': 'Easy Baked Chicken Thighs', 'url': 'https://www.allrecipes.com/recipe/235153/easy-baked-chicken-thighs/'}, {'title': 'Crispy Baked Chicken Thighs', 'url': 'https://www.allrecipes.com/recipe/258878/crispy-baked-chicken-thighs/'}, {'title': 'Crispy and Tender Baked Chicken Thighs', 'url': 'https://www.allrecipes.com/recipe/235151/crispy-and-tender-baked-chicken-thighs/'}, {'title': 'Million Dollar Chicken', 'url': 'https://www.allrecipes.com/recipe/233953/million-dollar-chicken/'}, {'title': 'Simple Whole Roasted Chicken', 'url': 'https://www.allrecipes.com/recipe/70679/simple-whole-roasted-chicken/'}, {'title': 'Beer Can Chicken', 'url': 'https://www.allrecipes.com/recipe/214618/beer-can-chicken/'}, {'title': 'Air Fryer Chicken Thighs', 'url': 'https://www.allrecipes.com/recipe/272858/air-fryer-chicken-thighs/'}, {'title': 'Happy Roast Chicken', 'url': 'https://www.allrecipes.com/recipe/214478/happy-roast-chicken/'}]\u001b[0m\n",
|
227 |
+
"Thought:\u001b[32;1m\u001b[1;3mThe 'Beer Can Chicken' recipe likely includes alcohol as an ingredient. I should use the Get a Recipe tool to confirm this and find out how long it takes to prepare.\n",
|
228 |
+
"Action: Get a Recipe tool\n",
|
229 |
+
"Action Input: 'https://www.allrecipes.com/recipe/214618/beer-can-chicken/'\u001b[0m"
|
230 |
+
]
|
231 |
+
},
|
232 |
+
{
|
233 |
+
"ename": "InvalidSchema",
|
234 |
+
"evalue": "No connection adapters were found for \"'https://www.allrecipes.com/recipe/214618/beer-can-chicken/'\"",
|
235 |
+
"output_type": "error",
|
236 |
+
"traceback": [
|
237 |
+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
|
238 |
+
"\u001b[1;31mInvalidSchema\u001b[0m Traceback (most recent call last)",
|
239 |
+
"\u001b[1;32mc:\\sc\\ai\\rag-demo-1\\demo-tools-1.ipynb Cell 8\u001b[0m line \u001b[0;36m2\n\u001b[0;32m <a href='vscode-notebook-cell:/c%3A/sc/ai/rag-demo-1/demo-tools-1.ipynb#W4sZmlsZQ%3D%3D?line=12'>13</a>\u001b[0m zero_shot_agent \u001b[39m=\u001b[39m initialize_agent(\n\u001b[0;32m <a href='vscode-notebook-cell:/c%3A/sc/ai/rag-demo-1/demo-tools-1.ipynb#W4sZmlsZQ%3D%3D?line=13'>14</a>\u001b[0m agent\u001b[39m=\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mzero-shot-react-description\u001b[39m\u001b[39m\"\u001b[39m, \n\u001b[0;32m <a href='vscode-notebook-cell:/c%3A/sc/ai/rag-demo-1/demo-tools-1.ipynb#W4sZmlsZQ%3D%3D?line=14'>15</a>\u001b[0m tools\u001b[39m=\u001b[39mtools, \n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m <a href='vscode-notebook-cell:/c%3A/sc/ai/rag-demo-1/demo-tools-1.ipynb#W4sZmlsZQ%3D%3D?line=17'>18</a>\u001b[0m max_iterations\u001b[39m=\u001b[39m\u001b[39m7\u001b[39m,\n\u001b[0;32m <a href='vscode-notebook-cell:/c%3A/sc/ai/rag-demo-1/demo-tools-1.ipynb#W4sZmlsZQ%3D%3D?line=18'>19</a>\u001b[0m )\n\u001b[0;32m <a href='vscode-notebook-cell:/c%3A/sc/ai/rag-demo-1/demo-tools-1.ipynb#W4sZmlsZQ%3D%3D?line=20'>21</a>\u001b[0m \u001b[39m#zero_shot_agent.run(\"Can you show tell me what ingredients I need for the first baked chicken recipe? \")\u001b[39;00m\n\u001b[0;32m <a href='vscode-notebook-cell:/c%3A/sc/ai/rag-demo-1/demo-tools-1.ipynb#W4sZmlsZQ%3D%3D?line=21'>22</a>\u001b[0m \u001b[39m#zero_shot_agent.run(\"Can you show tell me what ingredients I need for the last baked chicken recipe? \")\u001b[39;00m\n\u001b[0;32m <a href='vscode-notebook-cell:/c%3A/sc/ai/rag-demo-1/demo-tools-1.ipynb#W4sZmlsZQ%3D%3D?line=22'>23</a>\u001b[0m \u001b[39m#zero_shot_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\u001b[39;00m\n\u001b[1;32m---> <a href='vscode-notebook-cell:/c%3A/sc/ai/rag-demo-1/demo-tools-1.ipynb#W4sZmlsZQ%3D%3D?line=23'>24</a>\u001b[0m zero_shot_agent\u001b[39m.\u001b[39;49mrun(\u001b[39m\"\u001b[39;49m\u001b[39mIs there a Chicken recipe that\u001b[39;49m\u001b[39m'\u001b[39;49m\u001b[39ms prepared with an alchohol? And if so how long does it take to prepare?\u001b[39;49m\u001b[39m\"\u001b[39;49m)\n",
|
240 |
+
"File \u001b[1;32mc:\\sc\\ai\\rag-demo-1\\.venv\\lib\\site-packages\\langchain\\chains\\base.py:503\u001b[0m, in \u001b[0;36mChain.run\u001b[1;34m(self, callbacks, tags, metadata, *args, **kwargs)\u001b[0m\n\u001b[0;32m 501\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mlen\u001b[39m(args) \u001b[39m!=\u001b[39m \u001b[39m1\u001b[39m:\n\u001b[0;32m 502\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mValueError\u001b[39;00m(\u001b[39m\"\u001b[39m\u001b[39m`run` supports only one positional argument.\u001b[39m\u001b[39m\"\u001b[39m)\n\u001b[1;32m--> 503\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39;49m(args[\u001b[39m0\u001b[39;49m], callbacks\u001b[39m=\u001b[39;49mcallbacks, tags\u001b[39m=\u001b[39;49mtags, metadata\u001b[39m=\u001b[39;49mmetadata)[\n\u001b[0;32m 504\u001b[0m _output_key\n\u001b[0;32m 505\u001b[0m ]\n\u001b[0;32m 507\u001b[0m \u001b[39mif\u001b[39;00m kwargs \u001b[39mand\u001b[39;00m \u001b[39mnot\u001b[39;00m args:\n\u001b[0;32m 508\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39m(kwargs, callbacks\u001b[39m=\u001b[39mcallbacks, tags\u001b[39m=\u001b[39mtags, metadata\u001b[39m=\u001b[39mmetadata)[\n\u001b[0;32m 509\u001b[0m _output_key\n\u001b[0;32m 510\u001b[0m ]\n",
|
241 |
+
"File \u001b[1;32mc:\\sc\\ai\\rag-demo-1\\.venv\\lib\\site-packages\\langchain\\chains\\base.py:308\u001b[0m, in \u001b[0;36mChain.__call__\u001b[1;34m(self, inputs, return_only_outputs, callbacks, tags, metadata, run_name, include_run_info)\u001b[0m\n\u001b[0;32m 306\u001b[0m \u001b[39mexcept\u001b[39;00m \u001b[39mBaseException\u001b[39;00m \u001b[39mas\u001b[39;00m e:\n\u001b[0;32m 307\u001b[0m run_manager\u001b[39m.\u001b[39mon_chain_error(e)\n\u001b[1;32m--> 308\u001b[0m \u001b[39mraise\u001b[39;00m e\n\u001b[0;32m 309\u001b[0m run_manager\u001b[39m.\u001b[39mon_chain_end(outputs)\n\u001b[0;32m 310\u001b[0m final_outputs: Dict[\u001b[39mstr\u001b[39m, Any] \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mprep_outputs(\n\u001b[0;32m 311\u001b[0m inputs, outputs, return_only_outputs\n\u001b[0;32m 312\u001b[0m )\n",
|
242 |
+
"File \u001b[1;32mc:\\sc\\ai\\rag-demo-1\\.venv\\lib\\site-packages\\langchain\\chains\\base.py:302\u001b[0m, in \u001b[0;36mChain.__call__\u001b[1;34m(self, inputs, return_only_outputs, callbacks, tags, metadata, run_name, include_run_info)\u001b[0m\n\u001b[0;32m 295\u001b[0m run_manager \u001b[39m=\u001b[39m callback_manager\u001b[39m.\u001b[39mon_chain_start(\n\u001b[0;32m 296\u001b[0m dumpd(\u001b[39mself\u001b[39m),\n\u001b[0;32m 297\u001b[0m inputs,\n\u001b[0;32m 298\u001b[0m name\u001b[39m=\u001b[39mrun_name,\n\u001b[0;32m 299\u001b[0m )\n\u001b[0;32m 300\u001b[0m \u001b[39mtry\u001b[39;00m:\n\u001b[0;32m 301\u001b[0m outputs \u001b[39m=\u001b[39m (\n\u001b[1;32m--> 302\u001b[0m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_call(inputs, run_manager\u001b[39m=\u001b[39;49mrun_manager)\n\u001b[0;32m 303\u001b[0m \u001b[39mif\u001b[39;00m new_arg_supported\n\u001b[0;32m 304\u001b[0m \u001b[39melse\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_call(inputs)\n\u001b[0;32m 305\u001b[0m )\n\u001b[0;32m 306\u001b[0m \u001b[39mexcept\u001b[39;00m \u001b[39mBaseException\u001b[39;00m \u001b[39mas\u001b[39;00m e:\n\u001b[0;32m 307\u001b[0m run_manager\u001b[39m.\u001b[39mon_chain_error(e)\n",
|
243 |
+
"File \u001b[1;32mc:\\sc\\ai\\rag-demo-1\\.venv\\lib\\site-packages\\langchain\\agents\\agent.py:1141\u001b[0m, in \u001b[0;36mAgentExecutor._call\u001b[1;34m(self, inputs, run_manager)\u001b[0m\n\u001b[0;32m 1139\u001b[0m \u001b[39m# We now enter the agent loop (until it returns something).\u001b[39;00m\n\u001b[0;32m 1140\u001b[0m \u001b[39mwhile\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_should_continue(iterations, time_elapsed):\n\u001b[1;32m-> 1141\u001b[0m next_step_output \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_take_next_step(\n\u001b[0;32m 1142\u001b[0m name_to_tool_map,\n\u001b[0;32m 1143\u001b[0m color_mapping,\n\u001b[0;32m 1144\u001b[0m inputs,\n\u001b[0;32m 1145\u001b[0m intermediate_steps,\n\u001b[0;32m 1146\u001b[0m run_manager\u001b[39m=\u001b[39;49mrun_manager,\n\u001b[0;32m 1147\u001b[0m )\n\u001b[0;32m 1148\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39misinstance\u001b[39m(next_step_output, AgentFinish):\n\u001b[0;32m 1149\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_return(\n\u001b[0;32m 1150\u001b[0m next_step_output, intermediate_steps, run_manager\u001b[39m=\u001b[39mrun_manager\n\u001b[0;32m 1151\u001b[0m )\n",
|
244 |
+
"File \u001b[1;32mc:\\sc\\ai\\rag-demo-1\\.venv\\lib\\site-packages\\langchain\\agents\\agent.py:991\u001b[0m, in \u001b[0;36mAgentExecutor._take_next_step\u001b[1;34m(self, name_to_tool_map, color_mapping, inputs, intermediate_steps, run_manager)\u001b[0m\n\u001b[0;32m 989\u001b[0m tool_run_kwargs[\u001b[39m\"\u001b[39m\u001b[39mllm_prefix\u001b[39m\u001b[39m\"\u001b[39m] \u001b[39m=\u001b[39m \u001b[39m\"\u001b[39m\u001b[39m\"\u001b[39m\n\u001b[0;32m 990\u001b[0m \u001b[39m# We then call the tool on the tool input to get an observation\u001b[39;00m\n\u001b[1;32m--> 991\u001b[0m observation \u001b[39m=\u001b[39m tool\u001b[39m.\u001b[39mrun(\n\u001b[0;32m 992\u001b[0m agent_action\u001b[39m.\u001b[39mtool_input,\n\u001b[0;32m 993\u001b[0m verbose\u001b[39m=\u001b[39m\u001b[39mself\u001b[39m\u001b[39m.\u001b[39mverbose,\n\u001b[0;32m 994\u001b[0m color\u001b[39m=\u001b[39mcolor,\n\u001b[0;32m 995\u001b[0m callbacks\u001b[39m=\u001b[39mrun_manager\u001b[39m.\u001b[39mget_child() \u001b[39mif\u001b[39;00m run_manager \u001b[39melse\u001b[39;00m \u001b[39mNone\u001b[39;00m,\n\u001b[0;32m 996\u001b[0m \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mtool_run_kwargs,\n\u001b[0;32m 997\u001b[0m )\n\u001b[0;32m 998\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[0;32m 999\u001b[0m tool_run_kwargs \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39magent\u001b[39m.\u001b[39mtool_run_logging_kwargs()\n",
|
245 |
+
"File \u001b[1;32mc:\\sc\\ai\\rag-demo-1\\.venv\\lib\\site-packages\\langchain\\tools\\base.py:364\u001b[0m, in \u001b[0;36mBaseTool.run\u001b[1;34m(self, tool_input, verbose, start_color, color, callbacks, tags, metadata, run_name, **kwargs)\u001b[0m\n\u001b[0;32m 362\u001b[0m \u001b[39mexcept\u001b[39;00m (\u001b[39mException\u001b[39;00m, \u001b[39mKeyboardInterrupt\u001b[39;00m) \u001b[39mas\u001b[39;00m e:\n\u001b[0;32m 363\u001b[0m run_manager\u001b[39m.\u001b[39mon_tool_error(e)\n\u001b[1;32m--> 364\u001b[0m \u001b[39mraise\u001b[39;00m e\n\u001b[0;32m 365\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[0;32m 366\u001b[0m run_manager\u001b[39m.\u001b[39mon_tool_end(\n\u001b[0;32m 367\u001b[0m \u001b[39mstr\u001b[39m(observation), color\u001b[39m=\u001b[39mcolor, name\u001b[39m=\u001b[39m\u001b[39mself\u001b[39m\u001b[39m.\u001b[39mname, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs\n\u001b[0;32m 368\u001b[0m )\n",
|
246 |
+
"File \u001b[1;32mc:\\sc\\ai\\rag-demo-1\\.venv\\lib\\site-packages\\langchain\\tools\\base.py:336\u001b[0m, in \u001b[0;36mBaseTool.run\u001b[1;34m(self, tool_input, verbose, start_color, color, callbacks, tags, metadata, run_name, **kwargs)\u001b[0m\n\u001b[0;32m 333\u001b[0m \u001b[39mtry\u001b[39;00m:\n\u001b[0;32m 334\u001b[0m tool_args, tool_kwargs \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_to_args_and_kwargs(parsed_input)\n\u001b[0;32m 335\u001b[0m observation \u001b[39m=\u001b[39m (\n\u001b[1;32m--> 336\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_run(\u001b[39m*\u001b[39mtool_args, run_manager\u001b[39m=\u001b[39mrun_manager, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mtool_kwargs)\n\u001b[0;32m 337\u001b[0m \u001b[39mif\u001b[39;00m new_arg_supported\n\u001b[0;32m 338\u001b[0m \u001b[39melse\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_run(\u001b[39m*\u001b[39mtool_args, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mtool_kwargs)\n\u001b[0;32m 339\u001b[0m )\n\u001b[0;32m 340\u001b[0m \u001b[39mexcept\u001b[39;00m ToolException \u001b[39mas\u001b[39;00m e:\n\u001b[0;32m 341\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mhandle_tool_error:\n",
|
247 |
+
"File \u001b[1;32mc:\\sc\\ai\\rag-demo-1\\.venv\\lib\\site-packages\\langchain\\tools\\base.py:515\u001b[0m, in \u001b[0;36mTool._run\u001b[1;34m(self, run_manager, *args, **kwargs)\u001b[0m\n\u001b[0;32m 506\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mfunc:\n\u001b[0;32m 507\u001b[0m new_argument_supported \u001b[39m=\u001b[39m signature(\u001b[39mself\u001b[39m\u001b[39m.\u001b[39mfunc)\u001b[39m.\u001b[39mparameters\u001b[39m.\u001b[39mget(\u001b[39m\"\u001b[39m\u001b[39mcallbacks\u001b[39m\u001b[39m\"\u001b[39m)\n\u001b[0;32m 508\u001b[0m \u001b[39mreturn\u001b[39;00m (\n\u001b[0;32m 509\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mfunc(\n\u001b[0;32m 510\u001b[0m \u001b[39m*\u001b[39margs,\n\u001b[0;32m 511\u001b[0m callbacks\u001b[39m=\u001b[39mrun_manager\u001b[39m.\u001b[39mget_child() \u001b[39mif\u001b[39;00m run_manager \u001b[39melse\u001b[39;00m \u001b[39mNone\u001b[39;00m,\n\u001b[0;32m 512\u001b[0m \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs,\n\u001b[0;32m 513\u001b[0m )\n\u001b[0;32m 514\u001b[0m \u001b[39mif\u001b[39;00m new_argument_supported\n\u001b[1;32m--> 515\u001b[0m \u001b[39melse\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mfunc(\u001b[39m*\u001b[39margs, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs)\n\u001b[0;32m 516\u001b[0m )\n\u001b[0;32m 517\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mNotImplementedError\u001b[39;00m(\u001b[39m\"\u001b[39m\u001b[39mTool does not support sync\u001b[39m\u001b[39m\"\u001b[39m)\n",
|
248 |
+
"\u001b[1;32mc:\\sc\\ai\\rag-demo-1\\demo-tools-1.ipynb Cell 8\u001b[0m line \u001b[0;36m2\n\u001b[0;32m <a href='vscode-notebook-cell:/c%3A/sc/ai/rag-demo-1/demo-tools-1.ipynb#W4sZmlsZQ%3D%3D?line=18'>19</a>\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mget_recipe\u001b[39m(url: \u001b[39mstr\u001b[39m): \n\u001b[1;32m---> <a href='vscode-notebook-cell:/c%3A/sc/ai/rag-demo-1/demo-tools-1.ipynb#W4sZmlsZQ%3D%3D?line=19'>20</a>\u001b[0m \u001b[39mreturn\u001b[39;00m get_recipe_as_json(url)\n",
|
249 |
+
"\u001b[1;32mc:\\sc\\ai\\rag-demo-1\\demo-tools-1.ipynb Cell 8\u001b[0m line \u001b[0;36m9\n\u001b[0;32m <a href='vscode-notebook-cell:/c%3A/sc/ai/rag-demo-1/demo-tools-1.ipynb#W4sZmlsZQ%3D%3D?line=7'>8</a>\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mget_recipe_as_json\u001b[39m(url: \u001b[39mstr\u001b[39m) \u001b[39m-\u001b[39m\u001b[39m>\u001b[39m \u001b[39mdict\u001b[39m: \n\u001b[1;32m----> <a href='vscode-notebook-cell:/c%3A/sc/ai/rag-demo-1/demo-tools-1.ipynb#W4sZmlsZQ%3D%3D?line=8'>9</a>\u001b[0m html \u001b[39m=\u001b[39m requests\u001b[39m.\u001b[39;49mget(url)\u001b[39m.\u001b[39mtext \n\u001b[0;32m <a href='vscode-notebook-cell:/c%3A/sc/ai/rag-demo-1/demo-tools-1.ipynb#W4sZmlsZQ%3D%3D?line=9'>10</a>\u001b[0m soup \u001b[39m=\u001b[39m BeautifulSoup(html)\n\u001b[0;32m <a href='vscode-notebook-cell:/c%3A/sc/ai/rag-demo-1/demo-tools-1.ipynb#W4sZmlsZQ%3D%3D?line=10'>11</a>\u001b[0m script \u001b[39m=\u001b[39m soup\u001b[39m.\u001b[39mfind_all(\u001b[39m\"\u001b[39m\u001b[39mscript\u001b[39m\u001b[39m\"\u001b[39m, {\u001b[39m\"\u001b[39m\u001b[39mid\u001b[39m\u001b[39m\"\u001b[39m: \u001b[39m\"\u001b[39m\u001b[39mallrecipes-schema_1-0\u001b[39m\u001b[39m\"\u001b[39m})\n",
|
250 |
+
"File \u001b[1;32mc:\\sc\\ai\\rag-demo-1\\.venv\\lib\\site-packages\\requests\\api.py:73\u001b[0m, in \u001b[0;36mget\u001b[1;34m(url, params, **kwargs)\u001b[0m\n\u001b[0;32m 62\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mget\u001b[39m(url, params\u001b[39m=\u001b[39m\u001b[39mNone\u001b[39;00m, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs):\n\u001b[0;32m 63\u001b[0m \u001b[39m \u001b[39m\u001b[39mr\u001b[39m\u001b[39m\"\"\"Sends a GET request.\u001b[39;00m\n\u001b[0;32m 64\u001b[0m \n\u001b[0;32m 65\u001b[0m \u001b[39m :param url: URL for the new :class:`Request` object.\u001b[39;00m\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 70\u001b[0m \u001b[39m :rtype: requests.Response\u001b[39;00m\n\u001b[0;32m 71\u001b[0m \u001b[39m \"\"\"\u001b[39;00m\n\u001b[1;32m---> 73\u001b[0m \u001b[39mreturn\u001b[39;00m request(\u001b[39m\"\u001b[39m\u001b[39mget\u001b[39m\u001b[39m\"\u001b[39m, url, params\u001b[39m=\u001b[39mparams, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs)\n",
|
251 |
+
"File \u001b[1;32mc:\\sc\\ai\\rag-demo-1\\.venv\\lib\\site-packages\\requests\\api.py:59\u001b[0m, in \u001b[0;36mrequest\u001b[1;34m(method, url, **kwargs)\u001b[0m\n\u001b[0;32m 55\u001b[0m \u001b[39m# By using the 'with' statement we are sure the session is closed, thus we\u001b[39;00m\n\u001b[0;32m 56\u001b[0m \u001b[39m# avoid leaving sockets open which can trigger a ResourceWarning in some\u001b[39;00m\n\u001b[0;32m 57\u001b[0m \u001b[39m# cases, and look like a memory leak in others.\u001b[39;00m\n\u001b[0;32m 58\u001b[0m \u001b[39mwith\u001b[39;00m sessions\u001b[39m.\u001b[39mSession() \u001b[39mas\u001b[39;00m session:\n\u001b[1;32m---> 59\u001b[0m \u001b[39mreturn\u001b[39;00m session\u001b[39m.\u001b[39mrequest(method\u001b[39m=\u001b[39mmethod, url\u001b[39m=\u001b[39murl, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs)\n",
|
252 |
+
"File \u001b[1;32mc:\\sc\\ai\\rag-demo-1\\.venv\\lib\\site-packages\\requests\\sessions.py:589\u001b[0m, in \u001b[0;36mSession.request\u001b[1;34m(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)\u001b[0m\n\u001b[0;32m 584\u001b[0m send_kwargs \u001b[39m=\u001b[39m {\n\u001b[0;32m 585\u001b[0m \u001b[39m\"\u001b[39m\u001b[39mtimeout\u001b[39m\u001b[39m\"\u001b[39m: timeout,\n\u001b[0;32m 586\u001b[0m \u001b[39m\"\u001b[39m\u001b[39mallow_redirects\u001b[39m\u001b[39m\"\u001b[39m: allow_redirects,\n\u001b[0;32m 587\u001b[0m }\n\u001b[0;32m 588\u001b[0m send_kwargs\u001b[39m.\u001b[39mupdate(settings)\n\u001b[1;32m--> 589\u001b[0m resp \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39msend(prep, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39msend_kwargs)\n\u001b[0;32m 591\u001b[0m \u001b[39mreturn\u001b[39;00m resp\n",
|
253 |
+
"File \u001b[1;32mc:\\sc\\ai\\rag-demo-1\\.venv\\lib\\site-packages\\requests\\sessions.py:697\u001b[0m, in \u001b[0;36mSession.send\u001b[1;34m(self, request, **kwargs)\u001b[0m\n\u001b[0;32m 694\u001b[0m hooks \u001b[39m=\u001b[39m request\u001b[39m.\u001b[39mhooks\n\u001b[0;32m 696\u001b[0m \u001b[39m# Get the appropriate adapter to use\u001b[39;00m\n\u001b[1;32m--> 697\u001b[0m adapter \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mget_adapter(url\u001b[39m=\u001b[39;49mrequest\u001b[39m.\u001b[39;49murl)\n\u001b[0;32m 699\u001b[0m \u001b[39m# Start time (approximately) of the request\u001b[39;00m\n\u001b[0;32m 700\u001b[0m start \u001b[39m=\u001b[39m preferred_clock()\n",
|
254 |
+
"File \u001b[1;32mc:\\sc\\ai\\rag-demo-1\\.venv\\lib\\site-packages\\requests\\sessions.py:794\u001b[0m, in \u001b[0;36mSession.get_adapter\u001b[1;34m(self, url)\u001b[0m\n\u001b[0;32m 791\u001b[0m \u001b[39mreturn\u001b[39;00m adapter\n\u001b[0;32m 793\u001b[0m \u001b[39m# Nothing matches :-/\u001b[39;00m\n\u001b[1;32m--> 794\u001b[0m \u001b[39mraise\u001b[39;00m InvalidSchema(\u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mNo connection adapters were found for \u001b[39m\u001b[39m{\u001b[39;00murl\u001b[39m!r}\u001b[39;00m\u001b[39m\"\u001b[39m)\n",
|
255 |
+
"\u001b[1;31mInvalidSchema\u001b[0m: No connection adapters were found for \"'https://www.allrecipes.com/recipe/214618/beer-can-chicken/'\""
|
256 |
+
]
|
257 |
+
}
|
258 |
+
],
|
259 |
+
"source": [
|
260 |
+
"from langchain.agents import load_tools\n",
|
261 |
+
"from langchain.agents import initialize_agent\n",
|
262 |
+
"from langchain.llms import OpenAI\n",
|
263 |
+
"from langchain.chat_models import ChatOpenAI\n",
|
264 |
+
"\n",
|
265 |
+
"lm = OpenAI(temperature=0)\n",
|
266 |
+
"from langchain.agents import initialize_agent\n",
|
267 |
+
"\n",
|
268 |
+
"\n",
|
269 |
+
"#llm = ChatOpenAI(temperature=0.9, model_name='gpt-3.5-turbo')\n",
|
270 |
+
"llm = ChatOpenAI(temperature=0.3, model_name='gpt-4')\n",
|
271 |
+
"\n",
|
272 |
+
"zero_shot_agent = initialize_agent(\n",
|
273 |
+
" agent=\"zero-shot-react-description\", \n",
|
274 |
+
" tools=tools, \n",
|
275 |
+
" llm=llm,\n",
|
276 |
+
" verbose=True,\n",
|
277 |
+
" max_iterations=7,\n",
|
278 |
+
")\n",
|
279 |
+
"\n",
|
280 |
+
"#zero_shot_agent.run(\"Can you show tell me what ingredients I need for the first baked chicken recipe? \")\n",
|
281 |
+
"#zero_shot_agent.run(\"Can you show tell me what ingredients I need for the last baked chicken recipe? \")\n",
|
282 |
+
"#zero_shot_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",
|
283 |
+
"zero_shot_agent.run(\"Is there a Chicken recipe that's prepared with an alchohol? And if so how long does it take to prepare?\")\n",
|
284 |
+
"\n"
|
285 |
+
]
|
286 |
+
}
|
287 |
+
],
|
288 |
+
"metadata": {
|
289 |
+
"kernelspec": {
|
290 |
+
"display_name": ".venv",
|
291 |
+
"language": "python",
|
292 |
+
"name": "python3"
|
293 |
+
},
|
294 |
+
"language_info": {
|
295 |
+
"codemirror_mode": {
|
296 |
+
"name": "ipython",
|
297 |
+
"version": 3
|
298 |
+
},
|
299 |
+
"file_extension": ".py",
|
300 |
+
"mimetype": "text/x-python",
|
301 |
+
"name": "python",
|
302 |
+
"nbconvert_exporter": "python",
|
303 |
+
"pygments_lexer": "ipython3",
|
304 |
+
"version": "3.10.6"
|
305 |
+
}
|
306 |
+
},
|
307 |
+
"nbformat": 4,
|
308 |
+
"nbformat_minor": 2
|
309 |
+
}
|
vector-stores-create-beautiful-soap-1.ipynb → tools-allrecipes-1.ipynb
RENAMED
File without changes
|