如何将用户文本直接传递给LUIS,即Pro选项,该选项不存在

时间:2018-04-30 14:39:38

标签: c# botframework luis

我一直致力于Bot框架,因为我创建了提示选择为是,不,我是独立的问题类型。但是,如果我输入任何不存在的文本选项List,那么我应该将用户文本传递给LUIS以达到相应的Intent。我试过通过在Catch块中获取用户文本但是(等待结果)没有获取用户文本。enter image description here

1 个答案:

答案 0 :(得分:1)

请考虑以下提示:

PromptDialog.Choice(context, this.ChoiceReceivedAsync, new List<string>() { "Choice 1", "Choice 2" }, "stuff and things");

在简历之后的方法中,您可以对用户选择应用逻辑,如:

    private Task ChoiceReceivedAsync(IDialogContext context, IAwaitable<string> result)
    {
       Activity a = context.Activity as Activity;
        switch (a.Text)
        {
            case "Choice 1":
                //do stuff
                break;
            case "Choice 2":
                //do stuff
                break;
            default:
                context.Forward(new Luis(), afterLuis, context.Activity, CancellationToken.None);
                break;
        }

        a = a.CreateReply("things");
        context.PostAsync(a);
        return Task.CompletedTask;
    }

这样,如果用户输入除选择之外的任何内容,它将被发送到Luis(),在我的情况下是public class Luis : LuisDialog<object>或Luis对话框来处理Luis调用。

您也可以这样做,调用LUIS API而不是使用LUIS对话框。

    private async Task ChoiceReceivedAsync(IDialogContext context, IAwaitable<string> result)
    {
       Activity a = context.Activity as Activity;
        switch (a.Text)
        {
            case "Choice 1":
                //do stuff
                break;
            case "Choice 2":
                //do stuff
                break;
            default:
                using (HttpClient client = new HttpClient())
                {
                    string RequestURI = "https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/YOUR_MODEL_ID?" +
                                        "subscription-key=YOUR_SUBSCRIPTION_KEY&amp;verbose=true&amp;timezoneOffset=0&amp;q=" +
                                        a.Text;

                    HttpResponseMessage msg = await client.GetAsync(RequestURI);
                    if (msg.IsSuccessStatusCode)
                    {
                        var JsonDataResponse = await msg.Content.ReadAsStringAsync();
                        LUISData luisData = JsonConvert.DeserializeObject<LUISData>(JsonDataResponse);
                    }
                }
                break;
        }
        a = a.CreateReply("things");
        await context.PostAsync(a);
    }

为了这样做,你必须添加这样的类来支持反序列化。

    public class LUISData
    {
        public string query { get; set; }
        public LUISIntent[] intents { get; set; }
        public LUISEntity[] entities { get; set; }
    }
    public class LUISEntity
    {
        public string Entity { get; set; }
        public string Type { get; set; }
        public string StartIndex { get; set; }
        public string EndIndex { get; set; }
        public float Score { get; set; }
    }
    public class LUISIntent
    {
        public string Intent { get; set; }
        public float Score { get; set; }
    }