Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# --------------------------------------------------------------------------

import six

import copy
from .models import TextRecord


Expand Down Expand Up @@ -62,3 +62,38 @@ def _verify_qna_id_and_question(query_knowledgebase_options):
)
if qna_id and question:
raise TypeError("You can not specify both `qna_id` and `question`.")

def _handle_metadata_filter_conversion(options_input):
options = copy.deepcopy(options_input)
filters = options.filters if hasattr(options, "filters") else options.get("filters", {})
try:
if (
filters and
filters.metadata_filter and
filters.metadata_filter.metadata
):
metadata_input = filters.metadata_filter.metadata
else:
metadata_input = None
in_class = True
except AttributeError:
metadata_input = filters.get("metadataFilter", {}).get("metadata")
in_class = False
if not metadata_input:
return options
try:
if any(t for t in metadata_input if len(t) != 2):
raise ValueError("'metadata' must be a sequence of key-value tuples.")
except TypeError:
raise ValueError("'metadata' must be a sequence of key-value tuples.")

metadata_modified = [
{"key": m[0], "value": m[1]}
for m in metadata_input
]
if in_class:
filters.metadata_filter.metadata = metadata_modified
else:
filters["metadataFilter"]["metadata"] = metadata_modified
return options

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

from ... import models as _models
from ...operations._operations import build_query_knowledge_base_request, build_query_text_request
from ..._patch import _validate_text_records, _get_positional_body, _verify_qna_id_and_question
from ..._patch import _validate_text_records, _get_positional_body, _verify_qna_id_and_question, _handle_metadata_filter_conversion

T = TypeVar("T")
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, AsyncHttpResponse], T, Dict[str, Any]], Any]]
Expand Down Expand Up @@ -159,6 +159,7 @@ async def query_knowledge_base(
include_unstructured_sources=kwargs.pop("include_unstructured_sources", None)
)
_verify_qna_id_and_question(options)
options = _handle_metadata_filter_conversion(options)
cls = kwargs.pop("cls", None) # type: ClsType["_models.KnowledgeBaseAnswers"]
error_map = {401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError}
error_map.update(kwargs.pop("error_map", {}))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,22 +421,22 @@ class MetadataFilter(msrest.serialization.Model):
"""Find QnAs that are associated with the given list of metadata.

:ivar metadata:
:vartype metadata: list[tuple[str]]
:vartype metadata: list[tuple[str, str]]
:ivar logical_operation: Operation used to join metadata filters. Possible values include:
"AND", "OR".
:vartype logical_operation: str or
~azure.ai.language.questionanswering.models.LogicalOperationKind
"""

_attribute_map = {
"metadata": {"key": "metadata", "type": "[[str]]"},
"metadata": {"key": "metadata", "type": "[object]"},
"logical_operation": {"key": "logicalOperation", "type": "str"},
}

def __init__(self, **kwargs):
"""
:keyword metadata:
:paramtype metadata: list[tuple[str]]
:paramtype metadata: list[tuple[str, str]]
:keyword logical_operation: Operation used to join metadata filters. Possible values include:
"AND", "OR".
:paramtype logical_operation: str or
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -483,28 +483,28 @@ class MetadataFilter(msrest.serialization.Model):
"""Find QnAs that are associated with the given list of metadata.

:ivar metadata:
:vartype metadata: list[tuple[str]]
:vartype metadata: list[tuple[str, str]]
:ivar logical_operation: Operation used to join metadata filters. Possible values include:
"AND", "OR".
:vartype logical_operation: str or
~azure.ai.language.questionanswering.models.LogicalOperationKind
"""

_attribute_map = {
"metadata": {"key": "metadata", "type": "[[str]]"},
"metadata": {"key": "metadata", "type": "[object]"},
"logical_operation": {"key": "logicalOperation", "type": "str"},
}

def __init__(
self,
*,
metadata: Optional[List[Tuple[str]]] = None,
metadata: Optional[List[Tuple[str, str]]] = None,
logical_operation: Optional[Union[str, "LogicalOperationKind"]] = None,
**kwargs
):
"""
:keyword metadata:
:paramtype metadata: list[tuple[str]]
:paramtype metadata: list[tuple[str, str]]
:keyword logical_operation: Operation used to join metadata filters. Possible values include:
"AND", "OR".
:paramtype logical_operation: str or
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from msrest import Serializer

from .. import models as _models
from .._patch import _validate_text_records, _get_positional_body, _verify_qna_id_and_question
from .._patch import _validate_text_records, _get_positional_body, _verify_qna_id_and_question, _handle_metadata_filter_conversion

if TYPE_CHECKING:
# pylint: disable=unused-import,ungrouped-imports
Expand Down Expand Up @@ -217,6 +217,7 @@ def query_knowledge_base(
include_unstructured_sources=kwargs.pop("include_unstructured_sources", None)
)
_verify_qna_id_and_question(options)
options = _handle_metadata_filter_conversion(options)
cls = kwargs.pop("cls", None) # type: ClsType["_models.KnowledgeBaseAnswers"]
error_map = {401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError}
error_map.update(kwargs.pop("error_map", {}))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ directive:
where: $["definitions"]
transform: >
delete $["MetadataFilter"]["properties"]["metadata"]["items"]["$ref"];
$["MetadataFilter"]["properties"]["metadata"]["items"]["type"] = "array";
$["MetadataFilter"]["properties"]["metadata"]["items"]["items"] = {"type": "string"};
$["MetadataFilter"]["properties"]["metadata"]["items"]["type"] = "object";
delete $["MetadataRecord"];
```
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
interactions:
- request:
body: '{"question": "Battery life", "top": 3, "filters": {"metadataFilter": {"metadata":
[["explicitlytaggedheading", "check the battery level"], ["explicitlytaggedheading",
"make your battery last"]]}, "logicalOperation": "OR"}}'
[{"key": "explicitlytaggedheading", "value": "check the battery level"}, {"key":
"explicitlytaggedheading", "value": "make your battery last"}], "logicalOperation":
"OR"}}}'
headers:
Accept:
- application/json
Expand All @@ -11,55 +12,48 @@ interactions:
Connection:
- keep-alive
Content-Length:
- '222'
- '254'
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)
- azsdk-python-ai-language-questionanswering/1.0.0b3 Python/3.9.5 (macOS-11.6-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
0.6544,\n \"id\": 133,\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\":
on Surface.com.\",\n \"confidenceScore\": 0.42950000000000005,\n \"id\":
136,\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}"
[]\n }\n }\n ]\n}"
headers:
apim-request-id:
- 7884df53-4df8-488a-b78c-692ac7f4f783
- 4fdc8aef-6dde-4298-aa8b-e9825d04a481
content-length:
- '1622'
- '1143'
content-type:
- application/json; charset=utf-8
csp-billing-usage:
- CognitiveServices.TextAnalytics.QuestionAnsweringTextRecords=1
date:
- Thu, 30 Sep 2021 15:52:33 GMT
- Wed, 27 Oct 2021 16:40:37 GMT
strict-transport-security:
- max-age=31536000; includeSubDomains; preload
x-content-type-options:
- nosniff
x-envoy-upstream-service-time:
- '700'
- '1408'
status:
code: 200
message: OK
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
interactions:
- request:
body: '{"question": "Battery life", "top": 3, "filters": {"metadataFilter": {"metadata":
[{"key": "explicitlytaggedheading", "value": "check the battery level"}, {"key":
"explicitlytaggedheading", "value": "make your battery last"}], "logicalOperation":
"or"}}}'
headers:
Accept:
- application/json
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
Content-Length:
- '254'
Content-Type:
- application/json
User-Agent:
- azsdk-python-ai-language-questionanswering/1.0.0b3 Python/3.9.5 (macOS-11.6-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.6544,\n \"id\": 133,\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.42950000000000005,\n \"id\":
136,\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}"
headers:
apim-request-id:
- 8f48f294-2767-4589-8dfd-4c93163a23be
content-length:
- '1143'
content-type:
- application/json; charset=utf-8
csp-billing-usage:
- CognitiveServices.TextAnalytics.QuestionAnsweringTextRecords=1
date:
- Wed, 27 Oct 2021 16:40:37 GMT
strict-transport-security:
- max-age=31536000; includeSubDomains; preload
x-content-type-options:
- nosniff
x-envoy-upstream-service-time:
- '196'
status:
code: 200
message: OK
version: 1
Loading