简体   繁体   中英

Multi-language bot using LUIS

I'm trying to create a multilanguage bot by detecting the language and selecting the proper set of LUIS keys and strings. My problem is, that my LuisDialog serializes itself and the MakeRoot method is not being called anymore.

My code (roughly):

 public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
 {
    if (activity.Type == ActivityTypes.Message)
    {
        var languageData = DetectLanguage(activity); // here I have the keys, strings etc.

        await Conversation.SendAsync(activity, () => new Dialogs.RootDialog(languageData));
    }
    else
    {
        HandleSystemMessage(activity);
    }
    var response = Request.CreateResponse(HttpStatusCode.OK);
    return response;
}

I've tried with the intermediate dialog, which selects the language and context.Forward everything to the LuisDialog, but I'm struggling with managing that. If this is a good strategy, I can share more code. I'm also considering scoreables.

If your need is about switching the language of your LUIS Dialog at its start

You have to make a method to get each LUIS parameter by language and as you know the language with your DetectLanguage , choose the right ones.

Then pass them to your LuisDialog like the following:

public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
    if (activity.Type == ActivityTypes.Message)
    {
        var languageData = DetectLanguage(activity); // here I have the keys, strings etc.

        var luisService = new LuisService(new LuisModelAttribute("yourLuisAppIdGivenTheLanguageData", "yourLuisAppKeyGivenTheLanguageData", domain: "yourLuisDomainGivenTheLanguageData"));
        await Conversation.SendAsync(activity, () => new Dialogs.RootDialog(luisService));
    }
    else
    {
        HandleSystemMessage(activity);
    }
    var response = Request.CreateResponse(HttpStatusCode.OK);
    return response;
}

And your RootDialog shoud look like this:

public class RootDialog : LuisDialog<object>
{
    public RootDialog(params ILuisService[] services) : base(services)
    {
    }

If you have a more complex project

We implemented a complex project allowing language switch at any time. Due to this possibility, activity's locale field could not be fully trusted even if you override it on the beginning of its processing.

The strategy is the following:

  • Detect user's language in MessagesController's Post method, for every incoming message
  • Store the user language in bot state (userData) when it's changing

Then:

  • When displaying a text, get the value in the right language using userData
  • When using a language specific tool like LUIS, get the right parameter using userData

You will need an intermediate RootDialog to handle this LUIS language switch, and you have to complete your LuisDialog after every detection (or to check the language before every MessageReceived on your LuisDialog).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM