{"id":610,"date":"2020-11-24T08:04:38","date_gmt":"2020-11-24T08:04:38","guid":{"rendered":"https:\/\/machine-learning.webcloning.com\/2020\/11\/24\/building-natural-conversation-flows-using-context-management-in-amazon-lex\/"},"modified":"2020-11-24T08:04:38","modified_gmt":"2020-11-24T08:04:38","slug":"building-natural-conversation-flows-using-context-management-in-amazon-lex","status":"publish","type":"post","link":"https:\/\/salarydistribution.com\/machine-learning\/2020\/11\/24\/building-natural-conversation-flows-using-context-management-in-amazon-lex\/","title":{"rendered":"Building natural conversation flows using context management in Amazon Lex"},"content":{"rendered":"<div id=\"\">\n<p><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-18758 aligncenter\" src=\"https:\/\/d2908q01vomqb2.cloudfront.net\/f1f836cb4ea6efb2a0b1b99f41ad8b103eff4b59\/2020\/11\/21\/Context-Image.jpg\" alt=\"\" width=\"800\" height=\"304\"><br \/>Understanding the direction and context of an ever-evolving conversation is beneficial to building natural, human-like conversational interfaces. Being able to classify utterances as the conversation develops requires managing context across multiple turns. Consider a caller who asks their financial planner for insights regarding their monthly expenses: \u201cWhat were my expenses this year?\u201d They may also ask for more granular information, such as \u201cHow about for last month?\u201d As the conversation progresses, the bot needs to understand if the context is changing and adjust its responses accordingly.<\/p>\n<p>Amazon Lex is a service for building conversational interface in voice and text. Previously, you had to write code to manage context via session attributes. Depending on the intent, the code had to orchestrate the invocation of the next intent. As the conversation complexity and the intent count increased, managing the orchestration could become more cumbersome.<\/p>\n<p>Starting today, <a href=\"https:\/\/aws.amazon.com\/lex\" target=\"_blank\" rel=\"noopener noreferrer\">Amazon Lex<\/a> supports context management natively, so you can manage the context directly without the need for custom code. As initial prerequisite intents are filled, you can create <em>contexts<\/em> to invoke related intents. This simplifies bot design and expedites the creation of conversational experiences.<\/p>\n<h2>Use case<\/h2>\n<p>This post uses the following conversation to model a bot for financial planning:<\/p>\n<p><strong>User:<\/strong>\u00a0\u00a0\u00a0\u00a0What was my income in August?<br \/><strong>Agent:<\/strong>\u00a0\u00a0Your income in August was $2345.<br \/><strong>User:<\/strong>\u00a0\u00a0\u00a0\u00a0Ok. How about September?<br \/><strong>Agent:<\/strong>\u00a0\u00a0Your income in September was $4567.<br \/><strong>User:<\/strong>\u00a0 \u00a0 What were my expenses in July?<br \/><strong>Agent:<\/strong>\u00a0 Your expenses for July were $123.<br \/><strong>User:\u00a0\u00a0\u00a0\u00a0<\/strong>Ok thanks.<span class=\"c-message__edited_label\" dir=\"ltr\" data-sk=\"tooltip_parent\">\u00a0<\/span><\/p>\n<h2>Building the Amazon Lex bot FinancialPlanner<\/h2>\n<p>In this post, we build an Amazon Lex bot called <code>FinancialPlanner<\/code>, which is available for <a href=\"https:\/\/aws-ml-blog.s3.amazonaws.com\/artifacts\/lex-context-management\/ContextDemo.zip\" target=\"_blank\" rel=\"noopener noreferrer\">download<\/a>. Complete the following steps:<\/p>\n<ol>\n<li>Create the following intents:\n<ol type=\"a\">\n<li>\n<strong>ExpensesIntent<\/strong> \u2013 Elicits information, such as account ID and period, and provides expenses detail<\/li>\n<li>\n<strong>IncomeIntent<\/strong> \u2013 Elicits information, such as account ID and period, and provides income detail<\/li>\n<li>\n<strong>ExpensesFollowup<\/strong> \u2013 Invoked after expenses intent to respond to a follow-up query, such as \u201cHow about <em>[expenses] <\/em>last month?\u201d<\/li>\n<li>\n<strong>IncomeFollowup<\/strong> \u2013 Invoked after income intent to respond to a follow-up query about income, such as \u201cHow about <em>[income]<\/em> last month?\u201d<\/li>\n<li>\n<strong>Fallback<\/strong> \u2013 Captures any input that the bot can\u2019t process by the configured intents<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<ol start=\"2\">\n<li>Set up context tags for the expenses intents.<\/li>\n<\/ol>\n<p>The context management feature defines input tags and output tags that the bot developer can set. You use these tags to manage the conversation flow. For our use case, we set expenses as the output context tag in <code>ExpensesIntent<\/code>. We also use this as the input context for <code>ExpensesFollowupIntent<\/code>. We can also configure the output tag with a timeout, measured by conversation turns or seconds since the initial intent was invoked.<\/p>\n<p>The following screenshot shows the <strong>Context<\/strong> configuration section on the Amazon Lex console.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-18723 size-full\" src=\"https:\/\/d2908q01vomqb2.cloudfront.net\/f1f836cb4ea6efb2a0b1b99f41ad8b103eff4b59\/2020\/11\/20\/Building-natural-conversation-1.jpg\" alt=\"\" width=\"800\" height=\"277\"><\/p>\n<p>The following screenshot shows the specific parameters for the <code>expenses<\/code> tag.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-18722\" src=\"https:\/\/d2908q01vomqb2.cloudfront.net\/f1f836cb4ea6efb2a0b1b99f41ad8b103eff4b59\/2020\/11\/20\/Building-natural-conversation-2.jpg\" alt=\"\" width=\"800\" height=\"478\"><\/p>\n<ol start=\"3\">\n<li>Set up context tags for the income intents.<\/li>\n<\/ol>\n<p>Similar to expenses, we now set the context for income intents. For <code>IncomeIntent<\/code><strong>,<\/strong> set the output context tag as income. We use this context as the input context for <code>IncomeFollowupIntent<\/code>.<\/p>\n<ol start=\"4\">\n<li>Build the bot and test it on the Amazon Lex console.<\/li>\n<\/ol>\n<p>To test the bot, provide the input \u201cWhat were my expenses this year\u201d followed by \u201cHow about last month?\u201d For the second request, the bot selects <code>ExpensesFollowupIntent<\/code> because the expenses context is active. Alternatively, if you start with \u201cWhat was my income this year?\u201d followed by \u201cHow about last year?\u201d, the bot invokes the <code>IncomeFollowupIntent<\/code> because the <code>income<\/code> context is active.<\/p>\n<p>The following screenshot illustrates how the context tags are used to invoke the appropriate intent.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-18724\" src=\"https:\/\/d2908q01vomqb2.cloudfront.net\/f1f836cb4ea6efb2a0b1b99f41ad8b103eff4b59\/2020\/11\/20\/Building-natural-conversation-3.jpg\" alt=\"\" width=\"800\" height=\"523\"><\/p>\n<p>You can configure the behavior of the context attributes by editing the threshold. Editing the number of turns sets the limit for the number of interactions with the bot, and the number of seconds is from the original input tag being set. As long as the intent with the output tag occurs before the turn- or time-based timeout, the user can invoke the intent based on the input context.<\/p>\n<p>Along with the context management feature, you can also set default slot values. You can set the slots to populate from a context, a session attribute, or a value. In our sample bot model, the <code>{month}<\/code> slot in <code>ExpensesIntent<\/code> is set to August as the default slot value.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-18725\" src=\"https:\/\/d2908q01vomqb2.cloudfront.net\/f1f836cb4ea6efb2a0b1b99f41ad8b103eff4b59\/2020\/11\/20\/Building-natural-conversation-4.jpg\" alt=\"\" width=\"800\" height=\"247\"><\/p>\n<h2>Conclusion<\/h2>\n<p>With the new Amazon Lex context management feature, you can easily orchestrate when to enable intents based on prior intents, and pass specific user data values from one intent to another. This capability allows you to create sophisticated, multi-turn conversational experiences without having to write custom code. Context carry-over, along with default slot values, simplifies bot development and allows you to easily create more natural, conversational user experiences. For more information, see <a href=\"https:\/\/docs.aws.amazon.com\/lex\/latest\/dg\/context-mgmt-active-context.html\" target=\"_blank\" rel=\"noopener noreferrer\">Setting Intent Context documentation<\/a>.<\/p>\n<p>\u00a0<\/p>\n<hr>\n<h3>About the Authors<\/h3>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-17628 alignleft\" src=\"https:\/\/d2908q01vomqb2.cloudfront.net\/f1f836cb4ea6efb2a0b1b99f41ad8b103eff4b59\/2020\/10\/29\/blake.png\" alt=\"\" width=\"100\" height=\"100\"><strong>Blake DeLee<\/strong> is a Rochester, NY-based conversational AI consultant with AWS Professional Services. He has spent five years in the field of conversational AI and voice, and has experience bringing innovative solutions to dozens of Fortune 500 businesses. Blake draws on a wide-ranging career in different fields to build exceptional chatbot and voice solutions.<\/p>\n<p>\u00a0<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-18731 alignleft\" src=\"https:\/\/d2908q01vomqb2.cloudfront.net\/f1f836cb4ea6efb2a0b1b99f41ad8b103eff4b59\/2020\/11\/20\/Harshal-Pimpalkhute.jpg\" alt=\"\" width=\"100\" height=\"109\">As a Product Manager on the Amazon Lex team,\u00a0<strong>Harshal Pimpalkhute<\/strong>\u00a0spends his time trying to get machines to engage (nicely) with humans.<\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-18733 alignleft\" src=\"https:\/\/d2908q01vomqb2.cloudfront.net\/f1f836cb4ea6efb2a0b1b99f41ad8b103eff4b59\/2020\/11\/20\/Esther-Lee.jpg\" alt=\"\" width=\"100\" height=\"132\"><strong>Esther Lee\u00a0<\/strong>is a Product Manager for AWS Language AI Services. She is passionate about the intersection of technology and education. Out of the office, Esther enjoys long walks along the beach, dinners with friends and friendly rounds of Mahjong.<\/p>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/aws.amazon.com\/blogs\/machine-learning\/building-natural-conversation-flows-using-context-management-in-amazon-lex\/<\/p>\n","protected":false},"author":0,"featured_media":611,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/salarydistribution.com\/machine-learning\/wp-json\/wp\/v2\/posts\/610"}],"collection":[{"href":"https:\/\/salarydistribution.com\/machine-learning\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/salarydistribution.com\/machine-learning\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/salarydistribution.com\/machine-learning\/wp-json\/wp\/v2\/comments?post=610"}],"version-history":[{"count":0,"href":"https:\/\/salarydistribution.com\/machine-learning\/wp-json\/wp\/v2\/posts\/610\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/salarydistribution.com\/machine-learning\/wp-json\/wp\/v2\/media\/611"}],"wp:attachment":[{"href":"https:\/\/salarydistribution.com\/machine-learning\/wp-json\/wp\/v2\/media?parent=610"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/salarydistribution.com\/machine-learning\/wp-json\/wp\/v2\/categories?post=610"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/salarydistribution.com\/machine-learning\/wp-json\/wp\/v2\/tags?post=610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}