-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateIntent.ts
More file actions
483 lines (438 loc) · 13.4 KB
/
createIntent.ts
File metadata and controls
483 lines (438 loc) · 13.4 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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
import axios from 'axios'
import * as es from 'elasticsearch'
import * as limit from 'simple-rate-limiter'
import {
Suggestion,
Intent,
Utterance,
Message,
CustomMessage,
Context,
ArticleLink
} from './interfaceTypes'
const ES_USERNAME = 'es4askit'
const ES_PASSWORD = 'codyGo4'
const ES_HOST = [{
host: '18.216.253.76',
port: '80',
auth: ES_USERNAME + ':' + ES_PASSWORD
}]
const ARTICLE_INDEX_NAME = 'askit'
const ARTICLE_TYPE_NAME = 'kb'
const SUGGESTIONS_INDEX_NAME = 'suggestions'
const SUGGESTIONS_TYPE_NAME = '_doc'
const INITIAL_SUGGESTIONS_LIST: Suggestion = {
key: '',
label: '',
next: '0',
root: '',
endpoint: false
}
const INTENTS_TO_CREATE_PER_BATCH = 45
const MINUTE = 1000*60
const dialogFlowAPIBasePath = 'https://api.dialogflow.com/v1'
const intentsEndpoint = '/intents'
const versioningParam = "?v=20170712"
const DEVELOPER_ACCESS_TOKEN = process.argv[2] === 'prod' ? '249f1ae954f0421ab2b3e84979d066fa' : '4e911d4bfbce43dbbc7cda08bcead4c8'
const utterancePrefixPhrases = [
'',
'I need help with ',
'I have a query regarding '
]
const suggestionResponses = [
'Noted. Could you be a tad more specific by choosing one of the options below?',
'Okay, does any of the following options relate to your query?',
'Alright! I need a little more information to solve your query. Could you select one of the options from the list below?'
]
const articleResponses = [
"Okay, here's what I found:",
"Here's an article that might address your query:"
]
const linkListResponses = [
"Here's a selection of articles related to your question:",
"Check out these articles I found that might help you:",
"I think these articles might help you out:"
]
const satisfactionResponsesArticle = [
"Did it help solve your query?",
"Was the article helpful?",
"Does the article help resolve your question?"
]
const satisfactionResponsesLinkList = [
"Did any of these articles help resolve your query?",
"Were any of these articles helpful?",
"Did you find the answer to your question in any of these articles?"
]
const satisfactionSuggestions = [{
"suggestion": "Yes"
},{
"suggestion": "No"
}]
const intakeResponses = [
"Would you like to try again, or do you want to create a ticket with the NYU ServiceDesk regarding this?",
"Do you want me to try again, or should I create a ticket for you with the NYU ServiceDesk?"
]
const intakeSuggestions = [{
"suggestion": "Retry"
},{
"suggestion": "Create Ticket"
},{
"suggestion": "Nah, leave it"
}]
const feedbackResponses = [[
"I hope I was able to help you out today!",
"Would you like to provide some feedback about your experience?"
], [
"I try my best to be as helpful as I can.",
"Your feedback would help me grow better! Would you mind sharing how your experience was today?"
], [
"I'd love to know how your experience was today!",
"Would you like to share some comments about your experience today?"
]]
const feedbackSuggestions = [{
"suggestion": "Sure!"
},{
"suggestion": "Not right now"
}]
const customContexts = {
"satisfaction": {
"lifespan": 1,
"name": "satisfaction"
},
"intake": {
"lifespan": 1,
"name": "create_intake"
},
"feedback": {
"lifespan": 1,
"name": "feedback"
}
}
let insertedCount = 0
axios.interceptors.request.use(request => {
// console.log('\nBody:\n', JSON.stringify(request.data))
return request
})
// Remove non-alphanumeric symbols from string (since Dialogflow does not accept them) and
// replace whitespaces with underscore (required for context names).
function removeSpecialChars(str: string, escapeSpaces: boolean): string {
try {
str = require('emoji-strip')(str)
str = str.replace(/[^\w\s]/gi, ' ').trim()
if (escapeSpaces) {
str = str.replace(/ /g, "_")
}
} catch {
console.log(str)
}
return str
}
function formatTitle(str: string): string {
try {
return unescape(str.split(':')[1].trim())
} catch(err) {
return str
}
}
// Add the standard utterance prefix phrases to the query (most often the suggestion) and
// return an array of complete sentences as the training text for Dialogflow.
function generateUtteranceSentences(query: string): Array < Utterance > {
return utterancePrefixPhrases.map((phrase) => {
return {
"data": [{
"text": phrase + removeSpecialChars(query, false)
}]
}
})
}
// Instantiates and returns an Elasticsearch client object.
function instantiateElasticsearch(host: object): es.Client {
let x: es.Client
try {
x = new es.Client({
host
})
} catch (err) {
console.log(err)
}
return x
}
// Get data from Elasticsearch corresponding to the suggestion ID passed.
async function getDataFromES(suggestion: Suggestion): Promise < any > {
let response: es.GetResponse < {} >
const clusterUp = await esClient.ping({
// ping usually has a 3000ms timeout
requestTimeout: 1000
})
if (!clusterUp) {
console.trace('elasticsearch cluster is down!');
} else {
if (!suggestion.endpoint) {
try {
response = await esClient.get({
index: SUGGESTIONS_INDEX_NAME,
type: SUGGESTIONS_TYPE_NAME,
id: suggestion.next
})
} catch (err) {
console.log(suggestion)
}
} else {
response = await esClient.get({
index: ARTICLE_INDEX_NAME,
type: ARTICLE_TYPE_NAME,
id: suggestion.next
})
}
return response._source
}
}
// Call the Dialogflow Create Intent API with the generated intent.
async function createDialogflowIntent(intent: Intent ): Promise<void> {
axios({
method: 'post',
url: dialogFlowAPIBasePath + intentsEndpoint + versioningParam,
data: intent,
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + DEVELOPER_ACCESS_TOKEN
}
})
.then(() => console.log(`Inserted Intent ${++insertedCount}`))
.catch((err) => {
console.log(err.response.data)
})
}
// Generate the output contexts that should be activated when the intent is invoked, which
// should be similar to the suggestions selected up to this point.
function generateAffectedContexts(selectedSuggestions: Array < string > ): Array < Context > {
// return selectedSuggestions.map((suggestion) => {
// return {
// "lifespan": 3,
// "name": suggestion
// }
// })
return [{
"lifespan": 3,
"name": selectedSuggestions[0]
}]
}
// Generate the input -- textual or via suggestion selection -- that the user is expected to
// make that will activate the intent.
function generateUserUtterances(queries: Array < string > ): Array < Utterance > {
let userSays = []
queries.map((query) => {
userSays = userSays.concat(generateUtteranceSentences(query))
})
return userSays
}
// Generate the list of suggestions to be shown to the user when the intent is invoked.
function generateBotResponses(suggestionList: Array < Suggestion > ): [CustomMessage, Message] {
let botResponses: any = [{
"speech": suggestionResponses,
"type": 0
} as Message
]
botResponses.push({
"platform": "slack",
"speech": suggestionResponses,
"type": 0
})
botResponses.push({
"platform": "slack",
"replies": suggestionList.slice(0, 9).map(suggestion => suggestion.label.substring(0,18)),
"type": 2
})
const suggestionsToDisplay = suggestionList.map(suggestion => ({ "suggestion": suggestion.label }))
botResponses.push({
"payload": {
"dataType": "suggestions",
"data": suggestionsToDisplay
},
"type": 4
} as CustomMessage)
return botResponses
}
async function generateLinkList(linkSuggestions: Array< Suggestion >): Promise<[CustomMessage, Message]> {
let botResponses: any = [{
"speech": linkListResponses,
"type": 0
} as Message
]
let linkList: ArticleLink[] = []
for(const suggestion of linkSuggestions) {
const { description, solution, solutionUrl } = await getDataFromES(suggestion)
linkList.push({
"title": formatTitle(description),
"content": solution,
"contentURL": solutionUrl
})
}
botResponses.push({
"payload": {
"dataType": "linkList",
"data": linkList
},
"type": 4
} as CustomMessage)
botResponses.push({
"speech": satisfactionResponsesLinkList,
"type": 0
} as Message)
botResponses.push({
"payload": {
"dataType": "suggestions",
"data": satisfactionSuggestions
},
"type": 4
} as CustomMessage)
return botResponses
}
// Generate the intent to be added to Dialogflow on the basis of suggestion selection.
async function generateIntent(suggestion: Suggestion, selectedSuggestions: Array < string > , nextData: any): Promise< Intent[]> {
let intentName = ''
let affectedContexts: Context[]
let userSays: Utterance[]
let messages: any
let intents: Intent[] = []
let resetContexts: boolean = false
if (!suggestion.endpoint) {
intentName = selectedSuggestions.join("_") + "_" + suggestion.key
userSays = generateUserUtterances([suggestion.key, suggestion.label])
if(nextData.suggestions.every(nextLevelSuggestion => nextLevelSuggestion.endpoint === true)){
// resetContexts = true
affectedContexts = [customContexts['satisfaction']]
messages = await generateLinkList(nextData.suggestions)
}
else {
const outputContexts = selectedSuggestions.concat(removeSpecialChars(suggestion.key, true))
affectedContexts = generateAffectedContexts(outputContexts)
messages = generateBotResponses(nextData.suggestions)
// resetContexts = false
}
if(selectedSuggestions.length > 0){
const intentWithoutContext: Intent = {
"auto": true,
"contexts": [],
"name": suggestion.key,
"responses": [{
affectedContexts,
messages,
resetContexts
}],
userSays,
webhookUsed: false
}
intents.push(intentWithoutContext)
}
} else {
affectedContexts = [customContexts['satisfaction']]
// resetContexts = true
const description = formatTitle(nextData.description)
// TODO:
// Use keyword n-grams calculated during suggestion list creation as additional user utterances input to Dialogflow.
// The description is generally of the form "<description>: <headline>". The description is the
// same for several articles, hence we discard it to generate the user utterance.
userSays = [{
"data": [{
"text": description
}]
}]
messages = [{
"speech": articleResponses,
"type": 0
},{
"platform": "slack",
"speech": articleResponses,
"type": 0
},{
"platform": "slack",
"speech": JSON.stringify({
"title": description,
"content": nextData.solution,
"contentURL": nextData.solutionUrl
}),
"type": 0
}, {
"payload": {
"dataType": "article",
"data": {
"title": description,
"content": nextData.solution,
"contentURL": nextData.solutionUrl
}
},
"type": 4
}, {
"speech": satisfactionResponsesArticle,
"type": 0
}, {
"payload": {
"dataType": "suggestions",
"data": satisfactionSuggestions
},
"type": 4
}]
const intentWithoutContext: Intent = {
"auto": true,
"contexts": [],
"name": description,
"responses": [{
affectedContexts,
messages,
resetContexts
}],
userSays,
webhookUsed: true
}
intents.push(intentWithoutContext)
intentName = selectedSuggestions.join("_") + "_" + description
intentName = intentName.substring(0, 95)
}
intents.push({
"auto": true,
"contexts": [selectedSuggestions[0]],
"name": intentName,
"responses": [{
affectedContexts,
messages,
resetContexts
}],
userSays,
webhookUsed: suggestion.endpoint
})
return intents
}
async function recurseOverSuggestions(suggestion: Suggestion, suggestionsList: Array < string > ): Promise < Intent[] > {
// For each suggestion, create a standalone Dialogflow intent and a contextual Dialogflow intent. Then get the
// next level suggestions for that suggestion (if not an endpoint) and similarly recurse.
let allIntents = []
const nextData = await getDataFromES(suggestion)
const intents = await generateIntent(suggestion, suggestionsList, nextData)
allIntents = allIntents.concat(intents)
// await createDialogflowIntent(intents)
if (!suggestion.endpoint) {
const suggestionsListNew = suggestionsList.concat(removeSpecialChars(suggestion.key, true))
for (let subSuggestion of nextData.suggestions) {
const subIntents = await recurseOverSuggestions(subSuggestion, suggestionsListNew)
allIntents = allIntents.concat(subIntents)
}
}
return allIntents;
}
async function main() {
//Get the first level suggestions from Elasticsearch and call recursor to get
// the info of each of the suggestions.
const initialSuggestionsList = await getDataFromES(INITIAL_SUGGESTIONS_LIST)
let allIntents = []
for (let suggestion of initialSuggestionsList.suggestions) {
const intents = await recurseOverSuggestions(suggestion, [])
allIntents = allIntents.concat(intents)
}
const createIntentInDialogflow = limit(createDialogflowIntent).to(INTENTS_TO_CREATE_PER_BATCH).per(MINUTE)
allIntents.forEach((intent, index) => {
createIntentInDialogflow(intent)
})
}
const esClient = instantiateElasticsearch(ES_HOST)
main()