This repository has been archived by the owner on Feb 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcreate-new-knowledge-base.js
167 lines (141 loc) · 5.6 KB
/
create-new-knowledge-base.js
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
'use strict';
let fs = require('fs');
let https = require('https');
// **********************************************
// *** Update or verify the following values. ***
// **********************************************
// Components used to create HTTP request URIs for QnA Maker operations.
let host = 'westus.api.cognitive.microsoft.com';
let service = '/qnamaker/v4.0';
let method = '/knowledgebases/create';
// Build your path URL.
let path = service + method;
// Replace this with a valid subscription key.
let subscriptionKey = '<qna-maker-subscription-key>';
// Dictionary that holds the knowledge base.
// The data source includes a QnA pair with metadata, the URL for the
// QnA Maker FAQ article, and the URL for the Azure Bot Service FAQ article.
let kb_model = {
"name": "QnA Maker FAQ",
"qnaList": [
{
"id": 0,
"answer": "You can use our REST APIs to manage your Knowledge Base. See here for details: https://westus.dev.cognitive.microsoft.com/docs/services/58994a073d9e04097c7ba6fe/operations/58994a073d9e041ad42d9baa",
"source": "Custom Editorial",
"questions": [
"How do I programmatically update my Knowledge Base?"
],
"metadata": [
{
"name": "category",
"value": "api"
}
]
}
],
"urls": [
"https://docs.microsoft.com/en-in/azure/cognitive-services/qnamaker/faqs",
"https://docs.microsoft.com/en-us/bot-framework/resources-bot-framework-faq"
],
"files": []
};
// Convert the JSON object to a string.
let content = JSON.stringify(kb_model);
// Formats and indents JSON for display.
let pretty_print = function (s) {
return JSON.stringify(JSON.parse(s), null, 4);
};
// The 'callback' is called from the entire response.
let response_handler = function (callback, response) {
let body = '';
response.on('data', function (d) {
body += d;
});
response.on('end', function () {
// Call the 'callback' with the status code, headers, and body of the response.
callback({ status: response.statusCode, headers: response.headers, body: body });
});
response.on('error', function (e) {
console.log('Error: ' + e.message);
});
};
// Get an HTTP response handler that calls 'callback' from the entire response.
let get_response_handler = function (callback) {
// Return a function that takes an HTTP response and is closed over the specified callback.
// This function signature is required by https.request, hence the need for the closure.
return function (response) {
response_handler(callback, response);
};
};
// Call 'callback' when we have the entire response from the POST request.
let post = function (path, content, callback) {
let request_params = {
method: 'POST',
hostname: host,
path: path,
headers: {
'Content-Type': 'application/json',
'Content-Length': content.length,
'Ocp-Apim-Subscription-Key': subscriptionKey
}
};
// Pass the 'callback' function to the response handler.
let req = https.request(request_params, get_response_handler(callback));
req.write(content);
req.end();
};
// Call 'callback' when we have the response from the /knowledgebases/create POST method.
let create_kb = function (path, req, callback) {
console.log('Calling ' + host + path + '.');
// Send the POST request.
post(path, req, function (response) {
// Extract the data we want from the POST response and pass it to the callback function.
callback({ operation: response.headers.location, response: response.body });
});
};
// Call 'callback' when we have the entire response from the GET request.
let get = function (path, callback) {
let request_params = {
method: 'GET',
hostname: host,
path: path,
headers: {
'Ocp-Apim-Subscription-Key': subscriptionKey
}
};
// Pass the callback function to the response handler.
let req = https.request(request_params, get_response_handler(callback));
req.end();
};
// Call 'callback' when we have the response from the GET request to check the status.
let check_status = function (path, callback) {
console.log('Calling ' + host + path + '.');
// Send the GET request.
get(path, function (response) {
// Extract the data we want from the GET response and pass it to the callback function.
callback({ wait: response.headers['retry-after'], response: response.body });
});
};
create_kb(path, content, function (result) {
// Formats and indents the JSON response from the /knowledgebases/create method for display.
console.log(pretty_print(result.response));
// Loop until the operation is complete.
let loop = function () {
// add operation ID to the path
path = service + result.operation;
// Check the status of the operation.
check_status(path, function (status) {
// Formats and indents the JSON for display.
console.log(pretty_print(status.response));
// Convert the status into an object and get the value of the 'operationState'.
var state = (JSON.parse(status.response)).operationState;
// If the operation isn't complete, wait and query again.
if (state === 'Running' || state === 'NotStarted') {
console.log('Waiting ' + status.wait + ' seconds...');
setTimeout(loop, status.wait * 1000);
}
});
};
// Begin the loop.
loop();
});