我正在尝试将LUIS和QnA服务集成到一个机器人中。我使用了Github上可用的示例代码来获得意图的响应。 我尝试使用相同的代码-
var restify = require('restify');
var builder = require('botbuilder');
var cognitiveservices = require('./node_modules/botbuilder-cognitiveservices/lib/botbuilder-cognitiveservices');
var botbuilder_azure = require("botbuilder-azure");
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
console.log('%s listening to %s', server.name, server.url);
});
var connector = new builder.ChatConnector({
appId: process.env.MicrosoftAppId,
appPassword: process.env.MicrosoftAppPassword,
openIdMetadata: process.env.BotOpenIdMetadata
});
server.post('/api/messages', connector.listen());
var tableName = 'botdata';
var azureTableClient = new botbuilder_azure.AzureTableClient(tableName, process.env['AzureWebJobsStorage']);
var tableStorage = new botbuilder_azure.AzureBotStorage({ gzipData: false }, azureTableClient);
var bot = new builder.UniversalBot(connector);
//bot.set('storage', new builder.MemoryBotStorage()); // Register in-memory state storage
bot.set('storage', tableStorage);
var luisAppId = process.env.LuisAppId;
var luisSubscriptionKey = process.env.LuisAPIKey;
var luisApiHostName = process.env.LuisApiHostName || 'westus.api.cognitive.microsoft.com';
var luisModelUrl = 'https://' + luisApiHostName + '/luis/v2.0/apps/' + luisAppId + '?subscription-key=' + luisSubscriptionKey;
var recognizer = new builder.LuisRecognizer(luisModelUrl);
var qnarecognizer = new cognitiveservices.QnAMakerRecognizer({
knowledgeBaseId: process.env.QnAKnowledgebaseId,
authKey: process.env.QnAAuthKey,
});
var intents = new builder.IntentDialog({ recognizers: [recognizer, qnarecognizer] });
bot.dialog('/', intents);
intents.matches('azureBotDevelopment', [
function (session, args, next) {
var answerEntity = builder.EntityRecognizer.findEntity(args.entities, 'answer');
session.send(answerEntity.entity);
}
]);
intents.onDefault([
function(session){
session.send('Sorry!! No match!!');
}
]);
当我在网络聊天中运行该漫游器时,对于每个问题,它都会回复Oops. Something went wrong and we need to start over.
在在线编辑器中,这会产生以下错误-
Error: QnA request returned a 404 code with body: [object Object]
at Request._callback (D:\home\site\wwwroot\node_modules\botbuilder-cognitiveservices\lib\QnAMakerRecognizer.js:98:37)
at Request.self.callback (D:\home\site\wwwroot\node_modules\request\request.js:185:22)
at emitTwo (events.js:106:13)
at Request.emit (events.js:191:7)
at Request.<anonymous> (D:\home\site\wwwroot\node_modules\request\request.js:1161:10)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
at IncomingMessage.<anonymous> (D:\home\site\wwwroot\node_modules\request\request.js:1083:12)
at IncomingMessage.g (events.js:291:16)
at emitNone (events.js:91:20)
at IncomingMessage.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:974:12)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickDomainCallback (internal/process/next_tick.js:122:9)
任何帮助将不胜感激。
答案 0 :(得分:0)
如下所示组织代码即可。我使用网络聊天和仿真器对此进行了测试,并获得了积极的结果。
从本质上讲,bot.recognizer的作用类似于将返回的LUIS意图传递给bot并将其与分配了相同值的任何触发器匹配的中间件。对于QnA,通过将关联的识别器传递到对话框中,可以在QnA知识库中匹配用户输入,然后返回匹配的响应。
var bot = new builder.UniversalBot(connector);
bot.set('storage', new builder.MemoryBotStorage());
var luisAppId = process.env.LuisAppId;
var luisAPIKey = process.env.LuisAPIKey;
var luisAPIHostName = process.env.LuisAPIHostName // 'westus.api.cognitive.microsoft.com';
const LuisModelUrl = 'https://' + luisAPIHostName + '/luis/v2.0/apps/' + luisAppId + '?subscription-key=' + luisAPIKey;
var luisRecognizer = new builder.LuisRecognizer(LuisModelUrl);
var qnarecognizer = new cognitiveservices.QnAMakerRecognizer({
knowledgeBaseId: process.env.QnAKnowledgebaseId,
authKey: process.env.QnAAuthKey,
endpointHostName: process.env.QnAEndpointHostName
});
var basicQnAMakerDialog = new cognitiveservices.QnAMakerDialog({
recognizers: [qnarecognizer],
defaultMessage: 'No match! Try changing the query terms!',
qnaThreshold: 0.3
});
bot.recognizer(luisRecognizer);
bot.dialog('/', basicQnAMakerDialog);
bot.dialog('GreetingDialog',[
(session) => {
session.send('You reached the Greeting intent. You said \'%s\'.', session.message.text);
builder.Prompts.text(session, "What is your name?");
},
(session, results) => {
session.userData.name = results.response;
session.send("Glad you could make it, " + session.userData.name);
builder.Prompts.text(session, "Ask me something!");
},
(session, results) => {
session.conversationData.question = results.response;
session.send(session.conversationData.question + " is an interesting topic!")
session.endDialog();
}
]).triggerAction({
matches: 'Greeting'
})
bot.dialog('HelpDialog',
(session) => {
session.send('You reached the Help intent. You said \'%s\'.', session.message.text);
session.endDialog();
}
).triggerAction({
matches: 'Help'
})
您应该意识到,v3 SDK将在2019年12月失去其支持。那时,Azure将不再提供新的v3机器人。我建议您考虑使用较新的v4 SDK构建机器人。您可以通过v4实现here了解更多信息。
希望有帮助!