如何根据Nodejs中LUIS识别的意图从对话框中实现瀑布对话框?

时间:2019-10-22 10:41:05

标签: node.js azure botframework chatbot luis

我有一个主对话框,其中只有一个步骤的瀑布对话框可以识别意图,并根据意图,我决定是否调用另一个对话框(这是具有3个步骤的另一个瀑布对话框)或直接回覆使用者,并回覆使用者的问候和取消等意图。以下是我的主对话框的代码-

const chalk = require('chalk')
const path = require('path');

const {
  ComponentDialog,
  DialogSet,
  DialogTurnStatus,
  WaterfallDialog
} = require('botbuilder-dialogs');
const {
  TopLevelDialog,
  TOP_LEVEL_DIALOG
} = require('./topLevelDialog');
const ENV_FILE = path.join(__dirname, '../.env');
require('dotenv').config({
  path: ENV_FILE
});

const {
  LuisRecognizer
} = require('botbuilder-ai');

const MAIN_DIALOG = 'MAIN_DIALOG';
const WATERFALL_DIALOG = 'WATERFALL_DIALOG';
const USER_PROFILE_PROPERTY = 'USER_PROFILE_PROPERTY';

const dispatchRecognizer = new LuisRecognizer({
  applicationId: process.env.LuisAppId,
  endpointKey: process.env.LuisAPIKey,
  endpoint: `https://${process.env.LuisAPIHostName}`
}, {
  includeAllIntents: true,
  includeInstanceData: true
}, true);

class MainDialog extends ComponentDialog {
  constructor(userState) {
    super(MAIN_DIALOG);
    this.userState = userState;
    this.userProfileAccessor = userState.createProperty(USER_PROFILE_PROPERTY);

    this.addDialog(new TopLevelDialog());
    this.addDialog(new WaterfallDialog(WATERFALL_DIALOG, [
      this.initialStep.bind(this),
    ]));
    this.initialDialogId = WATERFALL_DIALOG;
  }
  async run(turnContext, accessor) {
    const dialogSet = new DialogSet(accessor);
    dialogSet.add(this);
    const dialogContext = await dialogSet.createContext(turnContext);
    const results = await dialogContext.continueDialog();
    if (results.status === DialogTurnStatus.empty) {
      await dialogContext.beginDialog(this.id);
    }
  }
  async initialStep(stepContext) {
    const recognizerResult = await dispatchRecognizer.recognize(stepContext.context);
    const intent = LuisRecognizer.topIntent(recognizerResult);
    console.log(chalk.yellow(intent))
    await this.dispatchToTopIntentAsync(stepContext, intent);
    console.log(chalk.green('after dispatch'))
    return await stepContext.endDialog();
  }
  async dispatchToTopIntentAsync(stepContext, intent) {
    console.log(chalk.blue('in dispatch to top intent'))
    switch (intent) {
      case 'Greeting':
        console.log(chalk.red('greeting'))
        return await this.greeting(stepContext);
      default:
        console.log(`Dispatch unrecognized intent: ${ intent }.`);
        await stepContext.context.sendActivity(`Dispatch unrecognized intent: ${ intent }.`);
        return await stepContext.beginDialog(TOP_LEVEL_DIALOG);
    }
  }

  async greeting(stepContext) {
    return await stepContext.context.sendActivity(`Welcome! how can I help you`);
  }
}
module.exports.MainDialog = MainDialog;
module.exports.MAIN_DIALOG = MAIN_DIALOG;

在我的情况下,当我键入与该意图相关的任何查询并询问该瀑布的第一步(询问名称)时,即为TOP_LEVEL_DIALOG。 但是,当我输入我的名字时,它会从瀑布对话框中显示出来,并在LUIS将我的名字标识为“问候意图”时显示问候响应。

如何解决此类问题?

1 个答案:

答案 0 :(得分:0)

我不认为问题出在您的代码上,而是在您的LUIS模型上。如果输入名称并返回Greeting意向,则应首先查看Greeting意向的话语以及如何训练/发布LUIS模型。

您应该导航到LUIS.ai网站,并通过输入名称来测试模型并查看返回的结果。

enter image description here