This commit is contained in:
Julian van der Horst 2025-06-18 00:43:22 +03:00 committed by GitHub
commit 19415f1608
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 134 additions and 1 deletions

View file

@ -43,6 +43,7 @@ const routes = {
recipesCategory: `${prefix}/recipes/category`, recipesCategory: `${prefix}/recipes/category`,
recipesParseIngredient: `${prefix}/parser/ingredient`, recipesParseIngredient: `${prefix}/parser/ingredient`,
recipesParseIngredients: `${prefix}/parser/ingredients`, recipesParseIngredients: `${prefix}/parser/ingredients`,
recipesConvertUnits: `${prefix}/parser/convert-units`,
recipesTimelineEvent: `${prefix}/recipes/timeline/events`, recipesTimelineEvent: `${prefix}/recipes/timeline/events`,
recipesRecipeSlug: (recipe_slug: string) => `${prefix}/recipes/${recipe_slug}`, recipesRecipeSlug: (recipe_slug: string) => `${prefix}/recipes/${recipe_slug}`,
@ -175,6 +176,11 @@ export class RecipeAPI extends BaseCRUDAPI<CreateRecipe, Recipe, Recipe> {
return await this.requests.post<ParsedIngredient[]>(routes.recipesParseIngredients, { parser, ingredients }); return await this.requests.post<ParsedIngredient[]>(routes.recipesParseIngredients, { parser, ingredients });
} }
async convertUnits(parser: Parser, ingredients: Array<string>, convert_to: "metric" | "imperial") {
parser = "openai";
return await this.requests.post<ParsedIngredient[]>(routes.recipesConvertUnits, { parser, ingredients, convert_to });
}
async parseIngredient(parser: Parser, ingredient: string) { async parseIngredient(parser: Parser, ingredient: string) {
parser = parser || "nlp"; parser = parser || "nlp";
return await this.requests.post<ParsedIngredient>(routes.recipesParseIngredient, { parser, ingredient }); return await this.requests.post<ParsedIngredient>(routes.recipesParseIngredient, { parser, ingredient });

View file

@ -19,6 +19,10 @@
<div class="d-flex mt-n3 mb-4 justify-end" style="gap: 5px"> <div class="d-flex mt-n3 mb-4 justify-end" style="gap: 5px">
<BaseButton cancel class="mr-auto" @click="$router.go(-1)"></BaseButton> <BaseButton cancel class="mr-auto" @click="$router.go(-1)"></BaseButton>
<BaseButton color="info" :disabled="parserLoading" @click="convertUnits">
<template #icon> {{ $globals.icons.cog}}</template>
Parse and convert units (Metric)
</BaseButton>
<BaseButton color="info" :disabled="parserLoading" @click="fetchParsed"> <BaseButton color="info" :disabled="parserLoading" @click="fetchParsed">
<template #icon> {{ $globals.icons.foods }}</template> <template #icon> {{ $globals.icons.foods }}</template>
{{ $tc("recipe.parser.parse-all") }} {{ $tc("recipe.parser.parse-all") }}
@ -241,6 +245,38 @@ export default defineComponent({
} }
} }
async function convertUnits() {
if (!recipe.value || !recipe.value.recipeIngredient) {
return;
}
const raw = recipe.value.recipeIngredient.map((ing) => ing.note ?? "");
parserLoading.value = true;
const { data } = await api.recipes.convertUnits(parser.value, raw, "metric");
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) { function isError(ing: ParsedIngredient) {
if (!ing?.confidence?.average) { if (!ing?.confidence?.average) {
return true; return true;
@ -379,6 +415,7 @@ export default defineComponent({
panels, panels,
asPercentage, asPercentage,
fetchParsed, fetchParsed,
convertUnits,
parsedIng, parsedIng,
recipe, recipe,
loading, loading,

View file

@ -2,7 +2,7 @@ from fastapi import APIRouter
from mealie.routes._base import BaseUserController, controller from mealie.routes._base import BaseUserController, controller
from mealie.schema.recipe import ParsedIngredient from mealie.schema.recipe import ParsedIngredient
from mealie.schema.recipe.recipe_ingredient import IngredientRequest, IngredientsRequest from mealie.schema.recipe.recipe_ingredient import IngredientRequest, IngredientsConvertRequest, IngredientsRequest
from mealie.services.parser_services import get_parser from mealie.services.parser_services import get_parser
router = APIRouter(prefix="/parser") router = APIRouter(prefix="/parser")
@ -20,3 +20,8 @@ class IngredientParserController(BaseUserController):
async def parse_ingredients(self, ingredients: IngredientsRequest): async def parse_ingredients(self, ingredients: IngredientsRequest):
parser = get_parser(ingredients.parser, self.group_id, self.session) parser = get_parser(ingredients.parser, self.group_id, self.session)
return await parser.parse(ingredients.ingredients) return await parser.parse(ingredients.ingredients)
@router.post("/convert-units", response_model=list[ParsedIngredient])
async def parse_and_convert_ingredients(self, ingredients: IngredientsConvertRequest):
parser = get_parser(ingredients.parser, self.group_id, self.session)
return await parser.convert_units(ingredients.ingredients, ingredients.convert_to.value)

View file

@ -357,6 +357,17 @@ class IngredientsRequest(MealieModel):
ingredients: list[str] ingredients: list[str]
class ConvertTo(enum.Enum):
metric = "metric"
imperial = "imperial"
class IngredientsConvertRequest(MealieModel):
parser: RegisteredParser = RegisteredParser.openai
ingredients: list[str]
convert_to: ConvertTo
class IngredientRequest(MealieModel): class IngredientRequest(MealieModel):
parser: RegisteredParser = RegisteredParser.nlp parser: RegisteredParser = RegisteredParser.nlp
ingredient: str ingredient: str

View file

@ -0,0 +1,23 @@
You are a bot that parses and 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
You will receive whether you should convert from imperial to metric or the other way around. These are the rules you should follow when converting the units:
- When converting from units such as cups you can approximate the answer.
- When converting the units you should use units such as liters, ml or cl for liquid ingredients. Example: 100ml of milk, 2l of stock.
- When converting the units you should use units such as grams, kg or mg for solid ingredients. Example: 10g of salt, 300gr of mozzarella.
- When converting round up the numbers which make sense depending on the size of the ingredient. For example, if you have 1/3 cup of sugar, you should round it to 70mg of sugar.
- You should convert temperature measurements to Celsius. Example: '350°F' should be '180°C'.
- You should also convert length measurements to measurements like meter, cm and mm. Example: '1/8 inch thick slices' should be '3mm thick slices'.
- You need to convert units and measurements which are in the notes field.
- Teaspoons and pinches you can keep the same.
It is imperative that you do not create any data or otherwise make up any information, however you may approximate the conversion. 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.

View file

@ -154,6 +154,9 @@ class ABCIngredientParser(ABC):
@abstractmethod @abstractmethod
async def parse(self, ingredients: list[str]) -> list[ParsedIngredient]: ... 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: def find_ingredient_match(self, ingredient: ParsedIngredient) -> ParsedIngredient:
if ingredient.ingredient.food and (food_match := self.data_matcher.find_food_match(ingredient.ingredient.food)): if ingredient.ingredient.food and (food_match := self.data_matcher.find_food_match(ingredient.ingredient.food)):
ingredient.ingredient.food = food_match ingredient.ingredient.food = food_match

View file

@ -4,6 +4,7 @@ from collections.abc import Awaitable
from mealie.schema.openai.recipe_ingredient import OpenAIIngredient, OpenAIIngredients from mealie.schema.openai.recipe_ingredient import OpenAIIngredient, OpenAIIngredients
from mealie.schema.recipe.recipe_ingredient import ( from mealie.schema.recipe.recipe_ingredient import (
ConvertTo,
CreateIngredientFood, CreateIngredientFood,
CreateIngredientUnit, CreateIngredientUnit,
IngredientConfidence, IngredientConfidence,
@ -108,3 +109,50 @@ class OpenAIParser(ABCIngredientParser):
async def parse(self, ingredients: list[str]) -> list[ParsedIngredient]: async def parse(self, ingredients: list[str]) -> list[ParsedIngredient]:
response = await self._parse(ingredients) response = await self._parse(ingredients)
return [self._convert_ingredient(ing) for ing in response.ingredients] return [self._convert_ingredient(ing) for ing in response.ingredients]
async def convert_units(self, ingredients: list[str], convert_to: ConvertTo) -> 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="The direction to which you should convert. You should convert to:", value=convert_to
),
]
prompt = service.get_prompt("recipes.convert-recipe-units", data_injections=data_injections)
# 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]