first function to analyze recipe done

This commit is contained in:
Quentin Leblanc 2025-05-22 17:14:12 +02:00
parent 92ba75194e
commit a86db5adff
4 changed files with 53 additions and 1 deletions

4
.gitignore vendored
View File

@ -1 +1,5 @@
.idea/
.env
**/__pycache__/**
test.**
test_*.py

5
requirements.txt Normal file
View File

@ -0,0 +1,5 @@
fastapi
google
requests
dotenv
mistralai

41
src/model.py Normal file
View File

@ -0,0 +1,41 @@
import json
from typing import Any
from mistralai import Mistral
from .utils import create_message
def recipe_analysis(recipe: str, **kwargs) -> dict[str, Any]:
"""
Analyze a recipe returning ingredients and preparation steps
Call a MistralAI model with parameters in kwargs
:param recipe:
:param kwargs:
:return:
"""
task = f"""Your task is to analyze a recipe.
For this analysis you have to extract ingredients and their quantitites.
The quantities extracted must be expressed in grams (g).
If any other unit are used, convert it to grams.
You also have to extract the preparation method.
Temperatures in the preparation method must be expressed in °Celsius (°C).
If Temperatures are in Farhenheit (°F) convert it in °Celsius (°C) and round it to the nearest integer multiple of ten.
Your answer must be formatted so that it can be stored in a json format.
The json must be containing the fields:
- "ingredients", that is an array of objects with field "name" that is a string and "amount" that is a number;
- "preparation", that is the text of the preparation method.
If the original language of the recipe is not english, translate all the ingredients name and the preparation process to english.
Here is the recipe you have to analyze
RECIPE:
{recipe}
"""
client = Mistral(api_key=kwargs["api_key"])
return json.loads(client.chat.complete(
model=kwargs["model"],
messages=[create_message("user", task)],
response_format={ "type": "json_object" }
).choices[0].message.content)

2
src/utils.py Normal file
View File

@ -0,0 +1,2 @@
def create_message(role: str, content: str):
return {"role": role, "content": content}