我正在尝试将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)



任何帮助将不胜感激。

最佳答案

像下面这样组织代码即可。我使用Web Chat和Emulator测试了此结果,并获得了积极的结果。

本质上,bot.recognizer的行为就像中间件,将返回的LUIS意图传递给机器人,并将其与分配了相同值的任何触发器匹配。对于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了解更多信息。

希望有帮助!

07-26 09:35