diff --git a/ShoppingAssistantApi.Application/ShoppingAssistantApi.Application.csproj b/ShoppingAssistantApi.Application/ShoppingAssistantApi.Application.csproj index 46083d4..e7b7e9f 100644 --- a/ShoppingAssistantApi.Application/ShoppingAssistantApi.Application.csproj +++ b/ShoppingAssistantApi.Application/ShoppingAssistantApi.Application.csproj @@ -16,4 +16,5 @@ + diff --git a/ShoppingAssistantApi.Infrastructure/Services/ProductService.cs b/ShoppingAssistantApi.Infrastructure/Services/ProductService.cs index af4e45a..7753a8d 100644 --- a/ShoppingAssistantApi.Infrastructure/Services/ProductService.cs +++ b/ShoppingAssistantApi.Infrastructure/Services/ProductService.cs @@ -1,31 +1,137 @@ using System.Collections.ObjectModel; +using System.Linq.Expressions; +using Newtonsoft.Json.Linq; using ShoppingAssistantApi.Application.IRepositories; using ShoppingAssistantApi.Application.IServices; +using ShoppingAssistantApi.Application.Models.CreateDtos; +using ShoppingAssistantApi.Application.Models.OpenAi; +using ShoppingAssistantApi.Application.Models.ProductSearch; using ShoppingAssistantApi.Domain.Entities; +using ShoppingAssistantApi.Domain.Enums; namespace ShoppingAssistantApi.Infrastructure.Services; -public class ProductService +public class ProductService : IProductService { - /*private readonly IWishlistsRepository _wishlistsRepository; + private readonly IWishlistsRepository _wishlistsRepository; + + private readonly IWishlistsService _wishlistsService; private readonly IOpenAiService _openAiService; - private readonly IProductService _productService; - public ProductServices(IOpenAiService openAiService, IProductService productService) + public ProductService(IOpenAiService openAiService, IWishlistsService wishlistsService, IWishlistsRepository wishlistsRepository) { _openAiService = openAiService; - _productService = productService; - } - - public async Task> StartNewSearchAndReturnWishlist(Message message, - CancellationToken cancellationToken) - { - return null; + _wishlistsService = wishlistsService; + _wishlistsRepository = wishlistsRepository; } - public async Task> GetProductFromSearch(Message message, CancellationToken cancellationToken) + public async Task> StartNewSearchAndReturnWishlist(Message message, + CancellationToken cancellationToken) { - return null; - }*/ + List messages = new List() + { + new OpenAiMessage() + { + Role = OpenAiRole.User, + Content = PromptForProductSearch(message.Text) + } + }; + + var chatRequest = new ChatCompletionRequest + { + Messages = messages + }; + + var openAiMessage = await _openAiService.GetChatCompletion(chatRequest, cancellationToken); + + var openAiContent = JObject.Parse(openAiMessage.Content); + var productNames = openAiContent["Name"]?.ToObject>() ?? new List(); + + + WishlistCreateDto newWishlist = new WishlistCreateDto() + { + Type = "Product", + FirstMessageText = message.Text + }; + + var resultWishList = _wishlistsService.StartPersonalWishlistAsync(newWishlist, cancellationToken); + + return productNames.Select(productName => productName.Name).ToList(); + } + + public async Task> GetProductFromSearch(Message message, CancellationToken cancellationToken) + { + List messages = new List() + { + new OpenAiMessage() + { + Role = OpenAiRole.User, + Content = PromptForProductSearch(message.Text) + } + }; + + var chatRequest = new ChatCompletionRequest + { + Messages = messages + }; + + var openAiMessage = await _openAiService.GetChatCompletion(chatRequest, cancellationToken); + + var openAiContent = JObject.Parse(openAiMessage.Content); + var productNames = openAiContent["Name"]?.ToObject>() ?? new List(); + + return productNames.Select(productName => productName.Name).ToList(); + } + + public async Task> GetRecommendationsForProductFromSearch(Message message, CancellationToken cancellationToken) + { + List messages = new List() + { + new OpenAiMessage() + { + Role = OpenAiRole.User, + Content = PromptForRecommendationsForProductSearch(message.Text) + } + }; + + var chatRequest = new ChatCompletionRequest + { + Messages = messages + }; + + var openAiMessage = await _openAiService.GetChatCompletion(chatRequest, cancellationToken); + + var openAiContent = JObject.Parse(openAiMessage.Content); + var recommendations = openAiContent["Recommendation"]?.ToObject>() ?? new List(); + + return recommendations; + } + + public string PromptForProductSearch(string message) + { + string promptForSearch = "Return information in JSON. " + + "\nProvide information, only that indicated in the type of answer, namely only the name. " + + "\nAsk additional questions to the user if there is not enough information. " + + "\nIf there are several answer options, list them. " + + "\nYou don't need to display questions and products together! " + + "\nDo not output any text other than JSON!!! " + + $"\n\nQuestion: {message} " + + $"\nType of answer: Question:[] " + + $"\n\nif there are no questions, then just display the products " + + $"\nType of answer: Name:"; + return promptForSearch; + } + + public string PromptForRecommendationsForProductSearch(string message) + { + string promptForSearch = "Return information in JSON. " + + "\nProvide only information indicated in the type of answer, namely only the recommendation. " + + "\nIf there are several answer options, list them. " + + "\nDo not output any text other than JSON." + + $"\n\nGive recommendations for this question: {message} " + + "\nType of answer: " + + "\n\nRecommendation :"; + return promptForSearch; + } } \ No newline at end of file