带有路易斯意图的链



,因此他们在Echobot样本中有一个很好的示例以演示链条

 public static readonly IDialog<string> dialog = Chain.PostToChain()
        .Select(msg => msg.Text)
        .Switch(
            new Case<string, IDialog<string>>(text =>
                {
                    var regex = new Regex("^reset");
                    return regex.Match(text).Success;
                }, (context, txt) =>
                {
                    return Chain.From(() => new PromptDialog.PromptConfirm("Are you sure you want to reset the count?",
                    "Didn't get that!", 3)).ContinueWith<bool, string>(async (ctx, res) =>
                    {
                        string reply;
                        if (await res)
                        {
                            ctx.UserData.SetValue("count", 0);
                            reply = "Reset count.";
                        }
                        else
                        {
                            reply = "Did not reset count.";
                        }
                        return Chain.Return(reply);
                    });
                }),
            new RegexCase<IDialog<string>>(new Regex("^help", RegexOptions.IgnoreCase), (context, txt) =>
                {
                    return Chain.Return("I am a simple echo dialog with a counter! Reset my counter by typing "reset"!");
                }),
            new DefaultCase<string, IDialog<string>>((context, txt) =>
                {
                    int count;
                    context.UserData.TryGetValue("count", out count);
                    context.UserData.SetValue("count", ++count);
                    string reply = string.Format("{0}: You said {1}", count, txt);
                    return Chain.Return(reply);
                }))
        .Unwrap()
        .PostToUser();
}

但是,我不想使用路易斯的意图来确定我的对话路径。我正在使用这个不错的代码来提取路易斯的意图。

public static async Task<LUISQuery> ParseUserInput(string strInput)
    {
        string strRet = string.Empty;
        string strEscaped = Uri.EscapeDataString(strInput);
        using (var client = new HttpClient())
        {
            string uri = Constants.Keys.LUISQueryUrl + strEscaped;
            HttpResponseMessage msg = await client.GetAsync(uri);
            if (msg.IsSuccessStatusCode)
            {
                var jsonResponse = await msg.Content.ReadAsStringAsync();
                var _Data = JsonConvert.DeserializeObject<LUISQuery>(jsonResponse);
                return _Data;
            }
        }
        return null;
    }

现在不幸的是,因为这是异步的,它在运行案例语句的LINQ查询方面效果不佳。谁能为我提供一些代码,可以让我根据路易斯意图在链条内有一个案例语句?

omg在他的评论中是正确的。

请记住,盲生族具有类型,含义,idialog可以返回自己指定的类型的对象:

public class TodoItemDialog : IDialog<TodoItem>
{
   // Somewhere, you'll call this to end the dialog
   public async Task FinishAsync(IDialogContext context, IMessageActivity activity)
   {
      var todoItem = _itemRepository.GetItemByTitle(activity.Text);
      context.Done(todoItem);
   }
}

呼叫 context.done()返回对话框要返回的对象。无论您正在阅读任何类型的didialog

的课程声明
public class TodoItemDialog : LuisDialog<TodoItem>

它有助于阅读为:

" todoitemdialog是一个对话框,它完成后返回待遇"

您可以使用 context.forward(),它基本上将相同的messageactitive转发到另一个对话框类别。

context.forward() and context.call()之间的区别在本质上是 context.forward()允许您转发 messageaCtivity 立即由称为对话框处理,而 context.call()只是启动一个新的对话框,而无需交出任何对话框。

从您的" root"对话框中,如果您需要使用路易斯来确定意图并返回特定对象,则可以通过使用 forward forward 对其进行messageactitive >然后在指定的回调中处理结果:

await context.Forward(new TodoItemDialog(), AfterTodoItemDialogAsync, messageActivity, CancellationToken.None);
private async Task AfterTodoItemDialogAsync(IDialogContext context, IAwaitable<TodoItem> result)
{
    var receivedTodoItem = await result;
    // Continue conversation
}

最后,您的Luisdialog类看起来像这样:

[Serializable, LuisModel("[ModelID]", "[SubscriptionKey]")]
public class TodoItemDialog : LuisDialog<TodoItem>
{
    [LuisIntent("GetTodoItem")]
    public async Task GetTodoItem(IDialogContext context, LuisResult result)
    {
        await context.PostAsync("Working on it, give me a moment...");
        result.TryFindEntity("TodoItemText", out EntityRecommendation entity);
        if(entity.Score > 0.9)
        {
            var todoItem = _todoItemRepository.GetByText(entity.Entity);
            context.Done(todoItem);
        }
    }
}

(从示例中,我没有其他陈述,这当然是您需要添加的)

最新更新