From 02eb8e3634d968f8e7b0dbf79ce751208b048f33 Mon Sep 17 00:00:00 2001 From: stasex Date: Sat, 11 Nov 2023 00:17:17 +0200 Subject: [PATCH] added a test group for the method to work --- .../Services/ProductService.cs | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/ShoppingAssistantApi.Infrastructure/Services/ProductService.cs b/ShoppingAssistantApi.Infrastructure/Services/ProductService.cs index a6f6ce1..721b2a8 100644 --- a/ShoppingAssistantApi.Infrastructure/Services/ProductService.cs +++ b/ShoppingAssistantApi.Infrastructure/Services/ProductService.cs @@ -19,6 +19,10 @@ public class ProductService : IProductService private readonly IOpenAiService _openAiService; private readonly IMessagesRepository _messagesRepository; + + private bool mqchecker = false; + + private SearchEventType currentDataType = SearchEventType.Wishlist; public ProductService(IOpenAiService openAiService, IWishlistsService wishlistsService, IMessagesRepository messagesRepository) { @@ -79,20 +83,26 @@ public class ProductService : IProductService var suggestionBuffer = new Suggestion(); var messageBuffer = new MessagePart(); var productBuffer = new ProductName(); - var currentDataType = SearchEventType.Wishlist; var dataTypeHolder = string.Empty; + var counter = 0; await foreach (var data in _openAiService.GetChatCompletionStream(chatRequest, cancellationToken)) { - if (data.Contains("[")) + counter++; + if (mqchecker && currentDataType == SearchEventType.Message && messageBuffer != null) { - if (dataTypeHolder=="[Message]" && messageBuffer.Text!=null) + if (data == "[") { _wishlistsService.AddMessageToPersonalWishlistAsync(wishlistId, new MessageCreateDto() { Text = messageBuffer.Text, }, cancellationToken); + mqchecker = false; } + } + + if (data.Contains("[")) + { dataTypeHolder = string.Empty; dataTypeHolder += data; } @@ -101,13 +111,17 @@ public class ProductService : IProductService { dataTypeHolder += data; currentDataType = DetermineDataType(dataTypeHolder); + if (currentDataType == SearchEventType.Message) + { + mqchecker = true; + } } else if (dataTypeHolder=="[" && !data.Contains("[")) { dataTypeHolder += data; } - + else { switch (currentDataType) @@ -118,6 +132,7 @@ public class ProductService : IProductService Event = SearchEventType.Message, Data = data }; + currentDataType = SearchEventType.Message; messageBuffer.Text += data; break; @@ -163,6 +178,11 @@ public class ProductService : IProductService } } } + _wishlistsService.AddMessageToPersonalWishlistAsync(wishlistId, new MessageCreateDto() + { + Text = messageBuffer.Text, + }, cancellationToken); + mqchecker = false; } private SearchEventType DetermineDataType(string dataTypeHolder)