-
Notifications
You must be signed in to change notification settings - Fork 518
/
quickstart.py
265 lines (216 loc) · 8.99 KB
/
quickstart.py
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# install QnA Maker package on Windows with command:
# py -m pip install azure-cognitiveservices-knowledge-qnamaker
# install QnA Maker package on Mac/Linux with command:
# pip install azure-cognitiveservices-knowledge-qnamaker
# ==========================================
# Tasks Included
# ==========================================
# This sample does the following tasks.
# - Create a knowledge base.
# - Update a knowledge base.
# - Publish a knowledge base.
# - Download a knowledge base.
# - Get runtime endpoint key.
# - Query a knowledge base.
# - Delete a knowledge base.
# ==========================================
# Further reading
#
# General documentation: https://docs.microsoft.com/azure/cognitive-services/QnAMaker
# Reference documentation: https://docs.microsoft.com/en-us/python/api/overview/azure/cognitiveservices/qnamaker?view=azure-python
# ==========================================
# <Dependencies>
import os
import time
from azure.cognitiveservices.knowledge.qnamaker.authoring import QnAMakerClient
from azure.cognitiveservices.knowledge.qnamaker.runtime import QnAMakerRuntimeClient
from azure.cognitiveservices.knowledge.qnamaker.authoring.models import QnADTO, MetadataDTO, CreateKbDTO, OperationStateType, UpdateKbOperationDTO, UpdateKbOperationDTOAdd, EndpointKeysDTO, QnADTOContext, PromptDTO
from azure.cognitiveservices.knowledge.qnamaker.runtime.models import QueryDTO
from msrest.authentication import CognitiveServicesCredentials
# </Dependencies>
# Set the `authoring_key` and `authoring_endpoint` variables to your
# QnA Maker authoring subscription key and endpoint.
#
# These values can be found in the Azure portal (ms.portal.azure.com/).
# Look up your QnA Maker resource. Then, in the "Resource management"
# section, find the "Keys and Endpoint" page.
#
# The value of `authoring_endpoint` has the format https://YOUR-RESOURCE-NAME.cognitiveservices.azure.com.
#
# Set the `runtime_endpoint` variable to your QnA Maker runtime endpoint.
# The value of `runtime_endpoint` has the format https://YOUR-RESOURCE-NAME.azurewebsites.net.
# <Resourcevariables>
subscription_key = 'PASTE_YOUR_QNA_MAKER_AUTHORING_SUBSCRIPTION_KEY_HERE'
authoring_endpoint = 'PASTE_YOUR_QNA_MAKER_AUTHORING_ENDPOINT_HERE'
runtime_endpoint = 'PASTE_YOUR_QNA_MAKER_RUNTIME_ENDPOINT_HERE'
# </Resourcevariables>
# <MonitorOperation>
def _monitor_operation(client, operation):
for i in range(20):
if operation.operation_state in [OperationStateType.not_started, OperationStateType.running]:
print("Waiting for operation: {} to complete.".format(operation.operation_id))
time.sleep(5)
operation = client.operations.get_details(operation_id=operation.operation_id)
else:
break
if operation.operation_state != OperationStateType.succeeded:
raise Exception("Operation {} failed to complete.".format(operation.operation_id))
return operation
# </MonitorOperation>
# <CreateKBMethod>
def create_kb(client):
print ("Creating knowledge base...")
qna1 = QnADTO(
answer="Yes, You can use our [REST APIs](https://docs.microsoft.com/rest/api/cognitiveservices/qnamaker/knowledgebase) to manage your knowledge base.",
questions=["How do I manage my knowledgebase?"],
metadata=[
MetadataDTO(name="Category", value="api"),
MetadataDTO(name="Language", value="REST"),
]
)
qna2 = QnADTO(
answer="Yes, You can use our [Python SDK](https://pypi.org/project/azure-cognitiveservices-knowledge-qnamaker/) with the [Python Reference Docs](https://docs.microsoft.com/python/api/azure-cognitiveservices-knowledge-qnamaker/azure.cognitiveservices.knowledge.qnamaker?view=azure-python) to manage your knowledge base.",
questions=["Can I program with Python?"],
metadata=[
MetadataDTO(name="Category", value="api"),
MetadataDTO(name="Language", value="Python"),
]
)
urls = []
files = [
FileDTO(
file_name = "structured.docx",
file_uri = "https://github.com/Azure-Samples/cognitive-services-sample-data-files/raw/master/qna-maker/data-source-formats/structured.docx"
)]
create_kb_dto = CreateKbDTO(
name="QnA Maker Python SDK Quickstart",
qna_list=[
qna1,
qna2
],
urls=urls,
files=[],
enable_hierarchical_extraction=True,
default_answer_used_for_extraction="No answer found.",
language="English"
)
create_op = client.knowledgebase.create(create_kb_payload=create_kb_dto)
create_op_monitor = _monitor_operation(client=client, operation=create_op)
# Get knowledge base ID from resourceLocation HTTP header
knowledge_base_ID = create_op_monitor.resource_location.replace("/knowledgebases/", "")
print("Created KB with ID: {}".format(knowledge_base_ID))
return knowledge_base_ID
# </CreateKBMethod>
# <UpdateKBMethod>
def update_kb(client, kb_id):
print ("Updating knowledge base...")
qna3 = QnADTO(
answer="goodbye",
questions=[
"bye",
"end",
"stop",
"quit",
"done"
],
metadata=[
MetadataDTO(name="Category", value="Chitchat"),
MetadataDTO(name="Chitchat", value="end"),
]
)
qna4 = QnADTO(
answer="Hello, please select from the list of questions or enter a new question to continue.",
questions=[
"hello",
"hi",
"start"
],
metadata=[
MetadataDTO(name="Category", value="Chitchat"),
MetadataDTO(name="Chitchat", value="begin"),
],
context = QnADTOContext(
is_context_only = False,
prompts = [
PromptDTO(
display_order =1,
display_text= "Use REST",
qna_id=1
),
PromptDTO(
display_order =2,
display_text= "Use .NET NuGet package",
qna_id=2
),
]
)
)
urls = [
"https://docs.microsoft.com/azure/cognitive-services/QnAMaker/troubleshooting"
]
update_kb_operation_dto = UpdateKbOperationDTO(
add=UpdateKbOperationDTOAdd(
qna_list=[
qna3,
qna4
],
urls = urls,
files=[]
),
delete=None,
update=None
)
update_op = client.knowledgebase.update(kb_id=kb_id, update_kb=update_kb_operation_dto)
_monitor_operation(client=client, operation=update_op)
print("Updated knowledge base.")
# </UpdateKBMethod>
# <PublishKB>
def publish_kb(client, kb_id):
print("Publishing knowledge base...")
client.knowledgebase.publish(kb_id=kb_id)
print("Published knowledge base.")
# </PublishKB>
# <DownloadKB>
def download_kb(client, kb_id):
print("Downloading knowledge base...")
kb_data = client.knowledgebase.download(kb_id=kb_id, environment="Prod")
print("Downloaded knowledge base. It has {} QnAs.".format(len(kb_data.qna_documents)))
# </DownloadKB>
# <DeleteKB>
def delete_kb(client, kb_id):
print("Deleting knowledge base...")
client.knowledgebase.delete(kb_id=kb_id)
print("Deleted knowledge base.")
# </DeleteKB>
# <GetQueryEndpointKey>
def getEndpointKeys_kb(client):
print("Getting runtime endpoint keys...")
keys = client.endpoint_keys.get_keys()
print("Primary runtime endpoint key: {}.".format(keys.primary_endpoint_key))
return keys.primary_endpoint_key
# </GetQueryEndpointKey>
# <GenerateAnswer>
def generate_answer(client, kb_id, runtimeKey):
print ("Querying knowledge base...")
authHeaderValue = "EndpointKey " + runtimeKey
listSearchResults = client.runtime.generate_answer(kb_id, QueryDTO(question = "How do I manage my knowledgebase?"), dict(Authorization=authHeaderValue))
for i in listSearchResults.answers:
print(f"Answer ID: {i.id}.")
print(f"Answer: {i.answer}.")
print(f"Answer score: {i.score}.")
# </GenerateAnswer>
# <Main>
# <AuthorizationAuthor>
client = QnAMakerClient(endpoint=authoring_endpoint, credentials=CognitiveServicesCredentials(subscription_key))
# </AuthorizationAuthor>
kb_id = create_kb(client=client)
update_kb (client=client, kb_id=kb_id)
publish_kb (client=client, kb_id=kb_id)
download_kb (client=client, kb_id=kb_id)
queryRuntimeKey = getEndpointKeys_kb(client=client)
# <AuthorizationQuery>
runtimeClient = QnAMakerRuntimeClient(runtime_endpoint=runtime_endpoint, credentials=CognitiveServicesCredentials(queryRuntimeKey))
# </AuthorizationQuery>
generate_answer(client=runtimeClient,kb_id=kb_id,runtimeKey=queryRuntimeKey)
delete_kb (client=client, kb_id=kb_id)
# </Main>