Compare commits

...

2 Commits

Author SHA1 Message Date
Jeas0001
76a0e6afee Merge branch 'master' of git.reim.ar:ReiMerc/easyeat 2025-05-01 09:06:49 +02:00
Jeas0001
76c126c2fd Chat GPT integration 2025-05-01 09:06:19 +02:00
4 changed files with 58 additions and 2 deletions

View File

@ -19,6 +19,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="MySql.EntityFrameworkCore" Version="8.0.8" />
<PackageReference Include="OpenAI" Version="2.1.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

View File

@ -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<IActionResult> 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);
}
}
}

View File

@ -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<ChatCompletion> 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<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}. " +
$"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;
}
}
}

View File

@ -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<UserLogic>();
services.AddScoped<RecipeDBAccess>();
services.AddScoped<RecipeLogic>();
services.AddScoped<ShoppingListDBAccess>();
services.AddScoped<ShoppingListLogic>();
services.AddScoped<OpenAiRecipes>();
services.AddControllers();