From da0dd828df4ecf251db9023b1be389a1480df0f4 Mon Sep 17 00:00:00 2001 From: Jeas0001 Date: Thu, 1 May 2025 12:43:37 +0200 Subject: [PATCH] Added allergi as a option for dishes --- backend/API/Controllers/RecipeController.cs | 2 +- .../Models/RecipeModels/GenerateRecipeDTO.cs | 2 ++ backend/API/Services/OpenAiRecipes.cs | 24 +++++++++++++++---- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/backend/API/Controllers/RecipeController.cs b/backend/API/Controllers/RecipeController.cs index bd65528..5fa9bdf 100644 --- a/backend/API/Controllers/RecipeController.cs +++ b/backend/API/Controllers/RecipeController.cs @@ -65,7 +65,7 @@ namespace API.Controllers [HttpPost("chatbot")] public async Task 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" }); diff --git a/backend/API/Models/RecipeModels/GenerateRecipeDTO.cs b/backend/API/Models/RecipeModels/GenerateRecipeDTO.cs index 6d869b5..7d3c0d5 100644 --- a/backend/API/Models/RecipeModels/GenerateRecipeDTO.cs +++ b/backend/API/Models/RecipeModels/GenerateRecipeDTO.cs @@ -7,5 +7,7 @@ public string Language { get; set; } public int NumberOfRecipes { get; set; } = 5; + + public List Allergi { get; set; } } } diff --git a/backend/API/Services/OpenAiRecipes.cs b/backend/API/Services/OpenAiRecipes.cs index 3c4a3cb..32d0a5a 100644 --- a/backend/API/Services/OpenAiRecipes.cs +++ b/backend/API/Services/OpenAiRecipes.cs @@ -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 ChatGPT(string message, string language, int numberOfRecipes) + public async Task 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 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);