Skip to content

Commit

Permalink
add metadata filter tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iscai-msft committed Sep 30, 2021
1 parent 255614b commit 2522ff3
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
QueryKnowledgeBaseOptions,
KnowledgeBaseAnswerRequestContext,
AnswerSpanRequest,
MetadataFilter,
LogicalOperationKind,
QueryFilters,
)


Expand Down Expand Up @@ -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()
Expand All @@ -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"]
)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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"]
)

0 comments on commit 2522ff3

Please sign in to comment.