我尝试将onEnabled
方法与QnAMakerRecognizer一起使用,以便我可以拥有多个QnAMakerRecognizer,并可以选择启用哪个以及禁用哪个。我已经尝试过这个example并尝试了类似的东西,但我收到了一个错误说:
TypeError :(中间值).onEnabled不是函数
如何正确使用onEnable
方法?
示例代码:
var bot = new builder.UniversalBot(connector);
var qnarecognizer1 = new cognitiveservices.QnAMakerRecognizer({
knowledgeBaseId: knowledgeId,
subscriptionKey: subscriptionId,
qnaThreshold:0.3,
top:1})
.onEnabled(function (session, callback) {
// Check to see if this recognizer should be enabled
if (session.conversationData.useqna1) {
callback(null, true);
} else {
callback(null, false);
}
});
var qnarecognizer2 = new cognitiveservices.QnAMakerRecognizer({
knowledgeBaseId: knowledgeId,
subscriptionKey: subscriptionId,
qnaThreshold:0.3,
top:1})
.onEnabled(function (session, callback) {
// Check to see if this recognizer should be enabled
if (session.conversationData.useqna2) {
callback(null, true);
} else {
callback(null, false);
}
});
var intentrecognizer = new builder.IntentDialog();
var intents = new builder.IntentDialog({ recognizers: [intentrecognizer, qnarecognizer1, qnarecognizer2] });
bot.dialog('/', intents);
intents.matches('qna', [
function (session, args, next) {
args.entities.forEach(function(element) {
session.send(element.entity);
}, this);
}
]);
intents.matchesAny([/hi/i, /main menu/i], [
function (session) {
builder.Prompts.choice(session, "Hi, What would you like to ask me about?", ["Service1","Service2"],{ listStyle: builder.ListStyle.button});
},
function (session, result) {
var selection = result.response.entity;
switch (selection) {
case "Service1":
session.conversationData.useqna1 = true;
session.conversationData.useqna2 = false;
session.send('You can now ask me anything about '+selection+'. Type \'main menu\' anytime to choose a different topic.')
session.endConversation();
return
case "Service2":
session.conversationData.useqna1 = false;
session.conversationData.useqna2 = true;
session.send('You can now ask me anything about '+selection+'. Type \'main menu\' anytime to choose a different topic.')
session.endConversation();
return
}
}
])
答案 0 :(得分:1)
QnAMakerRecognizer没有实现Bot Builder IntentRecognizer,也没有onEnabled方法。
编辑: 我在BotBuilder-CognitiveServices仓库上提交了一个问题:https://github.com/Microsoft/BotBuilder-CognitiveServices/issues/62
答案 1 :(得分:0)
在我们的项目中,我们结束了像这样的解决方法。
我们克隆了识别原型函数,并检查了sessionData布尔值以启用/禁用识别器。
希望它对您有用;)
qnaRecognizer.__proto__['_recognize'] = qnaRecognizer.__proto__['recognize']
qnaRecognizer.__proto__.recognize = function (context, cb) {
if(!context.conversationData || !context.conversationData.ignoreQnARecognizers){
return qnaRecognizer.__proto__._recognize.apply(this, [context, cb]);
} else {
cb(null, null);
}
}