使用 LUIS 的多语言机器人

Multi-language bot using LUIS

我正在尝试通过检测语言并选择正确的 LUIS 键和字符串集来创建多语言机器人。我的问题是,我的 LuisDialog 序列化了它自己并且不再调用 MakeRoot 方法。

我的代码(大致):

 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;
}

我试过使用中间对话框,它选择语言和 context.Forward LuisDialog 的所有内容,但我正在努力管理它。如果这是一个好策略,我可以分享更多代码。我也在考虑计分。

如果您的需要是在开始时切换 LUIS 对话框的语言

您必须制定一种方法来按语言获取每个 LUIS 参数,并且您知道 DetectLanguage 所使用的语言,请选择正确的语言。

然后将它们传递给您的 LuisDialog,如下所示:

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;
}

你的 RootDialog 应该是这样的:

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

如果您有更复杂的项目

我们实施了一个允许随时切换语言的复杂项目。由于这种可能性,activity 的区域设置字段无法完全信任,即使您在其处理开始时覆盖它也是如此。

策略如下:

  • 在 MessagesController 的 Post 方法中为每条传入消息检测用户的语言
  • 在更改时将用户语言存储在机器人状态 (userData) 中

然后:

  • 显示文本时,使用 userData 以正确的语言获取值
  • 使用 LUIS 等特定语言工具时,使用 userData 获取正确的参数

您将需要一个中间 RootDialog 来处理此 LUIS 语言切换,并且您必须在每次检测后完成 LuisDialog(或者在 LuisDialog 上的每个 MessageReceived 之前检查语言)。