Microsoft Bot Framework LUIS in waterfall conversation

情到浓时终转凉″ 提交于 2019-12-05 20:04:26

I think you can do this by having two different dialogs setup:

Dialog 1:

This is the dialog you have above, your normal Waterfall dialog that drives the conversation.

Dialog 2:

This dialog will be created with a LUIS Intent recognizer using your LUIS model. Dialog 1 will issue the prompt, then pass the user to this dialog and parse the text entered by the user. Since your Model is already trained to recognize location, all you need to do now is extract the entity.

After dialog 2 has parsed the location information using LUIS, and extracted the entity, you will end the dialog and return the entity (location) back to dialog 1, which will still be on the Dialog Stack.


Code

//create intent recognizer based on LUIS model
var luisModel = "<Your LUIS Model URL>";
var recognizer = new botbuilder.LuisRecognizer(luisModel);
//create dialog handler for info to be parsed by LUIS
var dialog = new botbuilder.IntentDialog({ recognizers: [recognizer] });

//root dialog
bot.dialog("/", [
    function(session){

        //prompt user and pop LUIS intent dialog onto dialog stack
        session.send("Hello, which city are you looking in?");
        session.beginDialog("/begin_loc_parse");

    },

    //this will be resumed after our location has been extracted
    function(session, results){

        //check for extracted location
        if(results.entity){
            //got location successfully
            session.send("Got city from user: " + results.entity);

            //resume normal waterfall with location.....

        } else {
            //start over
            session.beginDialog("/");
        }
    }
]);

//LUIS intent dialog
dialog.matches("input_location", function(session, args){

    //grab location entity
    var city = botbuilder.EntityRecognizer.findEntity(args.entities, "builtin.geography.city");

    if(city){
        //pop the LUIS dialog off of the dialog stack
        //and return the extracted location back to waterfall
        session.endDialogWithResult(city);
    } else session.endDialog("Couldn't extract city entity.");

});

//called if user doesn't enter something like "I am looking in [city]"
dialog.onDefault(function(session, args){
    session.send("I'm sorry, I didn't quite catch that. In which city are you looking?");
});

So basically, in the root dialog, when you prompt the user for the location, and then call session.beginDialog("/begin_loc_parse") you will have passed the conversation to your LUIS intent dialog.

Any text entered by the user after this point will be interpreted by your LUIS model. This allows you to use your model to recognize and extract the location information from the user.

Then, the key is to use session.endDialogWithResult()to pop the LUIS dialog off the stack, and to go back to your original waterfall with your newly extracted location.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!