web.xml 定义上下文_使用意图和上下文自定义Api.ai助手

web.xml 定义上下文

If you’re keen to learn more on AI, check out our screencast Microsoft Cognitive Services and the Text Analytics API, for AI sentiment in your bot.

如果您想了解有关AI的更多信息,请查看我们的截屏视频Microsoft Cognitive Services和Text Analytics API ,了解您机器人中的AI情绪。

Customizing Your Api.ai Assistant with Intent and Context

Api.ai is a really simple service that allows developers to create their own basic personal AI assistant/chatbot that works a bit like Siri and Amazon’s Alexa. I recently covered how to build your own AI assistant using Api.ai, where I showed the basics of setting up an AI assistant and teaching it some basic small talk. In this article, I’d like to go a step further and introduce “intents” and “contexts”, a way of teaching our AI assistants more specific actions that are personalized to our own needs. This is where things can get really exciting.

Api.ai是一项非常简单的服务,允许开发人员创建自己的基本个人AI助手/聊天机器人,其工作方式类似于Siri和亚马逊的Alexa。 最近,我介绍了如何使用Api.ai构建自己的AI助手 ,并在其中展示了设置AI助手并教授一些基本的闲聊的基础知识。 在本文中,我想更进一步,介绍“意图”和“上下文”,这是一种方法,可以教我们的AI助手一些更具体的操作,这些操作可以根据自己的需求进行个性化设置。 这是使事情变得真正令人兴奋的地方。

Note: this article was updated in 2017 to reflect recent changes to Api.ai.

注意:本文于2017年更新,以反映对Api.ai的最新更改。

使用Api.ai构建AI助手 (Building an AI assistant with Api.ai)

This post is one of a series of articles aimed to help you get a simple personal assistant running with Api.ai:

这篇文章是旨在帮助您获得一个使用Api.ai运行的简单个人助手的系列文章之一:

  1. How to Build Your Own AI Assistant Using Api.ai

    如何使用Api.ai构建自己的AI助手

  2. Customizing Your Api.ai Assistant with Intent and Context (this one!)

    使用意图和上下文自定义您的Api.ai助手(这一个!)
  3. Empowering Your Api.ai Assistant with Entities

    使用实体增强Api.ai助手的能力

  4. How to Connect Your Api.ai Assistant to the IoT

    如何将Api.ai助手连接到物联网

什么是意图? (What is an Intent?)

An intent is a concept that your assistant can be taught to understand and react to with a specific action. An intent contains a range of contexts that we can enter as sentences that the user might say to our assistant. A few examples could include “Order me lunch”, “Show me today’s daily Garfield comic strip”, “Send a random GIF to the SitePoint team on Slack”, “Cheer me up” and so on. Each of those would be custom intents which we could train our assistant to understand.

意图是一个概念,可以教您的助手理解特定动作并对其做出React。 目的包含一系列上下文,我们可以将这些上下文输入为用户可能对我们的助手说的句子。 例如,“为我订购午餐”,“向我展示今天的加菲猫每日漫画”,“在Slack上向SitePoint团队随机发送GIF”,“为我加油”等等。 所有这些都是自定义的意图,我们可以训练我们的助手来理解。

建立意图 (Creating an Intent)

To create an intent, log into the agent you’d like to add the new functionality to in the Api.ai Console Page and click on either the “Create Intent” button next to the “Intents” heading at the top of the page or the “Intents” plus icon in the left hand side menu:

要创建意图,请在Api.ai控制台页面中登录要添加新功能的代理 ,然后单击页面顶部“意图”标题旁边的“创建意图”按钮或左侧菜单中的“意图”加号图标:

Creating a new intent in Api.ai

The sample intent for this demo’s assistant is to teach our assistant to cheer people up when they’re feeling down with movie quotes, jokes and other things. To start, call the new intent “Cheer me up” and write your first trigger sentence underneath “User says”. The first sentence I’ve added below is “Cheer me up”. Hit the Enter key or click “Add” to add your sentence:

该演示助手的示例意图是教我们的助手在人们对电影报价,笑话和其他事物感到沮丧时为他们加油。 首先,请调用新的意图“为我加油”,并在“用户说”下写下您的第一个触发语句。 我在下面添加的第一句话是“振作起来”。 点击Enter键或单击“添加”添加句子:

Setting up our first intent

Typically, there’s a range of different ways we might say the same thing. To account for these, add in a range of statements that represent various ways a user might indicate they’d like cheering up, such as “Make me smile” and “I feel sad”:

通常,我们说同一件事的方式有很多种。 为了解决这些问题,请添加一系列表示用户可能表示愿意振作起来的方式的语句,例如“让我微笑”和“我感到难过”:

Cheer up sentences

Now there’s a range of sentences the assistant should understand, but you haven’t told it what action is expected when it hears them. To do so, create an “action”. The assistant will return “action” names back to your web app to allow it to respond.

现在,助理应该理解一系列的句子,但是您没有告诉它当听到它时应该采取什么行动。 为此,请创建一个“动作”。 助手将“动作”名称返回给您的Web应用程序以使其响应。

In this case, you won’t respond to the first action that’s been called “cheermeup”, but it will come in handy in future when responding to actions in your web app. I’d recommend always including action names for your intents.

在这种情况下,您不会响应被称为“ cheermeup”的第一个操作,但将来在响应Web应用程序中的操作时会派上用场。 我建议始终为您的意图添加动作名称。

Creating an action

You can add in parameters into your actions too, but I’ll cover that in detail within our next article on Api.ai!

您也可以在操作中添加参数,但是我将在下一篇有关Api.ai的文章中详细介绍!

语音回应指导 (Guiding Via Speech Response)

After your user has told the agent they’d like to be cheered up, you want to guide the conversation towards the user, telling the agent more about what they’d like. This helps provide the illusion of intelligence while limiting how much the chatbot needs to handle. To do so, you provide speech responses in the form of questions within the “Speech Response” section. For example, “Let’s cheer you up! Would you like a joke or a movie quote?”

在您的用户告诉代理商他们希望振作起来之后,您希望将对话引导给用户,从而告诉代理商更多他们想要的信息。 这有助于提供智能错觉,同时限制聊天机器人需要处理的数量。 为此,您可以在“语音回复”部分中以问题的形式提供语音回复。 例如,“让我们振作起来! 您要开个玩笑还是看电影?”

Our speech responses

Finally, click the “Save” button next to your intent name to save your progress.

最后,单击您的意图名称旁边的“保存”按钮以保存进度。

测试您的代理 (Testing Your Agent)

You can test out your new intent by typing a test statement into the test console on the right. Test it out by saying “Cheer me up”:

您可以通过在右侧的测试控制台中输入测试语句来测试您的新意图。 说出“为我加油”来测试一下:

Our first test

The agent responds back with one of your trained responses as intended. After you’ve provided the phrase once, Api.ai learns! It will then allow you to have variations on the phrasing of the statement. For example, “Make me smile please”, “Say something to make me smile” or “I feel sad right now” will result in your intent running too:

代理会根据您的要求提供经过培训的响应之一。 一旦提供了该短语,Api.ai就会学习! 然后,它将使您可以更改语句的措词。 例如,“请让我微笑”,“说点什么让我微笑”或“我现在感到难过”也将导致您的意图在运行:

A second test variation

These variations seemed to only trigger after I’d used the original phrase with the bot first. I’m not sure if it’s just a delay in how long it takes to generate understanding of similar phrases, but if a variation doesn’t work, try asking it the original statement first. If your variation is too different, you’ll need to add it into the context’s statements. The more statements you add here, the better our agent will be able to respond.

这些变化似乎只有在我首先将原始词组与漫游器一起使用后才会触发。 我不确定在理解相似短语所需的时间上是否只是一个延迟,但是如果变体不起作用,请尝试首先询问原始语句。 如果您的版本差异太大,则需要将其添加到上下文的语句中。 您在此处添加的语句越多,我们的代理商将越能做出响应。

One thing you might notice if you used a statement like “I’m sorry to hear that! How can I help you feel better?” is that it isn’t quite specific enough to guide the user. If they aren’t aware of the options of either “movie quote” or “joke”, then they might ask for something you haven’t covered! Over time, you can train up your agent to understand many other concepts. However, for now I’d recommend being specific with your questions!

如果您使用诸如“我很抱歉听到这个消息! 我如何帮助您感觉好些?” 指导用户不够具体。 如果他们不知道“电影报价”或“笑话”的选项,那么他们可能会要求您提供一些您未涵盖的内容! 随着时间的流逝,您可以训练您的代理以了解许多其他概念。 但是,现在建议您具体说明您的问题!

使用上下文 (Using Contexts)

By guiding the conversation with your speech response, your agent needs a way to follow what the conversation was about when the user next speaks to them. If a user says the words “A joke” or even “Either one” without any prior conversation, out of context that sentence might not be too clear for the agent to respond to. How would you respond if I talked up to you and just said “A joke”? That’d be what your assistant would be given at the moment, as it has no way of remembering where the conversation was leading previously.

通过用语音响应引导对话,您的代理需要一种方法来跟踪用户下次与之交谈时的对话内容。 如果用户在没有任何事先对话的情况下说出“开玩笑”甚至“任何一个”这两个词,那么从上下文出发,该句子可能不太清晰,代理无法响应。 如果我跟你说话,只是说“一个笑话”,你会如何回应? 那将是您当前助手要得到的,因为它无法记住对话的进行方向。

This is where setting contexts in Api.ai comes in. We create contexts to track what the user and agent have been speaking about. Without contexts, each sentence would be completely isolated from the one before it.

这就是在Api.ai中设置上下文的地方。我们创建上下文以跟踪用户和代理一直在谈论什么。 没有上下文,每个句子将与之前的句子完全隔离。

To create a context, click the “Define contexts” link at the top of the Api.ai console for your intent:

要创建上下文,请单击Api.ai控制台顶部的“定义上下文”链接以达到您的目的:

Opening the context options

Here you’ll have a section for input contexts and a section for output context. Input contexts tell the agent in which context the intent should be run. For your first intent, you want it to run any time, so leave input contexts blank. Output contexts are what set up an intent to be picked up in future messages. This is the one you want:

在这里,您将有一个用于输入上下文的部分和一个用于输出上下文的部分。 输入上下文告诉代理应该在哪个上下文中运行意图。 首先,您希望它可以随时运行,因此将输入上下文留空。 输出上下文是在将来的消息中建立意图的基础。 这是您想要的一个:

Input and output contexts

Now create an output context called “cheering-up”. When naming a context, Api.ai suggests alphanumeric names without spaces. Type in your context and hit the Enter key to add it. Then click “Save” to save your changes:

现在,创建一个名为“ cheering-up”的输出上下文。 命名上下文时,Api.ai建议使用字母数字名称,且不带空格。 键入您的上下文,然后按Enter键将其添加。 然后单击“保存”以保存您的更改:

Creating our output context

If you then test out your agent by asking them to “Cheer me up” once more, the result shows your context is now appearing too:

如果然后通过要求他们再次“为我加油”来测试您的代理,结果将显示您的上下文现在也出现了:

Our context in action

用上下文过滤意图 (Filtering Intents With Contexts)

Your agent now understands that there’s a conversation context of “cheering-up”. You can now set up an intent to run only if that context has occurred. As an example, create one possible response to your agent’s question — “A movie quote”. Go back to the menu on the left and click the plus icon to create a new intent:

您的代理现在可以理解,存在“振作起来”的对话环境。 现在,您可以设置一个仅在发生上下文时才运行的意图。 例如,为您的座席问题创建一个可能的答案-“电影报价”。 返回左侧菜单,然后单击加号图标以创建新的意图:

Creating our second intent

Call your intent “Movie quote” and set the input context to “cheering-up”. This tells your agent that they should only consider this response to our user if they have previously asked to be cheered up. We add a few sample ways the user might respond to say “I’d like a movie quote”:

将您的意图称为“电影报价”,并将输入上下文设置为“振作起来”。 这告诉您的代理,如果他们以前要求振作起来,他们应该只考虑对我们用户的答复。 我们添加了一些示例方式,用户可能会回答“我想要电影报价”:

Your custom movie quote intent

Then scroll down and in your response and include a range of movie quotes (feel free to include your own favorites):

然后向下滚动并在您的回复中加入一系列电影报价(可以随意添加自己的收藏夹):

Adding your movie quotes

Click “Save” next to your intent’s name once again to create your movie quote intent. Then in the test console beside it, try entering “Cheer me up” and follow it with “Movie quote”. The agent should now tell you a movie quote!

再次单击意图名称旁边的“保存”以创建电影引用意图。 然后,在旁边的测试控制台中,尝试输入“为我加油”,然后跟着“电影报价”。 代理商现在应该告诉您电影报价!

Your movie quote intent in action

You could then follow the same process to add a response for an “A joke” intent too.

然后,您也可以按照相同的过程为“笑话”意图添加响应。

You also don’t necessarily need to be limited to providing your agent with a list of hard coded responses. You could instead set an action name for each intent and respond to that action within your web app. This is another concept I’ll be covering in a future article! You can be prepared for future additions by giving your “Movie quote” intent an action called “cheermeup.moviequote” (the dot helps you ensure the action doesn’t get mixed up with any future generic “moviequote” action you add in).

您也不必仅限于为代理提供一系列硬编码的响应。 您可以为每个意图设置一个动作名称,然后在您的Web应用程序中对该动作进行响应。 这是我将在以后的文章中讨论的另一个概念! 您可以通过为“电影引用”意图提供一个名为“ cheermeup.movi​​equote”的动作来为将来的添加做准备(该点可帮助您确保该动作不会与您添加的任何将来的通用“ moviequote”动作混淆)。

行动中 (In Action)

If you added these intents into the same personal assistant used for your web app in the previous article, the new functionality should appear automatically! If you created a new one, you’ll need to update the API keys in your web app first. Open up your personal assistant web app in your web browser and try it out by asking your assistant to cheer you up:

如果您将这些意图添加到了上一篇文章中用于您的Web应用程序的同一个人助手中,那么新功能应该会自动出现! 如果创建了一个新密钥,则需要先更新Web应用程序中的API密钥。 在网络浏览器中打开您的个人助理网络应用,并通过要求您的助理为您加油来进行尝试:

Asking Barry to cheer me up

Then, tell it you’d like a movie quote and see what happens:

然后,告诉它您想要电影报价,然后看看会发生什么:

Following it up with asking for a movie quote

结论 (Conclusion)

There are plenty of ways to use the concepts of intents and contexts to personalize your assistant. Chances are you already have a few ideas in mind! There’s still more we can do to train up our Api.ai assistant by teaching it to recognize concepts (known as entities) within your custom intents, which we cover in the next article in this series!

有很多方法可以使用意图和上下文的概念来个性化您的助手。 您可能已经有了一些想法! 通过教我们识别您自定义意图中的概念(称为实体)的方法, 我们还有更多的方法可以训练Api.ai助手,这将在本系列的下一篇文章中介绍

If you’re following along and building your own personal assistant using Api.ai, I’d love to hear about it! What custom intents have you come up with? Let me know in the comments below, or get in touch with me on Twitter at @thatpatrickguy.

如果您正在跟随并使用Api.ai建立自己的私人助理,我很想听听它! 您提出了哪些自定义意图? 在下面的评论中让我知道,或者在Twitter上通过@thatpatrickguy与我联系

Give your AI the human touch with a sentiment tool. Check out our screencasts on the Microsoft Cognitive Services and the Text Analytics API.

使用情感工具让您的AI具有人性化的感觉。 查看我们有关Microsoft Cognitive Services和Text Analytics API的截屏视频。

翻译自: https://www.sitepoint.com/customizing-your-api-ai-assistant-with-intent-and-context/

web.xml 定义上下文

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值