diff --git a/cefies/logic/recipe_generator.py b/cefies/logic/recipe_generator.py index cd59357..cfe299a 100644 --- a/cefies/logic/recipe_generator.py +++ b/cefies/logic/recipe_generator.py @@ -6,13 +6,16 @@ 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} @@ -20,15 +23,20 @@ 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 = [ { diff --git a/cefies/models/generate.py b/cefies/models/generate.py index 66ba421..d5267f4 100644 --- a/cefies/models/generate.py +++ b/cefies/models/generate.py @@ -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 @@ -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): diff --git a/cefies/routes/index.py b/cefies/routes/index.py index 74ddc64..da6219e 100644 --- a/cefies/routes/index.py +++ b/cefies/routes/index.py @@ -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): @@ -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(), )