forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
myAppInsightsQnAMaker.js
76 lines (65 loc) · 3.17 KB
/
myAppInsightsQnAMaker.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
const { TurnContext } = require('botbuilder');
const { QnAMaker } = require('botbuilder-ai');
/**
* Custom wrapper around QnAMaker from botbuilder-ai.
* Sends custom events to Application Insights.
*/
class MyAppInsightsQnAMaker extends QnAMaker {
constructor(endpoint, options, logOriginalMessage, logUserName) {
super(endpoint);
this.QnAMsgEvent = 'QnAMessage';
this.requestConfig = options;
this.logOriginalMessage = logOriginalMessage;
this.logUserName = logUserName;
}
/**
* Calls QnA Maker and then sends a custom Event to Application Insights with results that matched closest with the user's message.
* Sends the top scoring Question and Answer pairing to Application Insights.
* @param {TurnContext} turnContext The TurnContext instance with the necessary information to perform the calls.
*/
async generateAnswer(turnContext) {
const { top, scoreThreshold } = this.requestConfig;
// Call QnAMaker.generateAnswer to retrieve possible Question and Answer pairings for the user's message.
const results = await super.generateAnswer(turnContext.activity.text, top, scoreThreshold);
// Retrieve the reference for the TelemetryClient that was cached for the Turn in TurnContext.turnState via MyAppInsightsMiddleware.
const telemetryClient = turnContext.turnState.get('AppInsightsLoggerMiddleware.AppInsightsContext');
const telemetryProperties = {};
const telemetryMetrics = {};
const activity = turnContext.activity;
// Make it so we can correlate our reports with Activity or Conversation.
telemetryProperties.ActivityId = activity.id;
if (activity.conversation.id) {
telemetryProperties.ConversationId = activity.conversation.id;
}
// For some customers, logging original text name within Application Insights might be an issue.
if (this.logOriginalMessage && !!activity.text) {
telemetryProperties.OriginalQuestion = activity.text;
}
// For some customers, logging user name within Application Insights might be an issue.
if (this.logUserName && !!activity.from.name) {
telemetryProperties.Username = activity.from.name;
}
// Fill in QnA Results (found or not).
if (results.length > 0) {
const queryResult = results[0];
telemetryProperties.Question = queryResult.questions[0];
telemetryProperties.Answer = queryResult.answer;
telemetryMetrics.Score = queryResult.score;
} else {
telemetryProperties.Question = 'No QnA Question matched.';
telemetryProperties.Answer = 'No QnA Question matched.';
}
// Finish constructing the event.
const qnaMsgEvent = {
name: 'QnAMessage',
properties: telemetryProperties,
measurements: telemetryMetrics
};
// Track the event.
telemetryClient.trackEvent(qnaMsgEvent);
return results;
}
}
exports.MyAppInsightsQnAMaker = MyAppInsightsQnAMaker;