Skip to content

Commit 865065c

Browse files
authored
[QnA] Added method overloads (Azure#19876)
* Added overloads * Overload tests * Regen and updates * Removed extra docs * Fixed docstring * Bad request builders internal
1 parent 03f1449 commit 865065c

19 files changed

+766
-66
lines changed

sdk/cognitivelanguage/azure-ai-language-questionanswering/README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ params = qna.KnowledgeBaseQueryOptions(
8080
)
8181

8282
output = client.query_knowledgebase(
83+
params,
8384
project_name="FAQ",
84-
knowledge_base_query_options=params
8585
)
8686
for candidate in output.answers:
8787
print("({}) {}".format(candidate.confidence_score, candidate.answer))
@@ -105,8 +105,8 @@ params = qna.models.KnowledgeBaseQueryOptions(
105105
)
106106

107107
output = client.query_knowledgebase(
108-
project_name="FAQ",
109-
knowledge_base_query_options=params
108+
params,
109+
project_name="FAQ"
110110
)
111111
for candidate in output.answers:
112112
print("({}) {}".format(candidate.confidence_score, candidate.answer))
@@ -128,8 +128,8 @@ params = qna.KnowledgeBaseQueryOptions(
128128
)
129129

130130
output = await client.query_knowledgebase(
131-
project_name="FAQ",
132-
knowledge_base_query_options=params
131+
params,
132+
project_name="FAQ"
133133
)
134134
```
135135

@@ -149,8 +149,8 @@ from azure.core.exceptions import HttpResponseError
149149

150150
try:
151151
client.query_knowledgebase(
152-
project_name="invalid-knowledge-base",
153-
knowledge_base_query_options=params
152+
params,
153+
project_name="invalid-knowledge-base"
154154
)
155155
except HttpResponseError as error:
156156
print("Query failed: {}".format(error.message))

sdk/cognitivelanguage/azure-ai-language-questionanswering/azure/ai/language/questionanswering/rest/__init__.py renamed to sdk/cognitivelanguage/azure-ai-language-questionanswering/azure/ai/language/questionanswering/_rest/__init__.py

File renamed without changes.

sdk/cognitivelanguage/azure-ai-language-questionanswering/azure/ai/language/questionanswering/rest/_request_builders.py renamed to sdk/cognitivelanguage/azure-ai-language-questionanswering/azure/ai/language/questionanswering/_rest/_request_builders.py

File renamed without changes.

sdk/cognitivelanguage/azure-ai-language-questionanswering/azure/ai/language/questionanswering/rest/_request_builders_py3.py renamed to sdk/cognitivelanguage/azure-ai-language-questionanswering/azure/ai/language/questionanswering/_rest/_request_builders_py3.py

File renamed without changes.

sdk/cognitivelanguage/azure-ai-language-questionanswering/azure/ai/language/questionanswering/aio/_question_answering_client.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,7 @@ def __init__(self, endpoint: str, credential: AzureKeyCredential, **kwargs: Any)
4242
def send_request(self, request: HttpRequest, **kwargs: Any) -> Awaitable[AsyncHttpResponse]:
4343
"""Runs the network request through the client's chained policies.
4444
45-
We have helper methods to create requests specific to this service in `azure.ai.language.questionanswering.rest`.
46-
Use these helper methods to create the request you pass to this method. See our example below:
47-
48-
>>> from azure.ai.language.questionanswering.rest import build_query_knowledgebase_request
49-
>>> request = build_query_knowledgebase_request(project_name=project_name, json=json, content=content, deployment_name=deployment_name, **kwargs)
50-
<HttpRequest [POST], url: '/:query-knowledgebases'>
51-
>>> response = await client.send_request(request)
52-
<AsyncHttpResponse: 200 OK>
53-
5445
For more information on this code flow, see https://aka.ms/azsdk/python/protocol/quickstart
55-
5646
For advanced cases, you can also create your own :class:`~azure.core.rest.HttpRequest`
5747
and pass it in.
5848

sdk/cognitivelanguage/azure-ai-language-questionanswering/azure/ai/language/questionanswering/aio/operations/_question_answering_client_operations.py

Lines changed: 131 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
77
# --------------------------------------------------------------------------
88
import functools
9-
from typing import Any, Callable, Dict, Generic, Optional, TypeVar
9+
from typing import Any, Callable, Dict, Generic, Optional, TypeVar, overload, Union, List
1010
import warnings
1111

1212
from azure.core.exceptions import (
@@ -20,13 +20,14 @@
2020
from azure.core.pipeline.transport import AsyncHttpResponse
2121
from azure.core.rest import HttpRequest
2222

23-
from ... import models as _models, rest as rest
23+
from ... import models as _models, _rest as rest
2424

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

2828

2929
class QuestionAnsweringClientOperationsMixin:
30+
@overload
3031
async def query_knowledgebase(
3132
self,
3233
knowledge_base_query_options: "_models.KnowledgeBaseQueryOptions",
@@ -37,8 +38,6 @@ async def query_knowledgebase(
3738
) -> "_models.KnowledgeBaseAnswers":
3839
"""Answers the specified question using your knowledge base.
3940
40-
Answers the specified question using your knowledge base.
41-
4241
:param knowledge_base_query_options: Post body of the request.
4342
:type knowledge_base_query_options:
4443
~azure.ai.language.questionanswering.models.KnowledgeBaseQueryOptions
@@ -51,10 +50,88 @@ async def query_knowledgebase(
5150
:rtype: ~azure.ai.language.questionanswering.models.KnowledgeBaseAnswers
5251
:raises: ~azure.core.exceptions.HttpResponseError
5352
"""
53+
...
54+
55+
@overload
56+
async def query_knowledgebase(
57+
self,
58+
*,
59+
project_name: str,
60+
deployment_name: Optional[str] = None,
61+
qna_id: Optional[int] = None,
62+
question: Optional[str] = None,
63+
top: Optional[int] = None,
64+
user_id: Optional[str] = None,
65+
confidence_score_threshold: Optional[float] = None,
66+
context: Optional["_models.KnowledgeBaseAnswerRequestContext"] = None,
67+
ranker_type: Optional[Union[str, "_models.RankerType"]] = None,
68+
strict_filters: Optional["_models.StrictFilters"] = None,
69+
answer_span_request: Optional["_models.AnswerSpanRequest"] = None,
70+
include_unstructured_sources: Optional[bool] = None,
71+
**kwargs: Any
72+
) -> "_models.KnowledgeBaseAnswers":
73+
"""Answers the specified question using your knowledge base.
74+
75+
:keyword project_name: The name of the project to use.
76+
:paramtype project_name: str
77+
:keyword deployment_name: The name of the specific deployment of the project to use.
78+
:paramtype deployment_name: str
79+
:keyword qna_id: Exact QnA ID to fetch from the knowledge base, this field takes priority over
80+
question.
81+
:paramtype qna_id: int
82+
:keyword question: User question to query against the knowledge base.
83+
:paramtype question: str
84+
:keyword top: Max number of answers to be returned for the question.
85+
:paramtype top: int
86+
:keyword user_id: Unique identifier for the user.
87+
:paramtype user_id: str
88+
:keyword confidence_score_threshold: Minimum threshold score for answers, value ranges from 0 to
89+
1.
90+
:paramtype confidence_score_threshold: float
91+
:keyword context: Context object with previous QnA's information.
92+
:paramtype context: ~azure.ai.language.questionanswering.models.KnowledgeBaseAnswerRequestContext
93+
:keyword ranker_type: (Optional) Set to 'QuestionOnly' for using a question only Ranker. Possible
94+
values include: "Default", "QuestionOnly".
95+
:paramtype ranker_type: str or ~azure.ai.language.questionanswering.models.RankerType
96+
:keyword strict_filters: Filter QnAs based on give metadata list and knowledge base source names.
97+
:paramtype strict_filters: ~azure.ai.language.questionanswering.models.StrictFilters
98+
:keyword answer_span_request: To configure Answer span prediction feature.
99+
:paramtype answer_span_request: ~azure.ai.language.questionanswering.models.AnswerSpanRequest
100+
:keyword include_unstructured_sources: (Optional) Flag to enable Query over Unstructured Sources.
101+
:paramtype include_unstructured_sources: bool
102+
:keyword callable cls: A custom type or function that will be passed the direct response
103+
:return: KnowledgeBaseAnswers, or the result of cls(response)
104+
:rtype: ~azure.ai.language.questionanswering.models.KnowledgeBaseAnswers
105+
:raises: ~azure.core.exceptions.HttpResponseError
106+
"""
107+
...
108+
109+
async def query_knowledgebase(
110+
self,
111+
*args,
112+
**kwargs: Any
113+
) -> "_models.KnowledgeBaseAnswers":
114+
if args:
115+
knowledge_base_query_options = args[0]
116+
else:
117+
knowledge_base_query_options = _models.KnowledgeBaseQueryOptions(
118+
qna_id=kwargs.pop("qna_id", None),
119+
question=kwargs.pop("question", None),
120+
top=kwargs.pop("top", None),
121+
user_id=kwargs.pop("user_id", None),
122+
confidence_score_threshold=kwargs.pop("confidence_score_threshold", None),
123+
context=kwargs.pop("context", None),
124+
ranker_type=kwargs.pop("ranker_type", None),
125+
strict_filters=kwargs.pop("strict_filters", None),
126+
answer_span_request=kwargs.pop("answer_span_request", None),
127+
include_unstructured_sources=kwargs.pop("include_unstructured_sources", None)
128+
)
54129
cls = kwargs.pop("cls", None) # type: ClsType["_models.KnowledgeBaseAnswers"]
55130
error_map = {401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError}
56131
error_map.update(kwargs.pop("error_map", {}))
57132
content_type = kwargs.pop("content_type", "application/json") # type: Optional[str]
133+
project_name = kwargs.pop("project_name") # type: str
134+
deployment_name = kwargs.pop("deployment_name", None) # type: Optional[str]
58135

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

@@ -89,18 +166,65 @@ async def query_knowledgebase(
89166

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

92-
async def query_text(self, text_query_options: "_models.TextQueryOptions", **kwargs: Any) -> "_models.TextAnswers":
169+
@overload
170+
async def query_text(
171+
self, text_query_options: "_models.TextQueryOptions", **kwargs: Any
172+
) -> "_models.TextAnswers":
93173
"""Answers the specified question using the provided text in the body.
94174
95-
Answers the specified question using the provided text in the body.
96-
97175
:param text_query_options: Post body of the request.
98176
:type text_query_options: ~azure.ai.language.questionanswering.models.TextQueryOptions
99177
:keyword callable cls: A custom type or function that will be passed the direct response
100178
:return: TextAnswers, or the result of cls(response)
101179
:rtype: ~azure.ai.language.questionanswering.models.TextAnswers
102180
:raises: ~azure.core.exceptions.HttpResponseError
103181
"""
182+
...
183+
184+
@overload
185+
async def query_text(
186+
self,
187+
*,
188+
question: str,
189+
records: List["_models.TextRecord"],
190+
language: Optional[str] = None,
191+
string_index_type: Optional[Union[str, "_models.StringIndexType"]] = "TextElements_v8",
192+
**kwargs: Any
193+
) -> "_models.TextAnswers":
194+
"""Answers the specified question using the provided text in the body.
195+
196+
:keyword question: Required. User question to query against the given text records.
197+
:paramtype question: str
198+
:keyword records: Required. Text records to be searched for given question.
199+
:paramtype records: list[~azure.ai.language.questionanswering.models.TextRecord]
200+
:keyword language: Language of the text records. This is BCP-47 representation of a language. For
201+
example, use "en" for English; "es" for Spanish etc. If not set, use "en" for English as
202+
default.
203+
:paramtype language: str
204+
:keyword string_index_type: Specifies the method used to interpret string offsets. Defaults to
205+
Text Elements (Graphemes) according to Unicode v8.0.0. For additional information see
206+
https://aka.ms/text-analytics-offsets. Possible values include: "TextElements_v8",
207+
"UnicodeCodePoint", "Utf16CodeUnit". Default value: "TextElements_v8".
208+
:paramtype string_index_type: str or ~azure.ai.language.questionanswering.models.StringIndexType
209+
:keyword callable cls: A custom type or function that will be passed the direct response
210+
:return: TextAnswers, or the result of cls(response)
211+
:rtype: ~azure.ai.language.questionanswering.models.TextAnswers
212+
:raises: ~azure.core.exceptions.HttpResponseError
213+
"""
214+
...
215+
216+
async def query_text(
217+
self, *args, **kwargs: Any
218+
) -> "_models.TextAnswers":
219+
if args:
220+
text_query_options = args[0]
221+
else:
222+
text_query_options = _models.TextQueryOptions(
223+
question=kwargs.pop("question"),
224+
records=kwargs.pop("records"),
225+
language=kwargs.pop("language", None),
226+
string_index_type=kwargs.pop("string_index_type", "TextElements_v8")
227+
)
104228
cls = kwargs.pop("cls", None) # type: ClsType["_models.TextAnswers"]
105229
error_map = {401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError}
106230
error_map.update(kwargs.pop("error_map", {}))

0 commit comments

Comments
 (0)