diff --git a/sdk/cognitivelanguage/azure-ai-language-questionanswering/tests/recordings/test_query_knowledgebase.test_query_knowledgebase_filter.yaml b/sdk/cognitivelanguage/azure-ai-language-questionanswering/tests/recordings/test_query_knowledgebase.test_query_knowledgebase_filter.yaml new file mode 100644 index 000000000000..2fb633fa5635 --- /dev/null +++ b/sdk/cognitivelanguage/azure-ai-language-questionanswering/tests/recordings/test_query_knowledgebase.test_query_knowledgebase_filter.yaml @@ -0,0 +1,66 @@ +interactions: +- request: + body: '{"question": "Battery life", "top": 3, "filters": {"metadataFilter": {"metadata": + [["explicitlytaggedheading", "check the battery level"], ["explicitlytaggedheading", + "make your battery last"]]}, "logicalOperation": "OR"}}' + headers: + Accept: + - application/json + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + Content-Length: + - '222' + Content-Type: + - application/json + User-Agent: + - azsdk-python-ai-language-questionanswering/1.0.0b2 Python/3.9.5 (macOS-11.5.2-x86_64-i386-64bit) + method: POST + uri: https://test-resource.api.cognitive.microsoft.com/language/:query-knowledgebases?projectName=test-project&deploymentName=test&api-version=2021-07-15-preview + response: + body: + string: "{\n \"answers\": [\n {\n \"questions\": [\n \"Check + the battery level\"\n ],\n \"answer\": \"**Check the battery level**\\n\\nYou + can check the battery level from the lock screen or the desktop:\",\n \"confidenceScore\": + 0.6905,\n \"id\": 24,\n \"source\": \"surface-pro-4-user-guide-EN.pdf\",\n + \ \"metadata\": {\n \"explicitlytaggedheading\": \"check the battery + level\"\n },\n \"dialog\": {\n \"isContextOnly\": false,\n + \ \"prompts\": []\n }\n },\n {\n \"questions\": [\n + \ \"Make your battery last\"\n ],\n \"answer\": \"**Make your + battery last**\\n\\nFor info on how to care for your battery and power supply, + conserve power, and make your Surface battery last longer, see [Surface battery + and power](http://www.microsoft.com/surface/support/hardware-and-drivers/battery-and-power) + on Surface.com.\",\n \"confidenceScore\": 0.5818,\n \"id\": 27,\n + \ \"source\": \"surface-pro-4-user-guide-EN.pdf\",\n \"metadata\": + {\n \"explicitlytaggedheading\": \"make your battery last\"\n },\n + \ \"dialog\": {\n \"isContextOnly\": false,\n \"prompts\": + []\n }\n },\n {\n \"questions\": [\n \"Desktop taskbar.\"\n + \ ],\n \"answer\": \"**Desktop taskbar.**\\n\\nBattery status appears + at the right side of the taskbar. Select the battery icon for info about the + charging and battery status, including the percent remaining. \u272A\",\n + \ \"confidenceScore\": 0.36340000000000006,\n \"id\": 26,\n \"source\": + \"surface-pro-4-user-guide-EN.pdf\",\n \"metadata\": {},\n \"dialog\": + {\n \"isContextOnly\": false,\n \"prompts\": []\n }\n }\n + \ ]\n}" + headers: + apim-request-id: + - d3b7407a-57b7-49c1-af6e-b6fc494f4bdb + content-length: + - '1622' + content-type: + - application/json; charset=utf-8 + csp-billing-usage: + - CognitiveServices.TextAnalytics.QuestionAnsweringTextRecords=1 + date: + - Thu, 30 Sep 2021 15:37:25 GMT + strict-transport-security: + - max-age=31536000; includeSubDomains; preload + x-content-type-options: + - nosniff + x-envoy-upstream-service-time: + - '902' + status: + code: 200 + message: OK +version: 1 diff --git a/sdk/cognitivelanguage/azure-ai-language-questionanswering/tests/recordings/test_query_knowledgebase_async.test_query_knowledgebase_filter.yaml b/sdk/cognitivelanguage/azure-ai-language-questionanswering/tests/recordings/test_query_knowledgebase_async.test_query_knowledgebase_filter.yaml new file mode 100644 index 000000000000..223d22e7228f --- /dev/null +++ b/sdk/cognitivelanguage/azure-ai-language-questionanswering/tests/recordings/test_query_knowledgebase_async.test_query_knowledgebase_filter.yaml @@ -0,0 +1,55 @@ +interactions: +- request: + body: '{"question": "Battery life", "top": 3, "filters": {"metadataFilter": {"metadata": + [["explicitlytaggedheading", "check the battery level"], ["explicitlytaggedheading", + "make your battery last"]]}, "logicalOperation": "OR"}}' + headers: + Accept: + - application/json + Content-Length: + - '222' + Content-Type: + - application/json + User-Agent: + - azsdk-python-ai-language-questionanswering/1.0.0b2 Python/3.9.5 (macOS-11.5.2-x86_64-i386-64bit) + method: POST + uri: https://test-resource.api.cognitive.microsoft.com/language/:query-knowledgebases?projectName=test-project&deploymentName=test&api-version=2021-07-15-preview + response: + body: + string: "{\n \"answers\": [\n {\n \"questions\": [\n \"Check + the battery level\"\n ],\n \"answer\": \"**Check the battery level**\\n\\nYou + can check the battery level from the lock screen or the desktop:\",\n \"confidenceScore\": + 0.6905,\n \"id\": 24,\n \"source\": \"surface-pro-4-user-guide-EN.pdf\",\n + \ \"metadata\": {\n \"explicitlytaggedheading\": \"check the battery + level\"\n },\n \"dialog\": {\n \"isContextOnly\": false,\n + \ \"prompts\": []\n }\n },\n {\n \"questions\": [\n + \ \"Make your battery last\"\n ],\n \"answer\": \"**Make your + battery last**\\n\\nFor info on how to care for your battery and power supply, + conserve power, and make your Surface battery last longer, see [Surface battery + and power](http://www.microsoft.com/surface/support/hardware-and-drivers/battery-and-power) + on Surface.com.\",\n \"confidenceScore\": 0.5818,\n \"id\": 27,\n + \ \"source\": \"surface-pro-4-user-guide-EN.pdf\",\n \"metadata\": + {\n \"explicitlytaggedheading\": \"make your battery last\"\n },\n + \ \"dialog\": {\n \"isContextOnly\": false,\n \"prompts\": + []\n }\n },\n {\n \"questions\": [\n \"Desktop taskbar.\"\n + \ ],\n \"answer\": \"**Desktop taskbar.**\\n\\nBattery status appears + at the right side of the taskbar. Select the battery icon for info about the + charging and battery status, including the percent remaining. \u272A\",\n + \ \"confidenceScore\": 0.36340000000000006,\n \"id\": 26,\n \"source\": + \"surface-pro-4-user-guide-EN.pdf\",\n \"metadata\": {},\n \"dialog\": + {\n \"isContextOnly\": false,\n \"prompts\": []\n }\n }\n + \ ]\n}" + headers: + apim-request-id: c381395f-d6a2-4c9c-9cd8-f80373269209 + content-length: '1622' + content-type: application/json; charset=utf-8 + csp-billing-usage: CognitiveServices.TextAnalytics.QuestionAnsweringTextRecords=1 + date: Thu, 30 Sep 2021 15:37:26 GMT + strict-transport-security: max-age=31536000; includeSubDomains; preload + x-content-type-options: nosniff + x-envoy-upstream-service-time: '384' + status: + code: 200 + message: OK + url: https://wuppe.api.cognitive.microsoft.com/language/:query-knowledgebases?projectName=190a9e13-8ede-4e4b-a8fd-c4d7f2aeab6c&deploymentName=test&api-version=2021-07-15-preview +version: 1 diff --git a/sdk/cognitivelanguage/azure-ai-language-questionanswering/tests/test_query_knowledgebase.py b/sdk/cognitivelanguage/azure-ai-language-questionanswering/tests/test_query_knowledgebase.py index 31e0391e0fbc..afef84a377ef 100644 --- a/sdk/cognitivelanguage/azure-ai-language-questionanswering/tests/test_query_knowledgebase.py +++ b/sdk/cognitivelanguage/azure-ai-language-questionanswering/tests/test_query_knowledgebase.py @@ -19,6 +19,9 @@ QueryKnowledgeBaseOptions, KnowledgeBaseAnswerRequestContext, AnswerSpanRequest, + MetadataFilter, + LogicalOperationKind, + QueryFilters, ) @@ -358,7 +361,7 @@ def test_query_knowledgebase_overload_positional_and_kwarg(self): with pytest.raises(TypeError): client.query_knowledge_base("positional_options_bag", options="options bag by name") - def test_query_knowledge_question_or_qna_id(self): + def test_query_knowledgebase_question_or_qna_id(self): with QuestionAnsweringClient("http://fake.com", AzureKeyCredential("123")) as client: options = QueryKnowledgeBaseOptions() @@ -374,3 +377,31 @@ def test_query_knowledge_question_or_qna_id(self): project_name="hello", deployment_name='test' ) + + @GlobalQuestionAnsweringAccountPreparer() + def test_query_knowledgebase_filter(self, qna_account, qna_key, qna_project): + """Thanks to @heaths for this test!""" + filters = QueryFilters( + metadata_filter=MetadataFilter( + metadata=[ + ("explicitlytaggedheading", "check the battery level"), + ("explicitlytaggedheading", "make your battery last") + ], + ), + logical_operation=LogicalOperationKind.OR_ENUM + ) + with QuestionAnsweringClient(qna_account, AzureKeyCredential(qna_key)) as client: + response = client.query_knowledge_base( + project_name=qna_project, + deployment_name='test', + question="Battery life", + filters=filters, + top=3, + ) + assert len(response.answers) == 3 + assert any( + [a for a in response.answers if a.metadata.get('explicitlytaggedheading') == "check the battery level"] + ) + assert any( + [a for a in response.answers if a.metadata.get('explicitlytaggedheading') == "make your battery last"] + ) diff --git a/sdk/cognitivelanguage/azure-ai-language-questionanswering/tests/test_query_knowledgebase_async.py b/sdk/cognitivelanguage/azure-ai-language-questionanswering/tests/test_query_knowledgebase_async.py index e57089d3d94e..880b2b1ad392 100644 --- a/sdk/cognitivelanguage/azure-ai-language-questionanswering/tests/test_query_knowledgebase_async.py +++ b/sdk/cognitivelanguage/azure-ai-language-questionanswering/tests/test_query_knowledgebase_async.py @@ -19,6 +19,9 @@ QueryKnowledgeBaseOptions, KnowledgeBaseAnswerRequestContext, AnswerSpanRequest, + QueryFilters, + MetadataFilter, + LogicalOperationKind, ) from azure.ai.language.questionanswering.aio import QuestionAnsweringClient from azure.ai.language.questionanswering.operations._operations import build_query_knowledge_base_request, build_query_text_request @@ -356,7 +359,7 @@ async def test_query_knowledgebase_overload_positional_and_kwarg(self): with pytest.raises(TypeError): await client.query_knowledge_base("positional_options_bag", options="options bag by name") - async def test_query_knowledge_question_or_qna_id(self): + async def test_query_knowledgebase_question_or_qna_id(self): async with QuestionAnsweringClient("http://fake.com", AzureKeyCredential("123")) as client: options = QueryKnowledgeBaseOptions() @@ -372,3 +375,31 @@ async def test_query_knowledge_question_or_qna_id(self): project_name="hello", deployment_name='test' ) + + @GlobalQuestionAnsweringAccountPreparer() + async def test_query_knowledgebase_filter(self, qna_account, qna_key, qna_project): + """Thanks to @heaths for this test!""" + filters = QueryFilters( + metadata_filter=MetadataFilter( + metadata=[ + ("explicitlytaggedheading", "check the battery level"), + ("explicitlytaggedheading", "make your battery last") + ], + ), + logical_operation=LogicalOperationKind.OR_ENUM + ) + async with QuestionAnsweringClient(qna_account, AzureKeyCredential(qna_key)) as client: + response = await client.query_knowledge_base( + project_name=qna_project, + deployment_name='test', + question="Battery life", + filters=filters, + top=3, + ) + assert len(response.answers) == 3 + assert any( + [a for a in response.answers if a.metadata.get('explicitlytaggedheading') == "check the battery level"] + ) + assert any( + [a for a in response.answers if a.metadata.get('explicitlytaggedheading') == "make your battery last"] + )