diff --git a/backend/API/API.csproj b/backend/API/API.csproj
index e1ff1ea..b8e2d8a 100644
--- a/backend/API/API.csproj
+++ b/backend/API/API.csproj
@@ -19,6 +19,7 @@
runtime; build; native; contentfiles; analyzers; buildtransitive
+
diff --git a/backend/API/Controllers/RecipeController.cs b/backend/API/Controllers/RecipeController.cs
index e36aaac..0d3ae17 100644
--- a/backend/API/Controllers/RecipeController.cs
+++ b/backend/API/Controllers/RecipeController.cs
@@ -1,5 +1,6 @@
using API.BusinessLogic;
using API.Models.RecipeModels;
+using API.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
@@ -8,13 +9,15 @@ namespace API.Controllers
{
[ApiController]
[Route("api/[controller]")]
- public class RecipeController :Controller
+ public class RecipeController : Controller
{
private readonly RecipeLogic _recipeLogic;
+ private readonly OpenAiRecipes _openAiRecipes;
- public RecipeController(RecipeLogic recipeLogic)
+ public RecipeController(RecipeLogic recipeLogic, OpenAiRecipes openAiRecipes)
{
_recipeLogic = recipeLogic;
+ _openAiRecipes = openAiRecipes;
}
[Authorize]
@@ -57,5 +60,17 @@ namespace API.Controllers
{
return await _recipeLogic.DeleteRecipe(recipeId);
}
+
+ [Authorize]
+ [HttpGet("chatbot/{dish}")]
+ public async Task GenerateRecipe(string dish, string language, int numberOfRecipes = 5)
+ {
+ var recipes = await _openAiRecipes.ChatGPT(dish, language);
+ if (recipes.Content[0].Text == null || recipes.Content[0].Text == "")
+ {
+ return new ConflictObjectResult(new { message = "Could not connect to chatGPT" });
+ }
+ return new OkObjectResult(recipes.Content[0].Text);
+ }
}
}
diff --git a/backend/API/Services/OpenAiRecipes.cs b/backend/API/Services/OpenAiRecipes.cs
new file mode 100644
index 0000000..ec69082
--- /dev/null
+++ b/backend/API/Services/OpenAiRecipes.cs
@@ -0,0 +1,36 @@
+using API.DBAccess;
+using OpenAI.Chat;
+
+namespace API.Services
+{
+ public class OpenAiRecipes
+ {
+ private readonly IConfiguration _configuration;
+
+ public OpenAiRecipes(IConfiguration configuration)
+ {
+ _configuration = configuration;
+ }
+
+ public async Task ChatGPT(string message, string language, int numberOfRecipes = 5)
+ {
+ ChatClient client = new(
+ model: _configuration["OpenAI:Model"],
+ apiKey: _configuration["OpenAI:APIKey"]
+ );
+
+ 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}. " +
+ $"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),
+ ];
+
+ ChatCompletion chat = await client.CompleteChatAsync(messages);
+
+ return chat;
+ }
+ }
+}
diff --git a/backend/API/Startup.cs b/backend/API/Startup.cs
index 7b0dd60..cfb1a88 100644
--- a/backend/API/Startup.cs
+++ b/backend/API/Startup.cs
@@ -5,6 +5,7 @@ using Microsoft.OpenApi.Models;
using System.Text;
using API.DBAccess;
using API.BusinessLogic;
+using API.Services;
namespace API
{
@@ -29,6 +30,9 @@ namespace API
services.AddScoped();
services.AddScoped();
services.AddScoped();
+ services.AddScoped();
+ services.AddScoped();
+ services.AddScoped();
services.AddControllers();