Search code examples
botframework

Select LUIS model by luisRecognizer based on user' prefer locale in Version#4 node js


I have used couple of LUIS model in bot framework v#3 Node js based on user preferred locale without using the text translator api. Now I would like to implement same thing in version#4 as part of migration.

Below is code snippet written in version#3.

    var many_language_recognizer = new builder.LuisRecognizer({
      'en': englishModel || process.env.EN_LUIS,
      'es': spanishModel || process.env.ES_LUIS,
      'fr': frenchModel || process.env.FR_LUIS
    });

    bot.recognizer(many_language_recognizer);

I would like to implement above version#3 code snippet in v#4. Please help me out. thanks in advance.


Solution

  • You would simply need to:

    1. Choose a locale
    2. Select LUIS model credentials based off of chosen locale
    3. Create LuisRecognizer passing in the credentials

    .env

    So in your .env you might have multiple LUIS credentials (as you should create different LUIS models for different languages)

    EsLuisAppId=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
    EsLuisAPIKey=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    EsLuisAPIHostName=yourEsHostname
    
    DeLuisAppId=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
    DeLuisAPIKey=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    DeLuisAPIHostName=yourDeHostname
    

    In Your Bot

    You would have some logic to select the right set of credentials based off of the locale.

    if (locale == 'es-es') {
        luisApplication = {
            applicationId: process.env.EsLuisAppId,
            endpointKey: process.env.EsLuisAPIKey,
            endpoint: `https://${ process.env.EsLuisAPIHostName }.api.cognitive.microsoft.com`
        };
    }
    
    if (locale == 'de-de') {
        luisApplication = {
            applicationId: process.env.DeLuisAppId,
            endpointKey: process.env.DeLuisAPIKey,
            endpoint: `https://${ process.env.DeLuisAPIHostName }.api.cognitive.microsoft.com`
        };
    }
    

    Note: your logic matching locale to correct LUIS credential might be different (perhaps you'd prefer switch statement, etc.)

    Then once you set your luisApplication credentials, create your LuisRecognizer

    const recognizer = new LuisRecognizer(luisApplication);


    Example of LUIS in Action in V4

    If you'd like an example of LUIS in action in v4, I'd recommend checking out the 14.nlp-with-dispatch sample in the botbuilder-samples repo.

    It doesn't mess with different locales, but at least you can see how the LUIS recognizer is used in an up-to-date bot.


    What You Need to Decide: Where to Get the Locale

    So what's up to you is where you decide to get the locale. You can check out the unit tests in this PR regarding internationalization in NumberPrompts, a different feature in V4 bots, for some ideas (e.g. getting locale from activity, having a default locale, making the dialog require a locale, etc.)