Skip to content

Commit ebce185

Browse files
authored
[qna] regen with latest and implement design changes (#20975)
1 parent abc9a1c commit ebce185

File tree

54 files changed

+1605
-1014
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1605
-1014
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,23 @@
55
* We are now targeting service version `2021-07-15-preview`
66

77
### Breaking changes
8+
89
* The method `QuestionAnsweringClient.query_knowledgebase` has been renamed to `query_knowledge_base`.
10+
* Options bag model `KnowledgeBaseQueryOptions` for `query_knowledge_base` is renamed to `QueryKnowledgeBaseOptions`
11+
* Options bag model `TextQueryOptions` for `query_text` is renamed to `QueryTextOptions`
12+
* The filters model `StrictFilters` is renamed to `QueryFilters`
13+
* Enum `CompoundOperationKind` is renamed to `LogicalOperationKind`
14+
* We have removed the `string_index_type` input to all models and operations. We have also removed the `StringIndexType` enum.
15+
* The type of input `metadata` to `MetadataFilter` has changed from a dictionary of strings to a list of key-value tuples.
16+
For example, the input has changed from `{"key": "value"}` to `[("key", "value")]`.
17+
* The input to the `query_knowledge_base` and `query_text` overloads that take in a positional model for the body should be
18+
considered positional only.
919

1020
### Features Added
21+
1122
* The method `QuestionAnsweringClient.query_text` now supports a list of records as strings, where the ID value will be automatically populated.
23+
* Added keyword argument `default_language` onto `QuestionAnsweringClient`, which has default value `'en'`. The default language for any operation call will
24+
be this default language value.
1225

1326

1427
## 1.0.0b1 (2021-07-27)

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

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@ Question Answering is a cloud-based API service that lets you create a conversat
66

77
[Source code][questionanswering_client_src] | [Package (PyPI)][questionanswering_pypi_package] | [API reference documentation][questionanswering_refdocs] | [Product documentation][questionanswering_docs] | [Samples][questionanswering_samples]
88

9+
## _Disclaimer_
10+
11+
_Azure SDK Python packages support for Python 2.7 is ending 01 January 2022. For more information and questions, please refer to https://github.com/Azure/azure-sdk-for-python/issues/20691_
12+
913
## Getting started
1014

1115
### Prerequisites
1216

13-
* Python 2.7, or 3.6 or later is required to use this package.
14-
* An [Azure subscription][azure_subscription]
15-
* An existing Question Answering resource
17+
- Python 2.7, or 3.6 or later is required to use this package.
18+
- An [Azure subscription][azure_subscription]
19+
- An existing Question Answering resource
1620

1721
> Note: the new unified Cognitive Language Services are not currently available for deployment.
1822
@@ -56,14 +60,15 @@ client = QuestionAnsweringClient(endpoint, credential)
5660

5761
### QuestionAnsweringClient
5862

59-
The [QuestionAnsweringClient][questionanswering_client_class] is the primary interface for asking questions using a knowledge base with your own information, or text input using pre-trained models.
63+
The [QuestionAnsweringClient][questionanswering_client_class] is the primary interface for asking questions using a knowledge base with your own information, or text input using pre-trained models.
6064
For asynchronous operations, an async `QuestionAnsweringClient` is in the `azure.ai.language.questionanswering.aio` namespace.
6165

6266
## Examples
6367

6468
The `azure-ai-language-questionanswering` client library provides both synchronous and asynchronous APIs.
6569

6670
The following examples show common scenarios using the `client` [created above](#create-questionansweringclient).
71+
6772
- [Ask a question](#ask-a-question)
6873
- [Ask a follow-up question](#ask-a-follow-up-question)
6974
- [Asynchronous operations](#asynchronous-operations)
@@ -75,7 +80,7 @@ The only input required to ask a question using a knowledge base is just the que
7580
```python
7681
from azure.ai.language.questionanswering import models as qna
7782

78-
params = qna.KnowledgeBaseQueryOptions(
83+
params = qna.QueryKnowledgeBaseOptions(
7984
question="How long should my Surface battery last?"
8085
)
8186

@@ -89,14 +94,14 @@ for candidate in output.answers:
8994

9095
```
9196

92-
You can set additional properties on `KnowledgeBaseQueryOptions` to limit the number of answers, specify a minimum confidence score, and more.
97+
You can set additional properties on `QueryKnowledgeBaseOptions` to limit the number of answers, specify a minimum confidence score, and more.
9398

9499
### Ask a follow-up question
95100

96101
If your knowledge base is configured for [chit-chat][questionanswering_docs_chat], the answers from the knowledge base may include suggested [prompts for follow-up questions][questionanswering_refdocs_prompts] to initiate a conversation. You can ask a follow-up question by providing the ID of your chosen answer as the context for the continued conversation:
97102

98103
```python
99-
params = qna.models.KnowledgeBaseQueryOptions(
104+
params = qna.models.QueryKnowledgeBaseOptions(
100105
question="How long should charging take?"
101106
context=qna.models.KnowledgeBaseAnswerRequestContext(
102107
previous_qna_id=previous_answer.id
@@ -112,17 +117,19 @@ for candidate in output.answers:
112117
print("Source: {}".format(candidate.source))
113118

114119
```
120+
115121
### Asynchronous operations
116122

117123
The above examples can also be run asynchronously using the client in the `aio` namespace:
124+
118125
```python
119126
from azure.core.credentials import AzureKeyCredential
120127
from azure.ai.language.questionanswering.aio import QuestionAnsweringClient
121128
from azure.ai.language.questionanswering import models as qna
122129

123130
client = QuestionAnsweringClient(endpoint, credential)
124131

125-
params = qna.KnowledgeBaseQueryOptions(
132+
params = qna.QueryKnowledgeBaseOptions(
126133
question="How long should my Surface battery last?"
127134
)
128135

@@ -133,11 +140,13 @@ output = await client.query_knowledgebase(
133140
```
134141

135142
## Optional Configuration
143+
136144
Optional keyword arguments can be passed in at the client and per-operation level. The azure-core [reference documentation][azure_core_ref_docs] describes available configurations for retries, logging, transport protocols, and more.
137145

138146
## Troubleshooting
139147

140148
### General
149+
141150
Azure QuestionAnswering clients raise exceptions defined in [Azure Core][azure_core_readme].
142151
When you interact with the Cognitive Language Services Question Answering client library using the Python SDK, errors returned by the service correspond to the same HTTP status codes returned for [REST API][questionanswering_rest_docs] requests.
143152

@@ -156,6 +165,7 @@ except HttpResponseError as error:
156165
```
157166

158167
### Logging
168+
159169
This library uses the standard
160170
[logging][python_logging] library for logging.
161171
Basic information about HTTP sessions (URLs, headers, etc.) is logged at INFO
@@ -168,9 +178,9 @@ See full SDK logging documentation with examples [here][sdk_logging_docs].
168178

169179
## Next steps
170180

171-
* View our [samples][questionanswering_samples].
172-
* Read about the different [features][questionanswering_docs_features] of the Question Answering service.
173-
* Try our service [demos][questionanswering_docs_demos].
181+
- View our [samples][questionanswering_samples].
182+
- Read about the different [features][questionanswering_docs_features] of the Question Answering service.
183+
- Try our service [demos][questionanswering_docs_demos].
174184

175185
## Contributing
176186

@@ -183,6 +193,7 @@ When you submit a pull request, a CLA-bot will automatically determine whether y
183193
This project has adopted the [Microsoft Open Source Code of Conduct][code_of_conduct]. For more information see the [Code of Conduct FAQ][coc_faq] or contact [[email protected]][coc_contact] with any additional questions or comments.
184194

185195
<!-- LINKS -->
196+
186197
[azure_cli]: https://docs.microsoft.com/cli/azure/
187198
[azure_portal]: https://portal.azure.com/
188199
[azure_subscription]: https://azure.microsoft.com/free/
@@ -196,7 +207,7 @@ This project has adopted the [Microsoft Open Source Code of Conduct][code_of_con
196207
[sdk_logging_docs]: https://docs.microsoft.com/azure/developer/python/azure-sdk-logging
197208
[azure_core_ref_docs]: https://azuresdkdocs.blob.core.windows.net/$web/python/azure-core/latest/azure.core.html
198209
[azure_core_readme]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/core/azure-core/README.md
199-
[pip_link]:https://pypi.org/project/pip/
210+
[pip_link]: https://pypi.org/project/pip/
200211
[questionanswering_client_class]: https://azuresdkdocs.blob.core.windows.net/$web/python/azure-ai-language-questionanswering/1.0.0b1/azure.ai.language.questionanswering.html#azure.ai.language.questionanswering.QuestionAnsweringClient
201212
[questionanswering_refdocs_prompts]: https://azuresdkdocs.blob.core.windows.net/$web/python/azure-ai-language-questionanswering/1.0.0b1/azure.ai.language.questionanswering.models.html#azure.ai.language.questionanswering.models.KnowledgeBaseAnswerDialog
202213
[questionanswering_client_src]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/cognitivelanguage/azure-ai-language-questionanswering/

sdk/cognitivelanguage/azure-ai-language-questionanswering/azure/ai/language/questionanswering/_patch.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,27 @@ def _validate_text_records(records):
3636
else:
3737
request_batch.append(doc)
3838
return request_batch
39+
40+
def _get_positional_body(*args, **kwargs):
41+
"""Verify args and kwargs are valid, and then return the positional body, if users passed it in."""
42+
if len(args) > 1:
43+
raise TypeError("There can only be one positional argument, which is the POST body of this request.")
44+
if args and "options" in kwargs:
45+
raise TypeError(
46+
"You have already supplied the request body as a positional parameter, "
47+
"you can not supply it as a keyword argument as well."
48+
)
49+
return args[0] if args else None
50+
51+
def _verify_qna_id_and_question(query_knowledgebase_options):
52+
"""For query_knowledge_base we require either `question` or `qna_id`."""
53+
try:
54+
qna_id = query_knowledgebase_options.qna_id
55+
question = query_knowledgebase_options.question
56+
except AttributeError:
57+
qna_id = query_knowledgebase_options.get("qna_id") or query_knowledgebase_options.get("qnaId")
58+
question = query_knowledgebase_options.get("question")
59+
if not (qna_id or question):
60+
raise TypeError(
61+
"You need to pass in either `qna_id` or `question`."
62+
)

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class QuestionAnsweringClient(QuestionAnsweringClientOperationsMixin):
3232
:type endpoint: str
3333
:param credential: Credential needed for the client to connect to Azure.
3434
:type credential: ~azure.core.credentials.AzureKeyCredential
35+
:keyword str default_language: Sets the default language to use for all operations.
36+
Defaults to "en".
3537
"""
3638

3739
def __init__(
@@ -49,6 +51,7 @@ def __init__(
4951
self._serialize = Serializer(client_models)
5052
self._deserialize = Deserializer(client_models)
5153
self._serialize.client_side_validation = False
54+
self._default_language = kwargs.pop("default_language", "en")
5255

5356
def send_request(
5457
self,

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class QuestionAnsweringClient(QuestionAnsweringClientOperationsMixin):
2727
:type endpoint: str
2828
:param credential: Credential needed for the client to connect to Azure.
2929
:type credential: ~azure.core.credentials.AzureKeyCredential
30+
:keyword str default_language: Sets the default language to use for all operations.
31+
Defaults to "en".
3032
"""
3133

3234
def __init__(self, endpoint: str, credential: AzureKeyCredential, **kwargs: Any) -> None:
@@ -38,6 +40,7 @@ def __init__(self, endpoint: str, credential: AzureKeyCredential, **kwargs: Any)
3840
self._serialize = Serializer(client_models)
3941
self._deserialize = Deserializer(client_models)
4042
self._serialize.client_side_validation = False
43+
self._default_language = kwargs.pop("default_language", "en")
4144

4245
def send_request(self, request: HttpRequest, **kwargs: Any) -> Awaitable[AsyncHttpResponse]:
4346
"""Runs the network request through the client's chained policies.

0 commit comments

Comments
 (0)