From e13bb4bbedc9763a09b519c9f0aa0331c7a7a2d0 Mon Sep 17 00:00:00 2001 From: shchoholiev Date: Sun, 17 Dec 2023 17:10:41 +0000 Subject: [PATCH] SA-243 Save message at the end of product search - Add message saving on "[DONE]" text in SearchProductAsync() --- .../Services/OpenAiService.cs | 6 +++++- .../Services/ProductService.cs | 17 ++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/ShoppingAssistantApi.Infrastructure/Services/OpenAiService.cs b/ShoppingAssistantApi.Infrastructure/Services/OpenAiService.cs index a45d423..40b16ad 100644 --- a/ShoppingAssistantApi.Infrastructure/Services/OpenAiService.cs +++ b/ShoppingAssistantApi.Infrastructure/Services/OpenAiService.cs @@ -73,7 +73,11 @@ public class OpenAiService : IOpenAiService if (string.IsNullOrEmpty(line)) continue; var json = line?.Substring(6, line.Length - 6); - if (json == "[DONE]") yield break; + if (json == "[DONE]") + { + yield return json; + yield break; + } var data = JsonConvert.DeserializeObject(json); diff --git a/ShoppingAssistantApi.Infrastructure/Services/ProductService.cs b/ShoppingAssistantApi.Infrastructure/Services/ProductService.cs index 5b97bc0..9328992 100644 --- a/ShoppingAssistantApi.Infrastructure/Services/ProductService.cs +++ b/ShoppingAssistantApi.Infrastructure/Services/ProductService.cs @@ -95,7 +95,20 @@ public class ProductService : IProductService await foreach (var data in _openAiService.GetChatCompletionStream(chatRequest, cancellationToken)) { - if (data.Contains('[')) + if (data == "[DONE]") + { + if (!string.IsNullOrEmpty(messageBuffer.Text)) + { + _ = await _wishlistsService.AddMessageToPersonalWishlistAsync(wishlistId, new MessageDto() + { + Text = messageBuffer.Text, + Role = MessageRoles.Application.ToString(), + }, cancellationToken); + } + + yield break; + } + else if (data.Contains('[')) { dataTypeHolder = data; } @@ -110,6 +123,8 @@ public class ProductService : IProductService Text = messageBuffer.Text, Role = MessageRoles.Application.ToString(), }, cancellationToken); + + messageBuffer.Text = string.Empty; } dataTypeHolder += data;