first function to analyze recipe done
This commit is contained in:
parent
92ba75194e
commit
a86db5adff
6
.gitignore
vendored
6
.gitignore
vendored
@ -1 +1,5 @@
|
||||
.idea/
|
||||
.idea/
|
||||
.env
|
||||
**/__pycache__/**
|
||||
test.**
|
||||
test_*.py
|
||||
5
requirements.txt
Normal file
5
requirements.txt
Normal file
@ -0,0 +1,5 @@
|
||||
fastapi
|
||||
google
|
||||
requests
|
||||
dotenv
|
||||
mistralai
|
||||
41
src/model.py
Normal file
41
src/model.py
Normal 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
2
src/utils.py
Normal file
@ -0,0 +1,2 @@
|
||||
def create_message(role: str, content: str):
|
||||
return {"role": role, "content": content}
|
||||
Loading…
x
Reference in New Issue
Block a user