Skip to content

Commit 5a2cfb4

Browse files
iscai-msftrakshith91
authored andcommitted
[qna] add migration guide (Azure#20997)
* add migration guide * remove en-us from links
1 parent 9924258 commit 5a2cfb4

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
# Guide for migrating to azure-ai-language-question-answering from azure-cognitiveservices-knowledge-qnamaker
2+
3+
This guide is intended to assist in the migration to `azure-ai-language-question-answering`. It will focus on side-by-side comparisons for similar operations between the two packages.
4+
5+
Familiarity with the `azure-ai-language-question-answering` package is assumed. For those new to the Key Vault client library for Python, please refer to the [README for `azure-ai-language-question-answering`][qna_readme] rather than this guide.
6+
7+
## Table of contents
8+
9+
* [Migration benefits](#migration-benefits)
10+
* [Important changes](#important-changes)
11+
- [Separate packages and clients](#separate-packages-and-clients)
12+
- [Client constructors](#client-constructors)
13+
- [Async operations](#async-operations)
14+
- [Query a Knowledge Base](#query-a-knowledge-base)
15+
* [Additional samples](#additional-samples)
16+
17+
## Migration benefits
18+
19+
A natural question to ask when considering whether or not to adopt a new version or library is what the benefits of doing so would be. As Azure has matured and been embraced by a more diverse group of developers, we have been focused on learning the patterns and practices to best support developer productivity and to understand the gaps that the Python client libraries have.
20+
21+
There were several areas of consistent feedback expressed across the Azure client library ecosystem. One of the most important is that the client libraries for different Azure services have not had a consistent approach to organization, naming, and API structure. Additionally, many developers have felt that the learning curve was difficult, and the APIs did not offer a good, approachable, and consistent onboarding story for those learning Azure or exploring a specific Azure service.
22+
23+
To try and improve the development experience across Azure services, a set of uniform [design guidelines][design_guidelines] was created for all languages to drive a consistent experience with established API patterns for all services. A set of [Python-specific guidelines][python_specific_guidelines] was also introduced to ensure that Python clients have a natural and idiomatic feel with respect to the Python ecosystem. Further details are available in the guidelines for those interested.
24+
25+
### Cross Service SDK improvements
26+
27+
The modern Question Answering client library also provides the ability to share in some of the cross-service improvements made to the Azure development experience, such as
28+
- using the new [`azure-identity`][identity_readme] library to share a single authentication approach between clients
29+
- a unified logging and diagnostics pipeline offering a common view of the activities across each of the client libraries
30+
31+
## Important changes
32+
33+
### Client constructors
34+
35+
Across all modern Azure client libraries, clients consistently take an endpoint or connection string along with token credentials. This differs from `QnAMakerClient`, which took an authentication delegate.
36+
37+
#### Authenticating
38+
39+
Previously in `azure-cognitiveservices-knowledge-qnamaker` you could create a `QnAMakerClient` by using `CognitiveServicesCredentials` from `msrest.authentication`:
40+
41+
```python
42+
from msrest.authentication import CognitiveServicesCredentials
43+
from azure.cognitiveservices.knowledge.qnamaker import QnAMakerClient
44+
45+
client = QnAMakerClient(
46+
endpoint="https://<my-cognitiveservices-account>.cognitiveservices.azure.com",
47+
credentials=CognitiveServicesCredentials("API key")
48+
)
49+
```
50+
51+
Now in `azure-ai-language-questionanswering` you can create a `QuestionAnsweringClient` using an [`AzureKeyCredential`][[azure_key_credential]]:
52+
53+
```python
54+
from azure.core.credentials import AzureKeyCredential
55+
from azure.ai.language.questionanswering import QuestionAnsweringClient
56+
57+
client = QuestionAnsweringClient(
58+
endpoint="https://<my-cognitiveservices-account>.cognitiveservices.azure.com",
59+
credential=AzureKeyCredential("API key")
60+
)
61+
```
62+
63+
### Async operations
64+
65+
The modern `azure-ai-language-question-answering` library includes a complete async API supported on Python 3.6+. To use it, you must first install an async transport, such as [aiohttp][aiohttp]. See [azure-core documentation][azure_core_transport] for more information.
66+
67+
Async operations are available on async clients, which should be closed when they're no longer needed. Each async client is an async context manager and defines an async `close` method. For example:
68+
69+
```python
70+
from azure.core.credentials import AzureKeyCredential
71+
from azure.ai.language.questionanswering import QuestionAnsweringClient
72+
73+
endpoint = "https://<my-cognitiveservices-account>.cognitiveservices.azure.com"
74+
credential = AzureKeyCredential("API key")
75+
76+
# call close when the client is no longer needed
77+
client = QuestionAnsweringClient(endpoint=endpoint, credential=credential)
78+
...
79+
await client.close()
80+
81+
# alternatively, use the client as an async context manager
82+
async with QuestionAnsweringClient(endpoint=endpoint, credential=credential) as client:
83+
...
84+
```
85+
86+
### Query a Knowledge Base
87+
88+
In `azure-cognitiveservices-knowledge-qnamaker`, you would have to import and create a model
89+
to query a knowledge base.
90+
91+
```python
92+
from azure.cognitiveservices.knowledge.qnamaker.models import QueryDTO
93+
94+
generate_answer_payload = QueryDTO(
95+
question="How long should my Surface battery last?",
96+
)
97+
98+
response = client.generate_answer(
99+
kb_id="<my-knowledge-base-id>",
100+
generate_answer_payload=generate_answer_payload,
101+
)
102+
best_answers = [a for a in response.answers if a.score > 0.9]
103+
```
104+
105+
The modern `azure-ai-language-questionanswering` has overloads for body input.
106+
You can either:
107+
108+
1. Import and create a model and pass it in as a positional parameter
109+
2. Create a JSON dict and pass it in as a positional parameter
110+
3. Pass in arguments directly to the method with keyword arguments.
111+
112+
In this sample, we will show how to pass in the arguments as keyword arguments.
113+
114+
```python
115+
response = client.query_knowledge_base(
116+
question="How long should my Surface battery last?",
117+
project_name="<my-qna-project-name>",
118+
deployment_name="<my-qna-deployment-name>"
119+
)
120+
best_answers = [a for a in response.answers if a.confidence_score > 0.9]
121+
```
122+
123+
## Additional Samples
124+
125+
The new `azure-ai-language-questionanswering` has new capability not supported by the old SDK, you can
126+
see the additional samples [here][qna_samples].
127+
128+
<!--LINKS-->
129+
[qna_readme]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/cognitivelanguage/azure-ai-language-questionanswering/README.md
130+
[design_guidelines]: https://azure.github.io/azure-sdk/python/guidelines/index.html
131+
[python_specific_guidelines]: https://azure.github.io/azure-sdk/python_design.html
132+
[identity_readme]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/identity/azure-identity/README.md
133+
[azure_key_credential]: https://docs.microsoft.com/python/api/azure-core/azure.core.credentials.azurekeycredential?view=azure-python
134+
[aiohttp]: https://pypi.org/project/aiohttp/
135+
[azure_core_transport]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/core/azure-core/CLIENT_LIBRARY_DEVELOPER.md#transport
136+
[qna_samples]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/cognitivelanguage/azure-ai-language-questionanswering/samples

0 commit comments

Comments
 (0)