From 49e3e0bafa2cf2f0babf036e68da75214d66ef6b Mon Sep 17 00:00:00 2001 From: Julian van der Horst Date: Tue, 29 Oct 2024 21:51:24 +0100 Subject: [PATCH] Parser sort of works prompt: I am Dutch so use imperial. You can approximate units like cups and depending on the ingredient use liters or grams. Obviously, ml should be used for ingredients that are liquid and grams for ingredients that are solid. Teaspoons and pinches you can keep the same. You may also convert inches to cm --- docker/Dockerfile | 2 +- frontend/lib/api/user/recipes/recipe.ts | 6 +++ .../_groupSlug/r/_slug/ingredient-parser.vue | 48 +++++++++++++++++++ mealie/routes/parser/ingredient_parser.py | 9 +++- mealie/schema/recipe/recipe_ingredient.py | 5 ++ .../prompts/recipes/convert-recipe-units.txt | 18 +++++++ mealie/services/parser_services/_base.py | 3 ++ .../services/parser_services/openai/parser.py | 46 ++++++++++++++++++ 8 files changed, 135 insertions(+), 2 deletions(-) create mode 100644 mealie/services/openai/prompts/recipes/convert-recipe-units.txt diff --git a/docker/Dockerfile b/docker/Dockerfile index 6c9c09066..7a4870f3e 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -134,7 +134,7 @@ HEALTHCHECK CMD python $MEALIE_HOME/mealie/scripts/healthcheck.py || exit 1 ENV STATIC_FILES=/spa/static COPY --from=builder /app/dist ${STATIC_FILES} -ENV HOST 0.0.0.0 +ENV HOST 127.0.0.1 EXPOSE ${APP_PORT} COPY ./docker/entry.sh $MEALIE_HOME/run.sh diff --git a/frontend/lib/api/user/recipes/recipe.ts b/frontend/lib/api/user/recipes/recipe.ts index 87288814c..0e98bf2bf 100644 --- a/frontend/lib/api/user/recipes/recipe.ts +++ b/frontend/lib/api/user/recipes/recipe.ts @@ -40,6 +40,7 @@ const routes = { recipesCategory: `${prefix}/recipes/category`, recipesParseIngredient: `${prefix}/parser/ingredient`, recipesParseIngredients: `${prefix}/parser/ingredients`, + recipesConvertUnits: `${prefix}/parser/convert-units`, recipesTimelineEvent: `${prefix}/recipes/timeline/events`, recipesRecipeSlug: (recipe_slug: string) => `${prefix}/recipes/${recipe_slug}`, @@ -165,6 +166,11 @@ export class RecipeAPI extends BaseCRUDAPI { return await this.requests.post(routes.recipesParseIngredients, { parser, ingredients }); } + async convertUnits(parser: Parser, ingredients: Array, user_prompt: string) { + parser = "openai"; + return await this.requests.post(routes.recipesConvertUnits, { parser, ingredients, user_prompt }); + } + async parseIngredient(parser: Parser, ingredient: string) { parser = parser || "nlp"; return await this.requests.post(routes.recipesParseIngredient, { parser, ingredient }); diff --git a/frontend/pages/g/_groupSlug/r/_slug/ingredient-parser.vue b/frontend/pages/g/_groupSlug/r/_slug/ingredient-parser.vue index 0c978d9f0..b2f52805d 100644 --- a/frontend/pages/g/_groupSlug/r/_slug/ingredient-parser.vue +++ b/frontend/pages/g/_groupSlug/r/_slug/ingredient-parser.vue @@ -22,9 +22,21 @@ :items="availableParsers" /> +
+
USer prompt:
+ +
+ + + convert units + @@ -183,6 +195,8 @@ export default defineComponent({ parserPreferences.value.parser = val; }); + const userPrompt = ref(null); + function processIngredientError(ing: ParsedIngredient, index: number): Error { const unitError = !checkForUnit(ing.ingredient.unit); const foodError = !checkForFood(ing.ingredient.food); @@ -248,6 +262,38 @@ export default defineComponent({ } } + async function convertUnits() { + if (!recipe.value || !recipe.value.recipeIngredient || !userPrompt.value) { + return; + } + const raw = recipe.value.recipeIngredient.map((ing) => ing.note ?? ""); + + parserLoading.value = true; + const { data } = await api.recipes.convertUnits(parser.value, raw, userPrompt.value); + parserLoading.value = false; + + if (data) { + // When we send the recipe ingredient text to be parsed, we lose the reference to the original unparsed ingredient. + // Generally this is fine, but if the unparsed ingredient had a title, we lose it; we add back the title for each ingredient here. + try { + for (let i = 0; i < recipe.value.recipeIngredient.length; i++) { + data[i].ingredient.title = recipe.value.recipeIngredient[i].title; + } + } catch (TypeError) { + console.error("Index Mismatch Error during recipe ingredient parsing; did the number of ingredients change?"); + } + + parsedIng.value = data; + + errors.value = data.map((ing, index: number) => { + return processIngredientError(ing, index); + }); + } else { + alert.error(i18n.t("events.something-went-wrong") as string); + parsedIng.value = []; + } + } + function isError(ing: ParsedIngredient) { if (!ing?.confidence?.average) { return true; @@ -373,6 +419,7 @@ export default defineComponent({ return { parser, + userPrompt, availableParsers, saveAll, createFood, @@ -386,6 +433,7 @@ export default defineComponent({ panels, asPercentage, fetchParsed, + convertUnits, parsedIng, recipe, loading, diff --git a/mealie/routes/parser/ingredient_parser.py b/mealie/routes/parser/ingredient_parser.py index a4a36d2aa..303bff1e3 100644 --- a/mealie/routes/parser/ingredient_parser.py +++ b/mealie/routes/parser/ingredient_parser.py @@ -2,7 +2,7 @@ from fastapi import APIRouter from mealie.routes._base import BaseUserController, controller from mealie.schema.recipe import ParsedIngredient -from mealie.schema.recipe.recipe_ingredient import IngredientRequest, IngredientsRequest +from mealie.schema.recipe.recipe_ingredient import IngredientRequest, IngredientsRequest, IngredientsConvertRequest from mealie.services.parser_services import get_parser router = APIRouter(prefix="/parser") @@ -20,3 +20,10 @@ class IngredientParserController(BaseUserController): async def parse_ingredients(self, ingredients: IngredientsRequest): parser = get_parser(ingredients.parser, self.group_id, self.session) return await parser.parse(ingredients.ingredients) + + @router.post("/convert-units", response_model=list[ParsedIngredient]) + async def parse_ingredients(self, ingredients: IngredientsConvertRequest): + print("df") + parser = get_parser(ingredients.parser, self.group_id, self.session) + return await parser.convert_units(ingredients.ingredients, ingredients.user_prompt) + diff --git a/mealie/schema/recipe/recipe_ingredient.py b/mealie/schema/recipe/recipe_ingredient.py index 086991bf3..51b703484 100644 --- a/mealie/schema/recipe/recipe_ingredient.py +++ b/mealie/schema/recipe/recipe_ingredient.py @@ -342,6 +342,11 @@ class IngredientsRequest(MealieModel): parser: RegisteredParser = RegisteredParser.nlp ingredients: list[str] +class IngredientsConvertRequest(MealieModel): + parser: RegisteredParser = RegisteredParser.openai + ingredients: list[str] + user_prompt: str + class IngredientRequest(MealieModel): parser: RegisteredParser = RegisteredParser.nlp diff --git a/mealie/services/openai/prompts/recipes/convert-recipe-units.txt b/mealie/services/openai/prompts/recipes/convert-recipe-units.txt new file mode 100644 index 000000000..02da0778c --- /dev/null +++ b/mealie/services/openai/prompts/recipes/convert-recipe-units.txt @@ -0,0 +1,18 @@ +You are a bot that converts units of ingredients in recipes. You will receive a list of one or more ingredients, each containing one or more of the following components: quantity, unit, food, and note. Their definitions are stated in the JSON schema below. While parsing the ingredients, there are some things to keep in mind: + - If you cannot accurately determine the quantity, unit, food, or note, you should place everything into the note field and leave everything else empty. It's better to err on the side of putting everything in the note field than being wrong + - You may receive recipe ingredients from multiple different languages. You should adhere to the grammar rules of the input language when trying to parse the ingredient string + - Sometimes foods or units will be in their singular, plural, or other grammatical forms. You must interpret all of them appropriately + - Sometimes ingredients will have text in parenthesis (like this). Parenthesis typically indicate something that should appear in the notes. For example: an input of "3 potatoes (roughly chopped)" would parse "roughly chopped" into the notes. Notice that when this occurs, the parenthesis are dropped, and you should use "roughly chopped" instead of "(roughly chopped)" in the note + - It's possible for the input to contain typos. For instance, you might see the word "potatos" instead of "potatoes". If it is a common misspelling, you may correct it + - Pay close attention to what can be considered a unit of measurement. There are common measurements such as tablespoon, teaspoon, and gram, abbreviations such as tsp, tbsp, and oz, and others such as sprig, can, bundle, bunch, unit, cube, package, and pinch + - Sometimes quantities can be given a range, such as "3-5" or "1 to 2" or "three or four". In this instance, choose the lower quantity; do not try to average or otherwise calculate the quantity. For instance, if the input it "2-3 lbs of chicken breast" the quantity should be "2" + - Any text that does not appear in the unit or food must appear in the notes. No text should be left off. The only exception for this is if a quantity is converted from text into a number. For instance, if you convert "2 dozen" into the number "24", you should not put the word "dozen" into any other field + +It is imperative that you do not create any data or otherwise make up any information, you may approximate the conversion however. Failure to adhere to this rule is illegal and will result in harsh punishment. If you are unsure, place the entire string into the note section of the response. Do not make things up. + +Below you will receive the JSON schema for your response. Your response must be in valid JSON in the below schema as provided. You must respond in this JSON schema; failure to do so is illegal. It is imperative that you follow the schema precisely to avoid punishment. You must follow the JSON schema. + +The user message that you receive will be the list of one or more recipe ingredients for you to parse. Your response should have exactly one item for each item provided. For instance, if you receive 12 items to parse, then your response should be an array of 12 parsed items. + +The user message will also include a prompt indicating their preferred units of measurement. The user message may also specify their country +or region, infer the preferred units from the country or region and add them to the preferred units list. You should convert all units to the preferred units. diff --git a/mealie/services/parser_services/_base.py b/mealie/services/parser_services/_base.py index 33e5a265c..8a91a79ed 100644 --- a/mealie/services/parser_services/_base.py +++ b/mealie/services/parser_services/_base.py @@ -154,6 +154,9 @@ class ABCIngredientParser(ABC): @abstractmethod async def parse(self, ingredients: list[str]) -> list[ParsedIngredient]: ... + @abstractmethod + async def convert_units(self, ingredients: list[str], user_prompt: str) -> list[ParsedIngredient]: ... + def find_ingredient_match(self, ingredient: ParsedIngredient) -> ParsedIngredient: if ingredient.ingredient.food and (food_match := self.data_matcher.find_food_match(ingredient.ingredient.food)): ingredient.ingredient.food = food_match diff --git a/mealie/services/parser_services/openai/parser.py b/mealie/services/parser_services/openai/parser.py index 0e01931be..cdee30489 100644 --- a/mealie/services/parser_services/openai/parser.py +++ b/mealie/services/parser_services/openai/parser.py @@ -108,3 +108,49 @@ class OpenAIParser(ABCIngredientParser): async def parse(self, ingredients: list[str]) -> list[ParsedIngredient]: response = await self._parse(ingredients) return [self._convert_ingredient(ing) for ing in response.ingredients] + + async def convert_units(self, ingredients: list[str], user_prompt: str) -> list[ParsedIngredient]: + service = OpenAIService() + data_injections = [ + OpenAIDataInjection( + description=( + "This is the JSON response schema. You must respond in valid JSON that follows this schema. " + "Your payload should be as compact as possible, eliminating unncessesary whitespace. Any fields " + "with default values which you do not populate should not be in the payload." + ), + value=OpenAIIngredients, + ), + OpenAIDataInjection(description="User Prompt containing the preferences of the user", value=user_prompt), + ] + prompt = service.get_prompt("recipes.convert-recipe-units", data_injections=data_injections) + print(prompt) + # chunk ingredients and send each chunk to its own worker + ingredient_chunks = self._chunk_messages(ingredients, n=service.workers) + tasks: list[Awaitable[str | None]] = [] + for ingredient_chunk in ingredient_chunks: + message = json.dumps(ingredient_chunk, separators=(",", ":")) + tasks.append(service.get_response(prompt, message, force_json_response=True)) + + # re-combine chunks into one response + try: + responses_json = await asyncio.gather(*tasks) + except Exception as e: + raise Exception("Failed to call OpenAI services") from e + + try: + responses = [ + OpenAIIngredients.parse_openai_response(response_json) + for response_json in responses_json + if responses_json + ] + except Exception as e: + raise Exception("Failed to parse OpenAI response") from e + + if not responses: + raise Exception("No response from OpenAI") + + res = OpenAIIngredients( + ingredients=[ingredient for response in responses for ingredient in response.ingredients] + ) + + return [self._convert_ingredient(ing) for ing in res.ingredients]