Skip to content

Commit

Permalink
Merge pull request #10 from Chefies/feat/bannable-recipes
Browse files Browse the repository at this point in the history
Feat/bannable recipes
  • Loading branch information
rorre authored Jun 15, 2024
2 parents 8805689 + 96e5ec0 commit c78dc9e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
22 changes: 15 additions & 7 deletions cefies/logic/recipe_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,37 @@
Generate a recipe containing name, ingredients, and steps in JSON format from subset of given ingredients.
If it is impossible to create one, tell us the reason in JSON format (see below), but you are expected
to make creative recipe with limited ingredients. Minimal recipe with small number of ingredients is okay,
just create minimal recipe in it, you don't need to create perfect recipe. I'm asking for recipe, not code
to make it happen. The steps need to be detailed. The topic and the ingredients of the recipe will be given.
Also, you need to give the recipes in these languages (the naming is strict): {{LANGS}}. You must give me the
recipe in ALL of those languages, not only some of it. You are not allowed to answer anything other than in this format:
just create minimal recipe in it, you don't need to create perfect recipe. Though, these recipes are banned: {{BANNED}}.
I'm asking for recipe, not code to make it happen. The steps need to be detailed. The topic and the ingredients of the
recipe will be given. Also, you need to give the recipes in these languages (the naming is strict): {{LANGS}}. You only
need to make one recipe, but you need to present it in ALL those languages. If there's one language that is not available,
then you are failed. The recipe in all languages must be the same but in different language. If the lang is indonesian,
then the step must be indonesian too, not english! this is the same with EVERY other languages. You are not allowed to
answer anything other than in this format:
Success Format:
{"error": false, "recipes": {"name": str, "ingredients": list[str], "steps": list[str], "lang": {{LANGS}} }}
{"error": false, "recipes": []{"name": str, "ingredients": list[str], "steps": list[str], "lang": {{LANGS}} }}
Failed Format:
{"error": true, "message": str}
Note: For steps, don't give numbering or endline
"""

def generate_recipe(ingredients: list[str], topic: str):
def generate_recipe(ingredients: list[str], topic: str, banned_recipes: list[str]):
lang_list = "| ".join([f'"{lang.value}"' for lang in LangEnum])

input_ingredients = DEFAULT_INGREDIENTS.copy()
input_ingredients.extend(ingredients)

system_prompt = RECIPE_GENERATION_SYSTEM_TEMPLATE.replace("{{LANGS}}", lang_list)
if len(banned_recipes) == 0:
system_prompt = system_prompt.replace("{{BANNED}}", "No recipe are banned")
else:
system_prompt = system_prompt.replace("{{BANNED}}", ", ".join(banned_recipes))

user_prompt = f"Topic: {topic}\n"
user_prompt += "Ingredients List: " + ", ".join(ingredients)
user_prompt += "Ingredients List: " + ", ".join(input_ingredients)

messages = [
{
Expand Down
12 changes: 10 additions & 2 deletions cefies/models/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class LangEnum(str, Enum):
class RecipeGenerationModel(BaseModel):
ingredients: List[str] = Field(..., min_length=1)
topic: Annotated[str, StringConstraints(min_length=1)] = 'cheap meals'
banned_recipes: List[str] = []

class RecipeModel(BaseModel):
name: str
Expand All @@ -23,11 +24,18 @@ class RecipeListModel(BaseModel):
@field_validator('recipes')
@classmethod
def check_langs(cls, recipes: List[RecipeModel]):
lang_set = set([recipe.lang for recipe in recipes])
required_set = set([lang.value for lang in LangEnum])
lang_list = [recipe.lang for recipe in recipes]
required_list = [lang.value for lang in LangEnum]

lang_set = set(lang_list)
required_set = set(required_list)
intersect = lang_set.intersection(required_set)

if len(intersect) != len(required_set):
raise ValueError('required language not available')
if len(lang_set) < len(lang_list):
raise ValueError('duplicate recipes in single language')

return recipes

class GenerationErrorModel(BaseModel):
Expand Down
7 changes: 4 additions & 3 deletions cefies/routes/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ def generate_recipes(
):
ingredients = request_body.ingredients
topic = request_body.topic
recipe = generate_recipe(ingredients, topic)
if recipe:
banned_recipes = request_body.banned_recipes
recipe = generate_recipe(ingredients, topic, banned_recipes)
if isinstance(recipe, RecipeListModel):
return recipe

if isinstance(recipe, GenerationErrorModel):
Expand All @@ -38,5 +39,5 @@ def generate_recipes(

return JSONResponse(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
content=MessageResponse(error=True, message=message),
content=MessageResponse(error=True, message=message).model_dump(),
)

0 comments on commit c78dc9e

Please sign in to comment.