Playing with intermediate steps
Browse files- demo-tools-1.ipynb +199 -58
- tools-allrecipes-1.ipynb +9 -4
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,28 +35,31 @@
|
|
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 |
-
"\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 |
-
"
|
56 |
-
" del recipeDict['
|
57 |
-
" del recipeDict['
|
58 |
-
" del recipeDict['
|
59 |
-
" del recipeDict['
|
60 |
" \n",
|
61 |
" return recipeDict\n",
|
62 |
"\n",
|
@@ -73,7 +76,7 @@
|
|
73 |
},
|
74 |
{
|
75 |
"cell_type": "code",
|
76 |
-
"execution_count":
|
77 |
"metadata": {},
|
78 |
"outputs": [],
|
79 |
"source": [
|
@@ -174,7 +177,7 @@
|
|
174 |
},
|
175 |
{
|
176 |
"cell_type": "code",
|
177 |
-
"execution_count":
|
178 |
"metadata": {},
|
179 |
"outputs": [],
|
180 |
"source": [
|
@@ -200,7 +203,7 @@
|
|
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
|
204 |
" It uses the https://schema.org/Recipe format to store it's recipes. \n",
|
205 |
" \"\"\")\n",
|
206 |
"\n",
|
@@ -210,49 +213,66 @@
|
|
210 |
},
|
211 |
{
|
212 |
"cell_type": "code",
|
213 |
-
"execution_count":
|
214 |
"metadata": {},
|
215 |
"outputs": [
|
216 |
{
|
217 |
"name": "stdout",
|
218 |
"output_type": "stream",
|
219 |
"text": [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
220 |
"\n",
|
221 |
-
"
|
222 |
-
"
|
223 |
-
"
|
224 |
-
"
|
225 |
-
"
|
226 |
-
"
|
227 |
-
"
|
228 |
-
"
|
229 |
-
"
|
|
|
|
|
230 |
]
|
231 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
232 |
{
|
233 |
-
"
|
234 |
-
"
|
235 |
-
"
|
236 |
-
|
237 |
-
"\
|
238 |
-
"\u001b[
|
239 |
-
"\u001b[
|
240 |
-
"
|
241 |
-
"
|
242 |
-
"
|
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 |
],
|
@@ -262,27 +282,148 @@
|
|
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
|
|
|
278 |
")\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
279 |
"\n",
|
280 |
-
"#
|
281 |
-
"#
|
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": {
|
|
|
2 |
"cells": [
|
3 |
{
|
4 |
"cell_type": "code",
|
5 |
+
"execution_count": 12,
|
6 |
"metadata": {},
|
7 |
"outputs": [],
|
8 |
"source": [
|
|
|
35 |
},
|
36 |
{
|
37 |
"cell_type": "code",
|
38 |
+
"execution_count": 13,
|
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",
|
48 |
+
" if key in dictionary:\n",
|
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",
|
|
|
76 |
},
|
77 |
{
|
78 |
"cell_type": "code",
|
79 |
+
"execution_count": 14,
|
80 |
"metadata": {},
|
81 |
"outputs": [],
|
82 |
"source": [
|
|
|
177 |
},
|
178 |
{
|
179 |
"cell_type": "code",
|
180 |
+
"execution_count": 29,
|
181 |
"metadata": {},
|
182 |
"outputs": [],
|
183 |
"source": [
|
|
|
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",
|
206 |
+
" Useful for fetching a particular recipe, you can only fetch a recipe with it's url, you must get that using another tool\n",
|
207 |
" It uses the https://schema.org/Recipe format to store it's recipes. \n",
|
208 |
" \"\"\")\n",
|
209 |
"\n",
|
|
|
213 |
},
|
214 |
{
|
215 |
"cell_type": "code",
|
216 |
+
"execution_count": 30,
|
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 |
+
" print(i['title'])\n",
|
253 |
+
" \n",
|
254 |
+
"print()\n",
|
255 |
+
"print('Desserts:')\n",
|
256 |
+
"for i in dessertList:\n",
|
257 |
+
" print(i['title'])"
|
258 |
+
]
|
259 |
+
},
|
260 |
+
{
|
261 |
+
"cell_type": "code",
|
262 |
+
"execution_count": 44,
|
263 |
+
"metadata": {},
|
264 |
+
"outputs": [
|
265 |
{
|
266 |
+
"name": "stdout",
|
267 |
+
"output_type": "stream",
|
268 |
+
"text": [
|
269 |
+
"\n",
|
270 |
+
"\n",
|
271 |
+
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
272 |
+
"\u001b[32;1m\u001b[1;3mThis question is about astronomy, which is not related to the tools I have access to. I don't have the information to answer this question.\n",
|
273 |
+
"Final Answer: I'm sorry, I don't have the information to answer this question.\u001b[0m\n",
|
274 |
+
"\n",
|
275 |
+
"\u001b[1m> Finished chain.\u001b[0m\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
276 |
]
|
277 |
}
|
278 |
],
|
|
|
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",
|
287 |
"\n",
|
288 |
+
"agent = initialize_agent(\n",
|
|
|
|
|
|
|
|
|
289 |
" agent=\"zero-shot-react-description\", \n",
|
290 |
" tools=tools, \n",
|
291 |
" llm=llm,\n",
|
292 |
" verbose=True,\n",
|
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 |
+
"#response = agent({\"input\": f\"{system} Is the moon closer to earth or the sun?\"})\n",
|
307 |
+
"response = agent({\"input\": f\"{system} My bowl is broken, can I still make Nutella Ice Cream?\"})\n",
|
308 |
"\n",
|
309 |
+
"#print(\"################\")\n",
|
310 |
+
"#print(print(response[\"intermediate_steps\"]))\n",
|
|
|
|
|
311 |
"\n"
|
312 |
]
|
313 |
+
},
|
314 |
+
{
|
315 |
+
"cell_type": "code",
|
316 |
+
"execution_count": 46,
|
317 |
+
"metadata": {},
|
318 |
+
"outputs": [
|
319 |
+
{
|
320 |
+
"name": "stdout",
|
321 |
+
"output_type": "stream",
|
322 |
+
"text": [
|
323 |
+
"[]\n"
|
324 |
+
]
|
325 |
+
}
|
326 |
+
],
|
327 |
+
"source": [
|
328 |
+
"print(response[\"intermediate_steps\"])"
|
329 |
+
]
|
330 |
+
},
|
331 |
+
{
|
332 |
+
"cell_type": "markdown",
|
333 |
+
"metadata": {},
|
334 |
+
"source": [
|
335 |
+
"# UI"
|
336 |
+
]
|
337 |
+
},
|
338 |
+
{
|
339 |
+
"cell_type": "code",
|
340 |
+
"execution_count": 37,
|
341 |
+
"metadata": {},
|
342 |
+
"outputs": [
|
343 |
+
{
|
344 |
+
"name": "stdout",
|
345 |
+
"output_type": "stream",
|
346 |
+
"text": [
|
347 |
+
"Running on local URL: http://127.0.0.1:7862\n",
|
348 |
+
"\n",
|
349 |
+
"To create a public link, set `share=True` in `launch()`.\n"
|
350 |
+
]
|
351 |
+
},
|
352 |
+
{
|
353 |
+
"data": {
|
354 |
+
"text/html": [
|
355 |
+
"<div><iframe src=\"http://127.0.0.1:7862/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
356 |
+
],
|
357 |
+
"text/plain": [
|
358 |
+
"<IPython.core.display.HTML object>"
|
359 |
+
]
|
360 |
+
},
|
361 |
+
"metadata": {},
|
362 |
+
"output_type": "display_data"
|
363 |
+
},
|
364 |
+
{
|
365 |
+
"data": {
|
366 |
+
"text/plain": []
|
367 |
+
},
|
368 |
+
"execution_count": 37,
|
369 |
+
"metadata": {},
|
370 |
+
"output_type": "execute_result"
|
371 |
+
},
|
372 |
+
{
|
373 |
+
"name": "stdout",
|
374 |
+
"output_type": "stream",
|
375 |
+
"text": [
|
376 |
+
"\n",
|
377 |
+
"\n",
|
378 |
+
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
379 |
+
"\u001b[32;1m\u001b[1;3mI need to find a recipe for ice cream using the Dessert Recipes tool.\n",
|
380 |
+
"Action: Dessert Recipes tool\n",
|
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 |
+
]
|
398 |
+
}
|
399 |
+
],
|
400 |
+
"source": [
|
401 |
+
"import gradio as gr\n",
|
402 |
+
"\n",
|
403 |
+
"# # Import langchain stuff\n",
|
404 |
+
"# from langchain.agents import load_tools, initialize_agent, AgentType\n",
|
405 |
+
"# from langchain.llms import OpenAI\n",
|
406 |
+
"\n",
|
407 |
+
"\n",
|
408 |
+
"# # Initializing the LLM and the Toolset\n",
|
409 |
+
"# llm = OpenAI(temperature=0)\n",
|
410 |
+
"# tools = load_tools([\"wikipedia\"], llm=llm)\n",
|
411 |
+
"# agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)\n",
|
412 |
+
"\n",
|
413 |
+
"def langchain_bot(prompt):\n",
|
414 |
+
" if prompt:\n",
|
415 |
+
" text = agent.run(prompt)\n",
|
416 |
+
" return text\n",
|
417 |
+
" return \"Type a prompt to get a Wikipedia-like response!\"\n",
|
418 |
+
"\n",
|
419 |
+
"# Customize the appearance and style of the interface\n",
|
420 |
+
"iface = gr.Interface(fn=langchain_bot, inputs=gr.Textbox(lines=5, placeholder=\"Type your prompt here...\"), \n",
|
421 |
+
" outputs=gr.Textbox(), title=\"Wikipedia Langchain Bot 🦜\",\n",
|
422 |
+
" description=\"Generate Wikipedia-like responses using Langchain AI.\")\n",
|
423 |
+
"\n",
|
424 |
+
"# Launch the interface\n",
|
425 |
+
"iface.launch()"
|
426 |
+
]
|
427 |
}
|
428 |
],
|
429 |
"metadata": {
|
tools-allrecipes-1.ipynb
CHANGED
@@ -39,11 +39,9 @@
|
|
39 |
" return allUrls\n",
|
40 |
"\n",
|
41 |
"#links = get_links_on_url(\"https://www.allrecipes.com/recipes/17562/dinner/\", \"/recipe/\")\n",
|
42 |
-
"#
|
43 |
"#links = get_links_on_url(\"https://www.allrecipes.com/search?Poultry=Beef+Stews&offset=0&q=Beef+Stews\", \"/recipe/\")\n",
|
44 |
-
"
|
45 |
-
"\n",
|
46 |
-
"print(links)"
|
47 |
]
|
48 |
},
|
49 |
{
|
@@ -241,6 +239,13 @@
|
|
241 |
"]\n",
|
242 |
"\n"
|
243 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
244 |
}
|
245 |
],
|
246 |
"metadata": {
|
|
|
39 |
" return allUrls\n",
|
40 |
"\n",
|
41 |
"#links = get_links_on_url(\"https://www.allrecipes.com/recipes/17562/dinner/\", \"/recipe/\")\n",
|
42 |
+
"#links = get_links_on_url(\"https://www.allrecipes.com/search?q=Chicken\", \"/recipe/\")\n",
|
43 |
"#links = get_links_on_url(\"https://www.allrecipes.com/search?Poultry=Beef+Stews&offset=0&q=Beef+Stews\", \"/recipe/\")\n",
|
44 |
+
"#print(links)"
|
|
|
|
|
45 |
]
|
46 |
},
|
47 |
{
|
|
|
239 |
"]\n",
|
240 |
"\n"
|
241 |
]
|
242 |
+
},
|
243 |
+
{
|
244 |
+
"cell_type": "code",
|
245 |
+
"execution_count": null,
|
246 |
+
"metadata": {},
|
247 |
+
"outputs": [],
|
248 |
+
"source": []
|
249 |
}
|
250 |
],
|
251 |
"metadata": {
|