From a86db5adffae4cb8f4ef3f26d1dcf9582f37a946 Mon Sep 17 00:00:00 2001 From: Quentin Leblanc Date: Thu, 22 May 2025 17:14:12 +0200 Subject: [PATCH] first function to analyze recipe done --- .gitignore | 6 +++++- requirements.txt | 5 +++++ src/model.py | 41 +++++++++++++++++++++++++++++++++++++++++ src/utils.py | 2 ++ 4 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 requirements.txt create mode 100644 src/model.py create mode 100644 src/utils.py diff --git a/.gitignore b/.gitignore index 62c8935..e61a98b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,5 @@ -.idea/ \ No newline at end of file +.idea/ +.env +**/__pycache__/** +test.** +test_*.py \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..0299ade --- /dev/null +++ b/requirements.txt @@ -0,0 +1,5 @@ +fastapi +google +requests +dotenv +mistralai \ No newline at end of file diff --git a/src/model.py b/src/model.py new file mode 100644 index 0000000..0f217cd --- /dev/null +++ b/src/model.py @@ -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) \ No newline at end of file diff --git a/src/utils.py b/src/utils.py new file mode 100644 index 0000000..14d9ea3 --- /dev/null +++ b/src/utils.py @@ -0,0 +1,2 @@ +def create_message(role: str, content: str): + return {"role": role, "content": content} \ No newline at end of file