thrag commited on
Commit
d92b31b
·
1 Parent(s): 1dff19e
Files changed (2) hide show
  1. app.py +86 -114
  2. demo-tools-1.ipynb +170 -400
app.py CHANGED
@@ -1,11 +1,7 @@
1
  # %%
2
-
3
  import os
4
  import json
5
 
6
- print('here I am')
7
- print(os.environ['TEST_SECRET'])
8
-
9
  pathToSettings = '../../env/ai.json'
10
  if os.path.exists(pathToSettings):
11
  # Load setting from Json outside of project.
@@ -28,6 +24,15 @@ from bs4 import BeautifulSoup
28
  import json
29
  import validators
30
 
 
 
 
 
 
 
 
 
 
31
  def remove_keys(dictionary: dict, keyList: list):
32
  for key in keyList:
33
  if key in dictionary:
@@ -35,117 +40,71 @@ def remove_keys(dictionary: dict, keyList: list):
35
 
36
  def get_recipe_as_json(url: str) -> dict:
37
 
 
 
 
 
 
38
  url = url.replace("'", "").replace('"', '')
39
 
40
  if not validators.url(url):
41
  return {'error': 'Invalid url format', 'url': url }
 
 
 
42
 
43
  html = requests.get(url).text
44
- soup = BeautifulSoup(html, features='html.parser')
45
  script = soup.find_all("script", {"id": "allrecipes-schema_1-0"})
46
 
47
  recipeDict = json.loads(script[0].text)[0]
48
  remove_keys(recipeDict, ['review', 'image', 'mainEntityOfPage', 'publisher'])
49
 
 
 
50
  return recipeDict
51
 
52
  # url = "https://www.allrecipes.com/recipe/212498/easy-chicken-and-broccoli-alfredo/"
53
  # obj = get_recipe_as_json(url)
54
- # x = get_recipe_as_json('https://www.allrecipes.com/recipe/235153/easy-baked-chicken-thighs/')
 
55
  # print(x)
56
 
 
 
 
 
 
57
  # %% [markdown]
58
  # # Static recipe lists
59
 
60
  # %%
61
  dessertList = [
62
- {
63
- "title": "Chocolate Snack Cake",
64
- "url": "https://www.allrecipes.com/chocolate-snack-cake-recipe-8350343"
65
- },
66
- {
67
- "title": "Charred Spiced Pears with Smoky Vanilla Cherry Sauce",
68
- "url": "https://www.allrecipes.com/charred-spiced-pears-with-smoky-vanilla-cherry-sauce-recipe-8347080"
69
- },
70
- {
71
- "title": "Meringue Topped Banana Pudding",
72
- "url": "https://www.allrecipes.com/meringue-topped-banana-pudding-recipe-8347040"
73
- },
74
- {
75
- "title": "White Chocolate Cinnamon Toast Crunch Bars",
76
- "url": "https://www.allrecipes.com/white-chocolate-cinnamon-toast-crunch-bars-recipe-7556790"
77
- },
78
- {
79
- "title": "Plum Cobbler for Two",
80
- "url": "https://www.allrecipes.com/plum-cobbler-for-two-recipe-8304143"
81
- },
82
- {
83
- "title": "Pumpkin Cheesecake Cookies",
84
- "url": "https://www.allrecipes.com/pumpkin-cheesecake-cookies-recipe-7972485"
85
- },
86
- {
87
- "title": "Chocolate Whipped Cottage Cheese",
88
- "url": "https://www.allrecipes.com/chocolate-whipped-cottage-cheese-recipe-8303272"
89
- },
90
- {
91
- "title": "Nutella Ice Cream",
92
- "url": "https://www.allrecipes.com/nutella-ice-cream-recipe-7508716"
93
- },
94
- {
95
- "title": "3-Ingredient Banana Oatmeal Cookies",
96
- "url": "https://www.allrecipes.com/3-ingredient-banana-oatmeal-cookies-recipe-7972686"
97
- },
98
- {
99
- "title": "Caramel Apple Pie Cookies",
100
- "url": "https://www.allrecipes.com/caramel-apple-pie-cookies-recipe-7642173"
101
- }
102
  ]
103
 
104
  chickenDishList = [
105
- {
106
- "title": "Crispy Roasted Chicken",
107
- "url": "https://www.allrecipes.com/recipe/228363/crispy-roasted-chicken/"
108
- },
109
- {
110
- "title": "Roasted Spatchcocked Chicken With Potatoes",
111
- "url": "https://www.allrecipes.com/recipe/254877/roasted-spatchcocked-chicken-with-potatoes/"
112
- },
113
- {
114
- "title": "Easy Baked Chicken Thighs",
115
- "url": "https://www.allrecipes.com/recipe/235153/easy-baked-chicken-thighs/"
116
- },
117
- {
118
- "title": "Crispy Baked Chicken Thighs",
119
- "url": "https://www.allrecipes.com/recipe/258878/crispy-baked-chicken-thighs/"
120
- },
121
- {
122
- "title": "Crispy and Tender Baked Chicken Thighs",
123
- "url": "https://www.allrecipes.com/recipe/235151/crispy-and-tender-baked-chicken-thighs/"
124
- },
125
- {
126
- "title": "Million Dollar Chicken",
127
- "url": "https://www.allrecipes.com/recipe/233953/million-dollar-chicken/"
128
- },
129
- {
130
- "title": "Simple Whole Roasted Chicken",
131
- "url": "https://www.allrecipes.com/recipe/70679/simple-whole-roasted-chicken/"
132
- },
133
- {
134
- "title": "Beer Can Chicken",
135
- "url": "https://www.allrecipes.com/recipe/214618/beer-can-chicken/"
136
- },
137
- {
138
- "title": "Air Fryer Chicken Thighs",
139
- "url": "https://www.allrecipes.com/recipe/272858/air-fryer-chicken-thighs/"
140
- },
141
- {
142
- "title": "Happy Roast Chicken",
143
- "url": "https://www.allrecipes.com/recipe/214478/happy-roast-chicken/"
144
- }
145
  ]
146
 
147
-
148
-
149
  # %% [markdown]
150
  # # Setup Tools
151
 
@@ -173,21 +132,21 @@ def get_recipe(url: str):
173
 
174
  get_recipe_as_json_tool = Tool(name='Get a Recipe tool', func=get_recipe, description="""
175
  Useful for fetching a particular recipe, you can only fetch a recipe with it's url, you must get that using another tool
176
- It uses the https://schema.org/Recipe format to store it's recipes.
177
  """)
178
 
179
  # Tool list
180
  tools = [list_chicken_recipes_tool, list_dessert_recipes_tool, get_recipe_as_json_tool]
181
 
182
  # %%
183
- print('Chicken dishes:')
184
- for i in chickenDishList:
185
- print(i['title'])
186
 
187
- print('')
188
- print('Desserts:')
189
- for i in dessertList:
190
- print(i['title'])
191
 
192
  # %% [markdown]
193
  # # LLM
@@ -197,16 +156,23 @@ for i in dessertList:
197
  # 1 [tracking-inspecting-prompts-langchain-agents-weights-and-biases](https://kleiber.me/blog/2023/05/14/tracking-inspecting-prompts-langchain-agents-weights-and-biases/)
198
 
199
  # %%
200
- from langchain.agents import load_tools
201
- from langchain.agents import initialize_agent
202
- from langchain.llms import OpenAI
203
- from langchain.chat_models import ChatOpenAI
204
- from langchain.callbacks import StdOutCallbackHandler
205
- from langchain.agents import initialize_agent
206
 
 
207
  # llm = ChatOpenAI(temperature=0, model_name='gpt-4') # 'gpt-3.5-turbo'
208
- # agent = initialize_agent(agent="zero-shot-react-description", tools=tools, llm=llm, verbose=True, max_iterations=7, return_intermediate_steps=True)
209
 
 
 
 
 
 
 
 
 
210
  # 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."
211
  # #query = "Can you show tell me what ingredients I need for the first baked chicken recipe?"
212
  # #query = "Can you show tell me what ingredients I need for the last baked chicken recipe? "
@@ -216,27 +182,27 @@ from langchain.agents import initialize_agent
216
  # #query = "Is the moon closer to earth or the sun?"
217
  # #query = "How good are the apple pie cookies?"
218
  # query = "What tools do I need for the Nutella Ice Cream?"
219
-
220
  # #query = "My bowl is broken, can I still make Nutella Ice Cream? Answer as a yes/no."
 
221
 
222
- # response = agent({"input": f"{system} {query}"})
223
 
224
  # %%
 
 
 
 
225
  from langchain.load.dump import dumps
226
 
227
- # %% [markdown]
228
- # # UI - Simple UI
229
-
230
- # %%
231
- import gradio as gr
232
-
233
  def ask_query(query):
234
 
235
  # LLM
236
- llm = ChatOpenAI(temperature=0, model_name='gpt-4') # 'gpt-3.5-turbo'
237
  agent = initialize_agent(agent="zero-shot-react-description", tools=tools, llm=llm, verbose=True, max_iterations=7, return_intermediate_steps=True)
238
- 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."
239
- response = agent({"input": f"{system} {query}"})
 
 
 
240
 
241
  # Show response
242
  stepsDict = json.loads(dumps(response["intermediate_steps"], pretty=True))
@@ -255,6 +221,11 @@ def ask_query(query):
255
  resp += response['output']
256
  return resp
257
 
 
 
 
 
 
258
 
259
  with gr.Blocks() as demo:
260
  gr.Markdown("Start typing below and then click **Run** to see the output.")
@@ -263,6 +234,7 @@ with gr.Blocks() as demo:
263
  inp = gr.Textbox(placeholder="Type your question here?", label="Question")
264
  btn = gr.Button("Run")
265
  examples = ["Can you show tell me what ingredients I need for the first baked chicken recipe?",
 
266
  "Can you show tell me what ingredients I need for the last baked chicken recipe? ",
267
  "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
268
  "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?",
 
1
  # %%
 
2
  import os
3
  import json
4
 
 
 
 
5
  pathToSettings = '../../env/ai.json'
6
  if os.path.exists(pathToSettings):
7
  # Load setting from Json outside of project.
 
24
  import json
25
  import validators
26
 
27
+ pageCache = {}
28
+
29
+ def json_safe_loads(jsonString):
30
+ try:
31
+ obj = json.loads(jsonString)
32
+ except ValueError as e:
33
+ return None
34
+ return obj
35
+
36
  def remove_keys(dictionary: dict, keyList: list):
37
  for key in keyList:
38
  if key in dictionary:
 
40
 
41
  def get_recipe_as_json(url: str) -> dict:
42
 
43
+ obj = json_safe_loads(url)
44
+ if obj is not None:
45
+ if 'url' in obj:
46
+ url = obj['url']
47
+
48
  url = url.replace("'", "").replace('"', '')
49
 
50
  if not validators.url(url):
51
  return {'error': 'Invalid url format', 'url': url }
52
+
53
+ if url in pageCache:
54
+ return pageCache[url]
55
 
56
  html = requests.get(url).text
57
+ soup = BeautifulSoup(html)
58
  script = soup.find_all("script", {"id": "allrecipes-schema_1-0"})
59
 
60
  recipeDict = json.loads(script[0].text)[0]
61
  remove_keys(recipeDict, ['review', 'image', 'mainEntityOfPage', 'publisher'])
62
 
63
+ pageCache[url] = recipeDict
64
+
65
  return recipeDict
66
 
67
  # url = "https://www.allrecipes.com/recipe/212498/easy-chicken-and-broccoli-alfredo/"
68
  # obj = get_recipe_as_json(url)
69
+ #x = get_recipe_as_json('{ "url": "https://www.allrecipes.com/recipe/235153/easy-baked-chicken-thighs/" }')
70
+ # x = get_recipe_as_json('"https://www.allrecipes.com/recipe/235153/easy-baked-chicken-thighs/"')
71
  # print(x)
72
 
73
+ # %%
74
+ x = json.loads('{ "url": "https://www.allrecipes.com/recipe/235153/easy-baked-chicken-thighs/"}')
75
+ #x = json.loads('{"param1": 123}')
76
+ print(type(x))
77
+
78
  # %% [markdown]
79
  # # Static recipe lists
80
 
81
  # %%
82
  dessertList = [
83
+ "https://www.allrecipes.com/chocolate-snack-cake-recipe-8350343",
84
+ "https://www.allrecipes.com/charred-spiced-pears-with-smoky-vanilla-cherry-sauce-recipe-8347080",
85
+ "https://www.allrecipes.com/meringue-topped-banana-pudding-recipe-8347040",
86
+ "https://www.allrecipes.com/white-chocolate-cinnamon-toast-crunch-bars-recipe-7556790",
87
+ "https://www.allrecipes.com/plum-cobbler-for-two-recipe-8304143",
88
+ "https://www.allrecipes.com/pumpkin-cheesecake-cookies-recipe-7972485",
89
+ "https://www.allrecipes.com/chocolate-whipped-cottage-cheese-recipe-8303272",
90
+ "https://www.allrecipes.com/nutella-ice-cream-recipe-7508716",
91
+ "https://www.allrecipes.com/3-ingredient-banana-oatmeal-cookies-recipe-7972686",
92
+ "https://www.allrecipes.com/caramel-apple-pie-cookies-recipe-7642173"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
  ]
94
 
95
  chickenDishList = [
96
+ "https://www.allrecipes.com/recipe/228363/crispy-roasted-chicken/",
97
+ "https://www.allrecipes.com/recipe/254877/roasted-spatchcocked-chicken-with-potatoes/",
98
+ "https://www.allrecipes.com/recipe/235153/easy-baked-chicken-thighs/",
99
+ "https://www.allrecipes.com/recipe/258878/crispy-baked-chicken-thighs/",
100
+ "https://www.allrecipes.com/recipe/235151/crispy-and-tender-baked-chicken-thighs/",
101
+ "https://www.allrecipes.com/recipe/233953/million-dollar-chicken/",
102
+ "https://www.allrecipes.com/recipe/70679/simple-whole-roasted-chicken/",
103
+ "https://www.allrecipes.com/recipe/214618/beer-can-chicken/",
104
+ "https://www.allrecipes.com/recipe/272858/air-fryer-chicken-thighs/",
105
+ "https://www.allrecipes.com/recipe/214478/happy-roast-chicken/"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
  ]
107
 
 
 
108
  # %% [markdown]
109
  # # Setup Tools
110
 
 
132
 
133
  get_recipe_as_json_tool = Tool(name='Get a Recipe tool', func=get_recipe, description="""
134
  Useful for fetching a particular recipe, you can only fetch a recipe with it's url, you must get that using another tool
135
+ The tool uses the https://schema.org/Recipe format to store it's recipes.
136
  """)
137
 
138
  # Tool list
139
  tools = [list_chicken_recipes_tool, list_dessert_recipes_tool, get_recipe_as_json_tool]
140
 
141
  # %%
142
+ # print('Chicken dishes:')
143
+ # for i in chickenDishList:
144
+ # print(i['title'])
145
 
146
+ # print('')
147
+ # print('Desserts:')
148
+ # for i in dessertList:
149
+ # print(i['title'])
150
 
151
  # %% [markdown]
152
  # # LLM
 
156
  # 1 [tracking-inspecting-prompts-langchain-agents-weights-and-biases](https://kleiber.me/blog/2023/05/14/tracking-inspecting-prompts-langchain-agents-weights-and-biases/)
157
 
158
  # %%
159
+ # from langchain.agents import load_tools
160
+ # from langchain.agents import initialize_agent
161
+ # from langchain.llms import OpenAI
162
+ # from langchain.chat_models import ChatOpenAI
163
+ # from langchain.callbacks import StdOutCallbackHandler
 
164
 
165
+ # from langchain.agents import initialize_agent
166
  # llm = ChatOpenAI(temperature=0, model_name='gpt-4') # 'gpt-3.5-turbo'
 
167
 
168
+ # agent = initialize_agent(
169
+ # agent="zero-shot-react-description",
170
+ # tools=tools,
171
+ # llm=llm,
172
+ # verbose=True,
173
+ # max_iterations=7,
174
+ # return_intermediate_steps=True
175
+ # )
176
  # 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."
177
  # #query = "Can you show tell me what ingredients I need for the first baked chicken recipe?"
178
  # #query = "Can you show tell me what ingredients I need for the last baked chicken recipe? "
 
182
  # #query = "Is the moon closer to earth or the sun?"
183
  # #query = "How good are the apple pie cookies?"
184
  # query = "What tools do I need for the Nutella Ice Cream?"
 
185
  # #query = "My bowl is broken, can I still make Nutella Ice Cream? Answer as a yes/no."
186
+ # #response = agent({"input": f"{system} {query}"})
187
 
 
188
 
189
  # %%
190
+ from langchain.agents import load_tools
191
+ from langchain.agents import initialize_agent
192
+ from langchain.llms import OpenAI
193
+ from langchain.chat_models import ChatOpenAI
194
  from langchain.load.dump import dumps
195
 
 
 
 
 
 
 
196
  def ask_query(query):
197
 
198
  # LLM
199
+ llm = ChatOpenAI(temperature=0.2, model_name='gpt-4') # 'gpt-3.5-turbo'
200
  agent = initialize_agent(agent="zero-shot-react-description", tools=tools, llm=llm, verbose=True, max_iterations=7, return_intermediate_steps=True)
201
+ system = """
202
+ If the answer is not in the tools or context passed to you then don't answer. \n
203
+ If you don't know the answer then say so. \n
204
+ """
205
+ response = agent({"input": f"{system} [[RECIPENAME]] {query}"})
206
 
207
  # Show response
208
  stepsDict = json.loads(dumps(response["intermediate_steps"], pretty=True))
 
221
  resp += response['output']
222
  return resp
223
 
224
+ # %% [markdown]
225
+ # # UI - Simple UI
226
+
227
+ # %%
228
+ import gradio as gr
229
 
230
  with gr.Blocks() as demo:
231
  gr.Markdown("Start typing below and then click **Run** to see the output.")
 
234
  inp = gr.Textbox(placeholder="Type your question here?", label="Question")
235
  btn = gr.Button("Run")
236
  examples = ["Can you show tell me what ingredients I need for the first baked chicken recipe?",
237
+ "I have 15 guests coming to visit, if I make the 'Chocolate Snack Cake' recipe will there be enough? ",
238
  "Can you show tell me what ingredients I need for the last baked chicken recipe? ",
239
  "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
240
  "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?",
demo-tools-1.ipynb CHANGED
@@ -2,7 +2,7 @@
2
  "cells": [
3
  {
4
  "cell_type": "code",
5
- "execution_count": 278,
6
  "metadata": {},
7
  "outputs": [
8
  {
@@ -28,31 +28,7 @@
28
  " for key in settingsJson:\n",
29
  " os.environ[key] = settingsJson[key]\n",
30
  " \n",
31
- " del settingsJson\n",
32
- "else: \n",
33
- " # Manually set settings \n",
34
- " os.environ['HUGGING_FACE_API_KEY'] = 'Get here: https://huggingface.co/settings/tokens'\n",
35
- " os.environ['OPENAI_API_KEY'] = 'Get here: https://platform.openai.com/account/api-keys'"
36
- ]
37
- },
38
- {
39
- "cell_type": "code",
40
- "execution_count": 279,
41
- "metadata": {},
42
- "outputs": [
43
- {
44
- "data": {
45
- "text/plain": [
46
- "False"
47
- ]
48
- },
49
- "execution_count": 279,
50
- "metadata": {},
51
- "output_type": "execute_result"
52
- }
53
- ],
54
- "source": [
55
- "os.path.exists('noono.pj')"
56
  ]
57
  },
58
  {
@@ -64,15 +40,32 @@
64
  },
65
  {
66
  "cell_type": "code",
67
- "execution_count": 280,
68
  "metadata": {},
69
- "outputs": [],
 
 
 
 
 
 
 
 
70
  "source": [
71
  "import requests\n",
72
  "from bs4 import BeautifulSoup\n",
73
  "import json\n",
74
  "import validators \n",
75
  "\n",
 
 
 
 
 
 
 
 
 
76
  "def remove_keys(dictionary: dict, keyList: list):\n",
77
  " for key in keyList: \n",
78
  " if key in dictionary:\n",
@@ -80,10 +73,18 @@
80
  "\n",
81
  "def get_recipe_as_json(url: str) -> dict: \n",
82
  " \n",
 
 
 
 
 
83
  " url = url.replace(\"'\", \"\").replace('\"', '')\n",
84
  " \n",
85
  " if not validators.url(url):\n",
86
  " return {'error': 'Invalid url format', 'url': url }\n",
 
 
 
87
  "\n",
88
  " html = requests.get(url).text \n",
89
  " soup = BeautifulSoup(html)\n",
@@ -92,14 +93,36 @@
92
  " recipeDict = json.loads(script[0].text)[0]\n",
93
  " remove_keys(recipeDict, ['review', 'image', 'mainEntityOfPage', 'publisher'])\n",
94
  " \n",
 
 
95
  " return recipeDict\n",
96
  "\n",
97
  "# url = \"https://www.allrecipes.com/recipe/212498/easy-chicken-and-broccoli-alfredo/\"\n",
98
  "# obj = get_recipe_as_json(url)\n",
99
- "# x = get_recipe_as_json('https://www.allrecipes.com/recipe/235153/easy-baked-chicken-thighs/')\n",
 
100
  "# print(x)"
101
  ]
102
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
  {
104
  "cell_type": "markdown",
105
  "metadata": {},
@@ -109,94 +132,34 @@
109
  },
110
  {
111
  "cell_type": "code",
112
- "execution_count": 281,
113
  "metadata": {},
114
  "outputs": [],
115
  "source": [
116
  "dessertList = [\n",
117
- " {\n",
118
- " \"title\": \"Chocolate Snack Cake\",\n",
119
- " \"url\": \"https://www.allrecipes.com/chocolate-snack-cake-recipe-8350343\"\n",
120
- " },\n",
121
- " {\n",
122
- " \"title\": \"Charred Spiced Pears with Smoky Vanilla Cherry Sauce\",\n",
123
- " \"url\": \"https://www.allrecipes.com/charred-spiced-pears-with-smoky-vanilla-cherry-sauce-recipe-8347080\"\n",
124
- " },\n",
125
- " {\n",
126
- " \"title\": \"Meringue Topped Banana Pudding\",\n",
127
- " \"url\": \"https://www.allrecipes.com/meringue-topped-banana-pudding-recipe-8347040\"\n",
128
- " },\n",
129
- " {\n",
130
- " \"title\": \"White Chocolate Cinnamon Toast Crunch Bars\",\n",
131
- " \"url\": \"https://www.allrecipes.com/white-chocolate-cinnamon-toast-crunch-bars-recipe-7556790\"\n",
132
- " },\n",
133
- " {\n",
134
- " \"title\": \"Plum Cobbler for Two\",\n",
135
- " \"url\": \"https://www.allrecipes.com/plum-cobbler-for-two-recipe-8304143\"\n",
136
- " },\n",
137
- " {\n",
138
- " \"title\": \"Pumpkin Cheesecake Cookies\",\n",
139
- " \"url\": \"https://www.allrecipes.com/pumpkin-cheesecake-cookies-recipe-7972485\"\n",
140
- " },\n",
141
- " {\n",
142
- " \"title\": \"Chocolate Whipped Cottage Cheese\",\n",
143
- " \"url\": \"https://www.allrecipes.com/chocolate-whipped-cottage-cheese-recipe-8303272\"\n",
144
- " },\n",
145
- " {\n",
146
- " \"title\": \"Nutella Ice Cream\",\n",
147
- " \"url\": \"https://www.allrecipes.com/nutella-ice-cream-recipe-7508716\"\n",
148
- " },\n",
149
- " {\n",
150
- " \"title\": \"3-Ingredient Banana Oatmeal Cookies\",\n",
151
- " \"url\": \"https://www.allrecipes.com/3-ingredient-banana-oatmeal-cookies-recipe-7972686\"\n",
152
- " },\n",
153
- " {\n",
154
- " \"title\": \"Caramel Apple Pie Cookies\",\n",
155
- " \"url\": \"https://www.allrecipes.com/caramel-apple-pie-cookies-recipe-7642173\"\n",
156
- " }\n",
157
  "]\n",
158
  "\n",
159
  "chickenDishList = [\n",
160
- " {\n",
161
- " \"title\": \"Crispy Roasted Chicken\",\n",
162
- " \"url\": \"https://www.allrecipes.com/recipe/228363/crispy-roasted-chicken/\"\n",
163
- " },\n",
164
- " {\n",
165
- " \"title\": \"Roasted Spatchcocked Chicken With Potatoes\",\n",
166
- " \"url\": \"https://www.allrecipes.com/recipe/254877/roasted-spatchcocked-chicken-with-potatoes/\"\n",
167
- " },\n",
168
- " {\n",
169
- " \"title\": \"Easy Baked Chicken Thighs\",\n",
170
- " \"url\": \"https://www.allrecipes.com/recipe/235153/easy-baked-chicken-thighs/\"\n",
171
- " },\n",
172
- " {\n",
173
- " \"title\": \"Crispy Baked Chicken Thighs\",\n",
174
- " \"url\": \"https://www.allrecipes.com/recipe/258878/crispy-baked-chicken-thighs/\"\n",
175
- " },\n",
176
- " {\n",
177
- " \"title\": \"Crispy and Tender Baked Chicken Thighs\",\n",
178
- " \"url\": \"https://www.allrecipes.com/recipe/235151/crispy-and-tender-baked-chicken-thighs/\"\n",
179
- " },\n",
180
- " {\n",
181
- " \"title\": \"Million Dollar Chicken\",\n",
182
- " \"url\": \"https://www.allrecipes.com/recipe/233953/million-dollar-chicken/\"\n",
183
- " },\n",
184
- " {\n",
185
- " \"title\": \"Simple Whole Roasted Chicken\",\n",
186
- " \"url\": \"https://www.allrecipes.com/recipe/70679/simple-whole-roasted-chicken/\"\n",
187
- " },\n",
188
- " {\n",
189
- " \"title\": \"Beer Can Chicken\",\n",
190
- " \"url\": \"https://www.allrecipes.com/recipe/214618/beer-can-chicken/\"\n",
191
- " },\n",
192
- " {\n",
193
- " \"title\": \"Air Fryer Chicken Thighs\",\n",
194
- " \"url\": \"https://www.allrecipes.com/recipe/272858/air-fryer-chicken-thighs/\"\n",
195
- " },\n",
196
- " {\n",
197
- " \"title\": \"Happy Roast Chicken\",\n",
198
- " \"url\": \"https://www.allrecipes.com/recipe/214478/happy-roast-chicken/\"\n",
199
- " }\n",
200
  "]"
201
  ]
202
  },
@@ -209,7 +172,7 @@
209
  },
210
  {
211
  "cell_type": "code",
212
- "execution_count": 282,
213
  "metadata": {},
214
  "outputs": [],
215
  "source": [
@@ -236,7 +199,7 @@
236
  "\n",
237
  "get_recipe_as_json_tool = Tool(name='Get a Recipe tool', func=get_recipe, description=\"\"\"\n",
238
  " Useful for fetching a particular recipe, you can only fetch a recipe with it's url, you must get that using another tool\n",
239
- " It uses the https://schema.org/Recipe format to store it's recipes. \n",
240
  " \"\"\")\n",
241
  "\n",
242
  "# Tool list\n",
@@ -245,48 +208,18 @@
245
  },
246
  {
247
  "cell_type": "code",
248
- "execution_count": 283,
249
  "metadata": {},
250
- "outputs": [
251
- {
252
- "name": "stdout",
253
- "output_type": "stream",
254
- "text": [
255
- "Chicken dishes:\n",
256
- "Crispy Roasted Chicken\n",
257
- "Roasted Spatchcocked Chicken With Potatoes\n",
258
- "Easy Baked Chicken Thighs\n",
259
- "Crispy Baked Chicken Thighs\n",
260
- "Crispy and Tender Baked Chicken Thighs\n",
261
- "Million Dollar Chicken\n",
262
- "Simple Whole Roasted Chicken\n",
263
- "Beer Can Chicken\n",
264
- "Air Fryer Chicken Thighs\n",
265
- "Happy Roast Chicken\n",
266
- "\n",
267
- "Desserts:\n",
268
- "Chocolate Snack Cake\n",
269
- "Charred Spiced Pears with Smoky Vanilla Cherry Sauce\n",
270
- "Meringue Topped Banana Pudding\n",
271
- "White Chocolate Cinnamon Toast Crunch Bars\n",
272
- "Plum Cobbler for Two\n",
273
- "Pumpkin Cheesecake Cookies\n",
274
- "Chocolate Whipped Cottage Cheese\n",
275
- "Nutella Ice Cream\n",
276
- "3-Ingredient Banana Oatmeal Cookies\n",
277
- "Caramel Apple Pie Cookies\n"
278
- ]
279
- }
280
- ],
281
  "source": [
282
- "print('Chicken dishes:')\n",
283
- "for i in chickenDishList:\n",
284
- " print(i['title'])\n",
285
  " \n",
286
- "print('')\n",
287
- "print('Desserts:')\n",
288
- "for i in dessertList:\n",
289
- " print(i['title'])"
290
  ]
291
  },
292
  {
@@ -302,92 +235,62 @@
302
  },
303
  {
304
  "cell_type": "code",
305
- "execution_count": 284,
306
  "metadata": {},
307
  "outputs": [],
308
  "source": [
309
- "from langchain.agents import load_tools\n",
310
- "from langchain.agents import initialize_agent\n",
311
- "from langchain.llms import OpenAI\n",
312
- "from langchain.chat_models import ChatOpenAI\n",
313
- "from langchain.callbacks import StdOutCallbackHandler\n",
314
- "\n",
315
- "from langchain.agents import initialize_agent\n",
316
- "llm = ChatOpenAI(temperature=0, model_name='gpt-4') # 'gpt-3.5-turbo'\n",
317
- "\n",
318
- "agent = initialize_agent(\n",
319
- " agent=\"zero-shot-react-description\", \n",
320
- " tools=tools, \n",
321
- " llm=llm,\n",
322
- " verbose=True,\n",
323
- " max_iterations=7, \n",
324
- " return_intermediate_steps=True\n",
325
- ")\n",
326
- "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",
327
- "#query = \"Can you show tell me what ingredients I need for the first baked chicken recipe?\"\n",
328
- "#query = \"Can you show tell me what ingredients I need for the last baked chicken recipe? \"\n",
329
- "#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",
330
- "#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",
331
- "#query = \"Which is healthier the Caramel Apple Pie Cookies or the beer chicken? Please explain how you got to your answer.\"\n",
332
- "#query = \"Is the moon closer to earth or the sun?\"\n",
333
- "#query = \"How good are the apple pie cookies?\"\n",
334
- "query = \"What tools do I need for the Nutella Ice Cream?\"\n",
335
- "#query = \"My bowl is broken, can I still make Nutella Ice Cream? Answer as a yes/no.\"\n",
336
- "#response = agent({\"input\": f\"{system} {query}\"})\n"
337
- ]
338
- },
339
- {
340
- "cell_type": "markdown",
341
- "metadata": {},
342
- "source": [
343
- "# UI - Simple UI"
344
  ]
345
  },
346
  {
347
  "cell_type": "code",
348
- "execution_count": 309,
349
  "metadata": {},
350
- "outputs": [
351
- {
352
- "name": "stdout",
353
- "output_type": "stream",
354
- "text": [
355
- "Running on local URL: http://127.0.0.1:7923\n",
356
- "\n",
357
- "To create a public link, set `share=True` in `launch()`.\n"
358
- ]
359
- },
360
- {
361
- "data": {
362
- "text/html": [
363
- "<div><iframe src=\"http://127.0.0.1:7923/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
364
- ],
365
- "text/plain": [
366
- "<IPython.core.display.HTML object>"
367
- ]
368
- },
369
- "metadata": {},
370
- "output_type": "display_data"
371
- },
372
- {
373
- "data": {
374
- "text/plain": []
375
- },
376
- "execution_count": 309,
377
- "metadata": {},
378
- "output_type": "execute_result"
379
- }
380
- ],
381
  "source": [
382
- "import gradio as gr\n",
 
 
 
 
383
  "\n",
384
  "def ask_query(query):\n",
385
  " \n",
386
  " # LLM \n",
387
- " llm = ChatOpenAI(temperature=0, model_name='gpt-4') # 'gpt-3.5-turbo'\n",
388
  " agent = initialize_agent(agent=\"zero-shot-react-description\", tools=tools, llm=llm, verbose=True, max_iterations=7, return_intermediate_steps=True)\n",
389
- " 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",
390
- " response = agent({\"input\": f\"{system} {query}\"})\n",
 
 
 
391
  "\n",
392
  " # Show response \n",
393
  " stepsDict = json.loads(dumps(response[\"intermediate_steps\"], pretty=True))\n",
@@ -404,56 +307,26 @@
404
  "\n",
405
  " resp += '\\nFinal Thought:\\n'\n",
406
  " resp += response['output']\n",
407
- " return resp\n",
408
- "\n",
409
- "\n",
410
- "with gr.Blocks() as demo:\n",
411
- " gr.Markdown(\"Start typing below and then click **Run** to see the output.\")\n",
412
- " with gr.Row():\n",
413
- " with gr.Column():\n",
414
- " inp = gr.Textbox(placeholder=\"Type your question here or click on example below?\", label=\"Question\") \n",
415
- " btn = gr.Button(\"Run\") \n",
416
- " examples = [\"Can you show tell me what ingredients I need for the first baked chicken recipe?\",\n",
417
- " \"Can you show tell me what ingredients I need for the last baked chicken recipe? \",\n",
418
- " \"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",
419
- " \"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",
420
- " \"Which is healthier the Caramel Apple Pie Cookies or the beer chicken? Please explain how you got to your answer.\",\n",
421
- " \"Is the moon closer to earth or the sun?\",\n",
422
- " \"How good are the apple pie cookies?\",\n",
423
- " \"What tools do I need for the Nutella Ice Cream?\",\n",
424
- " \"My bowl is broken, can I still make Nutella Ice Cream? Answer as a yes/no.\"\n",
425
- " ] \n",
426
- " gr.Examples(examples, [inp])\n",
427
- " with gr.Column():\n",
428
- " out = gr.TextArea(label=\"ReAct Answer\", placeholder=\"The answer will go here...\")\n",
429
- " \n",
430
- " btn.click(fn=ask_query, inputs=inp, outputs=out)\n",
431
- " \n",
432
- "demo.launch(show_error=True)"
433
  ]
434
  },
435
  {
436
  "cell_type": "code",
437
- "execution_count": 289,
438
  "metadata": {},
439
  "outputs": [
440
- {
441
- "name": "stderr",
442
- "output_type": "stream",
443
- "text": [
444
- "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",
445
- " warnings.warn(\n",
446
- "c:\\sc\\ai\\rag-demo-1\\.venv\\lib\\site-packages\\gradio\\utils.py:810: UserWarning: Expected 1 arguments for function <function ask_query at 0x00000155E63AAB90>, received 2.\n",
447
- " warnings.warn(\n",
448
- "c:\\sc\\ai\\rag-demo-1\\.venv\\lib\\site-packages\\gradio\\utils.py:818: UserWarning: Expected maximum 1 arguments for function <function ask_query at 0x00000155E63AAB90>, received 2.\n",
449
- " warnings.warn(\n"
450
- ]
451
- },
452
  {
453
  "name": "stdout",
454
  "output_type": "stream",
455
  "text": [
456
- "Running on local URL: http://127.0.0.1:7906\n",
457
  "\n",
458
  "To create a public link, set `share=True` in `launch()`.\n"
459
  ]
@@ -461,7 +334,7 @@
461
  {
462
  "data": {
463
  "text/html": [
464
- "<div><iframe src=\"http://127.0.0.1:7906/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
465
  ],
466
  "text/plain": [
467
  "<IPython.core.display.HTML object>"
@@ -474,44 +347,10 @@
474
  "data": {
475
  "text/plain": []
476
  },
477
- "execution_count": 289,
478
  "metadata": {},
479
  "output_type": "execute_result"
480
- }
481
- ],
482
- "source": [
483
- "import gradio as gr\n",
484
- "\n",
485
- "def ask_query(query):\n",
486
- " response = agent({\"input\": f\"{system} {query}\"})\n",
487
- " \n",
488
- " stepsDict = json.loads(dumps(response[\"intermediate_steps\"], pretty=True))\n",
489
- " 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",
490
- " resp += '\\n\\n'\n",
491
- " resp += 'Steps to solve answer using ReAct\\n'\n",
492
- " for i in range(len(stepsDict)):\n",
493
- " resp += '##########################################\\n'\n",
494
- " resp += f'Step: {i+1} of {len(stepsDict)}\\n'\n",
495
- " resp += f\"Thought: {stepsDict[i][0]['kwargs']['log']}\\n\"\n",
496
- " resp += 'Below is what the tool returned...\\n'\n",
497
- " resp += f\"Action response: {stepsDict[i][1]}\\n\" \n",
498
- " resp += '\\n'\n",
499
- "\n",
500
- " resp += '\\nFinal Thought:\\n'\n",
501
- " resp += response['output']\n",
502
- " return resp\n",
503
- "\n",
504
- "demo = gr.Interface(fn=ask_query, inputs=[\"text\", \"text\"], outputs=\"text\", allow_flagging=False)\n",
505
- "#gr.Markdown(\"# Hello there\")\n",
506
- "#gr.Examples(\"text\", \"text\")\n",
507
- "demo.launch(show_error=True)"
508
- ]
509
- },
510
- {
511
- "cell_type": "code",
512
- "execution_count": 286,
513
- "metadata": {},
514
- "outputs": [
515
  {
516
  "name": "stdout",
517
  "output_type": "stream",
@@ -519,117 +358,48 @@
519
  "\n",
520
  "\n",
521
  "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
522
- "\u001b[32;1m\u001b[1;3mI need to find the apple cookie pie recipe and check its rating.\n",
523
- "Action: Dessert Recipes tool\n",
524
- "Action Input: None\u001b[0m\n",
525
- "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",
526
- "Thought:\u001b[32;1m\u001b[1;3mThe apple cookie pie recipe is not in the list of dessert recipes. I should say so.\n",
527
- "Final Answer: I'm sorry, I couldn't find the apple cookie pie recipe.\u001b[0m\n",
 
 
 
 
528
  "\n",
529
  "\u001b[1m> Finished chain.\u001b[0m\n"
530
  ]
531
  }
532
  ],
533
- "source": [
534
- "# import gradio as gr\n",
535
- "\n",
536
- "# def ask_question(question):\n",
537
- " \n",
538
- "# # LLM \n",
539
- "# llm = ChatOpenAI(temperature=0, model_name='gpt-4') # 'gpt-3.5-turbo'\n",
540
- "# agent = initialize_agent(agent=\"zero-shot-react-description\", tools=tools, llm=llm, verbose=True, max_iterations=7, return_intermediate_steps=True)\n",
541
- "# 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",
542
- "# # #question = \"Can you show tell me what ingredients I need for the first baked chicken recipe?\"\n",
543
- "# # #question = \"Can you show tell me what ingredients I need for the last baked chicken recipe? \"\n",
544
- "# # #question = \"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",
545
- "# # #question = \"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",
546
- "# # #question = \"Which is healthier the Caramel Apple Pie Cookies or the beer chicken? Please explain how you got to your answer.\"\n",
547
- "# # #question = \"Is the moon closer to earth or the sun?\"\n",
548
- "# # #question = \"How good are the apple pie cookies?\"\n",
549
- "# # #question = \"What tools do I need for the Nutella Ice Cream?\"\n",
550
- "# # #question = \"My bowl is broken, can I still make Nutella Ice Cream? Answer as a yes/no.\"\n",
551
- "# response = agent({\"input\": f\"{system} {question}\"})\n",
552
- "\n",
553
- "# # Show response \n",
554
- "# stepsDict = json.loads(dumps(response[\"intermediate_steps\"], pretty=True))\n",
555
- "# 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",
556
- "# resp += '\\n\\n'\n",
557
- "# resp += 'Steps to solve answer using ReAct\\n'\n",
558
- "# for i in range(len(stepsDict)):\n",
559
- "# resp += '##########################################\\n'\n",
560
- "# resp += f'Step: {i+1} of {len(stepsDict)}\\n'\n",
561
- "# resp += f\"Thought: {stepsDict[i][0]['kwargs']['log']}\\n\"\n",
562
- "# resp += 'Below is what the tool returned...\\n'\n",
563
- "# resp += f\"Action response: {stepsDict[i][1]}\\n\" \n",
564
- "# resp += '\\n'\n",
565
- "\n",
566
- "# resp += '\\nFinal Thought:\\n'\n",
567
- "# resp += response['output']\n",
568
- "# return resp\n",
569
- "\n",
570
- "# with gr.Blocks() as demo:\n",
571
- "# openAIKeyBox = gr.Text(label=\"OpenAI Key\")\n",
572
- "# queryTB = gr.Textbox(label=\"Query\")\n",
573
- "# submitBtn = gr.Button(\"Submit\")\n",
574
- "# submitBtn.click(\n",
575
- "# ask_question\n",
576
- "# )\n",
577
- "\n",
578
- "# # demo = gr.Interface(fn=ask_query, inputs=\"text\", outputs=\"text\", allow_flagging=False)\n",
579
- "# # #gr.Markdown(\"# Hello there\")\n",
580
- "# # #gr.Examples(\"text\", \"text\")\n",
581
- "# # demo.launch(show_error=True)"
582
- ]
583
- },
584
- {
585
- "cell_type": "code",
586
- "execution_count": 291,
587
- "metadata": {},
588
- "outputs": [
589
- {
590
- "name": "stdout",
591
- "output_type": "stream",
592
- "text": [
593
- "Running on local URL: http://127.0.0.1:7908\n",
594
- "\n",
595
- "To create a public link, set `share=True` in `launch()`.\n"
596
- ]
597
- },
598
- {
599
- "data": {
600
- "text/html": [
601
- "<div><iframe src=\"http://127.0.0.1:7908/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
602
- ],
603
- "text/plain": [
604
- "<IPython.core.display.HTML object>"
605
- ]
606
- },
607
- "metadata": {},
608
- "output_type": "display_data"
609
- },
610
- {
611
- "data": {
612
- "text/plain": []
613
- },
614
- "execution_count": 291,
615
- "metadata": {},
616
- "output_type": "execute_result"
617
- }
618
- ],
619
  "source": [
620
  "import gradio as gr\n",
621
- "def update(name):\n",
622
- " return f\"Welcome to Gradio, {name}!\"\n",
623
  "\n",
624
  "with gr.Blocks() as demo:\n",
625
  " gr.Markdown(\"Start typing below and then click **Run** to see the output.\")\n",
626
  " with gr.Row():\n",
627
- " inp = gr.Textbox(placeholder=\"What is your name?\")\n",
628
- " out = gr.Textbox()\n",
629
- " btn = gr.Button(\"Run\")\n",
630
- " btn.click(fn=update, inputs=inp, outputs=out)\n",
631
- "\n",
632
- "demo.launch()"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
633
  ]
634
  }
635
  ],
 
2
  "cells": [
3
  {
4
  "cell_type": "code",
5
+ "execution_count": 1,
6
  "metadata": {},
7
  "outputs": [
8
  {
 
28
  " for key in settingsJson:\n",
29
  " os.environ[key] = settingsJson[key]\n",
30
  " \n",
31
+ " del settingsJson"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  ]
33
  },
34
  {
 
40
  },
41
  {
42
  "cell_type": "code",
43
+ "execution_count": 37,
44
  "metadata": {},
45
+ "outputs": [
46
+ {
47
+ "name": "stdout",
48
+ "output_type": "stream",
49
+ "text": [
50
+ "{'@context': 'http://schema.org', '@type': ['Recipe', 'NewsArticle'], 'headline': 'Easy Baked Chicken Thighs', 'datePublished': '2016-03-02T13:02:00.000-05:00', 'dateModified': '2023-03-07T10:00:00.000-05:00', 'author': [{'@type': 'Person', 'name': 'KALENG', 'url': 'https://www.allrecipes.com/cook/1118863'}], 'description': 'These easy baked chicken thighs, seasoned with onion and garlic, take just 30 minutes to bake in the oven for a quick and delicious weeknight meal.', 'video': {'@type': 'VideoObject', 'contentUrl': 'https://content.jwplatform.com/videos/a9jndB0U-K3AjnAEN.mp4', 'description': \"In this video, we show you how to make the easiest, 30-minute, baked chicken recipe. If you've got a baking dish, an oven, some chicken thighs, garlic powder, and onion flakes, then you've got yourself a quick and tasty little dinner. Whether you're a beginner cook or just need to get food on the table, this recipe is for you. Try adding new spices or maybe whip up a savory sauce to go with this. \", 'duration': 'PT47S', 'name': 'Easy Baked Chicken Thighs', 'thumbnailUrl': 'https://cdn.jwplayer.com/v2/media/a9jndB0U/poster.jpg?width=720', 'uploadDate': '2021-03-08T18:54:35.211-05:00'}, 'name': 'Easy Baked Chicken Thighs', 'aggregateRating': {'@type': 'AggregateRating', 'ratingValue': '4.2', 'ratingCount': '201'}, 'cookTime': 'PT30M', 'nutrition': {'@type': 'NutritionInformation', 'calories': '162 kcal', 'carbohydrateContent': '3 g', 'cholesterolContent': '69 mg', 'fiberContent': '0 g', 'proteinContent': '20 g', 'saturatedFatContent': '2 g', 'sodiumContent': '58 mg', 'sugarContent': '1 g', 'fatContent': '8 g', 'unsaturatedFatContent': '0 g'}, 'prepTime': 'PT5M', 'recipeCategory': ['Dinner'], 'recipeIngredient': ['4 chicken thighs', '4 teaspoons garlic powder', '4 teaspoons onion flakes'], 'recipeInstructions': [{'@type': 'HowToStep', 'text': 'Preheat the oven to 375 degrees F (190 degrees C).'}, {'@type': 'HowToStep', 'text': 'Place chicken thighs in a baking dish; season both sides with garlic powder and onion flakes.'}, {'@type': 'HowToStep', 'image': [{'@type': 'ImageObject', 'url': 'https://www.allrecipes.com/thmb/BOL-8G2Bl2He3S9qVHwSl0oBP5A=/1500x0/filters:no_upscale():max_bytes(150000):strip_icc()/4348465-easy-baked-chicken-thighs-Meoshia_P-1x1-1-88ab86050c154776a8d3c44260ae385d.jpg'}], 'text': 'Bake in the preheated oven until no longer pink at the bone and juices run clear, about 30 minutes. An instant-read thermometer inserted into thickest part of thigh, near the bone, should read 165 degrees F (74 degrees C).'}], 'recipeYield': ['4'], 'totalTime': 'PT35M', 'about': []}\n"
51
+ ]
52
+ }
53
+ ],
54
  "source": [
55
  "import requests\n",
56
  "from bs4 import BeautifulSoup\n",
57
  "import json\n",
58
  "import validators \n",
59
  "\n",
60
+ "pageCache = {}\n",
61
+ "\n",
62
+ "def json_safe_loads(jsonString):\n",
63
+ " try:\n",
64
+ " obj = json.loads(jsonString)\n",
65
+ " except ValueError as e:\n",
66
+ " return None\n",
67
+ " return obj\n",
68
+ "\n",
69
  "def remove_keys(dictionary: dict, keyList: list):\n",
70
  " for key in keyList: \n",
71
  " if key in dictionary:\n",
 
73
  "\n",
74
  "def get_recipe_as_json(url: str) -> dict: \n",
75
  " \n",
76
+ " obj = json_safe_loads(url)\n",
77
+ " if obj is not None: \n",
78
+ " if 'url' in obj:\n",
79
+ " url = obj['url']\n",
80
+ " \n",
81
  " url = url.replace(\"'\", \"\").replace('\"', '')\n",
82
  " \n",
83
  " if not validators.url(url):\n",
84
  " return {'error': 'Invalid url format', 'url': url }\n",
85
+ " \n",
86
+ " if url in pageCache:\n",
87
+ " return pageCache[url] \n",
88
  "\n",
89
  " html = requests.get(url).text \n",
90
  " soup = BeautifulSoup(html)\n",
 
93
  " recipeDict = json.loads(script[0].text)[0]\n",
94
  " remove_keys(recipeDict, ['review', 'image', 'mainEntityOfPage', 'publisher'])\n",
95
  " \n",
96
+ " pageCache[url] = recipeDict\n",
97
+ " \n",
98
  " return recipeDict\n",
99
  "\n",
100
  "# url = \"https://www.allrecipes.com/recipe/212498/easy-chicken-and-broccoli-alfredo/\"\n",
101
  "# obj = get_recipe_as_json(url)\n",
102
+ "#x = get_recipe_as_json('{ \"url\": \"https://www.allrecipes.com/recipe/235153/easy-baked-chicken-thighs/\" }')\n",
103
+ "# x = get_recipe_as_json('\"https://www.allrecipes.com/recipe/235153/easy-baked-chicken-thighs/\"')\n",
104
  "# print(x)"
105
  ]
106
  },
107
+ {
108
+ "cell_type": "code",
109
+ "execution_count": 32,
110
+ "metadata": {},
111
+ "outputs": [
112
+ {
113
+ "name": "stdout",
114
+ "output_type": "stream",
115
+ "text": [
116
+ "<class 'dict'>\n"
117
+ ]
118
+ }
119
+ ],
120
+ "source": [
121
+ "x = json.loads('{ \"url\": \"https://www.allrecipes.com/recipe/235153/easy-baked-chicken-thighs/\"}')\n",
122
+ "#x = json.loads('{\"param1\": 123}')\n",
123
+ "print(type(x))"
124
+ ]
125
+ },
126
  {
127
  "cell_type": "markdown",
128
  "metadata": {},
 
132
  },
133
  {
134
  "cell_type": "code",
135
+ "execution_count": 3,
136
  "metadata": {},
137
  "outputs": [],
138
  "source": [
139
  "dessertList = [\n",
140
+ "\"https://www.allrecipes.com/chocolate-snack-cake-recipe-8350343\",\n",
141
+ "\"https://www.allrecipes.com/charred-spiced-pears-with-smoky-vanilla-cherry-sauce-recipe-8347080\",\n",
142
+ "\"https://www.allrecipes.com/meringue-topped-banana-pudding-recipe-8347040\",\n",
143
+ "\"https://www.allrecipes.com/white-chocolate-cinnamon-toast-crunch-bars-recipe-7556790\",\n",
144
+ "\"https://www.allrecipes.com/plum-cobbler-for-two-recipe-8304143\",\n",
145
+ "\"https://www.allrecipes.com/pumpkin-cheesecake-cookies-recipe-7972485\",\n",
146
+ "\"https://www.allrecipes.com/chocolate-whipped-cottage-cheese-recipe-8303272\",\n",
147
+ "\"https://www.allrecipes.com/nutella-ice-cream-recipe-7508716\",\n",
148
+ "\"https://www.allrecipes.com/3-ingredient-banana-oatmeal-cookies-recipe-7972686\",\n",
149
+ "\"https://www.allrecipes.com/caramel-apple-pie-cookies-recipe-7642173\"\n",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
  "]\n",
151
  "\n",
152
  "chickenDishList = [\n",
153
+ "\"https://www.allrecipes.com/recipe/228363/crispy-roasted-chicken/\",\n",
154
+ "\"https://www.allrecipes.com/recipe/254877/roasted-spatchcocked-chicken-with-potatoes/\",\n",
155
+ "\"https://www.allrecipes.com/recipe/235153/easy-baked-chicken-thighs/\",\n",
156
+ "\"https://www.allrecipes.com/recipe/258878/crispy-baked-chicken-thighs/\",\n",
157
+ "\"https://www.allrecipes.com/recipe/235151/crispy-and-tender-baked-chicken-thighs/\",\n",
158
+ "\"https://www.allrecipes.com/recipe/233953/million-dollar-chicken/\",\n",
159
+ "\"https://www.allrecipes.com/recipe/70679/simple-whole-roasted-chicken/\",\n",
160
+ "\"https://www.allrecipes.com/recipe/214618/beer-can-chicken/\",\n",
161
+ "\"https://www.allrecipes.com/recipe/272858/air-fryer-chicken-thighs/\",\n",
162
+ "\"https://www.allrecipes.com/recipe/214478/happy-roast-chicken/\"\n",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
  "]"
164
  ]
165
  },
 
172
  },
173
  {
174
  "cell_type": "code",
175
+ "execution_count": 13,
176
  "metadata": {},
177
  "outputs": [],
178
  "source": [
 
199
  "\n",
200
  "get_recipe_as_json_tool = Tool(name='Get a Recipe tool', func=get_recipe, description=\"\"\"\n",
201
  " Useful for fetching a particular recipe, you can only fetch a recipe with it's url, you must get that using another tool\n",
202
+ " The tool uses the https://schema.org/Recipe format to store it's recipes. \n",
203
  " \"\"\")\n",
204
  "\n",
205
  "# Tool list\n",
 
208
  },
209
  {
210
  "cell_type": "code",
211
+ "execution_count": 5,
212
  "metadata": {},
213
+ "outputs": [],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
214
  "source": [
215
+ "# print('Chicken dishes:')\n",
216
+ "# for i in chickenDishList:\n",
217
+ "# print(i['title'])\n",
218
  " \n",
219
+ "# print('')\n",
220
+ "# print('Desserts:')\n",
221
+ "# for i in dessertList:\n",
222
+ "# print(i['title'])"
223
  ]
224
  },
225
  {
 
235
  },
236
  {
237
  "cell_type": "code",
238
+ "execution_count": 6,
239
  "metadata": {},
240
  "outputs": [],
241
  "source": [
242
+ "# from langchain.agents import load_tools\n",
243
+ "# from langchain.agents import initialize_agent\n",
244
+ "# from langchain.llms import OpenAI\n",
245
+ "# from langchain.chat_models import ChatOpenAI\n",
246
+ "# from langchain.callbacks import StdOutCallbackHandler\n",
247
+ "\n",
248
+ "# from langchain.agents import initialize_agent\n",
249
+ "# llm = ChatOpenAI(temperature=0, model_name='gpt-4') # 'gpt-3.5-turbo'\n",
250
+ "\n",
251
+ "# agent = initialize_agent(\n",
252
+ "# agent=\"zero-shot-react-description\", \n",
253
+ "# tools=tools, \n",
254
+ "# llm=llm,\n",
255
+ "# verbose=True,\n",
256
+ "# max_iterations=7, \n",
257
+ "# return_intermediate_steps=True\n",
258
+ "# )\n",
259
+ "# 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",
260
+ "# #query = \"Can you show tell me what ingredients I need for the first baked chicken recipe?\"\n",
261
+ "# #query = \"Can you show tell me what ingredients I need for the last baked chicken recipe? \"\n",
262
+ "# #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",
263
+ "# #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",
264
+ "# #query = \"Which is healthier the Caramel Apple Pie Cookies or the beer chicken? Please explain how you got to your answer.\"\n",
265
+ "# #query = \"Is the moon closer to earth or the sun?\"\n",
266
+ "# #query = \"How good are the apple pie cookies?\"\n",
267
+ "# query = \"What tools do I need for the Nutella Ice Cream?\"\n",
268
+ "# #query = \"My bowl is broken, can I still make Nutella Ice Cream? Answer as a yes/no.\"\n",
269
+ "# #response = agent({\"input\": f\"{system} {query}\"})\n"
 
 
 
 
 
 
 
270
  ]
271
  },
272
  {
273
  "cell_type": "code",
274
+ "execution_count": 19,
275
  "metadata": {},
276
+ "outputs": [],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
277
  "source": [
278
+ "from langchain.agents import load_tools\n",
279
+ "from langchain.agents import initialize_agent\n",
280
+ "from langchain.llms import OpenAI\n",
281
+ "from langchain.chat_models import ChatOpenAI\n",
282
+ "from langchain.load.dump import dumps\n",
283
  "\n",
284
  "def ask_query(query):\n",
285
  " \n",
286
  " # LLM \n",
287
+ " llm = ChatOpenAI(temperature=0.2, model_name='gpt-4') # 'gpt-3.5-turbo'\n",
288
  " agent = initialize_agent(agent=\"zero-shot-react-description\", tools=tools, llm=llm, verbose=True, max_iterations=7, return_intermediate_steps=True)\n",
289
+ " system = \"\"\"\n",
290
+ " If the answer is not in the tools or context passed to you then don't answer. \\n\n",
291
+ " If you don't know the answer then say so. \\n \n",
292
+ " \"\"\" \n",
293
+ " response = agent({\"input\": f\"{system} [[RECIPENAME]] {query}\"})\n",
294
  "\n",
295
  " # Show response \n",
296
  " stepsDict = json.loads(dumps(response[\"intermediate_steps\"], pretty=True))\n",
 
307
  "\n",
308
  " resp += '\\nFinal Thought:\\n'\n",
309
  " resp += response['output']\n",
310
+ " return resp"
311
+ ]
312
+ },
313
+ {
314
+ "cell_type": "markdown",
315
+ "metadata": {},
316
+ "source": [
317
+ "# UI - Simple UI"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
318
  ]
319
  },
320
  {
321
  "cell_type": "code",
322
+ "execution_count": 20,
323
  "metadata": {},
324
  "outputs": [
 
 
 
 
 
 
 
 
 
 
 
 
325
  {
326
  "name": "stdout",
327
  "output_type": "stream",
328
  "text": [
329
+ "Running on local URL: http://127.0.0.1:7866\n",
330
  "\n",
331
  "To create a public link, set `share=True` in `launch()`.\n"
332
  ]
 
334
  {
335
  "data": {
336
  "text/html": [
337
+ "<div><iframe src=\"http://127.0.0.1:7866/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
338
  ],
339
  "text/plain": [
340
  "<IPython.core.display.HTML object>"
 
347
  "data": {
348
  "text/plain": []
349
  },
350
+ "execution_count": 20,
351
  "metadata": {},
352
  "output_type": "execute_result"
353
+ },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
354
  {
355
  "name": "stdout",
356
  "output_type": "stream",
 
358
  "\n",
359
  "\n",
360
  "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
361
+ "\u001b[32;1m\u001b[1;3mI need to use the Chicken Recipes tool to find the last baked chicken recipe.\n",
362
+ "Action: Chicken Recipes tool\n",
363
+ "Action Input: {}\u001b[0m\n",
364
+ "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",
365
+ "Thought:\u001b[32;1m\u001b[1;3mThe last URL in the list seems to be the last baked chicken recipe. I need to use the Get a Recipe tool to fetch the recipe details.\n",
366
+ "Action: Get a Recipe tool\n",
367
+ "Action Input: {'url': 'https://www.allrecipes.com/recipe/214478/happy-roast-chicken/'}\u001b[0m\n",
368
+ "Observation: \u001b[38;5;200m\u001b[1;3m{'error': 'Invalid url format', 'url': '{url: https://www.allrecipes.com/recipe/214478/happy-roast-chicken/}'}\u001b[0m\n",
369
+ "Thought:\u001b[32;1m\u001b[1;3mThe URL format seems to be invalid. I can't fetch the recipe details with this URL. \n",
370
+ "Final Answer: I'm sorry, but I can't fetch the recipe details at the moment due to an invalid URL format.\u001b[0m\n",
371
  "\n",
372
  "\u001b[1m> Finished chain.\u001b[0m\n"
373
  ]
374
  }
375
  ],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
376
  "source": [
377
  "import gradio as gr\n",
 
 
378
  "\n",
379
  "with gr.Blocks() as demo:\n",
380
  " gr.Markdown(\"Start typing below and then click **Run** to see the output.\")\n",
381
  " with gr.Row():\n",
382
+ " with gr.Column():\n",
383
+ " inp = gr.Textbox(placeholder=\"Type your question here?\", label=\"Question\") \n",
384
+ " btn = gr.Button(\"Run\") \n",
385
+ " examples = [\"Can you show tell me what ingredients I need for the first baked chicken recipe?\",\n",
386
+ " \"I have 15 guests coming to visit, if I make the 'Chocolate Snack Cake' recipe will there be enough? \",\n",
387
+ " \"Can you show tell me what ingredients I need for the last baked chicken recipe? \",\n",
388
+ " \"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",
389
+ " \"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",
390
+ " \"Which is healthier the Caramel Apple Pie Cookies or the beer chicken? Please explain how you got to your answer.\",\n",
391
+ " \"Is the moon closer to earth or the sun?\",\n",
392
+ " \"How good are the apple pie cookies?\",\n",
393
+ " \"What tools do I need for the Nutella Ice Cream?\",\n",
394
+ " \"My bowl is broken, can I still make Nutella Ice Cream? Answer as a yes/no.\"\n",
395
+ " ] \n",
396
+ " gr.Examples(examples, [inp])\n",
397
+ " with gr.Column():\n",
398
+ " out = gr.TextArea(label=\"ReAct Answer\", placeholder=\"The answer will go here...\")\n",
399
+ " \n",
400
+ " btn.click(fn=ask_query, inputs=inp, outputs=out)\n",
401
+ " \n",
402
+ "demo.launch(show_error=True)"
403
  ]
404
  }
405
  ],