{"id":2046,"date":"2022-04-04T16:59:07","date_gmt":"2022-04-04T16:59:07","guid":{"rendered":"https:\/\/salarydistribution.com\/machine-learning\/2022\/04\/04\/interpret-caller-input-using-grammar-slot-types-in-amazon-lex\/"},"modified":"2022-04-04T16:59:07","modified_gmt":"2022-04-04T16:59:07","slug":"interpret-caller-input-using-grammar-slot-types-in-amazon-lex","status":"publish","type":"post","link":"https:\/\/salarydistribution.com\/machine-learning\/2022\/04\/04\/interpret-caller-input-using-grammar-slot-types-in-amazon-lex\/","title":{"rendered":"Interpret caller input using grammar slot types in Amazon Lex"},"content":{"rendered":"<div id=\"\">\n<p>Customer service calls require customer agents to have the customer\u2019s account information to process the caller\u2019s request. For example, to provide a status on an insurance claim, the support agent needs policy holder information such as the policy ID and claim number. Such information is often collected in the interactive voice response (IVR) flow at the beginning of a customer support call. IVR systems have typically used grammars based on the <a href=\"https:\/\/www.w3.org\/TR\/speech-grammar\/\" target=\"_blank\" rel=\"noopener noreferrer\">Speech Recognition Grammar Specification<\/a> (SRGS) format to define rules and parse caller information (policy ID, claim number). You can now use the same grammars in <a href=\"https:\/\/aws.amazon.com\/lex\/\" target=\"_blank\" rel=\"noopener noreferrer\">Amazon Lex<\/a> to collect information in a speech conversation. You can also provide semantic interpretation rules using <a href=\"https:\/\/www.ecma-international.org\/publications-and-standards\/standards\/ecma-262\/\" target=\"_blank\" rel=\"noopener noreferrer\">ECMAScript<\/a> tags within the grammar files. The grammar support in Amazon Lex provides granular control for collecting and postprocessing user input so you can manage an effective dialog.<\/p>\n<p>In this post, we review the grammar support in Amazon Lex and author a sample grammar for use in an <a href=\"https:\/\/aws.amazon.com\/connect\/\" target=\"_blank\" rel=\"noopener noreferrer\">Amazon Connect<\/a> contact flow.<\/p>\n<h2>Use grammars to collect information in a conversation<\/h2>\n<p>You can author the grammar as a slot type in Amazon Lex. First, you provide a set of rules in the SRGS format to interpret user input. As an optional second step, you can write an ECMA script that transforms the information collected in the dialog. Lastly, you store the grammar as an XML file in an <a href=\"http:\/\/aws.amazon.com\/s3\" target=\"_blank\" rel=\"noopener noreferrer\">Amazon Simple Storage Service<\/a> (Amazon S3) bucket and reference the link in your bot definition. SRGS grammars are specifically designed for voice and DTMF modality. We use the following sample conversations to model our bot:<\/p>\n<p><strong>Conversation 1<\/strong><\/p>\n<p><em>IVR: Hello! How can I help you today?<\/em><\/p>\n<p><em>User: I want to check my account balance.<\/em><\/p>\n<p><em>IVR: Sure. Which account should I pull up?<\/em><\/p>\n<p><em>User: Checking.<\/em><\/p>\n<p><em>IVR: What is the account number?<\/em><\/p>\n<p><em>User: 1111 2222 3333 4444<\/em><\/p>\n<p><em>IVR: For verification purposes, what is your date of birth?<\/em><\/p>\n<p><em>User: Jan 1st 2000.<\/em><\/p>\n<p><em>IVR: Thank you. The balance on your checking account is $123 dollars.<\/em><\/p>\n<p><strong>Conversation 2<\/strong><\/p>\n<p><em>IVR: Hello! How can I help you today?<\/em><\/p>\n<p><em>User: I want to check my account balance.<\/em><\/p>\n<p><em>IVR: Sure. Which account should I pull up?<\/em><\/p>\n<p><em>User: Savings.<\/em><\/p>\n<p><em>IVR: What is the account number?<\/em><\/p>\n<p><em>User: I want to talk to an agent.<\/em><\/p>\n<p><em>IVR: Ok. Let me transfer the call. An agent should be able to help you with your request.<\/em><\/p>\n<p>In the sample conversations, the IVR requests the account type, account number, and date of birth to process the caller\u2019s requests. In this post, we review how to use the grammars to collect the information and postprocess it with ECMA scripts. The grammars for account ID and date cover multiple ways to provide the information. We also review the grammar in case the caller can\u2019t provide the requested details (for example, their savings account number) and instead opts to speak with an agent.<\/p>\n<h2>Build an Amazon Lex chatbot with grammars<\/h2>\n<p>We build an Amazon Lex bot with intents to perform common retail banking functions such as checking account balance, transferring funds, and ordering checks. The <code>CheckAccountBalance<\/code> intent collects details such as account type, account ID, and date of birth, and provides the balance amount. We use a grammar slot type to collect the account ID and date of birth. If the caller doesn\u2019t know the information or asks for an agent, the call is transferred to a human agent. Let\u2019s review the grammar for the account ID:<\/p>\n<div class=\"hide-language\">\n<pre><code class=\"lang-xml\">&lt;grammar version=\"1.0\" xmlns=\"http:\/\/www.w3.org\/2001\/06\/grammar\" xml:lang=\"en-US\" tag-format=\"semantics\/1.0\" root=\"captureAccount\"&gt;&lt;!-- Header definition for US language and the root rule \"captureAccount\" to start with--&gt;\n\n\t&lt;rule id=\"captureAccount\" scope=\"public\"&gt;\n\t\t&lt;tag&gt; out=\"\"&lt;\/tag&gt;\n\t\t&lt;one-of&gt;\n\t\t\t&lt;item&gt;&lt;ruleref uri=\"#digit\"\/&gt;&lt;tag&gt;out += rules.digit.accountNumber&lt;\/tag&gt;&lt;\/item&gt;&lt;!--Call the subrule to capture 16 digits--&gt; \n\t\t\t&lt;item&gt;&lt;ruleref uri=\"#agent\"\/&gt;&lt;tag&gt;out =rules.agent;&lt;\/tag&gt;&lt;\/item&gt;&lt;!--Exit point to route the caller to an agent--&gt; \n\t\t&lt;\/one-of&gt;\n\t&lt;\/rule&gt;\n\n\t&lt;rule id=\"digit\" scope=\"public\"&gt; &lt;!-- Capture digits from 1 to 9 --&gt;\n\t\t&lt;tag&gt;out.accountNumber=\"\"&lt;\/tag&gt;\n\t\t&lt;item repeat=\"16\"&gt;&lt;!-- Repeat the rule exactly 16 times --&gt;\n\t\t\t&lt;one-of&gt;\n\t\t\t\t&lt;item&gt;1&lt;tag&gt;out.accountNumber+=1;&lt;\/tag&gt;&lt;\/item&gt;\n\t\t\t\t&lt;item&gt;2&lt;tag&gt;out.accountNumber+=2;&lt;\/tag&gt;&lt;\/item&gt;\n\t\t\t\t&lt;item&gt;3&lt;tag&gt;out.accountNumber+=3;&lt;\/tag&gt;&lt;\/item&gt;\n\t\t\t\t&lt;item&gt;4&lt;tag&gt;out.accountNumber+=4;&lt;\/tag&gt;&lt;\/item&gt;\n\t\t\t\t&lt;item&gt;5&lt;tag&gt;out.accountNumber+=5;&lt;\/tag&gt;&lt;\/item&gt;\n\t\t\t\t&lt;item&gt;6&lt;tag&gt;out.accountNumber+=6;&lt;\/tag&gt;&lt;\/item&gt;\n\t\t\t\t&lt;item&gt;7&lt;tag&gt;out.accountNumber+=7;&lt;\/tag&gt;&lt;\/item&gt;\n\t\t\t\t&lt;item&gt;8&lt;tag&gt;out.accountNumber+=8;&lt;\/tag&gt;&lt;\/item&gt;\n\t\t\t\t&lt;item&gt;9&lt;tag&gt;out.accountNumber+=9;&lt;\/tag&gt;&lt;\/item&gt;\n\t\t\t\t&lt;item&gt;0&lt;tag&gt;out.accountNumber+=0;&lt;\/tag&gt;&lt;\/item&gt;\n\t\t\t\t&lt;item&gt;oh&lt;tag&gt;out.accountNumber+=0&lt;\/tag&gt;&lt;\/item&gt;\n\t\t\t\t&lt;item&gt;null&lt;tag&gt;out.accountNumber+=0;&lt;\/tag&gt;&lt;\/item&gt;\n\t\t\t&lt;\/one-of&gt;\n\t\t&lt;\/item&gt;\n\t&lt;\/rule&gt;\n\t\n\t&lt;rule id=\"agent\" scope=\"public\"&gt;&lt;!-- Exit point to talk to an agent--&gt;\n\t\t&lt;item&gt;\n\t\t\t&lt;item repeat=\"0-1\"&gt;i&lt;\/item&gt;\n\t\t\t&lt;item repeat=\"0-1\"&gt;want to&lt;\/item&gt;\n\t\t\t&lt;one-of&gt;\n\t\t\t\t&lt;item repeat=\"0-1\"&gt;speak&lt;\/item&gt;\n\t\t\t\t&lt;item repeat=\"0-1\"&gt;talk&lt;\/item&gt;\n\t\t\t&lt;\/one-of&gt;\n\t\t\t&lt;one-of&gt;\n\t\t\t\t&lt;item repeat=\"0-1\"&gt;to an&lt;\/item&gt;\n\t\t\t\t&lt;item repeat=\"0-1\"&gt;with an&lt;\/item&gt;\n\t\t\t&lt;\/one-of&gt;\n\t\t\t&lt;one-of&gt;\n\t\t\t\t&lt;item&gt;agent&lt;tag&gt;out=\"agent\"&lt;\/tag&gt;&lt;\/item&gt;\n\t\t\t\t&lt;item&gt;employee&lt;tag&gt;out=\"agent\"&lt;\/tag&gt;&lt;\/item&gt;\n\t\t\t&lt;\/one-of&gt;\n\t\t&lt;\/item&gt;\n    &lt;\/rule&gt;\n&lt;\/grammar&gt;<\/code><\/pre>\n<\/p><\/div>\n<p>The grammar has two rules to parse user input. The first rule interprets the digits provided by the caller. These digits are appended to the output via an ECMA script tag variable (<code>out<\/code>). The second rule manages the dialog if the caller wants to talk to an <code>agent<\/code>. In this case the <code>out<\/code> tag is populated with the word agent. After the rules are parsed, the out tag carries the account number (<code>out.AccountNumber<\/code>) or the string <code>agent<\/code>. The downstream business logic can now use the <code>out<\/code> tag handle the call.<\/p>\n<h2>Deploy the sample Amazon Lex bot<\/h2>\n<p>To create the sample bot and add the grammars, perform the following steps. This creates an Amazon Lex bot called <code>BankingBot<\/code>, and two grammar slot types (<code>accountNumber<\/code>, <code>dateOfBirth<\/code>).<\/p>\n<ol>\n<li>Download the <a href=\"https:\/\/aws-blogs-artifacts-public.s3.amazonaws.com\/artifacts\/ML-8878\/BankingBot-LexJson.zip\" target=\"_blank\" rel=\"noopener noreferrer\">Amazon Lex bot<\/a>.<\/li>\n<li>On the Amazon Lex console, choose <strong>Actions<\/strong>, then choose <strong>Import<\/strong>.<\/li>\n<li>Choose the file <code>BankingBot.zip<\/code> that you downloaded, and choose <strong>Import<\/strong>. In the IAM Permissions section, for Runtime role, choose <strong>Create a new role<\/strong> with basic Amazon Lex permissions.<\/li>\n<li>Choose the bot <code>BankingBot<\/code> on the Amazon Lex console.<\/li>\n<li>Download the XML files for <a href=\"https:\/\/aws-blogs-artifacts-public.s3.amazonaws.com\/artifacts\/ML-8878\/accountNumber.xml\" target=\"_blank\" rel=\"noopener noreferrer\">accountNumber<\/a> and <a href=\"https:\/\/aws-blogs-artifacts-public.s3.amazonaws.com\/artifacts\/ML-8878\/dateOfBirth.xml\" target=\"_blank\" rel=\"noopener noreferrer\">dateOfBirth<\/a>. (Note: In some browsers you will have to \u201cSave the link\u201d to download the XML files)<\/li>\n<li>On the Amazon S3 console, upload the XML files.<\/li>\n<li>Navigate to the slot types on the Amazon Lex console, and click on the <code>accountNumber<\/code> slot type<\/li>\n<li>In the slot type grammar select the S3 bucket with the XML file and provide the object key. Click on <strong>Save slot type<\/strong>.<\/li>\n<li>Navigate to the slot types on the Amazon Lex console, and click on the <code>dateOfBirth<\/code> slot type<\/li>\n<li>In the slot type grammar select the S3 bucket with the XML file and provide the object key. Click on <strong>Save slot type<\/strong>.<\/li>\n<li>After the grammars are saved, choose <strong>Build<\/strong>.<\/li>\n<li>Download the supporting <a href=\"https:\/\/aws-blogs-artifacts-public.s3.amazonaws.com\/artifacts\/ML-8878\/BankingBotEnglish.py\" target=\"_blank\" rel=\"noopener noreferrer\">AWS Lambda<\/a> and Navigate to the AWS Lambda console.<\/li>\n<li>On the create function page select <strong>Author from scratch<\/strong>. As basic information please provide the following: <strong>function name<\/strong> <code>BankingBotEnglish<\/code>, and<strong> Runtime<\/strong> <code>Python 3.8<\/code>.<\/li>\n<li>Click on <strong>Create function. <\/strong>In the Code source section, open <code>lambda_funciton.py<\/code> and <strong>delete<\/strong> the existing code. <strong>Download<\/strong> the code and open it in a text editor. <strong>Copy<\/strong> and <strong>paste<\/strong> the code into the empty <code>lambda_funciton.py<\/code> tab.<\/li>\n<li>Choose <strong>deploy.<\/strong><\/li>\n<li>Navigate to the Amazon Lex Console and select <code>BankingBot<\/code>. Click on <strong>Deployment<\/strong> and then <strong>Aliases <\/strong>followed by <code>TestBotAlias<\/code><\/li>\n<li>On the <strong>Aliases<\/strong> page select <strong>languages<\/strong> and navigate to <strong>English (US)<\/strong>.<\/li>\n<li>For <strong>source<\/strong> select <code>BankingBotEnglish<\/code>, for <strong>Lambda version or alias <\/strong>select <code>$LATEST<\/code><\/li>\n<li>Navigate to the Amazon Connect console, choose <strong>Contact flows<\/strong>.<\/li>\n<li>Download the <a href=\"https:\/\/aws-blogs-artifacts-public.s3.amazonaws.com\/artifacts\/ML-8878\/BankingBot_Contact_Flow\" target=\"_blank\" rel=\"noopener noreferrer\">contact flow<\/a> to integrate with the Amazon Lex bot.<\/li>\n<li>In the Amazon Lex section, select your Amazon Lex bot and make it available for use in the Amazon Connect contact flows.<\/li>\n<li>Select the contact flow to load it into the application.<\/li>\n<li>Make sure the right bot is configured in the \u201cGet Customer Input\u201d block. Add a phone number to the contact flow.<\/li>\n<li>Choose a queue in the \u201cSet working queue\u201d block.<\/li>\n<li>Test the IVR flow by calling in to the phone number.<\/li>\n<li>Test the solution.<\/li>\n<\/ol>\n<h2>Test the solution<\/h2>\n<p>You can call in to the Amazon Connect phone number and interact with the bot. You can also test the solution directly on the Amazon Lex V2 console using voice and DTMF.<\/p>\n<h2>Conclusion<\/h2>\n<p>Custom grammar slots provide the ability to collect different types of information in a conversation. You have the flexibility to capture transitions such as handover to an agent. Additionally, you can postprocess the information before running the business logic. You can enable grammar slot types via the Amazon Lex V2 console or AWS SDK. The capability is available in all AWS Regions where Amazon Lex operates in the English (Australia), English (UK), and English (US) locales.<\/p>\n<p>To learn more, refer to <a href=\"https:\/\/docs.aws.amazon.com\/lexv2\/latest\/dg\/building-srgs.html\" target=\"_blank\" rel=\"noopener noreferrer\">Using a custom grammar slot type<\/a>. You can also view the Amazon Lex documentation for <a href=\"https:\/\/docs.aws.amazon.com\/lexv2\/latest\/dg\/grammar-srgs-spec.html\" target=\"_blank\" rel=\"noopener noreferrer\">SRGS<\/a> or <a href=\"https:\/\/docs.aws.amazon.com\/lexv2\/latest\/dg\/grammar-ecmascript-spec.html\" target=\"_blank\" rel=\"noopener noreferrer\">ECMAScript<\/a> for more information.<\/p>\n<hr>\n<h3>About the Authors<\/h3>\n<p><strong><a href=\"https:\/\/d2908q01vomqb2.cloudfront.net\/f1f836cb4ea6efb2a0b1b99f41ad8b103eff4b59\/2022\/03\/29\/Kai-Loreck.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-34711 alignleft\" src=\"https:\/\/d2908q01vomqb2.cloudfront.net\/f1f836cb4ea6efb2a0b1b99f41ad8b103eff4b59\/2022\/03\/29\/Kai-Loreck.jpg\" alt=\"\" width=\"100\" height=\"99\"><\/a>Kai Loreck<\/strong> is a professional services Amazon Connect consultant. He works on designing and implementing scalable customer experience solutions. In his spare time, he can be found playing sports, snowboarding, or hiking in the mountains.<\/p>\n<p><strong><a href=\"https:\/\/d2908q01vomqb2.cloudfront.net\/f1f836cb4ea6efb2a0b1b99f41ad8b103eff4b59\/2020\/11\/20\/Harshal-Pimpalkhute.jpg\"><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\"><\/a>Harshal Pimpalkhute<\/strong> is a Product Manager on the Amazon Lex team. He spends his time trying to get machines to engage (nicely) with humans.<\/p>\n<p>       <!-- '\"` -->\n      <\/div>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/aws.amazon.com\/blogs\/machine-learning\/interpret-caller-input-using-grammar-slot-types-in-amazon-lex\/<\/p>\n","protected":false},"author":0,"featured_media":2047,"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\/2046"}],"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=2046"}],"version-history":[{"count":0,"href":"https:\/\/salarydistribution.com\/machine-learning\/wp-json\/wp\/v2\/posts\/2046\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/salarydistribution.com\/machine-learning\/wp-json\/wp\/v2\/media\/2047"}],"wp:attachment":[{"href":"https:\/\/salarydistribution.com\/machine-learning\/wp-json\/wp\/v2\/media?parent=2046"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/salarydistribution.com\/machine-learning\/wp-json\/wp\/v2\/categories?post=2046"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/salarydistribution.com\/machine-learning\/wp-json\/wp\/v2\/tags?post=2046"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}