Need testing for outputs
This commit is contained in:
parent
65d7e65a40
commit
75bd6ba48a
116
src/model.py
116
src/model.py
@ -10,7 +10,7 @@ from typing import Any
|
|||||||
|
|
||||||
from mistralai import Mistral
|
from mistralai import Mistral
|
||||||
|
|
||||||
from .utils import create_message, encode_image
|
from .utils import create_message, encode_image, response2text
|
||||||
|
|
||||||
|
|
||||||
def text_recipe_analysis(recipe: str, **kwargs) -> dict[str, Any]:
|
def text_recipe_analysis(recipe: str, **kwargs) -> dict[str, Any]:
|
||||||
@ -75,43 +75,77 @@ def img_recipe_analysis(recipe: str, **kwargs) -> dict[str, Any]:
|
|||||||
|
|
||||||
return text_recipe_analysis(text, **kwargs)
|
return text_recipe_analysis(text, **kwargs)
|
||||||
|
|
||||||
# def replace_ingredients(ingredients: list[str], **kwargs) -> dict[str, Any]:
|
def find_replacement(ingredients: list[str], **kwargs) -> dict[str, Any]:
|
||||||
# """
|
"""
|
||||||
# Ask a model to find replacements for the ingredients indicated
|
Ask a model to find replacements for the ingredients indicated
|
||||||
# :param ingredients: List of ingredients to replace
|
:param ingredients: List of ingredients to replace
|
||||||
# :param kwargs:
|
:param kwargs:
|
||||||
# :return: a dict with a list of replacements and indications about these replacements
|
:return: a dict with a list of replacements and indications about these replacements
|
||||||
# """
|
"""
|
||||||
# task = """Your task is to find replacements for the ingredients that will be provided.
|
client = Mistral(api_key=kwargs["api_key"])
|
||||||
# You have to search the web to do so.
|
|
||||||
# For each ingredient, find 3 replacements.
|
agent = client.beta.agents.create(
|
||||||
# For each replacement give the name of the ingredient found and the quantity used to replace 1g of the ingredient.
|
name="GoodRecipe Agent",
|
||||||
# You also have to list the pros and the cons of using this replacement.
|
description="Agent to retrieve ingredient replacements with web search",
|
||||||
# You will return a list with each ingredient and its replacement.
|
model=kwargs["model"],
|
||||||
#
|
instructions="You have the ability to perform web searches with `web_search` to find up-to-date information.",
|
||||||
# Your answer must be formatted so that it can be stored in a json format.
|
tools=[{"type": "web_search"}],
|
||||||
# The json must be containing the fields:
|
completion_args={
|
||||||
# - 'ingredient': that is the ingredient to be replaced.
|
"temperature": 0.1,
|
||||||
#
|
"top_p": 0.95,
|
||||||
# under 'ingredient', you will have the following field:
|
}
|
||||||
# - 'replacements': that is the list of 'replacement' ingredients.
|
)
|
||||||
#
|
replacements = {}
|
||||||
# Each 'replacement' will have the fields:
|
|
||||||
# - 'name': that is the name of the particular replacement;
|
for ingredient in ingredients:
|
||||||
# - 'quantity': that is the quantity of the particular replacement;
|
task = f"""You have the ability to perform web searches with `web_search` to find up-to-date information.
|
||||||
# - 'pros': A list of different benefits for this particular replacement;
|
Your task is to find replacements for a provided ingredient.
|
||||||
# - 'cons': A list of different drawbacks for this particular replacement;
|
You have to find at most three replacement with their benefits and drawbacks.
|
||||||
#
|
Give your answer in the form:
|
||||||
# Here are the ingredients you hae to replace:
|
*[Replacement]*
|
||||||
# """
|
- **pros**
|
||||||
#
|
- list of pros ...
|
||||||
# for ingredient in ingredients:
|
- **cons**
|
||||||
# task += f"- {ingredient};\n"
|
- list of cons ...
|
||||||
#
|
|
||||||
# client = Mistral(api_key=kwargs["api_key"])
|
INGREDIENT: {ingredient}
|
||||||
# return json.loads(client.chat.complete(
|
"""
|
||||||
# model=kwargs["model"],
|
response = response2text(client.beta.conversations.start(agent_id=agent.id, inputs=[{'role': 'user', 'content': task}]))
|
||||||
# messages=[create_message("user", task)],
|
|
||||||
# tools=[{"type": "web_search"}],
|
json_task = """You have to transform a markdown text into a list of json objects.
|
||||||
# response_format={"type": "json_object"}
|
The markdown text is a list of ingredients with their pros and cons in a substitution context.
|
||||||
# ).choices[0].message.content)
|
You will list each replacement in the following manner:
|
||||||
|
- name: name of the substitute ingredient;
|
||||||
|
- pros: benefits for this substitute;
|
||||||
|
- cons: drawbacks for this substitute;
|
||||||
|
|
||||||
|
The answer should look like:
|
||||||
|
{ "substitutes":
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"name": ...
|
||||||
|
"pros": ...
|
||||||
|
"cons": ...
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": ...
|
||||||
|
"pros": ...
|
||||||
|
"cons": ...
|
||||||
|
},
|
||||||
|
...
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
TEXT:
|
||||||
|
|
||||||
|
""" + response
|
||||||
|
|
||||||
|
answer = client.chat.complete(
|
||||||
|
model=kwargs["model"],
|
||||||
|
messages=[create_message("user", json_task)],
|
||||||
|
response_format={"type": "json_object"}
|
||||||
|
).choices[0].message.content
|
||||||
|
|
||||||
|
replacements[ingredient] = json.loads(answer)['substitutes']
|
||||||
|
|
||||||
|
return replacements
|
||||||
@ -5,6 +5,8 @@
|
|||||||
"""
|
"""
|
||||||
import base64
|
import base64
|
||||||
|
|
||||||
|
from mistralai import ConversationResponse, TextChunk
|
||||||
|
|
||||||
|
|
||||||
def create_message(role: str, content: str) -> dict[str, str]:
|
def create_message(role: str, content: str) -> dict[str, str]:
|
||||||
"""
|
"""
|
||||||
@ -26,4 +28,8 @@ def encode_image(image_path):
|
|||||||
return None
|
return None
|
||||||
except Exception as e: # Added general exception handling
|
except Exception as e: # Added general exception handling
|
||||||
print(f"Error: {e}")
|
print(f"Error: {e}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def response2text(response: ConversationResponse) -> str:
|
||||||
|
text = [output.text for output in response.outputs[1].content if type(output) is TextChunk]
|
||||||
|
return "".join(text)
|
||||||
Loading…
x
Reference in New Issue
Block a user