问题
I have an existing waterfall conversation. I want to adapt it so that it can extract data from more complex user responses to the bot's questions.
In my LUIS app I have created an intent called GetLocation
which is trained to find an entity called Location
. An example of this is the user typing "I am looking in Bristol" which would match the entity "Bristol". This is what I currently have:
function(session) {
builder.Prompts.text(session, "Hello... Which city are you looking in?");
},
function(session, results) {
session.privateConversationData.city = results.response;
builder.Prompts.number(session, "Ok, you are looking in " + results.response + ", How many bedrooms are you looking for?");
},
etc...
Instead of simply storing the response string, I want to send the response string off to LUIS and extract the city location from it. All of the LUIS examples I've found are for matching and going to new Intents however I simply want to keep the waterfall conversation going. How would I utilise LUIS to do this?
回答1:
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.
来源:https://stackoverflow.com/questions/42489733/microsoft-bot-framework-luis-in-waterfall-conversation