Compare commits

...

2 Commits

Author SHA1 Message Date
Jeas0001
67e6442255 Merge branch 'master' of git.reim.ar:ReiMerc/easyeat 2025-05-01 12:44:07 +02:00
Jeas0001
da0dd828df Added allergi as a option for dishes 2025-05-01 12:43:37 +02:00
3 changed files with 23 additions and 5 deletions

View File

@ -65,7 +65,7 @@ namespace API.Controllers
[HttpPost("chatbot")]
public async Task<IActionResult> GenerateRecipe([FromBody] GenerateRecipeDTO recipeDTO)
{
var recipes = await _openAiRecipes.ChatGPT(recipeDTO.Dish, recipeDTO.Language, recipeDTO.NumberOfRecipes);
var recipes = await _openAiRecipes.ChatGPT(recipeDTO);
if (recipes.Content[0].Text == null || recipes.Content[0].Text == "")
{
return new ConflictObjectResult(new { message = "Could not connect to chatGPT" });

View File

@ -7,5 +7,7 @@
public string Language { get; set; }
public int NumberOfRecipes { get; set; } = 5;
public List<string> Allergi { get; set; }
}
}

View File

@ -1,4 +1,5 @@
using API.DBAccess;
using API.Models.RecipeModels;
using OpenAI.Chat;
namespace API.Services
@ -12,20 +13,35 @@ namespace API.Services
_configuration = configuration;
}
public async Task<ChatCompletion> ChatGPT(string message, string language, int numberOfRecipes)
public async Task<ChatCompletion> ChatGPT(GenerateRecipeDTO recipeDTO)
{
ChatClient client = new(
model: _configuration["OpenAI:Model"],
apiKey: _configuration["OpenAI:APIKey"]
);
string allergi = "";
if (recipeDTO.Allergi.Count != 0)
{
foreach (var item in recipeDTO.Allergi)
{
allergi += item + ", ";
}
}
else
{
allergi = "none";
}
string jsonStructure = "{ name: string, description: string, directions: [string, string, ...], ingredients: [ { amount: number?, unit: string?, name: string }]}";
List<ChatMessage> messages = [
new SystemChatMessage($"You are a a helpful assistant. You give back {numberOfRecipes} recipes on the dish that you have been given. Answer in the language {language}. " +
new SystemChatMessage($"You are a a helpful assistant. You give back {recipeDTO.NumberOfRecipes} recipes on the dish that you have been given. " +
$"Answer in the language {recipeDTO.Language}. " +
$"Answer in .json format using this structure {jsonStructure}. " +
$"For the ingredients keep the names short ie. 'grated cheese' instead of 'cheese, grated (e.g., cheddar or gouda)'"),
new UserChatMessage(message),
$"For the ingredients keep the names short ie. 'grated cheese' instead of 'cheese, grated (e.g., cheddar or gouda)'" +
$"Avoid item/dishes that the person is allergic to here is a list {allergi}"),
new UserChatMessage(recipeDTO.Dish),
];
ChatCompletion chat = await client.CompleteChatAsync(messages);