From 0c7381e81e13e14a1f4c1549a47de734da7e6a52 Mon Sep 17 00:00:00 2001 From: sw-joelmut Date: Tue, 25 Apr 2023 16:36:42 -0300 Subject: [PATCH 1/2] Add userId property to Language Service --- .../Utils/LanguageServiceUtils.cs | 3 +- .../LanguageServiceTests.cs | 48 +++++++++++++++++-- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/libraries/Microsoft.Bot.Builder.AI.QnA/Utils/LanguageServiceUtils.cs b/libraries/Microsoft.Bot.Builder.AI.QnA/Utils/LanguageServiceUtils.cs index e860f60fda..904ebb712e 100644 --- a/libraries/Microsoft.Bot.Builder.AI.QnA/Utils/LanguageServiceUtils.cs +++ b/libraries/Microsoft.Bot.Builder.AI.QnA/Utils/LanguageServiceUtils.cs @@ -197,7 +197,8 @@ private async Task QueryKbAsync(Activity messageActivity, QnAMaker qnaId = options.QnAId, rankerType = options.RankerType, answerSpanRequest = new { enable = options.EnablePreciseAnswer }, - includeUnstructuredSources = options.IncludeUnstructuredSources + includeUnstructuredSources = options.IncludeUnstructuredSources, + userId = messageActivity.From?.Id }, Formatting.None, _settings); var httpRequestHelper = new HttpRequestUtils(_httpClient); diff --git a/tests/Microsoft.Bot.Builder.AI.QnA.Tests/LanguageServiceTests.cs b/tests/Microsoft.Bot.Builder.AI.QnA.Tests/LanguageServiceTests.cs index 7d34ac5f7a..f9b32caab1 100644 --- a/tests/Microsoft.Bot.Builder.AI.QnA.Tests/LanguageServiceTests.cs +++ b/tests/Microsoft.Bot.Builder.AI.QnA.Tests/LanguageServiceTests.cs @@ -64,9 +64,9 @@ public AdaptiveDialog LanguageServiceAction_ActiveLearningDialogBase() var mockHttp = new MockHttpMessageHandler(); mockHttp.When(HttpMethod.Post, GetFeedbackUrl()) .Respond(HttpStatusCode.NoContent, "application/json", "{ }"); - mockHttp.When(HttpMethod.Post, GetRequestUrl()).WithContent("{\"question\":\"Q12\",\"top\":3,\"filters\":{\"MetadataFilter\":{\"Metadata\":[],\"LogicalOperation\":\"AND\"},\"SourceFilter\":[],\"LogicalOperation\":null},\"confidenceScoreThreshold\":0.3,\"context\":{\"previousQnAId\":0,\"previousUserQuery\":\"\"},\"qnaId\":0,\"rankerType\":\"Default\",\"answerSpanRequest\":{\"enable\":true},\"includeUnstructuredSources\":true}") + mockHttp.When(HttpMethod.Post, GetRequestUrl()).WithContent("{\"question\":\"Q12\",\"top\":3,\"filters\":{\"MetadataFilter\":{\"Metadata\":[],\"LogicalOperation\":\"AND\"},\"SourceFilter\":[],\"LogicalOperation\":null},\"confidenceScoreThreshold\":0.3,\"context\":{\"previousQnAId\":0,\"previousUserQuery\":\"\"},\"qnaId\":0,\"rankerType\":\"Default\",\"answerSpanRequest\":{\"enable\":true},\"includeUnstructuredSources\":true,\"userId\":\"user1\"}") .Respond("application/json", GetResponse("LanguageService_ReturnsAnswer_WhenNoAnswerFoundInKb.json")); - mockHttp.When(HttpMethod.Post, GetRequestUrl()).WithContent("{\"question\":\"Q11\",\"top\":3,\"filters\":{\"MetadataFilter\":{\"Metadata\":[],\"LogicalOperation\":\"AND\"},\"SourceFilter\":[],\"LogicalOperation\":null},\"confidenceScoreThreshold\":0.3,\"context\":{\"previousQnAId\":0,\"previousUserQuery\":\"\"},\"qnaId\":0,\"rankerType\":\"Default\",\"answerSpanRequest\":{\"enable\":true},\"includeUnstructuredSources\":true}") + mockHttp.When(HttpMethod.Post, GetRequestUrl()).WithContent("{\"question\":\"Q11\",\"top\":3,\"filters\":{\"MetadataFilter\":{\"Metadata\":[],\"LogicalOperation\":\"AND\"},\"SourceFilter\":[],\"LogicalOperation\":null},\"confidenceScoreThreshold\":0.3,\"context\":{\"previousQnAId\":0,\"previousUserQuery\":\"\"},\"qnaId\":0,\"rankerType\":\"Default\",\"answerSpanRequest\":{\"enable\":true},\"includeUnstructuredSources\":true,\"userId\":\"user1\"}") .Respond("application/json", GetResponse("LanguageService_TopNAnswer.json")); return CreateLanguageServiceActionDialog(mockHttp, false); } @@ -144,9 +144,9 @@ public AdaptiveDialog LanguageServiceAction_MultiTurnDialogBase() { var mockHttp = new MockHttpMessageHandler(); - mockHttp.When(HttpMethod.Post, GetRequestUrl()).WithContent("{\"question\":\"I have issues related to KB\",\"top\":3,\"filters\":{\"MetadataFilter\":{\"Metadata\":[],\"LogicalOperation\":\"AND\"},\"SourceFilter\":[],\"LogicalOperation\":null},\"confidenceScoreThreshold\":0.3,\"context\":{\"previousQnAId\":0,\"previousUserQuery\":\"\"},\"qnaId\":0,\"rankerType\":\"Default\",\"answerSpanRequest\":{\"enable\":true},\"includeUnstructuredSources\":true}") + mockHttp.When(HttpMethod.Post, GetRequestUrl()).WithContent("{\"question\":\"I have issues related to KB\",\"top\":3,\"filters\":{\"MetadataFilter\":{\"Metadata\":[],\"LogicalOperation\":\"AND\"},\"SourceFilter\":[],\"LogicalOperation\":null},\"confidenceScoreThreshold\":0.3,\"context\":{\"previousQnAId\":0,\"previousUserQuery\":\"\"},\"qnaId\":0,\"rankerType\":\"Default\",\"answerSpanRequest\":{\"enable\":true},\"includeUnstructuredSources\":true,\"userId\":\"user1\"}") .Respond("application/json", GetResponse("LanguageService_ReturnAnswer_withPrompts.json")); - mockHttp.When(HttpMethod.Post, GetRequestUrl()).WithContent("{\"question\":\"Accidently deleted KB\",\"top\":3,\"filters\":{\"MetadataFilter\":{\"Metadata\":[],\"LogicalOperation\":\"AND\"},\"SourceFilter\":[],\"LogicalOperation\":null},\"confidenceScoreThreshold\":0.3,\"context\":{\"previousQnAId\":27,\"previousUserQuery\":\"\"},\"qnaId\":1,\"rankerType\":\"Default\",\"answerSpanRequest\":{\"enable\":true},\"includeUnstructuredSources\":true}") + mockHttp.When(HttpMethod.Post, GetRequestUrl()).WithContent("{\"question\":\"Accidently deleted KB\",\"top\":3,\"filters\":{\"MetadataFilter\":{\"Metadata\":[],\"LogicalOperation\":\"AND\"},\"SourceFilter\":[],\"LogicalOperation\":null},\"confidenceScoreThreshold\":0.3,\"context\":{\"previousQnAId\":27,\"previousUserQuery\":\"\"},\"qnaId\":1,\"rankerType\":\"Default\",\"answerSpanRequest\":{\"enable\":true},\"includeUnstructuredSources\":true,\"userId\":\"user1\"}") .Respond("application/json", GetResponse("LanguageService_ReturnAnswer_MultiTurnLevel1.json")); return CreateLanguageServiceActionDialog(mockHttp, false); @@ -1245,6 +1245,46 @@ public async Task LanguageService_ReturnsAnswerWithMetadataBoost() Assert.StartsWith("Kiki", results[0].Answer); } + /// + /// The LanguageService_ReturnsAnswer_WithoutUserId. + /// + /// The . + [Fact] + [Trait("TestCategory", "AI")] + [Trait("TestCategory", "LanguageService")] + public async Task LanguageService_ReturnsAnswer_WithoutUserId() + { + var mockHttp = new MockHttpMessageHandler(); + mockHttp.When(HttpMethod.Post, GetRequestUrl()) + .WithContent("{\"question\":\"how do I clean the stove?\",\"top\":1,\"filters\":{\"MetadataFilter\":{\"Metadata\":[],\"LogicalOperation\":\"AND\"},\"SourceFilter\":[],\"LogicalOperation\":null},\"confidenceScoreThreshold\":0.3,\"context\":null,\"qnaId\":0,\"rankerType\":\"Default\",\"answerSpanRequest\":{\"enable\":true},\"includeUnstructuredSources\":true,\"userId\":null}") + .Respond("application/json", GetResponse("LanguageService_ReturnsAnswer.json")); + + var adapter = new TestAdapter(TestAdapter.CreateConversation(nameof(LanguageService_ReturnsAnswer_WithoutUserId))); + var activity = new Activity + { + Type = ActivityTypes.Message, + Text = "how do I clean the stove?", + Conversation = new ConversationAccount(), + Recipient = new ChannelAccount(), + }; + var context = new TurnContext(adapter, activity); + var client = new HttpClient(mockHttp); + + var endpoint = new QnAMakerEndpoint + { + KnowledgeBaseId = _projectName, + EndpointKey = _endpointKey, + Host = _endpoint, + QnAServiceType = ServiceType.Language + }; + + var qna = new CustomQuestionAnswering(endpoint, httpClient: client); + var results = await qna.GetAnswersAsync(context); + + Assert.NotNull(results); + Assert.Single(results); + } + /// /// The LanguageService_TestThresholdInQueryOption. /// From a415f4d7de386b4412881205df06b9f2632baddb Mon Sep 17 00:00:00 2001 From: sw-joelmut Date: Thu, 27 Apr 2023 09:28:38 -0300 Subject: [PATCH 2/2] Change unit test name --- .../LanguageServiceTests.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Microsoft.Bot.Builder.AI.QnA.Tests/LanguageServiceTests.cs b/tests/Microsoft.Bot.Builder.AI.QnA.Tests/LanguageServiceTests.cs index f9b32caab1..f4363b5663 100644 --- a/tests/Microsoft.Bot.Builder.AI.QnA.Tests/LanguageServiceTests.cs +++ b/tests/Microsoft.Bot.Builder.AI.QnA.Tests/LanguageServiceTests.cs @@ -1246,20 +1246,20 @@ public async Task LanguageService_ReturnsAnswerWithMetadataBoost() } /// - /// The LanguageService_ReturnsAnswer_WithoutUserId. + /// The LanguageService_ReturnsAnswer_WithNullUserId. /// /// The . [Fact] [Trait("TestCategory", "AI")] [Trait("TestCategory", "LanguageService")] - public async Task LanguageService_ReturnsAnswer_WithoutUserId() + public async Task LanguageService_ReturnsAnswer_WithNullUserId() { var mockHttp = new MockHttpMessageHandler(); mockHttp.When(HttpMethod.Post, GetRequestUrl()) .WithContent("{\"question\":\"how do I clean the stove?\",\"top\":1,\"filters\":{\"MetadataFilter\":{\"Metadata\":[],\"LogicalOperation\":\"AND\"},\"SourceFilter\":[],\"LogicalOperation\":null},\"confidenceScoreThreshold\":0.3,\"context\":null,\"qnaId\":0,\"rankerType\":\"Default\",\"answerSpanRequest\":{\"enable\":true},\"includeUnstructuredSources\":true,\"userId\":null}") .Respond("application/json", GetResponse("LanguageService_ReturnsAnswer.json")); - var adapter = new TestAdapter(TestAdapter.CreateConversation(nameof(LanguageService_ReturnsAnswer_WithoutUserId))); + var adapter = new TestAdapter(TestAdapter.CreateConversation(nameof(LanguageService_ReturnsAnswer_WithNullUserId))); var activity = new Activity { Type = ActivityTypes.Message,