File: README.md

package info (click to toggle)
python-azure 20251118%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 783,356 kB
  • sloc: python: 6,474,533; ansic: 804; javascript: 287; sh: 205; makefile: 198; xml: 109
file content (213 lines) | stat: -rw-r--r-- 7,453 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# Azure AI Language Question Answering Authoring client library for Python

The `azure-ai-language-questionanswering-authoring` package provides **authoring / management capabilities** for Azure AI Language Question Answering: create and configure projects, add knowledge sources, manage QnA pairs and synonyms, and deploy versions. Runtime (query) operations live in the separate `azure-ai-language-questionanswering` package.

> NOTE: This is a preview (`1.0.0b1`) targeting a preview service API version (`2025-05-15-preview`). APIs, models, and LRO result payloads may change before GA.

[Product documentation][product_docs]

## Getting started

### Prerequisites

- Python 3.9+ (preview requires 3.9 or later)
- An Azure subscription
- An Azure AI Language resource with Question Answering enabled (custom subdomain endpoint recommended for AAD)

### Install the package

```bash
pip install --pre azure-ai-language-questionanswering-authoring
```

Optional (for Azure Active Directory auth):

```bash
pip install azure-identity
```

### Authenticate the client

You can authenticate with:

1. Azure Active Directory via `DefaultAzureCredential` (recommended)
2. A resource key via `AzureKeyCredential` (quick start / local experimentation)

AAD example:
```python
from azure.identity import DefaultAzureCredential
from azure.ai.language.questionanswering.authoring import QuestionAnsweringAuthoringClient

endpoint = "https://<resource-name>.cognitiveservices.azure.com"
credential = DefaultAzureCredential()
client = QuestionAnsweringAuthoringClient(endpoint, credential)
```

Key credential example:
```python
from azure.core.credentials import AzureKeyCredential
from azure.ai.language.questionanswering.authoring import QuestionAnsweringAuthoringClient

client = QuestionAnsweringAuthoringClient(
    endpoint="https://<resource-name>.cognitiveservices.azure.com",
    credential=AzureKeyCredential("<api-key>")
)
```

## Key concepts

- **Project**: A logical container for your knowledge sources, QnA pairs, synonyms, and deployments.
- **Knowledge Source**: A URL/file describing content from which QnA pairs can be extracted.
- **QnA Record**: A question and its answer plus metadata/alternative questions.
- **Synonyms**: Word alteration groups to normalize variations in user questions.
- **Deployment**: A named (e.g., `production`) deployed snapshot of your project used by runtime clients.
- **Long‑running operation (LRO)**: Certain operations (update sources/QnAs, import, export, deploy) return an `LROPoller`. In the current preview these resolve to `None`—treat `.result()` strictly as a completion signal.

## Examples

Below are minimal synchronous examples. More complete samples (including async equivalents) are in the samples directory. Environment variables used by samples: `AZURE_QUESTIONANSWERING_ENDPOINT`, `AZURE_QUESTIONANSWERING_KEY`.

### Create a project
```python
metadata = {
    "language": "en",
    "description": "FAQ project",
    "settings": {"defaultAnswer": "no answer"},
    "multilingualResource": True,
}
client.create_project(project_name="FAQ", body=metadata)
```

### List projects
```python
for proj in client.list_projects():
    print(proj.get("projectName"), proj.get("lastModifiedDateTime"))
```

### Add / update a knowledge source
```python
from azure.ai.language.questionanswering.authoring.models import UpdateSourceRecord,UpdateQnaSourceRecord 

poller = client.begin_update_sources(
    project_name="FAQ",
    body=[
        UpdateSourceRecord(
            op="add",
            value=UpdateQnaSourceRecord(
                display_name="ContosoFAQ",
                source="https://contoso.com/faq",
                source_uri="https://contoso.com/faq",
                source_kind="url",
                content_structure_kind="unstructured",
                refresh=False,
            ),
        )
    ],
)
poller.result()
```

### Add a QnA pair
```python
from azure.ai.language.questionanswering.authoring.models import UpdateQnaRecord,QnaRecord

poller = client.begin_update_qnas(
    project_name="FAQ",
    body=[
        UpdateQnaRecord(
            op="add",
            value=QnaRecord(
                id=1,
                answer="Use the Azure SDKs.",
                source="manual",
                questions=["How do I use Azure services in .NET?"],
            ),
        )
    ],
)
poller.result()
```

### Set synonyms
```python
from azure.ai.language.questionanswering.authoring.models import SynonymAssets,WordAlterations

client.update_synonyms(
    project_name="FAQ",
    body=SynonymAssets(
        value=[
            WordAlterations(alterations=["qnamaker", "qna maker"]),
            WordAlterations(alterations=["qna", "question and answer"]),
        ]
    ),
)
```

### Deploy
```python
client.begin_deploy_project(project_name="FAQ", deployment_name="production").result()
```

### Export / Import
```python
export_poller = client.begin_export(project_name="FAQ", format="json")
export_poller.result()  # current preview returns None

from azure.ai.language.questionanswering.authoring.models import ImportJobOptions,Assets,ImportQnaRecord
assets = ImportJobOptions(
    assets=Assets(
        qnas=[
            ImportQnaRecord(
                id=1,
                answer="Example answer",
                source="https://contoso.com/faq",
                questions=["Example question?"],
            )
        ]
    )
)
client.begin_import_assets(project_name="FAQ", body=assets, format="json").result()
```

## Troubleshooting

### Errors
Service errors raise `HttpResponseError` (or subclasses) from `azure-core`. Check the `.status_code` / `.message` for details.

```python
from azure.core.exceptions import HttpResponseError

try:
    client.list_projects()
except HttpResponseError as e:
    print("Request failed:", e.message)
```

### Logging
Enable basic logging:
```python
import logging
logging.basicConfig(level=logging.INFO)
```
For request/response details set environment variable `AZURE_LOG_LEVEL=info` or pass `logging_enable=True` per operation.

## Next steps

- Explore the full samples
- Learn about Question Answering concepts in [product documentation][product_docs]

## Contributing

See [CONTRIBUTING.md][contributing] for instructions on building, testing, and contributing.

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit <https://cla.microsoft.com>.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

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 <mailto:opencode@microsoft.com> with any additional questions or comments.

<!-- LINKS -->
[product_docs]: https://learn.microsoft.com/azure/ai-services/language-service/question-answering/overview
[contributing]: https://github.com/Azure/azure-sdk-for-python/blob/main/CONTRIBUTING.md
[code_of_conduct]: https://opensource.microsoft.com/codeofconduct/
[coc_faq]: https://opensource.microsoft.com/codeofconduct/faq/