Listed recipes on page with heading message about intent
Browse files- app.py +62 -71
- demo-tools-1.ipynb +33 -177
app.py
CHANGED
@@ -40,16 +40,6 @@ def remove_keys(dictionary: dict, keyList: list):
|
|
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 |
urlArray = re.findall(r'(https?://\S+)', url)
|
54 |
if len(urlArray) == 0:
|
55 |
return {'error': 'Invalid url format, please try again using a fully qualified URL', 'url': url }
|
@@ -66,7 +56,9 @@ def get_recipe_as_json(url: str) -> dict:
|
|
66 |
|
67 |
soup = BeautifulSoup(html)
|
68 |
script = soup.find_all("script", {"id": "allrecipes-schema_1-0"})
|
69 |
-
|
|
|
|
|
70 |
|
71 |
recipeDict = json.loads(script[0].text)[0]
|
72 |
print(type(recipeDict))
|
@@ -125,14 +117,21 @@ from langchain.agents import Tool
|
|
125 |
def list_chicken_recipes(query: str):
|
126 |
return chickenDishList
|
127 |
|
128 |
-
list_chicken_recipes_tool = Tool(name='Chicken Recipes tool', func= list_chicken_recipes,
|
|
|
|
|
|
|
|
|
129 |
|
130 |
# Dessert functions
|
131 |
def list_dessert_recipes(query: str):
|
132 |
return dessertList
|
133 |
|
134 |
list_dessert_recipes_tool = Tool(name='Dessert Recipes tool', func=list_dessert_recipes,
|
135 |
-
|
|
|
|
|
|
|
136 |
|
137 |
# Recipe fetcher functions
|
138 |
def get_recipe(fully_qualified_url: str):
|
@@ -141,23 +140,14 @@ def get_recipe(fully_qualified_url: str):
|
|
141 |
get_recipe_as_json_tool = Tool(name='Get a Recipe tool', func=get_recipe, description="""
|
142 |
Useful for fetching a particular recipe by passing in a fully qualified url.
|
143 |
It is important that the parameter to pass in must be a fully qualified url and nothing else.
|
|
|
144 |
Parameter:
|
145 |
url (str): A fully qualified URL, including the scheme (e.g., "https://").
|
146 |
The tool uses the https://schema.org/Recipe format to store it's recipes.
|
147 |
""")
|
148 |
|
149 |
# Tool list
|
150 |
-
tools = [list_chicken_recipes_tool, list_dessert_recipes_tool, get_recipe_as_json_tool]
|
151 |
-
|
152 |
-
# %%
|
153 |
-
# print('Chicken dishes:')
|
154 |
-
# for i in chickenDishList:
|
155 |
-
# print(i['title'])
|
156 |
-
|
157 |
-
# print('')
|
158 |
-
# print('Desserts:')
|
159 |
-
# for i in dessertList:
|
160 |
-
# print(i['title'])
|
161 |
|
162 |
# %% [markdown]
|
163 |
# # LLM
|
@@ -166,37 +156,6 @@ tools = [list_chicken_recipes_tool, list_dessert_recipes_tool, get_recipe_as_jso
|
|
166 |
#
|
167 |
# 1 [tracking-inspecting-prompts-langchain-agents-weights-and-biases](https://kleiber.me/blog/2023/05/14/tracking-inspecting-prompts-langchain-agents-weights-and-biases/)
|
168 |
|
169 |
-
# %%
|
170 |
-
# from langchain.agents import load_tools
|
171 |
-
# from langchain.agents import initialize_agent
|
172 |
-
# from langchain.llms import OpenAI
|
173 |
-
# from langchain.chat_models import ChatOpenAI
|
174 |
-
# from langchain.callbacks import StdOutCallbackHandler
|
175 |
-
|
176 |
-
# from langchain.agents import initialize_agent
|
177 |
-
# llm = ChatOpenAI(temperature=0, model_name='gpt-4') # 'gpt-3.5-turbo'
|
178 |
-
|
179 |
-
# agent = initialize_agent(
|
180 |
-
# agent="zero-shot-react-description",
|
181 |
-
# tools=tools,
|
182 |
-
# llm=llm,
|
183 |
-
# verbose=True,
|
184 |
-
# max_iterations=7,
|
185 |
-
# return_intermediate_steps=True
|
186 |
-
# )
|
187 |
-
# 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."
|
188 |
-
# #query = "Can you show tell me what ingredients I need for the first baked chicken recipe?"
|
189 |
-
# #query = "Can you show tell me what ingredients I need for the last baked chicken recipe? "
|
190 |
-
# #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
|
191 |
-
# #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?"
|
192 |
-
# #query = "Which is healthier the Caramel Apple Pie Cookies or the beer chicken? Please explain how you got to your answer."
|
193 |
-
# #query = "Is the moon closer to earth or the sun?"
|
194 |
-
# #query = "How good are the apple pie cookies?"
|
195 |
-
# query = "What tools do I need for the Nutella Ice Cream?"
|
196 |
-
# #query = "My bowl is broken, can I still make Nutella Ice Cream? Answer as a yes/no."
|
197 |
-
# #response = agent({"input": f"{system} {query}"})
|
198 |
-
|
199 |
-
|
200 |
# %%
|
201 |
from langchain.agents import load_tools
|
202 |
from langchain.agents import initialize_agent
|
@@ -204,11 +163,29 @@ from langchain.llms import OpenAI
|
|
204 |
from langchain.chat_models import ChatOpenAI
|
205 |
from langchain.load.dump import dumps
|
206 |
|
207 |
-
def ask_query(model, apiKey, query):
|
208 |
|
209 |
if len(apiKey) > 0:
|
210 |
os.environ['OPENAI_API_KEY'] = apiKey
|
211 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
212 |
print(model)
|
213 |
|
214 |
# LLM
|
@@ -222,9 +199,10 @@ def ask_query(model, apiKey, query):
|
|
222 |
|
223 |
# Show response
|
224 |
stepsDict = json.loads(dumps(response["intermediate_steps"], pretty=True))
|
225 |
-
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.
|
226 |
resp += '\n\n'
|
227 |
resp += 'Steps to solve answer using ReAct\n'
|
|
|
228 |
for i in range(len(stepsDict)):
|
229 |
resp += '##########################################\n'
|
230 |
resp += f'Step: {i+1} of {len(stepsDict)}\n'
|
@@ -237,16 +215,6 @@ def ask_query(model, apiKey, query):
|
|
237 |
resp += response['output']
|
238 |
return resp
|
239 |
|
240 |
-
# %%
|
241 |
-
# import re
|
242 |
-
|
243 |
-
# #url = "fetch url https://www.allrecipes.com/recipe/212498/easy-chicken-and-broccoli-alfredo/"
|
244 |
-
# url = '{ "url": "https://www.allrecipes.com/recipe/235153/easy-baked-chicken-thighs/" }'
|
245 |
-
# url = 'https://www.allrecipes.com/recipe/235151/crispy-and-tender-baked-chicken-thighs/'
|
246 |
-
|
247 |
-
# result = re.findall(r'(https?://\S+)', url)
|
248 |
-
# print(result)
|
249 |
-
|
250 |
# %% [markdown]
|
251 |
# # UI - Simple UI
|
252 |
|
@@ -254,12 +222,18 @@ def ask_query(model, apiKey, query):
|
|
254 |
import gradio as gr
|
255 |
|
256 |
with gr.Blocks() as demo:
|
257 |
-
|
258 |
with gr.Row():
|
259 |
with gr.Column():
|
260 |
-
|
|
|
|
|
|
|
|
|
|
|
261 |
openAIKey = gr.Textbox(placeholder="Paste your OpenAI API Key Here", label="OpenAI API Key")
|
262 |
-
inp = gr.Textbox(placeholder="Type your question here...", label="Question")
|
|
|
263 |
btn = gr.Button("Run")
|
264 |
examples = ["Can you show tell me what ingredients I need for the first baked chicken recipe?",
|
265 |
"I have 15 guests coming to visit, if I make the 'Chocolate Snack Cake' recipe will there be enough? ",
|
@@ -274,10 +248,27 @@ with gr.Blocks() as demo:
|
|
274 |
]
|
275 |
gr.Examples(examples, [inp])
|
276 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
277 |
with gr.Column():
|
278 |
out = gr.TextArea(label="ReAct Answer", placeholder="The answer will go here...")
|
279 |
|
280 |
-
btn.click(fn=ask_query, inputs=[modelDD, openAIKey, inp], outputs=out)
|
281 |
|
282 |
demo.launch(show_error=True)
|
283 |
|
|
|
40 |
|
41 |
def get_recipe_as_json(url: str) -> dict:
|
42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
urlArray = re.findall(r'(https?://\S+)', url)
|
44 |
if len(urlArray) == 0:
|
45 |
return {'error': 'Invalid url format, please try again using a fully qualified URL', 'url': url }
|
|
|
56 |
|
57 |
soup = BeautifulSoup(html)
|
58 |
script = soup.find_all("script", {"id": "allrecipes-schema_1-0"})
|
59 |
+
|
60 |
+
if len(script) == 0:
|
61 |
+
return "No recipe found."
|
62 |
|
63 |
recipeDict = json.loads(script[0].text)[0]
|
64 |
print(type(recipeDict))
|
|
|
117 |
def list_chicken_recipes(query: str):
|
118 |
return chickenDishList
|
119 |
|
120 |
+
list_chicken_recipes_tool = Tool(name='Chicken Recipes tool', func= list_chicken_recipes,
|
121 |
+
description="""
|
122 |
+
This tools lists the available Chicken Recipes.
|
123 |
+
Only call this tool to fetch Chicken recipes.
|
124 |
+
""")
|
125 |
|
126 |
# Dessert functions
|
127 |
def list_dessert_recipes(query: str):
|
128 |
return dessertList
|
129 |
|
130 |
list_dessert_recipes_tool = Tool(name='Dessert Recipes tool', func=list_dessert_recipes,
|
131 |
+
description="""
|
132 |
+
This tools lists the available Dessert Recipes.
|
133 |
+
Only call this tool for fetching Dessert Recipes.
|
134 |
+
""")
|
135 |
|
136 |
# Recipe fetcher functions
|
137 |
def get_recipe(fully_qualified_url: str):
|
|
|
140 |
get_recipe_as_json_tool = Tool(name='Get a Recipe tool', func=get_recipe, description="""
|
141 |
Useful for fetching a particular recipe by passing in a fully qualified url.
|
142 |
It is important that the parameter to pass in must be a fully qualified url and nothing else.
|
143 |
+
Don't call this function unless you have fetched a url from one of the other Tools first.
|
144 |
Parameter:
|
145 |
url (str): A fully qualified URL, including the scheme (e.g., "https://").
|
146 |
The tool uses the https://schema.org/Recipe format to store it's recipes.
|
147 |
""")
|
148 |
|
149 |
# Tool list
|
150 |
+
#tools = [list_chicken_recipes_tool, list_dessert_recipes_tool, get_recipe_as_json_tool]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
151 |
|
152 |
# %% [markdown]
|
153 |
# # LLM
|
|
|
156 |
#
|
157 |
# 1 [tracking-inspecting-prompts-langchain-agents-weights-and-biases](https://kleiber.me/blog/2023/05/14/tracking-inspecting-prompts-langchain-agents-weights-and-biases/)
|
158 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
# %%
|
160 |
from langchain.agents import load_tools
|
161 |
from langchain.agents import initialize_agent
|
|
|
163 |
from langchain.chat_models import ChatOpenAI
|
164 |
from langchain.load.dump import dumps
|
165 |
|
166 |
+
def ask_query(model, apiKey, query, accessList):
|
167 |
|
168 |
if len(apiKey) > 0:
|
169 |
os.environ['OPENAI_API_KEY'] = apiKey
|
170 |
|
171 |
+
#tools = [list_chicken_recipes_tool, list_dessert_recipes_tool, get_recipe_as_json_tool]
|
172 |
+
if len(accessList) == 0:
|
173 |
+
return "Please select at least one recipe list from the Access List"
|
174 |
+
|
175 |
+
tools = [get_recipe_as_json_tool]
|
176 |
+
if "Chicken recipes" in accessList:
|
177 |
+
tools.append(list_chicken_recipes_tool)
|
178 |
+
|
179 |
+
if "Desert recipes" in accessList:
|
180 |
+
tools.append(list_dessert_recipes_tool)
|
181 |
+
|
182 |
+
print('Chicken recipes selected:')
|
183 |
+
accessListMsg = ''
|
184 |
+
for i in accessList:
|
185 |
+
print(i)
|
186 |
+
accessListMsg += f'{i},'
|
187 |
+
|
188 |
+
print(query)
|
189 |
print(model)
|
190 |
|
191 |
# LLM
|
|
|
199 |
|
200 |
# Show response
|
201 |
stepsDict = json.loads(dumps(response["intermediate_steps"], pretty=True))
|
202 |
+
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.'
|
203 |
resp += '\n\n'
|
204 |
resp += 'Steps to solve answer using ReAct\n'
|
205 |
+
resp += 'You have access to the following recipe lists: \n' + accessListMsg
|
206 |
for i in range(len(stepsDict)):
|
207 |
resp += '##########################################\n'
|
208 |
resp += f'Step: {i+1} of {len(stepsDict)}\n'
|
|
|
215 |
resp += response['output']
|
216 |
return resp
|
217 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
218 |
# %% [markdown]
|
219 |
# # UI - Simple UI
|
220 |
|
|
|
222 |
import gradio as gr
|
223 |
|
224 |
with gr.Blocks() as demo:
|
225 |
+
|
226 |
with gr.Row():
|
227 |
with gr.Column():
|
228 |
+
headerMsg = "This demo shows the <b>Segregation of Data</b> "
|
229 |
+
headerMsg += "the intent of this demo is to show how at LLM uses Tools to fetch information from two specific recipe lists. And how if a user does "
|
230 |
+
headerMsg += "not have access to a given recipe then the LLM will not be able to pull information from it."
|
231 |
+
gr.Markdown(headerMsg)
|
232 |
+
|
233 |
+
modelDD = gr.Dropdown(['gpt-3.5-turbo', 'gpt-4'], value='gpt-3.5-turbo', label="Model to use")
|
234 |
openAIKey = gr.Textbox(placeholder="Paste your OpenAI API Key Here", label="OpenAI API Key")
|
235 |
+
inp = gr.Textbox(placeholder="Type your question here...", label="Question")
|
236 |
+
accessCG = gr.CheckboxGroup(["Chicken recipes", "Desert recipes"], label="Access List", info="The recipes below are what you have access to")
|
237 |
btn = gr.Button("Run")
|
238 |
examples = ["Can you show tell me what ingredients I need for the first baked chicken recipe?",
|
239 |
"I have 15 guests coming to visit, if I make the 'Chocolate Snack Cake' recipe will there be enough? ",
|
|
|
248 |
]
|
249 |
gr.Examples(examples, [inp])
|
250 |
|
251 |
+
recipesMsg = '<h1>Recipes</h1>'
|
252 |
+
recipesMsg += 'The lists below are the urls that the agent will reach out to and fetch, have a look at them and ask questions off them. Credit to AllRecipes.com'
|
253 |
+
|
254 |
+
recipesMsg += '<h2>Chicken Recipes</h2>'
|
255 |
+
recipesMsg += '<ol>'
|
256 |
+
for i in chickenDishList:
|
257 |
+
recipesMsg +=f"<li><a href='{i}'>{i}</a></li>"
|
258 |
+
recipesMsg += '</ol>'
|
259 |
+
|
260 |
+
recipesMsg += '<h2>Dessert Recipes</h2>'
|
261 |
+
recipesMsg += '<ol>'
|
262 |
+
for i in dessertList:
|
263 |
+
recipesMsg +=f"<li><a href='{i}'>{i}</a></li>"
|
264 |
+
recipesMsg += '</ol>'
|
265 |
+
|
266 |
+
gr.Markdown(recipesMsg)
|
267 |
+
|
268 |
with gr.Column():
|
269 |
out = gr.TextArea(label="ReAct Answer", placeholder="The answer will go here...")
|
270 |
|
271 |
+
btn.click(fn=ask_query, inputs=[modelDD, openAIKey, inp, accessCG], outputs=out)
|
272 |
|
273 |
demo.launch(show_error=True)
|
274 |
|
demo-tools-1.ipynb
CHANGED
@@ -2,7 +2,7 @@
|
|
2 |
"cells": [
|
3 |
{
|
4 |
"cell_type": "code",
|
5 |
-
"execution_count":
|
6 |
"metadata": {},
|
7 |
"outputs": [
|
8 |
{
|
@@ -40,7 +40,7 @@
|
|
40 |
},
|
41 |
{
|
42 |
"cell_type": "code",
|
43 |
-
"execution_count":
|
44 |
"metadata": {},
|
45 |
"outputs": [],
|
46 |
"source": [
|
@@ -65,16 +65,6 @@
|
|
65 |
"\n",
|
66 |
"def get_recipe_as_json(url: str) -> dict: \n",
|
67 |
" \n",
|
68 |
-
" # obj = json_safe_loads(url)\n",
|
69 |
-
" # if obj is not None: \n",
|
70 |
-
" # if 'url' in obj:\n",
|
71 |
-
" # url = obj['url']\n",
|
72 |
-
" \n",
|
73 |
-
" # url = url.replace(\"'\", \"\").replace('\"', '')\n",
|
74 |
-
" \n",
|
75 |
-
" # if not validators.url(url):\n",
|
76 |
-
" # return {'error': 'Invalid url format', 'url': url }\n",
|
77 |
-
" \n",
|
78 |
" urlArray = re.findall(r'(https?://\\S+)', url)\n",
|
79 |
" if len(urlArray) == 0: \n",
|
80 |
" return {'error': 'Invalid url format, please try again using a fully qualified URL', 'url': url }\n",
|
@@ -120,7 +110,7 @@
|
|
120 |
},
|
121 |
{
|
122 |
"cell_type": "code",
|
123 |
-
"execution_count":
|
124 |
"metadata": {},
|
125 |
"outputs": [],
|
126 |
"source": [
|
@@ -160,7 +150,7 @@
|
|
160 |
},
|
161 |
{
|
162 |
"cell_type": "code",
|
163 |
-
"execution_count":
|
164 |
"metadata": {},
|
165 |
"outputs": [],
|
166 |
"source": [
|
@@ -205,22 +195,6 @@
|
|
205 |
"#tools = [list_chicken_recipes_tool, list_dessert_recipes_tool, get_recipe_as_json_tool]"
|
206 |
]
|
207 |
},
|
208 |
-
{
|
209 |
-
"cell_type": "code",
|
210 |
-
"execution_count": 82,
|
211 |
-
"metadata": {},
|
212 |
-
"outputs": [],
|
213 |
-
"source": [
|
214 |
-
"# print('Chicken dishes:')\n",
|
215 |
-
"# for i in chickenDishList:\n",
|
216 |
-
"# print(i['title'])\n",
|
217 |
-
" \n",
|
218 |
-
"# print('')\n",
|
219 |
-
"# print('Desserts:')\n",
|
220 |
-
"# for i in dessertList:\n",
|
221 |
-
"# print(i['title'])"
|
222 |
-
]
|
223 |
-
},
|
224 |
{
|
225 |
"cell_type": "markdown",
|
226 |
"metadata": {},
|
@@ -234,43 +208,7 @@
|
|
234 |
},
|
235 |
{
|
236 |
"cell_type": "code",
|
237 |
-
"execution_count":
|
238 |
-
"metadata": {},
|
239 |
-
"outputs": [],
|
240 |
-
"source": [
|
241 |
-
"# from langchain.agents import load_tools\n",
|
242 |
-
"# from langchain.agents import initialize_agent\n",
|
243 |
-
"# from langchain.llms import OpenAI\n",
|
244 |
-
"# from langchain.chat_models import ChatOpenAI\n",
|
245 |
-
"# from langchain.callbacks import StdOutCallbackHandler\n",
|
246 |
-
"\n",
|
247 |
-
"# from langchain.agents import initialize_agent\n",
|
248 |
-
"# llm = ChatOpenAI(temperature=0, model_name='gpt-4') # 'gpt-3.5-turbo'\n",
|
249 |
-
"\n",
|
250 |
-
"# agent = initialize_agent(\n",
|
251 |
-
"# agent=\"zero-shot-react-description\", \n",
|
252 |
-
"# tools=tools, \n",
|
253 |
-
"# llm=llm,\n",
|
254 |
-
"# verbose=True,\n",
|
255 |
-
"# max_iterations=7, \n",
|
256 |
-
"# return_intermediate_steps=True\n",
|
257 |
-
"# )\n",
|
258 |
-
"# 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",
|
259 |
-
"# #query = \"Can you show tell me what ingredients I need for the first baked chicken recipe?\"\n",
|
260 |
-
"# #query = \"Can you show tell me what ingredients I need for the last baked chicken recipe? \"\n",
|
261 |
-
"# #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",
|
262 |
-
"# #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",
|
263 |
-
"# #query = \"Which is healthier the Caramel Apple Pie Cookies or the beer chicken? Please explain how you got to your answer.\"\n",
|
264 |
-
"# #query = \"Is the moon closer to earth or the sun?\"\n",
|
265 |
-
"# #query = \"How good are the apple pie cookies?\"\n",
|
266 |
-
"# query = \"What tools do I need for the Nutella Ice Cream?\"\n",
|
267 |
-
"# #query = \"My bowl is broken, can I still make Nutella Ice Cream? Answer as a yes/no.\"\n",
|
268 |
-
"# #response = agent({\"input\": f\"{system} {query}\"})\n"
|
269 |
-
]
|
270 |
-
},
|
271 |
-
{
|
272 |
-
"cell_type": "code",
|
273 |
-
"execution_count": 89,
|
274 |
"metadata": {},
|
275 |
"outputs": [],
|
276 |
"source": [
|
@@ -296,7 +234,7 @@
|
|
296 |
" if \"Desert recipes\" in accessList:\n",
|
297 |
" tools.append(list_dessert_recipes_tool)\n",
|
298 |
" \n",
|
299 |
-
" print(
|
300 |
" accessListMsg = ''\n",
|
301 |
" for i in accessList: \n",
|
302 |
" print(i)\n",
|
@@ -342,14 +280,14 @@
|
|
342 |
},
|
343 |
{
|
344 |
"cell_type": "code",
|
345 |
-
"execution_count":
|
346 |
"metadata": {},
|
347 |
"outputs": [
|
348 |
{
|
349 |
"name": "stdout",
|
350 |
"output_type": "stream",
|
351 |
"text": [
|
352 |
-
"Running on local URL: http://127.0.0.1:
|
353 |
"\n",
|
354 |
"To create a public link, set `share=True` in `launch()`.\n"
|
355 |
]
|
@@ -357,7 +295,7 @@
|
|
357 |
{
|
358 |
"data": {
|
359 |
"text/html": [
|
360 |
-
"<div><iframe src=\"http://127.0.0.1:
|
361 |
],
|
362 |
"text/plain": [
|
363 |
"<IPython.core.display.HTML object>"
|
@@ -370,122 +308,24 @@
|
|
370 |
"data": {
|
371 |
"text/plain": []
|
372 |
},
|
373 |
-
"execution_count":
|
374 |
"metadata": {},
|
375 |
"output_type": "execute_result"
|
376 |
-
},
|
377 |
-
{
|
378 |
-
"name": "stdout",
|
379 |
-
"output_type": "stream",
|
380 |
-
"text": [
|
381 |
-
"1\n",
|
382 |
-
"Desert recipes\n",
|
383 |
-
"Can you show tell me what ingredients I need for the first baked chicken recipe?\n",
|
384 |
-
"gpt-3.5-turbo\n",
|
385 |
-
"\n",
|
386 |
-
"\n",
|
387 |
-
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
388 |
-
"\u001b[32;1m\u001b[1;3mI need to find the recipe for the first baked chicken recipe and retrieve its ingredients.\n",
|
389 |
-
"Action: Get a Recipe tool\n",
|
390 |
-
"Action Input: url = \"https://www.example.com/recipes/baked-chicken\"\u001b[0m\n",
|
391 |
-
"url: https://www.example.com/recipes/baked-chicken\n",
|
392 |
-
"\n",
|
393 |
-
"Observation: \u001b[36;1m\u001b[1;3mNo recipe found.\u001b[0m\n",
|
394 |
-
"Thought:\u001b[32;1m\u001b[1;3mCould not parse LLM output: `I don't have the recipe for the first baked chicken recipe.`\u001b[0m\n",
|
395 |
-
"Observation: Check your output and make sure it conforms.\n",
|
396 |
-
"Thought:\u001b[32;1m\u001b[1;3mCould not parse LLM output: `I don't have the recipe for the first baked chicken recipe.`\u001b[0m\n",
|
397 |
-
"Observation: Check your output and make sure it conforms.\n",
|
398 |
-
"Thought:\u001b[32;1m\u001b[1;3mCould not parse LLM output: `I don't have the recipe for the first baked chicken recipe.`\u001b[0m\n",
|
399 |
-
"Observation: Check your output and make sure it conforms.\n",
|
400 |
-
"Thought:\u001b[32;1m\u001b[1;3mCould not parse LLM output: `I don't have the recipe for the first baked chicken recipe.`\u001b[0m\n",
|
401 |
-
"Observation: Check your output and make sure it conforms.\n",
|
402 |
-
"Thought:\u001b[32;1m\u001b[1;3mI now know the final answer\n",
|
403 |
-
"Final Answer: I don't have the recipe for the first baked chicken recipe.\u001b[0m\n",
|
404 |
-
"\n",
|
405 |
-
"\u001b[1m> Finished chain.\u001b[0m\n",
|
406 |
-
"1\n",
|
407 |
-
"Chicken recipes\n",
|
408 |
-
"Can you show tell me what ingredients I need for the first baked chicken recipe?\n",
|
409 |
-
"gpt-3.5-turbo\n",
|
410 |
-
"\n",
|
411 |
-
"\n",
|
412 |
-
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
413 |
-
"\u001b[32;1m\u001b[1;3mI need to find the first baked chicken recipe and retrieve its ingredients.\n",
|
414 |
-
"Action: Chicken Recipes tool\n",
|
415 |
-
"Action Input: None\u001b[0m\n",
|
416 |
-
"Observation: \u001b[33;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",
|
417 |
-
"Thought:\u001b[32;1m\u001b[1;3mI have a list of chicken recipes. I need to fetch the first one and retrieve its ingredients.\n",
|
418 |
-
"Action: Get a Recipe tool\n",
|
419 |
-
"Action Input: 'https://www.allrecipes.com/recipe/228363/crispy-roasted-chicken/'\u001b[0m\n",
|
420 |
-
"url: https://www.allrecipes.com/recipe/228363/crispy-roasted-chicken/\n",
|
421 |
-
"\n",
|
422 |
-
"Observation: \u001b[36;1m\u001b[1;3m{'@context': 'http://schema.org', '@type': ['Recipe'], 'headline': 'Crispy Roasted Chicken', 'datePublished': '2014-08-26T18:22:55.000-04:00', 'dateModified': '2023-01-30T04:44:54.000-05:00', 'author': [{'@type': 'Person', 'name': 'Doc Simonson', 'url': 'https://www.allrecipes.com/cook/docsimonson'}], 'description': 'This roasted half chicken is prepared Bavarian-style with spices and herbs, including caraway, sage, fennel, garlic, rosemary, coriander, and paprika.', 'name': 'Crispy Roasted Chicken', 'aggregateRating': {'@type': 'AggregateRating', 'ratingValue': '4.7', 'ratingCount': '134'}, 'cookTime': 'PT60M', 'nutrition': {'@type': 'NutritionInformation', 'calories': '495 kcal', 'carbohydrateContent': '3 g', 'cholesterolContent': '129 mg', 'fiberContent': '1 g', 'proteinContent': '42 g', 'saturatedFatContent': '8 g', 'sodiumContent': '446 mg', 'sugarContent': '1 g', 'fatContent': '35 g', 'unsaturatedFatContent': '0 g'}, 'prepTime': 'PT20M', 'recipeCategory': ['Dinner', 'Drink'], 'recipeCuisine': ['German'], 'recipeIngredient': ['1 teaspoon kosher salt', '0.5 teaspoon caraway seeds', '0.5 teaspoon dried sage', '0.25 teaspoon fennel seeds', '0.25 teaspoon coriander seeds', '0.25 teaspoon dried rosemary', '2 tablespoons paprika', '2 teaspoons garlic powder', '2 teaspoons all-purpose flour', '1 teaspoon onion powder', '5 tablespoons vegetable oil', '1 (4 pound) broiler-fryer chicken, cut in half lengthwise'], 'recipeInstructions': [{'@type': 'HowToStep', 'text': 'Preheat the oven to 425 degrees F (220 degrees C).'}, {'@type': 'HowToStep', 'text': 'Combine kosher salt, caraway, sage, fennel, coriander, and rosemary in a spice grinder or mortar. Grind to a coarse powder. Transfer spice mixture to a bowl; stir in paprika, garlic powder, flour, and onion powder. Mix in vegetable oil to make a smooth paste.'}, {'@type': 'HowToStep', 'text': 'Pat chicken halves dry with paper towels and tuck wing tips behind the back. Brush spice paste onto chicken halves, coating all sides and seasoning under wings and legs. Place chicken halves, skin-sides up, in a baking dish or roasting pan, leaving space around chickens so halves aren't touching.'}, {'@type': 'HowToStep', 'text': 'Roast in the preheated oven until no longer pink at the bone and the juices run clear, about 1 hour. An instant-read thermometer inserted into the thickest part of the thigh, near the bone, should read 165 degrees F (74 degrees C). Remove from the oven and let rest for 10 minutes before slicing.'}], 'recipeYield': ['6'], 'totalTime': 'PT90M', 'about': []}\u001b[0m\n",
|
423 |
-
"Thought:"
|
424 |
-
]
|
425 |
-
},
|
426 |
-
{
|
427 |
-
"name": "stderr",
|
428 |
-
"output_type": "stream",
|
429 |
-
"text": [
|
430 |
-
"Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.<locals>._completion_with_retry in 4.0 seconds as it raised APIConnectionError: Error communicating with OpenAI: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response')).\n",
|
431 |
-
"Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.<locals>._completion_with_retry in 4.0 seconds as it raised APIError: HTTP code 502 from API (<html>\n",
|
432 |
-
"<head><title>502 Bad Gateway</title></head>\n",
|
433 |
-
"<body>\n",
|
434 |
-
"<center><h1>502 Bad Gateway</h1></center>\n",
|
435 |
-
"<hr><center>cloudflare</center>\n",
|
436 |
-
"</body>\n",
|
437 |
-
"</html>\n",
|
438 |
-
").\n"
|
439 |
-
]
|
440 |
-
},
|
441 |
-
{
|
442 |
-
"name": "stdout",
|
443 |
-
"output_type": "stream",
|
444 |
-
"text": [
|
445 |
-
"\u001b[32;1m\u001b[1;3mI have retrieved the first baked chicken recipe and its ingredients. I can now provide the answer.\n",
|
446 |
-
"Final Answer: The ingredients needed for the first baked chicken recipe, \"Crispy Roasted Chicken,\" are:\n",
|
447 |
-
"- 1 teaspoon kosher salt\n",
|
448 |
-
"- 0.5 teaspoon caraway seeds\n",
|
449 |
-
"- 0.5 teaspoon dried sage\n",
|
450 |
-
"- 0.25 teaspoon fennel seeds\n",
|
451 |
-
"- 0.25 teaspoon coriander seeds\n",
|
452 |
-
"- 0.25 teaspoon dried rosemary\n",
|
453 |
-
"- 2 tablespoons paprika\n",
|
454 |
-
"- 2 teaspoons garlic powder\n",
|
455 |
-
"- 2 teaspoons all-purpose flour\n",
|
456 |
-
"- 1 teaspoon onion powder\n",
|
457 |
-
"- 5 tablespoons vegetable oil\n",
|
458 |
-
"- 1 (4 pound) broiler-fryer chicken, cut in half lengthwise\u001b[0m\n",
|
459 |
-
"\n",
|
460 |
-
"\u001b[1m> Finished chain.\u001b[0m\n",
|
461 |
-
"1\n",
|
462 |
-
"Desert recipes\n",
|
463 |
-
"How good are the apple pie cookies?\n",
|
464 |
-
"gpt-3.5-turbo\n",
|
465 |
-
"\n",
|
466 |
-
"\n",
|
467 |
-
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
468 |
-
"\u001b[32;1m\u001b[1;3mI need to find the recipe for apple pie cookies to determine how good they are.\n",
|
469 |
-
"Action: Get a Recipe tool\n",
|
470 |
-
"Action Input: url = \"https://www.example.com/apple-pie-cookies\"\u001b[0m\n",
|
471 |
-
"url: https://www.example.com/apple-pie-cookies\n",
|
472 |
-
"\n",
|
473 |
-
"Observation: \u001b[36;1m\u001b[1;3mNo recipe found.\u001b[0m\n",
|
474 |
-
"Thought:\u001b[32;1m\u001b[1;3mI don't have the recipe for apple pie cookies, so I can't determine how good they are.\n",
|
475 |
-
"Final Answer: I don't know how good the apple pie cookies are.\u001b[0m\n",
|
476 |
-
"\n",
|
477 |
-
"\u001b[1m> Finished chain.\u001b[0m\n"
|
478 |
-
]
|
479 |
}
|
480 |
],
|
481 |
"source": [
|
482 |
"import gradio as gr\n",
|
483 |
"\n",
|
484 |
"with gr.Blocks() as demo:\n",
|
485 |
-
"
|
486 |
" with gr.Row():\n",
|
487 |
" with gr.Column():\n",
|
488 |
-
"
|
|
|
|
|
|
|
|
|
|
|
489 |
" openAIKey = gr.Textbox(placeholder=\"Paste your OpenAI API Key Here\", label=\"OpenAI API Key\") \n",
|
490 |
" inp = gr.Textbox(placeholder=\"Type your question here...\", label=\"Question\") \n",
|
491 |
" accessCG = gr.CheckboxGroup([\"Chicken recipes\", \"Desert recipes\"], label=\"Access List\", info=\"The recipes below are what you have access to\")\n",
|
@@ -503,7 +343,23 @@
|
|
503 |
" ] \n",
|
504 |
" gr.Examples(examples, [inp])\n",
|
505 |
" \n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
506 |
" \n",
|
|
|
|
|
507 |
" with gr.Column():\n",
|
508 |
" out = gr.TextArea(label=\"ReAct Answer\", placeholder=\"The answer will go here...\")\n",
|
509 |
" \n",
|
|
|
2 |
"cells": [
|
3 |
{
|
4 |
"cell_type": "code",
|
5 |
+
"execution_count": 91,
|
6 |
"metadata": {},
|
7 |
"outputs": [
|
8 |
{
|
|
|
40 |
},
|
41 |
{
|
42 |
"cell_type": "code",
|
43 |
+
"execution_count": 92,
|
44 |
"metadata": {},
|
45 |
"outputs": [],
|
46 |
"source": [
|
|
|
65 |
"\n",
|
66 |
"def get_recipe_as_json(url: str) -> dict: \n",
|
67 |
" \n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
" urlArray = re.findall(r'(https?://\\S+)', url)\n",
|
69 |
" if len(urlArray) == 0: \n",
|
70 |
" return {'error': 'Invalid url format, please try again using a fully qualified URL', 'url': url }\n",
|
|
|
110 |
},
|
111 |
{
|
112 |
"cell_type": "code",
|
113 |
+
"execution_count": 93,
|
114 |
"metadata": {},
|
115 |
"outputs": [],
|
116 |
"source": [
|
|
|
150 |
},
|
151 |
{
|
152 |
"cell_type": "code",
|
153 |
+
"execution_count": 94,
|
154 |
"metadata": {},
|
155 |
"outputs": [],
|
156 |
"source": [
|
|
|
195 |
"#tools = [list_chicken_recipes_tool, list_dessert_recipes_tool, get_recipe_as_json_tool]"
|
196 |
]
|
197 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
198 |
{
|
199 |
"cell_type": "markdown",
|
200 |
"metadata": {},
|
|
|
208 |
},
|
209 |
{
|
210 |
"cell_type": "code",
|
211 |
+
"execution_count": 116,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
212 |
"metadata": {},
|
213 |
"outputs": [],
|
214 |
"source": [
|
|
|
234 |
" if \"Desert recipes\" in accessList:\n",
|
235 |
" tools.append(list_dessert_recipes_tool)\n",
|
236 |
" \n",
|
237 |
+
" print('Chicken recipes selected:')\n",
|
238 |
" accessListMsg = ''\n",
|
239 |
" for i in accessList: \n",
|
240 |
" print(i)\n",
|
|
|
280 |
},
|
281 |
{
|
282 |
"cell_type": "code",
|
283 |
+
"execution_count": 117,
|
284 |
"metadata": {},
|
285 |
"outputs": [
|
286 |
{
|
287 |
"name": "stdout",
|
288 |
"output_type": "stream",
|
289 |
"text": [
|
290 |
+
"Running on local URL: http://127.0.0.1:7904\n",
|
291 |
"\n",
|
292 |
"To create a public link, set `share=True` in `launch()`.\n"
|
293 |
]
|
|
|
295 |
{
|
296 |
"data": {
|
297 |
"text/html": [
|
298 |
+
"<div><iframe src=\"http://127.0.0.1:7904/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
299 |
],
|
300 |
"text/plain": [
|
301 |
"<IPython.core.display.HTML object>"
|
|
|
308 |
"data": {
|
309 |
"text/plain": []
|
310 |
},
|
311 |
+
"execution_count": 117,
|
312 |
"metadata": {},
|
313 |
"output_type": "execute_result"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
314 |
}
|
315 |
],
|
316 |
"source": [
|
317 |
"import gradio as gr\n",
|
318 |
"\n",
|
319 |
"with gr.Blocks() as demo:\n",
|
320 |
+
" \n",
|
321 |
" with gr.Row():\n",
|
322 |
" with gr.Column():\n",
|
323 |
+
" headerMsg = \"This demo shows the <b>Segregation of Data</b> \"\n",
|
324 |
+
" headerMsg += \"the intent of this demo is to show how at LLM uses Tools to fetch information from two specific recipe lists. And how if a user does \" \n",
|
325 |
+
" headerMsg += \"not have access to a given recipe then the LLM will not be able to pull information from it.\" \n",
|
326 |
+
" gr.Markdown(headerMsg)\n",
|
327 |
+
" \n",
|
328 |
+
" modelDD = gr.Dropdown(['gpt-3.5-turbo', 'gpt-4'], value='gpt-3.5-turbo', label=\"Model to use\")\n",
|
329 |
" openAIKey = gr.Textbox(placeholder=\"Paste your OpenAI API Key Here\", label=\"OpenAI API Key\") \n",
|
330 |
" inp = gr.Textbox(placeholder=\"Type your question here...\", label=\"Question\") \n",
|
331 |
" accessCG = gr.CheckboxGroup([\"Chicken recipes\", \"Desert recipes\"], label=\"Access List\", info=\"The recipes below are what you have access to\")\n",
|
|
|
343 |
" ] \n",
|
344 |
" gr.Examples(examples, [inp])\n",
|
345 |
" \n",
|
346 |
+
" recipesMsg = '<h1>Recipes</h1>'\n",
|
347 |
+
" recipesMsg += 'The lists below are the urls that the agent will reach out to and fetch, have a look at them and ask questions off them. Credit to AllRecipes.com'\n",
|
348 |
+
" \n",
|
349 |
+
" recipesMsg += '<h2>Chicken Recipes</h2>'\n",
|
350 |
+
" recipesMsg += '<ol>'\n",
|
351 |
+
" for i in chickenDishList:\n",
|
352 |
+
" recipesMsg +=f\"<li><a href='{i}'>{i}</a></li>\"\n",
|
353 |
+
" recipesMsg += '</ol>'\n",
|
354 |
+
" \n",
|
355 |
+
" recipesMsg += '<h2>Dessert Recipes</h2>'\n",
|
356 |
+
" recipesMsg += '<ol>'\n",
|
357 |
+
" for i in dessertList:\n",
|
358 |
+
" recipesMsg +=f\"<li><a href='{i}'>{i}</a></li>\"\n",
|
359 |
+
" recipesMsg += '</ol>'\n",
|
360 |
" \n",
|
361 |
+
" gr.Markdown(recipesMsg)\n",
|
362 |
+
" \n",
|
363 |
" with gr.Column():\n",
|
364 |
" out = gr.TextArea(label=\"ReAct Answer\", placeholder=\"The answer will go here...\")\n",
|
365 |
" \n",
|