jazzedge
8/1/2017 - 12:44 PM

Bot - Multiple LUIS models #1

Bot - Multiple LUIS models #1

// See: https://docs.microsoft.com/en-us/bot-framework/nodejs/bot-builder-nodejs-recognize-intent

// Add a global LUIS recognizer to the bot by using the endpoint URL of the LUIS app
// See: https://github.com/Microsoft/BotBuilder-Samples/tree/master/Node/intelligence-LUIS

var model = 'https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/c413b2ef-382c-45bd-8ff0-f76d60e2a821?subscription-key=6d0966209c6e4f6b835ce34492f3e6d9';
bot.recognizer(new builder.LuisRecognizer(model));

//Disambiguate between multiple intents
//See: https://github.com/Microsoft/BotBuilder/blob/master/Node/examples/feature-onDisambiguateRoute/app.js

//Your bot can register more than one recognizer. Notice that the custom recognizer example involves assigning
// a numerical score to each intent. This is done since your bot may have more than one recognizer, and the 
// Bot Builder SDK provides built-in logic to disambiguate between intents returned by multiple recognizers. 
//The score assigned to an intent is typically between 0.0 and 1.0, but a custom recognizer may define an intent 
//greater than 1.1 to ensure that that intent will always be chosen by the Bot Builder SDK's disambiguation logic.
//By default, recognizers run in parallel, but you can set recognizeOrder in IIntentRecognizerSetOptions so the 
//process quits as soon as your bot finds one that gives a score of 1.0.

//There is a limit of 80 for Intents and 30 for entities that can be defined for a LUIS app.

var recognizer1 = new builder.LuisRecognizer('<model 1>');
var recognizer2 = new builder.LuisRecognizer('<model 2>');
var intents = new builder.IntentDialog({ recognizers: [recognizer1, recognizer2] });

//Example #1 - See: https://stackoverflow.com/questions/41399989/how-to-have-luisrecognizer-with-a-separate-model-for-each-language

var model_en = 'https://api.projectoxford.ai/luis/v2.0/apps/{YOUR ENGLISH MODEL}';
var model_es = 'https://api.projectoxford.ai/luis/v2.0/apps/{YOUR SPANISH MODEL}';
var recognizer = new builder.LuisRecognizer({'en': model_en, 'es' : model_es});

//=========================================================
// Bots Dialogs
//=========================================================
var intents = new builder.IntentDialog({ recognizers: [recognizer] });

intents.matches('hello', function (session) {
    session.send('Hello!');
});

intents.matches('goodbye', function (session) {
    session.send('Goodbye!');
});

intents.matches('spanish', function (session) {
    session.send('Switching to Spanish Model');
    session.preferredLocale('es');
});

intents.matches('english', function (session) {
     session.send('Switching to English Model');
    session.preferredLocale('en');
});

intents.matches('None', function (session) {
    if (session.preferredLocale() == 'en')
    {
        session.send('I do not understand');
    }
    else
    {
        session.send('No entiendo');
    }
});


bot.dialog('/', intents);

#Example 2 - See: https://github.com/Microsoft/BotBuilder/issues/1565

intents.matches('SaveMoney', [
    var savings = builder.EntityRecognizer.findAllEntities(args.entities, 'builtin.number');
   var amount = session.dialogData.amount = {};
    function(session, args, next) {
        if (savings.length == 0) {
            builder.Prompts.text(session, 'How much money would you like to save?');
        } else {
            if (savings.length > 1) {
                    builder.Prompts.number(session, "Please enter a single amount. If you enter more than one amount, the first amount will be accepted");
            } else {
                amount.savings = savings[0].entity;
                next();
            }
        }
    },
  function(session, results)
    {
        var amount = session.dialogData.amount;
        var amountSaved = 0;
        if (results.response) {
            amountSaved = results.response;
            amount.savings = results.response;
        } else if (amount.savings) {
            amountSaved = amount.savings;
        }
        session.endDialog('Good choice to save ' + amountSaved);
    }]);