mirror of
https://github.com/hay-kot/mealie.git
synced 2025-08-24 07:15:24 -07:00
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
This commit is contained in:
parent
8b6c75877d
commit
49e3e0bafa
8 changed files with 135 additions and 2 deletions
|
@ -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
|
||||
|
|
|
@ -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<CreateRecipe, Recipe, Recipe> {
|
|||
return await this.requests.post<ParsedIngredient[]>(routes.recipesParseIngredients, { parser, ingredients });
|
||||
}
|
||||
|
||||
async convertUnits(parser: Parser, ingredients: Array<string>, user_prompt: string) {
|
||||
parser = "openai";
|
||||
return await this.requests.post<ParsedIngredient[]>(routes.recipesConvertUnits, { parser, ingredients, user_prompt });
|
||||
}
|
||||
|
||||
async parseIngredient(parser: Parser, ingredient: string) {
|
||||
parser = parser || "nlp";
|
||||
return await this.requests.post<ParsedIngredient>(routes.recipesParseIngredient, { parser, ingredient });
|
||||
|
|
|
@ -22,9 +22,21 @@
|
|||
:items="availableParsers"
|
||||
/>
|
||||
</div>
|
||||
<div v-if="parser === 'openai'" class="d-flex align-center mb-n4">
|
||||
<div class="mb-4">USer prompt:</div>
|
||||
<v-textarea
|
||||
v-model="userPrompt"
|
||||
class="mx-2 mb-4"
|
||||
placeholder="Enter a prompt for the OpenAI parser"
|
||||
/>
|
||||
</div>
|
||||
</BaseCardSectionTitle>
|
||||
|
||||
<div class="d-flex mt-n3 mb-4 justify-end" style="gap: 5px">
|
||||
<BaseButton color="info" :disabled="parserLoading" @click="convertUnits">
|
||||
<template #icon> {{ $globals.icons.foods }}</template>
|
||||
convert units
|
||||
</BaseButton>
|
||||
<BaseButton cancel class="mr-auto" @click="$router.go(-1)"></BaseButton>
|
||||
<BaseButton color="info" :disabled="parserLoading" @click="fetchParsed">
|
||||
<template #icon> {{ $globals.icons.foods }}</template>
|
||||
|
@ -183,6 +195,8 @@ export default defineComponent({
|
|||
parserPreferences.value.parser = val;
|
||||
});
|
||||
|
||||
const userPrompt = ref<string | null>(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,
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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.
|
|
@ -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
|
||||
|
|
|
@ -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]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue