ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

c# – 具有LUIS意图的链

2019-07-11 09:06:55  阅读:137  来源: 互联网

标签:c botframework luis


所以他们在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();
}

但是,我宁愿使用LUIS Intent而不是使用REGEX来确定我的会话路径.我正在使用这段漂亮的代码来提取LUIS Intent.

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查询不能很好地运行case语句.任何人都可以为我提供一些代码,允许我根据LUIS Intents在我的链中包含一个case语句吗?

解决方法:

Omg在他的评论中是正确的.

请记住,IDialogs有一个TYPE,意思是,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()的调用返回您的Dialog要返回的对象.当你在阅读任何类型的IDialog的类声明时

public class TodoItemDialog : LuisDialog<TodoItem>

它有助于将其读作:

“TodoItemDialog是一个Dialog类,它在完成后返回一个TodoItem”

您可以使用context.Forward()而不是链接,它基本上将相同的messageActivity转发到另一个对话框类.

context.Forward()和context.Call()之间的区别主要在于context.Forward()允许你转发一个messageActivity,它由被调用的对话框立即处理,而context.Call()只是启动一个新的对话框,没有移交任何东西.

在“根”对话框中,如果需要使用LUIS来确定意图并返回特定对象,则只需使用Forward将messageActivity转发给它,然后在指定的回调中处理结果:

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

(为简洁起见,我在示例中没有ELSE语句,这是您当然需要添加的内容)

标签:c,botframework,luis
来源: https://codeday.me/bug/20190711/1430580.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有