Compare commits
2 Commits
7c45526563
...
67e6442255
Author | SHA1 | Date | |
---|---|---|---|
|
67e6442255 | ||
|
da0dd828df |
@ -65,7 +65,7 @@ namespace API.Controllers
|
||||
[HttpPost("chatbot")]
|
||||
public async Task<IActionResult> 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" });
|
||||
|
@ -7,5 +7,7 @@
|
||||
public string Language { get; set; }
|
||||
|
||||
public int NumberOfRecipes { get; set; } = 5;
|
||||
|
||||
public List<string> Allergi { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -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<ChatCompletion> ChatGPT(string message, string language, int numberOfRecipes)
|
||||
public async Task<ChatCompletion> 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<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}. " +
|
||||
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);
|
||||
|
Loading…
Reference in New Issue
Block a user