Azure Bot Framework V4 (NodeJS) - LUIS 识别 returns 错误?

Azure Bot Framework V4 (NodeJS) - LUIS recognizer returns error?

使用 Azure Bot Framework 和 LUIS.ai 识别用户意图。使用文本 returns 对端点执行获取请求 json 我期待的对象,但是使用内置的 Luis 识别器我收到以下错误:'无法读取 属性 'get' 的未定义'。从这里的文档来看:https://docs.microsoft.com/en-us/azure/cognitive-services/luis/luis-nodejs-tutorial-bf-v4 这似乎是正确的配置,所以我不确定出了什么问题。有什么想法吗?


const { ComponentDialog, DialogSet, DialogTurnStatus, WaterfallDialog, ChoicePrompt, TextPrompt } = require('botbuilder-dialogs');
const { TopLevelDialog, TOP_LEVEL_DIALOG } = require('./topLevelDialog');

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

const MAIN_DIALOG = 'MAIN_DIALOG';
const WATERFALL_DIALOG = 'WATERFALL_DIALOG';
const USER_PROFILE_PROPERTY = 'USER_PROFILE_PROPERTY';
const CHOICE_PROMPT = 'CHOICE_PROMPT';
const TEXT_PROMPT = 'TEXT_PROMPT';


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

        this.addDialog(new TextPrompt(TEXT_PROMPT));
        this.addDialog(new TopLevelDialog());
        this.addDialog(new WaterfallDialog(WATERFALL_DIALOG, [
            this.initialStep.bind(this),
            this.askIfFinishedStep.bind(this),
            this.finalStep.bind(this)
        ]));

        this.initialDialogId = WATERFALL_DIALOG;

        let luisConfig = {
            applicationId: '',
            endpointKey: '',
            endpoint: '',
        };

        this.Luis = new LuisRecognizer(
            luisConfig, 
            {
                includeAllIntents: true,
                log: true,
                staging: false            
            },
            true
            );        

    }

    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) {

        let luisAnalysis = await this.Luis.recognize(stepContext);

        let queryString = encodeURIComponent(stepContext.context._activity.text);

        /*
         Ignore this if statement - only in use with the get request 
        */
        if(luisResponse.data.topScoringIntent.intent === 'TrainingExpiry' && luisResponse.data.topScoringIntent.score > .75)
        {
            return await stepContext.beginDialog(TOP_LEVEL_DIALOG);
        }
        else 
        {
            await stepContext.context.sendActivity("I'm sorry, that is not supported at this time or a high enough intent was not acknowledged.");
            await stepContext.context.sendActivity("Top intent: " + luisResponse.data.topScoringIntent.intent + " Score: " + luisResponse.data.topScoringIntent.score);

            return await stepContext.next();
        }        
    }

    async askIfFinishedStep(stepContext) {
        const promptOptions = { prompt: 'Is there anything else I can assist you with?' };

        return await stepContext.prompt(TEXT_PROMPT, promptOptions);
    }

    async finalStep(stepContext) {

        if(stepContext.context._activity.text.toLowerCase() === 'no')
        {
            await stepContext.context.sendActivity("Good bye");

            return await stepContext.endDialog();
        }
        else 
        {
            return await stepContext.beginDialog(MAIN_DIALOG);
        }
    }
}

module.exports.MainDialog = MainDialog;
module.exports.MAIN_DIALOG = MAIN_DIALOG;

注意:问题出在我的参数被传递给识别器时,正如@billoverton 指出的那样。解决办法是传stepContext.context。

从 botbuilder-ai 模块查看 luisRecognizer.js,错误是因为识别器需要一个 turnContext(带有 turnState 属性)而您正在发送一个 stepContext。 stepContext 上不存在 turnState,因此 get 属性 失败并导致您的错误。如果您改为发送 stepContext.context,这将解决问题,即 let luisAnalysis = await this.Luis.recognize(stepContext.context);