我试图在本地(在Azure中创建)的机器人中使用调度模型将LUIS和知识库集成到由Node.js开发的单个机器人中。
我使用了Github上可用的示例代码来获得每个意图的响应。
我使用Visual Studio Code将该机器人部署回了Azure,但是在门户网站上,网络聊天不起作用。它只会继续加载,说Waiting for the bot to be ready
。
这里是dispatchBot.js
文件代码-
const { ActivityHandler } = require('botbuilder');
const { LuisRecognizer, QnAMaker } = require('botbuilder-ai');
class DispatchBot extends ActivityHandler {
/**
* @param {any} logger object for logging events, defaults to console if none is provided
*/
constructor(logger) {
super();
if (!logger) {
logger = console;
logger.log('[DispatchBot]: logger not passed in, defaulting to console');
}
const dispatchRecognizer = new LuisRecognizer({
applicationId: process.env.LuisAppId,
endpointKey: process.env.LuisAPIKey,
endpoint: `https://${ process.env.LuisAPIHostName }.api.cognitive.microsoft.com`
}, {
includeAllIntents: true,
includeInstanceData: true
}, true);
const qnaMaker = new QnAMaker({
knowledgeBaseId: process.env.QnAKnowledgebaseId,
endpointKey: process.env.QnAAuthKey,
host: process.env.QnAEndpointHostName
});
this.logger = logger;
this.dispatchRecognizer = dispatchRecognizer;
this.qnaMaker = qnaMaker;
this.onMessage(async (context, next) => {
this.logger.log('Processing Message Activity.');
// First, we use the dispatch model to determine which cognitive service (LUIS or QnA) to use.
const recognizerResult = await dispatchRecognizer.recognize(context);
// Top intent tell us which cognitive service to use.
const intent = LuisRecognizer.topIntent(recognizerResult);
// Next, we call the dispatcher with the top intent.
await this.dispatchToTopIntentAsync(context, intent, recognizerResult);
await next();
});
this.onMembersAdded(async (context, next) => {
const welcomeText = 'Type a greeting or a question about the weather to get started.';
const membersAdded = context.activity.membersAdded;
for (let member of membersAdded) {
if (member.id !== context.activity.recipient.id) {
await context.sendActivity(`Welcome to Dispatch bot ${ member.name }. ${ welcomeText }`);
}
}
// By calling next() you ensure that the next BotHandler is run.
await next();
});
}
async dispatchToTopIntentAsync(context, intent, recognizerResult) {
switch (intent) {
case 'l_luis':
await this.processLuis(context, recognizerResult.luisResult);
break;
case 'q_sample-qna':
await this.processSampleQnA(context);
break;
default:
this.logger.log(`Dispatch unrecognized intent: ${ intent }.`);
await context.sendActivity(`Dispatch unrecognized intent: ${ intent }.`);
break;
}
}
async processLuis(context, luisResult) {
this.logger.log('processLuis');
// Retrieve LUIS result for Process Automation.
const result = luisResult.connectedServiceResult;
const intent = result.topScoringIntent.intent;
await context.sendActivity(`Luis top intent ${ intent }.`);
await context.sendActivity(`Luis intents detected: ${ luisResult.intents.map((intentObj) => intentObj.intent).join('\n\n') }.`);
if (luisResult.entities.length > 0) {
await context.sendActivity(`Luis entities were found in the message: ${ luisResult.entities.map((entityObj) => entityObj.entity).join('\n\n') }.`);
}
}
async processSampleQnA(context) {
this.logger.log('processSampleQnA');
const results = await this.qnaMaker.getAnswers(context);
if (results.length > 0) {
await context.sendActivity(`${ results[0].answer }`);
} else {
await context.sendActivity('Sorry, could not find an answer in the Q and A system.');
}
}
}
module.exports.DispatchBot = DispatchBot;
这是app.js
文件代码-
const path = require('path');
const restify = require('restify');
var { BotFrameworkAdapter} = require('botbuilder');
const { DispatchBot } = require('./bots/dispatchBot');
var botbuilder_azure = require('botbuilder-azure');
const ENV_FILE = path.join(__dirname, '.env');
require('dotenv').config({ path: ENV_FILE });
const connector = new BotFrameworkAdapter({
appId: process.env.MicrosoftAppId,
appPassword: process.env.MicrosoftAppPassword
});
connector.onTurnError = async (context, error) => {
console.error(`\n [onTurnError]: ${ error }`);
await context.sendActivity(`Oops. Something went wrong!`);
};
var tableName = 'botdata';
var azureTableClient = new botbuilder_azure.AzureTableClient(tableName, process.env['AzureWebJobsStorage']);
var tableStorage = new botbuilder_azure.AzureBotStorage({ gzipData: false }, azureTableClient);
// Pass in a logger to the bot. For this sample, the logger is the console, but alternatives such as Application Insights and Event Hub exist for storing the logs of the bot.
const logger = console;
// Create the main dialog.
let bot = new DispatchBot(logger);
let server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function() {
console.log(`\n${ server.name } listening to ${ server.url }`);
});
server.post('/api/messages', (req, res) => {
// Route received a request to adapter for processing
connector.processActivity(req, res, async (turnContext) => {
// route to bot activity handler.
await bot.run(turnContext);
});
});
当我尝试在Visual Studio代码上运行此bot时,程序成功执行,并且我得到了Restify端口地址。我无法在模拟器中进行测试,因为由于模拟器中的某些错误而无法正常工作。 任何帮助或建议都将非常有帮助。