Chat GPT integration
This commit is contained in:
parent
f5d580afe7
commit
76c126c2fd
@ -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>
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
36
backend/API/Services/OpenAiRecipes.cs
Normal file
36
backend/API/Services/OpenAiRecipes.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user