如何将邮件重定向到相应的LUIS应用程序

时间:2017-11-08 11:25:13

标签: c# botframework luis

我有一个带有LuisDialog根的机器人和另外4个带有不同LUIS模型的LuisDialog。对话开始后here我实施了类似的DialogFactory策略。

当用户在我的根对话框中发送与“无”意图匹配的问题时,我会评估其余的对话框,直到找到匹配项,然后将该消息转发给“获胜者”。

我遇到的问题是我在查询LUIS (BaseDialog类)时遇到 http错误:429(请求太多)。

关于如何面对这个问题的任何想法?

我的根对话框中的“无”意图:

    [LuisIntent("None")]
public async Task None(IDialogContext context, IAwaitable<IMessageActivity> message, LuisResult result)
{
    var activity = await message;

    var factory = new DialogFactory();
    BaseDialog<object> dialog = await factory.Create(result.Query);
    if (dialog != null)
    {
        await context.Forward(dialog, EndDialog, activity, CancellationToken.None);
    }
    else
    {
        await context.PostAsync("No results!");
    }
}

public static async Task EndDialog(IDialogContext context, IAwaitable<object> result)
{
    //...
}

DialogFactory类:

    using System;
using System.Collections.Generic;
using System.Configuration;
using System.Threading.Tasks;

namespace CodeBot.Dialogs
{
    public class DialogFactory
    {
        private static object _lock = new object();
        private static List<BaseDialog<object>> Dialogs { get; set; }

        public async Task<BaseDialog<object>> Create(string query)
        {
            query = query.ToLowerInvariant();
            EnsureDialogs();
            foreach (var dialog in Dialogs)
            {
                if (await dialog.CanHandle(query))
                {
                    return dialog;
                }
            }
            return null;
        }

        private void EnsureDialogs()
        {
            if (Dialogs == null || (Dialogs.Count != 4))
            {
                lock (_lock)
                {
                    if (Dialogs == null)
                    {
                        Dialogs = new List<BaseDialog<object>>();
                    }
                    else if (Dialogs.Count != 4)
                    {
                        Dialogs.Clear();
                    }

                    Dialogs.Add((BaseDialog<object>)Activator.CreateInstance(typeof(Dialog1));
                    Dialogs.Add((BaseDialog<object>)Activator.CreateInstance(typeof(Dialog2));
                    Dialogs.Add((BaseDialog<object>)Activator.CreateInstance(typeof(Dialog3));
                    Dialogs.Add((BaseDialog<object>)Activator.CreateInstance(typeof(Dialog4));
                }
            }
        }
    }
}

最后,BaseDialog类(我收到错误):

    using Microsoft.Bot.Builder.Dialogs;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Luis;
using System;

namespace CodeBot.Dialogs
{
    [Serializable]
    public class BaseDialog<R> : LuisDialog<R>
    {
        public LuisModelAttribute Luis_Model { get; private set; }

        public BaseDialog(LuisModelAttribute luisModel) : base(new LuisService(luisModel))
        {
            Luis_Model = luisModel;
        }

        public async Task<bool> CanHandle(string query)
        {
            try
            {
                var tasks = services.Select(s => s.QueryAsync(query, CancellationToken.None)).ToArray();
                var results = await Task.WhenAll(tasks);  <-- Error!!!

                var winners = from result in results.Select((value, index) => new { value, index })
                              let resultWinner = BestIntentFrom(result.value)
                              where resultWinner != null
                              select new LuisServiceResult(result.value, resultWinner, this.services[result.index]);

                var winner = this.BestResultFrom(winners);

                return winner != null && !winner.BestIntent.Intent.Equals(Constants.NONE, StringComparison.InvariantCultureIgnoreCase);
            }
            catch(Exception e)
            {
                System.Diagnostics.Debug.WriteLine($"CanHandle error: {e.Message}");
                return false;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

429错误是由于您的应用程序(密钥)过度使用LUIS API造成的。

您需要限制请求以确保您保持低于免费套餐的阈值,或升级到Basic plan which allows 50 requests a second