-
Notifications
You must be signed in to change notification settings - Fork 20
/
app.js
352 lines (321 loc) · 13.9 KB
/
app.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
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
/**
* Copyright 2017-2020 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the 'License'); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
'use strict';
require('dotenv').config({
silent: true,
});
const users = require('./recommender/mockUsers.json');
const recMethods = require('./recommender/recMethods.js');
const express = require('express'); // app server
const bodyParser = require('body-parser'); // parser for post requests
const AssistantV2 = require('ibm-watson/assistant/v2');
const DiscoveryV1 = require('ibm-watson/discovery/v1');
const { getAuthenticatorFromEnvironment } = require('ibm-watson/auth');
// HEY! Bump the API version dates here (to yesterday) when updating and TESTING.
const ASST_API_VERSION = '2020-05-04';
const DISCO_API_VERSION = '2020-05-04';
// Optional Watson Assistant client, if configured
const assistantId = process.env.ASSISTANT_ID;
let assistant = false;
if (assistantId) {
// need to manually set url and disableSslVerification to get around
// current Cloud Pak for Data SDK issue IF user uses
// `CONVERSATION_` prefix in run-time environment.
let url;
let disableSSL = false;
let auth;
try {
// ASSISTANT should be used
auth = getAuthenticatorFromEnvironment('assistant');
url = process.env.ASSISTANT_URL;
if (process.env.ASSISTANT_DISABLE_SSL === 'true') {
disableSSL = true;
}
} catch (e) {
// but handle if alternate CONVERSATION is used
auth = getAuthenticatorFromEnvironment('conversation');
url = process.env.CONVERSATION_URL;
if (process.env.CONVERSATION_DISABLE_SSL === 'true') {
disableSSL = true;
}
}
// Truncate to allow the /v2/ URL from API Details to work.
if (url) {
url = url.split('/v2/')[0];
}
assistant = new AssistantV2({
version: ASST_API_VERSION,
authenticator: auth,
url: url,
disableSslVerification: disableSSL,
});
}
// Optional Watson Discovery client, if configured
let discovery = false;
const discoveryParams = {};
if (process.env.DISCOVERY_COLLECTION_ID) {
discovery = new DiscoveryV1({
version: DISCO_API_VERSION,
});
discoveryParams.collectionId = process.env.DISCOVERY_COLLECTION_ID;
discoveryParams.environmentId = process.env.DISCOVERY_ENVIRONMENT_ID;
}
const DISCOVERY_ACTION = 'disco';
const app = express();
app.use(express.static('./public')); // load UI from public folder
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
// Chatbot endpoint to be called from the client side
app.post('/api/message', async (req, res) => {
if (!assistant) {
const text = 'No Watson Assistant. Please configure the runtime environment and restart the server.';
return res.json({ output: { generic: [{ response_type: 'text', text }] } });
}
const sessionId = (req.body.context && req.body.context.global.session_id) || (await assistant.createSession({ assistantId: assistantId })).result.session_id;
console.log('SID: ', sessionId);
// Add customerId to the context as if somebody logged in.
if (!req.body.context) {
req.body.context = { skills: { 'main skill': { user_defined: { customerId: 'user1234' } } } };
}
const input = req.body.input || {};
input.options = { return_context: true };
const payload = {
assistantId: assistantId,
sessionId: sessionId,
input: input,
context: req.body.context,
};
callAssistant(payload);
/**
* Send the input to the Assistant service. Optionally, NLU first.
* @param params
*/
async function callAssistant(params) {
console.log('Assistant params: ', params);
try {
const data = await assistant.message(params);
console.log('assistant.message result: ', JSON.stringify(data.result, null, 2));
checkForLookupRequests(params, data, function (err, data) {
if (err) {
console.log(err);
return res.status(err.code || 500).json(err);
} else {
return res.json(data);
}
});
} catch (err) {
console.log('assistant.message error: ', err);
return res.json({ output: { generic: [{ response_type: 'text', text: err.toString() }] } });
}
}
});
/**
* Looks for actions requested by Assistant service and provides the requested data.
*/
async function checkForLookupRequests(input, output, callback) {
console.log('checkForLookupRequests');
const data = output.result;
console.log('Assistant result to act on: ' + JSON.stringify(data, null, 2));
const mainContext = data.context.skills['main skill'] || {};
// TODO: How can we handle a webhook config error here or in the dialog?
if (data.output.user_defined && data.output.user_defined.webhook_error) {
const webhook_error = data.output.user_defined.webhook_error;
console.log('webhook error: ', webhook_error);
Object.keys(webhook_error).forEach((k) => console.log(`key=${k} message=${webhook_error[k].message}`));
}
if (data.output.generic.length && data.output.generic[0].response_type === 'search') {
data.output.generic.push({ response_type: 'text', text: data.output.generic[0].results[0].highlight.text[0] });
callback(null, data);
} else if (
// Lookups using an action set in context.
mainContext.user_defined &&
mainContext.user_defined.action &&
mainContext.user_defined.action.lookup &&
mainContext.user_defined.action.lookup !== 'complete'
) {
const payload = {
workspaceId: assistantId,
context: data.context,
input: input.input,
};
console.log('mainContext.user_defined.action.lookup: ', mainContext.user_defined.action.lookup);
// Assistant requests a data lookup action
// Only call Discovery directly if configured. Ideally Assistant
// did this for us with the search skill.
if (mainContext.user_defined.action.lookup === DISCOVERY_ACTION) {
// Mark the context's action complete so we don't use it over and over.
mainContext.user_defined.action.lookup = 'complete';
console.log('************** Discovery *************** InputText : ' + payload.input.text);
let discoveryResponse = '';
if (!discovery) {
console.log('Discovery is not ready configured. Add runtime environment vars and restart server.');
discoveryResponse = 'Sorry, currently I do not have a response. Discovery is not configured.';
if (data.output.generic) {
data.output.generic.push({ response_type: 'text', text: discoveryResponse });
}
callback(null, data);
} else {
const queryParams = {
naturalLanguageQuery: payload.input.text,
passages: true,
};
Object.assign(queryParams, discoveryParams);
discoveryResponse = 'Sorry, currently I do not have a response. Our Customer representative will get in touch with you shortly.';
try {
const searchResponse = await discovery.query(queryParams);
if (searchResponse.result.matching_results > 0) {
// we have a valid response from Discovery
// now check if we are using SDU or just a simple document query
let bestLine;
// console.log('Disco result: ' + JSON.stringify(searchResponse, null, 2));
let bestScore = 0;
if (searchResponse.result.results[0].text) {
for (let i = 0, size = searchResponse.result.results.length; i < size; i++) {
if (searchResponse.result.results[i].result_metadata['confidence'] > bestScore) {
bestLine = searchResponse.result.results[i].text;
bestScore = searchResponse.result.results[i].result_metadata['confidence'];
}
}
console.log('**** Returning highest confidence text response to customer ****');
console.log(bestLine);
} else if ('passages' in searchResponse.result && searchResponse.result.passages.length) {
console.log('Using Passage feature');
// use Passage feature
const bestPassage = searchResponse.result.passages[0];
console.log('Passage score: ', bestPassage.passage_score);
console.log('Passage text: ', bestPassage.passage_text);
// set a default value
bestLine = bestPassage.passage_text;
// Trim the passage to try to get just the answer part of it.
const lines = bestPassage.passage_text.split('.');
// just use the first sentence in the response.
// if it contains a question mark, use the portion after it
const line = lines[0].trim();
if (line.indexOf('?') > -1) {
const subline = line.split('?');
bestLine = subline[1];
} else {
bestLine = line;
}
} else {
console.log('Using default response');
// other formats, like from CPD (non-SDU)
if ('text' in searchResponse.result.results[0]) {
const lines = searchResponse.result.results[0].text.split('.');
const line = lines[0].trim();
if (line.indexOf('?') > -1) {
const subline = line.split('?');
bestLine = subline[1];
} else {
bestLine = line;
}
}
}
discoveryResponse =
bestLine || 'Sorry I currently do not have an appropriate response for your query. Our customer care executive will call you in 24 hours.';
}
if (data.output.generic) {
data.output.generic.push({ response_type: 'text', text: discoveryResponse });
}
console.log('Discovery response: ' + discoveryResponse);
callback(null, data);
} catch (err) {
console.error('Error searching for documents: ' + err);
}
}
} else {
callback(null, data);
}
} else if (data.output.intents.length > 0 && data.output.intents[0]['intent'] == 'describe_damage') {
const description = input.input.text;
recMethods
.classifyDamage(description)
.then((result) => {
if (data.output.generic && result) {
console.log('response received');
console.log(JSON.stringify(data.output));
const recommendations = recMethods.getRankings(result);
const shopNames = recommendations.map((r, idx) => `${r[0].name}`);
const shopNamesString = '\n\n' + recommendations.map((r, idx) => `${idx}. ${r[0].name}`).join('\n');
const recResponse = {
damage_description: result,
recs: shopNames,
recsString: shopNamesString,
};
const responsePrefix = `It sounds like you need a mechanic that specializes in "${result}" repairs. Here are a few suggestions for mechanics near you. Would you like to select one? `;
// set context
data.context.skills['main skill']['user_defined'].recResponse = recResponse;
const r = { response_type: 'text', text: responsePrefix + shopNamesString };
data.output.generic.push(r);
callback(null, data);
} else {
const r = { response_type: 'text', text: 'Unable to determine repair type. Is NLU configured?' };
data.output.generic.push(r);
callback(null, data);
}
})
.catch((err) => {
console.log(err);
callback(null, data);
});
} else if (data.output.intents.length > 0 && data.output.intents[0]['intent'] == 'Insurance_View_Claim_Status') {
// lookup claim status
const userId = data.context.skills['main skill']['user_defined']['customerId'];
const userList = users.filter((user) => user.id.toLowerCase() == userId.toLowerCase());
if (userList.length > 0 && userList[0].claims.length > 0) {
const user = userList[0];
// returning latest claim in array
const latestClaim = user.claims.slice(-1)[0];
console.log(`retrieved claim ${JSON.stringify(latestClaim)}`);
data.context.skills['main skill']['user_defined'].claim = latestClaim;
const response = `You have a ${latestClaim.status} claim. Your vehicle is being serviced at "${latestClaim.assignedMech}"`;
const r = { response_type: 'text', text: response };
data.output.generic.push(r);
console.log(`updated context ${JSON.stringify(data)}`);
callback(null, data);
} else {
callback(null, data);
}
} else if (data.output.generic && data.output.generic.filter((r) => r.text.includes('Assigning') && r.text.includes('to your claim')).length > 0) {
console.log('adding mechanic to claim');
// lookup mechanic from selection
const selection = data.output.entities.filter((e) => e.entity == 'sys-number');
const mechanicIdx = selection[0].value;
const mechanic = data.context.skills['main skill']['user_defined']['recResponse']['recs'][mechanicIdx];
// lookup user from context
const userId = data.context.skills['main skill']['user_defined']['customerId'];
const userIdx = users.findIndex((user) => user.id == userId);
if (userIdx != -1) {
// assign mechanic to user
users[userIdx].claims.slice(-1)[0].assignedMech = mechanic;
callback(null, data);
} else {
callback(null, data);
}
} else {
// anything_else
callback(null, data);
}
}
const config = `
Watson Assistant configured to use...<br/>
<br/>
ASSISTANT_ID = ${assistantId}</br>
<br/>
`;
// Testing a status page
app.get('/config', (req, res) => res.send(config));
module.exports = app;