|
|
|
import os |
|
import json |
|
|
|
pathToSettings = '../../env/ai.json' |
|
if os.path.exists(pathToSettings): |
|
|
|
print(f'Reading settings from {pathToSettings}') |
|
f = open(pathToSettings) |
|
settingsJson = json.load(f) |
|
del f |
|
|
|
for key in settingsJson: |
|
os.environ[key] = settingsJson[key] |
|
|
|
del settingsJson |
|
else: |
|
|
|
os.environ['HUGGING_FACE_API_KEY'] = 'Get here: https://huggingface.co/settings/tokens' |
|
os.environ['OPENAI_API_KEY'] = 'Get here: https://platform.openai.com/account/api-keys' |
|
|
|
|
|
|
|
|
|
|
|
import requests |
|
from bs4 import BeautifulSoup |
|
import json |
|
import validators |
|
|
|
def remove_keys(dictionary: dict, keyList: list): |
|
for key in keyList: |
|
if key in dictionary: |
|
del dictionary[key] |
|
|
|
def get_recipe_as_json(url: str) -> dict: |
|
|
|
url = url.replace("'", "").replace('"', '') |
|
|
|
if not validators.url(url): |
|
return {'error': 'Invalid url format', 'url': url } |
|
|
|
html = requests.get(url).text |
|
soup = BeautifulSoup(html, features='html.parser') |
|
script = soup.find_all("script", {"id": "allrecipes-schema_1-0"}) |
|
|
|
recipeDict = json.loads(script[0].text)[0] |
|
remove_keys(recipeDict, ['review', 'image', 'mainEntityOfPage', 'publisher']) |
|
|
|
return recipeDict |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dessertList = [ |
|
{ |
|
"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" |
|
} |
|
] |
|
|
|
chickenDishList = [ |
|
{ |
|
"title": "Crispy Roasted Chicken", |
|
"url": "https://www.allrecipes.com/recipe/228363/crispy-roasted-chicken/" |
|
}, |
|
{ |
|
"title": "Roasted Spatchcocked Chicken With Potatoes", |
|
"url": "https://www.allrecipes.com/recipe/254877/roasted-spatchcocked-chicken-with-potatoes/" |
|
}, |
|
{ |
|
"title": "Easy Baked Chicken Thighs", |
|
"url": "https://www.allrecipes.com/recipe/235153/easy-baked-chicken-thighs/" |
|
}, |
|
{ |
|
"title": "Crispy Baked Chicken Thighs", |
|
"url": "https://www.allrecipes.com/recipe/258878/crispy-baked-chicken-thighs/" |
|
}, |
|
{ |
|
"title": "Crispy and Tender Baked Chicken Thighs", |
|
"url": "https://www.allrecipes.com/recipe/235151/crispy-and-tender-baked-chicken-thighs/" |
|
}, |
|
{ |
|
"title": "Million Dollar Chicken", |
|
"url": "https://www.allrecipes.com/recipe/233953/million-dollar-chicken/" |
|
}, |
|
{ |
|
"title": "Simple Whole Roasted Chicken", |
|
"url": "https://www.allrecipes.com/recipe/70679/simple-whole-roasted-chicken/" |
|
}, |
|
{ |
|
"title": "Beer Can Chicken", |
|
"url": "https://www.allrecipes.com/recipe/214618/beer-can-chicken/" |
|
}, |
|
{ |
|
"title": "Air Fryer Chicken Thighs", |
|
"url": "https://www.allrecipes.com/recipe/272858/air-fryer-chicken-thighs/" |
|
}, |
|
{ |
|
"title": "Happy Roast Chicken", |
|
"url": "https://www.allrecipes.com/recipe/214478/happy-roast-chicken/" |
|
} |
|
] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from langchain.agents import Tool |
|
|
|
|
|
|
|
def list_chicken_recipes(query: str): |
|
return chickenDishList |
|
|
|
list_chicken_recipes_tool = Tool(name='Chicken Recipes tool', func= list_chicken_recipes, description="This tools lists the available Chicken Recipes it returns a list of recipes with a TITLE and a URL where you can fetch the recipe.") |
|
|
|
|
|
def list_dessert_recipes(query: str): |
|
return dessertList |
|
|
|
list_dessert_recipes_tool = Tool(name='Dessert Recipes tool', func=list_dessert_recipes, |
|
description="This tools lists the available Dessert Recipes it returns a list of recipes with a TITLE and a URL where you can fetch the recipe.") |
|
|
|
|
|
def get_recipe(url: str): |
|
return get_recipe_as_json(url) |
|
|
|
get_recipe_as_json_tool = Tool(name='Get a Recipe tool', func=get_recipe, description=""" |
|
Useful for fetching a particular recipe, you can only fetch a recipe with it's url, you must get that using another tool |
|
It uses the https://schema.org/Recipe format to store it's recipes. |
|
""") |
|
|
|
|
|
tools = [list_chicken_recipes_tool, list_dessert_recipes_tool, get_recipe_as_json_tool] |
|
|
|
|
|
print('Chicken dishes:') |
|
for i in chickenDishList: |
|
print(i['title']) |
|
|
|
print('') |
|
print('Desserts:') |
|
for i in dessertList: |
|
print(i['title']) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from langchain.agents import load_tools |
|
from langchain.agents import initialize_agent |
|
from langchain.llms import OpenAI |
|
from langchain.chat_models import ChatOpenAI |
|
from langchain.callbacks import StdOutCallbackHandler |
|
from langchain.agents import initialize_agent |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from langchain.load.dump import dumps |
|
|
|
|
|
|
|
|
|
|
|
import gradio as gr |
|
|
|
def ask_query(query): |
|
|
|
|
|
llm = ChatOpenAI(temperature=0, model_name='gpt-4') |
|
agent = initialize_agent(agent="zero-shot-react-description", tools=tools, llm=llm, verbose=True, max_iterations=7, return_intermediate_steps=True) |
|
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." |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
response = agent({"input": f"{system} {query}"}) |
|
|
|
|
|
stepsDict = json.loads(dumps(response["intermediate_steps"], pretty=True)) |
|
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. ' |
|
resp += '\n\n' |
|
resp += 'Steps to solve answer using ReAct\n' |
|
for i in range(len(stepsDict)): |
|
resp += '##########################################\n' |
|
resp += f'Step: {i+1} of {len(stepsDict)}\n' |
|
resp += f"Thought: {stepsDict[i][0]['kwargs']['log']}\n" |
|
resp += 'Below is what the tool returned...\n' |
|
resp += f"Action response: {stepsDict[i][1]}\n" |
|
resp += '\n' |
|
|
|
resp += '\nFinal Thought:\n' |
|
resp += response['output'] |
|
return resp |
|
|
|
demo = gr.Interface(fn=ask_query, inputs="text", outputs="text", allow_flagging=False) |
|
|
|
|
|
demo.launch(show_error=True) |
|
|
|
|
|
|