added a test group for the method to work

This commit is contained in:
stasex 2023-11-11 00:17:17 +02:00
parent 8c630da4d9
commit 02eb8e3634

View File

@ -19,6 +19,10 @@ public class ProductService : IProductService
private readonly IOpenAiService _openAiService; private readonly IOpenAiService _openAiService;
private readonly IMessagesRepository _messagesRepository; private readonly IMessagesRepository _messagesRepository;
private bool mqchecker = false;
private SearchEventType currentDataType = SearchEventType.Wishlist;
public ProductService(IOpenAiService openAiService, IWishlistsService wishlistsService, IMessagesRepository messagesRepository) public ProductService(IOpenAiService openAiService, IWishlistsService wishlistsService, IMessagesRepository messagesRepository)
{ {
@ -79,20 +83,26 @@ public class ProductService : IProductService
var suggestionBuffer = new Suggestion(); var suggestionBuffer = new Suggestion();
var messageBuffer = new MessagePart(); var messageBuffer = new MessagePart();
var productBuffer = new ProductName(); var productBuffer = new ProductName();
var currentDataType = SearchEventType.Wishlist;
var dataTypeHolder = string.Empty; var dataTypeHolder = string.Empty;
var counter = 0;
await foreach (var data in _openAiService.GetChatCompletionStream(chatRequest, cancellationToken)) 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() _wishlistsService.AddMessageToPersonalWishlistAsync(wishlistId, new MessageCreateDto()
{ {
Text = messageBuffer.Text, Text = messageBuffer.Text,
}, cancellationToken); }, cancellationToken);
mqchecker = false;
} }
}
if (data.Contains("["))
{
dataTypeHolder = string.Empty; dataTypeHolder = string.Empty;
dataTypeHolder += data; dataTypeHolder += data;
} }
@ -101,13 +111,17 @@ public class ProductService : IProductService
{ {
dataTypeHolder += data; dataTypeHolder += data;
currentDataType = DetermineDataType(dataTypeHolder); currentDataType = DetermineDataType(dataTypeHolder);
if (currentDataType == SearchEventType.Message)
{
mqchecker = true;
}
} }
else if (dataTypeHolder=="[" && !data.Contains("[")) else if (dataTypeHolder=="[" && !data.Contains("["))
{ {
dataTypeHolder += data; dataTypeHolder += data;
} }
else else
{ {
switch (currentDataType) switch (currentDataType)
@ -118,6 +132,7 @@ public class ProductService : IProductService
Event = SearchEventType.Message, Event = SearchEventType.Message,
Data = data Data = data
}; };
currentDataType = SearchEventType.Message;
messageBuffer.Text += data; messageBuffer.Text += data;
break; 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) private SearchEventType DetermineDataType(string dataTypeHolder)