-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathupdate-knowledge-base.py
More file actions
128 lines (116 loc) · 4.25 KB
/
Copy pathupdate-knowledge-base.py
File metadata and controls
128 lines (116 loc) · 4.25 KB
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
# -*- coding: utf-8 -*-
import http.client, urllib.parse, json, time
# **********************************************
# *** Update or verify the following values. ***
# **********************************************
# Replace this with a valid subscription key.
subscriptionKey = 'ADD KEY HERE'
# Replace this with a valid knowledge base ID.
kb = 'ADD ID HERE';
# Represents the various elements used to create HTTP request path
# for QnA Maker operations.
host = 'westus.api.cognitive.microsoft.com'
service = '/qnamaker/v4.0'
method = '/knowledgebases/'
'''
Formats and indents JSON for display.
:param content: The JSON to format and indent.
:type: string
:return: Formatted and indented JSON.
:rtype: string
'''
def pretty_print(content):
# Note: We convert content to and from an object so we can pretty-print it.
return json.dumps(json.loads(content), indent=4)
'''
Sends a PATCH HTTP request.
:param path: The URL path of your request.
:param content: The contents of your PATCH request.
:type: string
:return: URL with status of PATCH request in updating the kb, actual response.
:rtype: string, string
'''
def update_kb(path, content):
print('Calling ' + host + path + '.')
headers = {
'Ocp-Apim-Subscription-Key': subscriptionKey,
'Content-Type': 'application/json',
'Content-Length': len (content)
}
conn = http.client.HTTPSConnection(host)
conn.request ("PATCH", path, content, headers)
response = conn.getresponse ()
return response.getheader('Location'), response.read ()
'''
Gets the status of the specified QnA Maker operation.
:param path: The URL of the request.
:type: string
:return: Header from retrying of the request (if retry is needed), response of the retry.
:rtype: string, string
'''
def check_status (path):
print('Calling ' + host + path + '.')
headers = {'Ocp-Apim-Subscription-Key': subscriptionKey}
conn = http.client.HTTPSConnection(host)
conn.request("GET", path, None, headers)
response = conn.getresponse ()
# If the operation is not finished, /operations returns an HTTP header named
# 'Retry-After' with the number of seconds to wait before querying the operation again.
return response.getheader('Retry-After'), response.read ()
'''
Dictionary that holds the knowledge base.
Modifications to the knowledge base are made here, using 'update', 'delete' and so on.
'''
req = {
'add': {
'qnaList': [
{
'id': 1,
'answer': 'You can change the default message if you use the QnAMakerDialog. '
+ 'See this for details: https://docs.botframework.com/en-us'
+ '/azure-bot-service/templates/qnamaker/#navtitle',
'source': 'Custom Editorial',
'questions': [
'How can I change the default message from QnA Maker?'
],
'metadata': []
}
],
'urls': []
},
'update' : {
'name' : 'New KB Name'
},
'delete': {
'ids': [
0
]
}
}
# Builds the path URL.
path = service + method + kb
# Convert the request to a string.
content = json.dumps(req)
# Retrieve the operation ID to check status, and JSON result.
operation, result = update_kb(path, content)
# Print request response in JSON with presentable formatting.
print(pretty_print(result))
'''
Iteratively gets the operation state, updating the knowledge base.
Once state is no longer "Running" or "NotStarted", the loop ends.
'''
done = False
while False == done:
path = service + operation
# Gets the status of the operation.
wait, status = check_status(path)
# Print status checks in JSON with presentable formatting
print(pretty_print(status))
# Convert the JSON response into an object and get the value of the operationState field.
state = json.loads(status)['operationState']
# If the operation isn't finished, wait and query again.
if state == 'Running' or state == 'NotStarted':
print('Waiting ' + wait + ' seconds...')
time.sleep (int(wait))
else:
done = True # request has been processed, if successful, knowledge base is updated