Skip to content

Commit

Permalink
[QnA] Review feedback (#20133)
Browse files Browse the repository at this point in the history
* Support auto record ID

* Fix knowledge base casing

* Updated changelog

* Remove chardet
  • Loading branch information
annatisch authored Aug 12, 2021
1 parent a7898c2 commit e3c8167
Show file tree
Hide file tree
Showing 21 changed files with 422 additions and 317 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Release History

## 1.0.0b2 (unreleased)

### Breaking changes
* The method `QuestionAnsweringClient.query_knowledgebase` has been renamed to `query_knowledge_base`.

### Features Added
* The method `QuestionAnsweringClient.query_text` now supports a list of records as strings, where the ID value will be automatically populated.


## 1.0.0b1 (2021-07-27)

### Features Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ params = qna.KnowledgeBaseQueryOptions(
question="How long should my Surface battery last?"
)

output = client.query_knowledgebase(
output = client.query_knowledge_base(
params,
project_name="FAQ",
)
Expand All @@ -104,7 +104,7 @@ params = qna.models.KnowledgeBaseQueryOptions(
)
)

output = client.query_knowledgebase(
output = client.query_knowledge_base(
params,
project_name="FAQ"
)
Expand All @@ -127,7 +127,7 @@ params = qna.KnowledgeBaseQueryOptions(
question="How long should my Surface battery last?"
)

output = await client.query_knowledgebase(
output = await client.query_knowledge_base(
params,
project_name="FAQ"
)
Expand All @@ -148,7 +148,7 @@ For example, if you submit a question to a non-existant knowledge base, a `400`
from azure.core.exceptions import HttpResponseError

try:
client.query_knowledgebase(
client.query_knowledge_base(
params,
project_name="invalid-knowledge-base"
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# coding=utf-8
# --------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------

import six

from .models import TextRecord


def _validate_text_records(records):
if not records:
raise ValueError("Input records can not be empty or None")

if isinstance(records, six.string_types):
raise TypeError("Input records cannot be a string.")

if isinstance(records, dict):
raise TypeError("Input records cannot be a dict")

if not all(isinstance(x, six.string_types) for x in records):
if not all(
isinstance(x, (dict, TextRecord))
for x in records
):
raise TypeError(
"Mixing string and dictionary/object record input unsupported."
)

request_batch = []
for idx, doc in enumerate(records):
if isinstance(doc, six.string_types):
record = {"id": str(idx), "text": doc}
request_batch.append(record)
else:
request_batch.append(doc)
return request_batch
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
# --------------------------------------------------------------------------

try:
from ._request_builders_py3 import build_query_knowledgebase_request
from ._request_builders_py3 import build_query_knowledge_base_request
from ._request_builders_py3 import build_query_text_request
except (SyntaxError, ImportError):
from ._request_builders import build_query_knowledgebase_request # type: ignore
from ._request_builders import build_query_knowledge_base_request # type: ignore
from ._request_builders import build_query_text_request # type: ignore

__all__ = [
"build_query_knowledgebase_request",
"build_query_knowledge_base_request",
"build_query_text_request",
]
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

# fmt: off

def build_query_knowledgebase_request(
def build_query_knowledge_base_request(
**kwargs # type: Any
):
# type: (...) -> HttpRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
_SERIALIZER = Serializer()


def build_query_knowledgebase_request(
def build_query_knowledge_base_request(
*, project_name: str, json: Any = None, content: Any = None, deployment_name: Optional[str] = None, **kwargs: Any
) -> HttpRequest:
"""Answers the specified question using your knowledge base.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@
from azure.core.rest import HttpRequest

from ... import models as _models, _rest as rest
from ..._patch import _validate_text_records

T = TypeVar("T")
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, AsyncHttpResponse], T, Dict[str, Any]], Any]]


class QuestionAnsweringClientOperationsMixin:
@overload
async def query_knowledgebase(
async def query_knowledge_base(
self,
knowledge_base_query_options: "_models.KnowledgeBaseQueryOptions",
*,
Expand All @@ -53,7 +54,7 @@ async def query_knowledgebase(
...

@overload
async def query_knowledgebase(
async def query_knowledge_base(
self,
*,
project_name: str,
Expand Down Expand Up @@ -106,7 +107,7 @@ async def query_knowledgebase(
"""
...

async def query_knowledgebase(
async def query_knowledge_base(
self,
*args,
**kwargs: Any
Expand Down Expand Up @@ -172,12 +173,12 @@ async def query_knowledgebase(

json = self._serialize.body(knowledge_base_query_options, "KnowledgeBaseQueryOptions")

request = rest.build_query_knowledgebase_request(
request = rest.build_query_knowledge_base_request(
content_type=content_type,
project_name=project_name,
deployment_name=deployment_name,
json=json,
template_url=self.query_knowledgebase.metadata["url"],
template_url=self.query_knowledge_base.metadata["url"],
)._to_pipeline_transport_request()
path_format_arguments = {
"Endpoint": self._serialize.url("self._config.endpoint", self._config.endpoint, "str", skip_quote=True),
Expand All @@ -201,7 +202,7 @@ async def query_knowledgebase(

return deserialized

query_knowledgebase.metadata = {"url": "/:query-knowledgebases"} # type: ignore
query_knowledge_base.metadata = {"url": "/:query-knowledgebases"} # type: ignore

@overload
async def query_text(
Expand Down Expand Up @@ -286,6 +287,11 @@ async def query_text(
language=kwargs.pop("language", None),
string_index_type=kwargs.pop("string_index_type", "TextElements_v8")
)
try:
text_query_options['records'] = _validate_text_records(text_query_options['records'])
except TypeError:
text_query_options.records = _validate_text_records(text_query_options.records)

cls = kwargs.pop("cls", None) # type: ClsType["_models.TextAnswers"]
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 @@ -21,6 +21,7 @@
from azure.core.rest import HttpRequest

from .. import models as _models, _rest as rest
from .._patch import _validate_text_records

if TYPE_CHECKING:
# pylint: disable=unused-import,ungrouped-imports
Expand All @@ -32,7 +33,7 @@

class QuestionAnsweringClientOperationsMixin(object):
@overload
def query_knowledgebase(
def query_knowledge_base(
self,
knowledge_base_query_options, # type: "_models.KnowledgeBaseQueryOptions"
**kwargs # type: Any
Expand All @@ -55,7 +56,7 @@ def query_knowledgebase(
pass

@overload
def query_knowledgebase(
def query_knowledge_base(
self,
**kwargs # type: Any
):
Expand Down Expand Up @@ -96,7 +97,7 @@ def query_knowledgebase(
"""
pass

def query_knowledgebase(
def query_knowledge_base(
self,
*args, # type: "_models.KnowledgeBaseQueryOptions"
**kwargs # type: Any
Expand Down Expand Up @@ -163,12 +164,12 @@ def query_knowledgebase(

json = self._serialize.body(knowledge_base_query_options, "KnowledgeBaseQueryOptions")

request = rest.build_query_knowledgebase_request(
request = rest.build_query_knowledge_base_request(
content_type=content_type,
project_name=project_name,
deployment_name=deployment_name,
json=json,
template_url=self.query_knowledgebase.metadata["url"],
template_url=self.query_knowledge_base.metadata["url"],
)._to_pipeline_transport_request()
path_format_arguments = {
"Endpoint": self._serialize.url("self._config.endpoint", self._config.endpoint, "str", skip_quote=True),
Expand All @@ -190,7 +191,7 @@ def query_knowledgebase(

return deserialized

query_knowledgebase.metadata = {"url": "/:query-knowledgebases"} # type: ignore
query_knowledge_base.metadata = {"url": "/:query-knowledgebases"} # type: ignore

@overload
def query_text(
Expand Down Expand Up @@ -221,7 +222,7 @@ def query_text(
:keyword question: Required. User question to query against the given text records.
:paramtype question: str
:keyword records: Required. Text records to be searched for given question.
:paramtype records: list[~azure.ai.language.questionanswering.models.TextInput]
:paramtype records: list[str or ~azure.ai.language.questionanswering.models.TextRecord]
:keyword language: Language of the text records. This is BCP-47 representation of a language. For
example, use "en" for English; "es" for Spanish etc. If not set, use "en" for English as
default.
Expand Down Expand Up @@ -254,7 +255,7 @@ def query_text(
:paramtype question: str
:keyword records: Text records to be searched for given question. Provide either `text_query_options`, OR
individual keyword arguments. If both are provided, only the options object will be used.
:paramtype records: list[~azure.ai.language.questionanswering.models.TextInput]
:paramtype records: list[str or ~azure.ai.language.questionanswering.models.TextRecord]
:keyword language: Language of the text records. This is BCP-47 representation of a language. For
example, use "en" for English; "es" for Spanish etc. If not set, use "en" for English as default.
:paramtype language: str
Expand All @@ -277,6 +278,11 @@ def query_text(
language=kwargs.pop("language", None),
string_index_type=kwargs.pop("string_index_type", "TextElements_v8")
)
try:
text_query_options['records'] = _validate_text_records(text_query_options['records'])
except TypeError:
text_query_options.records = _validate_text_records(text_query_options.records)

cls = kwargs.pop("cls", None) # type: ClsType["_models.TextAnswers"]
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 @@ -31,7 +31,7 @@ async def sample_chit_chat():

endpoint = os.environ["AZURE_QUESTIONANSWERING_ENDPOINT"]
key = os.environ["AZURE_QUESTIONANSWERING_KEY"]
knowledgebase_project = os.environ["AZURE_QUESTIONANSWERING_PROJECT"]
knowledge_base_project = os.environ["AZURE_QUESTIONANSWERING_PROJECT"]

client = QuestionAnsweringClient(endpoint, AzureKeyCredential(key))
async with client:
Expand All @@ -47,9 +47,9 @@ async def sample_chit_chat():
),
)

output = await client.query_knowledgebase(
output = await client.query_knowledge_base(
first_question,
project_name=knowledgebase_project,
project_name=knowledge_base_project,
deployment_name="test"
)
best_candidate = [a for a in output.answers if a.confidence_score > 0.9][0]
Expand All @@ -72,9 +72,9 @@ async def sample_chit_chat():
include_unstructured_sources=True
)

output = await client.query_knowledgebase(
output = await client.query_knowledge_base(
followup_question,
project_name=knowledgebase_project,
project_name=knowledge_base_project,
deployment_name="test"
)
print("Q: {}".format(followup_question.question))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async def sample_query_knowledgebase():

endpoint = os.environ["AZURE_QUESTIONANSWERING_ENDPOINT"]
key = os.environ["AZURE_QUESTIONANSWERING_KEY"]
knowledgebase_project = os.environ["AZURE_QUESTIONANSWERING_PROJECT"]
knowledge_base_project = os.environ["AZURE_QUESTIONANSWERING_PROJECT"]

client = QuestionAnsweringClient(endpoint, AzureKeyCredential(key))
async with client:
Expand All @@ -47,9 +47,9 @@ async def sample_query_knowledgebase():
),
)

output = await client.query_knowledgebase(
output = await client.query_knowledge_base(
input,
project_name=knowledgebase_project,
project_name=knowledge_base_project,
deployment_name="test"
)
best_candidate = [a for a in output.answers if a.confidence_score > 0.9][0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def sample_chit_chat():

endpoint = os.environ["AZURE_QUESTIONANSWERING_ENDPOINT"]
key = os.environ["AZURE_QUESTIONANSWERING_KEY"]
knowledgebase_project = os.environ["AZURE_QUESTIONANSWERING_PROJECT"]
knowledge_base_project = os.environ["AZURE_QUESTIONANSWERING_PROJECT"]

client = QuestionAnsweringClient(endpoint, AzureKeyCredential(key))
with client:
Expand All @@ -45,9 +45,9 @@ def sample_chit_chat():
),
)

output = client.query_knowledgebase(
output = client.query_knowledge_base(
first_question,
project_name=knowledgebase_project,
project_name=knowledge_base_project,
deployment_name="test"
)
best_candidate = [a for a in output.answers if a.confidence_score > 0.9][0]
Expand All @@ -70,9 +70,9 @@ def sample_chit_chat():
include_unstructured_sources=True
)

output = client.query_knowledgebase(
output = client.query_knowledge_base(
followup_question,
project_name=knowledgebase_project,
project_name=knowledge_base_project,
deployment_name="test"
)
print("Q: {}".format(followup_question.question))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def sample_query_knowledgebase():

endpoint = os.environ["AZURE_QUESTIONANSWERING_ENDPOINT"]
key = os.environ["AZURE_QUESTIONANSWERING_KEY"]
knowledgebase_project = os.environ["AZURE_QUESTIONANSWERING_PROJECT"]
knowledge_base_project = os.environ["AZURE_QUESTIONANSWERING_PROJECT"]

client = QuestionAnsweringClient(endpoint, AzureKeyCredential(key))
with client:
Expand All @@ -45,9 +45,9 @@ def sample_query_knowledgebase():
),
)

output = client.query_knowledgebase(
output = client.query_knowledge_base(
input,
project_name=knowledgebase_project,
project_name=knowledge_base_project,
deployment_name="test"
)
best_candidate = [a for a in output.answers if a.confidence_score > 0.9][0]
Expand Down
Loading

0 comments on commit e3c8167

Please sign in to comment.