changed the logic in the service and added some changes to the tests

This commit is contained in:
stasex 2023-10-27 01:16:02 +03:00
parent 51aca2e270
commit 98377dbf9d
4 changed files with 98 additions and 127 deletions

View File

@ -55,28 +55,13 @@ public class ProductService : IProductService
}
};
if (countOfMessage==1)
{
yield return new ServerSentEvent
{
Event = SearchEventType.Message,
Data = previousMessages.Items.FirstOrDefault()?.Text
};
}
if(countOfMessage>1)
{
var messagesForOpenAI = new List<OpenAiMessage>();
messagesForOpenAI.Add(new OpenAiMessage()
{
Role = OpenAiRoleExtensions.RequestConvert(OpenAiRole.System),
Content = promptForGpt
});
foreach (var item in previousMessages.Items)
{
messagesForOpenAI.Add(
new OpenAiMessage()
messagesForOpenAI
.Add(new OpenAiMessage()
{
Role = item.Role.ToLower(),
Content = item.Text
@ -170,7 +155,6 @@ public class ProductService : IProductService
ImagesUrls = new []{"", ""},
WasOpened = false
}, cancellationToken);
break;
}
productBuffer.Name += data;
@ -179,7 +163,6 @@ public class ProductService : IProductService
}
}
}
}
private SearchEventType DetermineDataType(string dataTypeHolder)
{

View File

@ -220,13 +220,8 @@ public class DbInitializer
},
new Message
{
Text = "You are a Shopping Assistant that helps people find product recommendations. Ask user additional questions if more context needed." +
"\nYou must return data with one of the prefixes:" +
"\n[Question] - return question" +
"\n[Suggestions] - return semicolon separated suggestion how to answer to a question" +
"\n[Message] - return text" +
"\n[Products] - return semicolon separated product names",
Role = "system",
Text = "What are you looking for?",
Role = "assistant",
WishlistId = wishlistId4,
CreatedById = user2.Id,
CreatedDateUtc = DateTime.UtcNow
@ -234,15 +229,7 @@ public class DbInitializer
new Message
{
Text = "What are you looking for?",
Role = "system",
WishlistId = wishlistId4,
CreatedById = user2.Id,
CreatedDateUtc = DateTime.UtcNow
},
new Message
{
Text = "What are you looking for?",
Role = "system",
Role = "assistant",
WishlistId = wishlistId3,
CreatedById = user2.Id,
CreatedDateUtc = DateTime.UtcNow

View File

@ -57,7 +57,7 @@ public class ProductsTests : TestsBase
if (eventName == "event: Message")
{
foundMessageEvent = true;
Assert.Equal("\"What are you looking for?\"", eventData);
Assert.NotNull(eventData);
break;
}
}

View File

@ -49,7 +49,8 @@ public class ProductTests
" 3070TI", " ;", " GTX", " 4070TI", " ;", " ?"
};
var expectedMessages = new List<string> { "What are you looking for?" };
var expectedMessages = new List<string> { " What", " u", " want", " ?" };
var expectedSuggestion = new List<string> { " USB-C", " Keyboard ultra", " USB-C" };
// Mock the GetChatCompletionStream method to provide the expected SSE data
_openAiServiceMock.Setup(x => x.GetChatCompletionStream(It.IsAny<ChatCompletionRequest>(), cancellationToken))
@ -63,10 +64,10 @@ public class ProductTests
_wishListServiceMock
.Setup(m => m.GetMessagesPageFromPersonalWishlistAsync(
It.IsAny<string>(), // Очікуваний параметр wishlistId
It.IsAny<int>(), // Очікуваний параметр pageNumber
It.IsAny<int>(), // Очікуваний параметр pageSize
It.IsAny<CancellationToken>()) // Очікуваний параметр cancellationToken
It.IsAny<string>(),
It.IsAny<int>(),
It.IsAny<int>(),
It.IsAny<CancellationToken>())
)
.ReturnsAsync(new PagedList<MessageDto>(
new List<MessageDto>
@ -78,7 +79,6 @@ public class ProductTests
CreatedById = "User2",
Role = "User"
},
},
1,
1,
@ -105,6 +105,7 @@ public class ProductTests
// Check if the actual SSE events match the expected SSE events
Assert.NotNull(actualSseEvents);
Assert.Equal(expectedMessages, receivedMessages);
Assert.Equal(expectedSuggestion, receivedSuggestions);
}