Spaces:
Sleeping
Sleeping
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/02_vegan_recipe_assistant.ipynb. | |
# %% auto 0 | |
__all__ = ['VEGAN_RECIPE_SEARCH_TOOL_SCHEMA', 'SYSTEM_PROMPT', 'get_vegan_recipes_edamam_api', 'vegan_recipe_edamam_search'] | |
# %% ../nbs/02_vegan_recipe_assistant.ipynb 3 | |
import os | |
from typing import Dict | |
import requests | |
from openai import OpenAI | |
import json | |
from .utils import load_json, dump_json | |
import constants | |
from tenacity import retry, wait_random_exponential, stop_after_attempt | |
import logging | |
# %% ../nbs/02_vegan_recipe_assistant.ipynb 10 | |
def get_vegan_recipes_edamam_api(params: Dict) -> requests.Response: | |
""" | |
type is required and can be "any", "public", "user" | |
""" | |
if "health" in params: | |
params["health"].append("vegan") | |
else: | |
params["health"] = ["vegan"] | |
params["app_id"] = os.environ["EDAMAM_APP_ID"] | |
params["app_key"] = os.environ["EDAMAM_APP_KEY"] | |
params["type"] = "public" | |
query = params["q"] | |
if "vegan" not in query.lower(): | |
params["q"] = "vegan " + query | |
return requests.get("https://api.edamam.com/api/recipes/v2", params=params) | |
# %% ../nbs/02_vegan_recipe_assistant.ipynb 14 | |
def vegan_recipe_edamam_search(query: str) -> str: | |
""" | |
Searches for vegan recipes based on a query. | |
If the request fails an explanation should be returned. | |
If the cause of the failure was due to no recipes found, prompt the user to try again with a provided shorter query with one word removed. | |
""" | |
max_chars = 45 # 5 chars per word * 9 max words | |
if len(query) > max_chars: | |
return json.dumps( | |
{ | |
"ok": False, | |
"msg": f"The query is too long, politetly tell the user to try again with a query that is under {max_chars} characters in length.", | |
} | |
) | |
params = { | |
"q": query, | |
"field": ["label", "url", "totalTime", "ingredientLines"], | |
} | |
response = get_vegan_recipes_edamam_api(params) | |
if not response.ok: | |
return json.dumps( | |
{ | |
"ok": False, | |
"msg": f"Received an error from Edamam API: {response.status_code} {response.text}", | |
} | |
) | |
if response.json()["count"] <= 0: | |
return json.dumps( | |
{ | |
"ok": False, | |
"msg": f"""No recipes found for query {query}. | |
This usually occurs when there are too many keywords or ingredients that are not commonly found together in recipes. | |
Politetly tell the user to try again with fewer words in the query.""", | |
} | |
) | |
return json.dumps( | |
{"ok": True, "recipes": [r["recipe"] for r in response.json()["hits"][0:3]]} | |
) | |
# %% ../nbs/02_vegan_recipe_assistant.ipynb 18 | |
VEGAN_RECIPE_SEARCH_TOOL_SCHEMA = { | |
"type": "function", | |
"function": { | |
"name": "vegan_recipe_edamam_search", | |
"description": "Searches an external API for vegan recipes based on a provided query.", | |
"parameters": { | |
"type": "object", | |
"properties": { | |
"query": { | |
"type": "string", | |
"description": "Query that includes a few vegan ingredients or recipe keywords.", | |
}, | |
}, | |
"required": ["query"], | |
}, | |
}, | |
} | |
# %% ../nbs/02_vegan_recipe_assistant.ipynb 24 | |
SYSTEM_PROMPT = """The following is a conversation between a human and a friendly vegan AI. | |
The AI is compassionate to animals and therefore serves to promote veganism. | |
Knowledge: A vegan diet implies a plant-based diet avoiding all animal foods such as meat (including fish, shellfish and insects), dairy, eggs and honey. | |
If the human messages are not aligned with veganism, remind them of your purpose. | |
The AI NEVER generates vegan recipes itself but instead uses a tool. | |
If an image image URL is included by the recipe search output, keep the full URL with all params.""" | |