easyeat/backend/API/Services/OpenAiRecipes.cs
2025-05-01 09:06:19 +02:00

37 lines
1.4 KiB
C#

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;
}
}
}