Last Updated: April 2019
Amazon Lex is a service for building conversational interfaces into any application using voice and text. Amazon Lex provides the advanced deep learning functionalities of automatic speech recognition (ASR) for converting speech to text, and natural language understanding (NLU) to recognize the intent of the text, to enable you to build applications with highly engaging user experiences and lifelike conversational interactions. With Amazon Lex, the same deep learning technologies that power Amazon Alexa are now available to any developer, enabling you to quickly and easily build sophisticated, natural language, conversational bots ("chatbots"). This lab is designed to demonstrate how to create a new bot including defining intents and slots. This lab will walk you through the following:
- Creating a Lex bot
- Adding intents
- Adding slot types
- Using AWS Lambda as the back-end logic for Lex
- Log in to the AWS console and navigate to the Amazon Lex service
- Please ensure you have selected North Virginia as the region in the top right (Amazon Connect is not available in all regions yet)
- If you have never created a bot, click "Get Started"
- Choose "Custom bot", which will display a dialog asking you to defined your bot
- Our bot name will be "PersonalBanker"
- Choose your preferred output voice
- Session timeout should be 5 minute
- Choose "No" to the Children's Online Privacy Protection Act (COPPA) question
The form should now look as follows, noting that we're going to accept the default IAM role.
- Click "Create"
- We will start by creating an intent, which represents an action that the user wants to perform. For example, we're going to create one intent in this lab for "Get Balance Check" ; (If you have time afterwards you can create a separate intent for getting last transaction) Click the "Create Intent" button.
- In the window that pops-up click the "Create intent" link
- Our first intent enables the user to get account details, so name this intent "GetBalanceCheck" then click "Add".
- We now want to provide samples of what our user would type or say to perform this action (i.e. to activate this intent). Under "Sample utterances", type the below phrases and hit [enter] or click the blue "+" sign after each phrase. Make sure you do not add a question mark at the end of the phrase as this will cause build issues later on.
- Check my bank balance
- how much money is in my account
- how much money do i have
- Next we define a slot which is information we need to process the users request. This information can be included in the utterance (query) that the user types or says, and if not included, Lex will prompt the user for the information. While Lex includes many built-in slot types (such as number, colour, city, food, etc), in this case we want to define a custom slot to get the account type that the user is referring to.
Click on the blue "+" sign next to "Slot types" on the left hand side of the screen and select the "Create slot type" link - note, "Slot types" is initially greyed out, and on some laptop screens may not be obvious
-
For 'Slot type name' enter "AccountType" and optionally enter a description (although description is not required)
-
Select restrict to Slot values and Synonyms
-
For Value, we want to allow the user to make queries against either their "Saving" or "Current" account so enter those as values, clicking the blue "+" sign after each word.
-
Click "Add slot to intent"
-
We now have to link the slot types to the Intent with additional information such as whether it is required or not and the prompt to use (ie how Lex will ask?) Enter 'What type of account do you have (current or saving?)'.
In the existing Slot list change the "Name" field from "slotOne" to "AccountType" so that it matches the slot name that we specified when we created the sample utterences.
-
Specify "What type of account do you want to check (Current or Savings)?" for the "Prompt" field. This prompt will be used by our bot if the user does not specify an account type when asking a question.
- We are now going to ask a security follow up question and ask the user to enter their four digit pin number.
Add another slot and add the name as "PinNumber". Select the slot type AMAZON.FOUR_DIGIT_NUMBER and add the prompt as "what is your pin number for your {AccountType} account". Ensure you click on the plus icon to add your new slot.
It is worth noting as you build other intents you can modify the order of the Slot collection (ie what questions get asked in which order) and also whether or not the slot is "Required" before passing the values to our external function.
- Scroll down and click "Save Intent"
If at any point you made a mistake in the steps above, selecting the "Latest" version of the intent at the top, next to the intent name, will allow you to edit your choices.
- Let's build this simple Bot: Hit the grey Build button at the top right corner. You will be asked for confirmation to build. Click "Build".
The build process takes approximately a minute. Once complete, you can ask your bot a question as a way to test it. For example, you could type "what is my balance ?" in the chat window, or click the microphone symbol, speak your request and client it again to have Lex translate your speech to text. At this stage since we have not added in the backend Lambda function, the response will be that the bot is ready for fulfillment and will show you the values which will be transferred.
- It is possible to give the user a simpler interface on the bot to multiple choice questions using Response Cards. If you click on the small cog icon next to the "AccountType" slot you get the option to add a "Prompt response card". Add a title "Select your card type" and add button title Saving Account (choose value Saving) and Current Account (value Current). Click "Save" and rebuild and test. You will now be presented with a multiple choice option select.
We are now in a position that we can transfer the answers to the 'slots' over to a function to perform logic and get a result for the user.
Here we will create a Lambda function that has some Javascript code to detect the intent name ('GetBalanceCheck' and return values based on the AccountType and if the Pin number was entered correctly.
In the Lambda function we have hard-coded an Array of data but in a real world example we would have authenticated the user and would use a database lookup for the account balances.
- Use the AWS Console to navigate to Lambda.
- Click on the orange 'Create a function' link under the 'Getting Started' section
- On the "Create function" page, click the "Author from scratch" button
- Let's give our function the name of "myPersonalBanker" and optionally provide a description
- Choose Node.js 14.x as the Runtime
- We will "Create new role from template – give it a Lex-style role name (such as "LexRole") and select "Test Harness permissions" as the policy template.
- Hit "Create function" on the bottom right and you'll be take to the "Configuration" window. We are not adding any additional triggers, nor are we using Lambda Layers, so scroll down to the "Function code" section
- Open the lambda function code you will find here (myPersonalBanker_v1.js). Copy and paste the code into the inline editor – make sure that you overwrite any template code that is already in the code box
- Scroll down to the '"Execution role" section and ensure that the role you created previously is selected in the "Existing role" drop-down – if not then please select it
- Leave the rest unchanged, then hit the orange "Save" button at the top of the screen
In this step we will link the three intents we created to the Lambda function. We do this by providing the Lambda function as the method that contains the business logic used to 'fulfill' the users requests. Once this is done (and the bot rebuilt), when a user specifies an intent (such as 'what is my checking account balance'), Lex will call our Lambda function and pass it the intent name ('GetAccountDetail') and the slot value ('checking').
To do this, we go back to the Lex Console.
- Click on Personal Banker
- Enure the 'GetBalanceCheck' intent is selected
- Make sure that the 'Latest' version is selected for both the bot and the intent
- Scroll down to "Fulfillment", select "AWS Lambda function", choose "myPersonalBanker" and click "OK" in the popup warning window which opens. It indicates you are giving Lex the permission to run this Lambda function.
- Click "Save intent"
- Repeat the above steps 3, 4 and 5 for intents "GetLoanDetail" and "GetLoanProducts"
- Click "Build" and then click "Build" again on the confirmation screen.
- If you now test the bot as you did at the bottom of Step 1 you will see a sample response back from the lambda function. This function has been setup to show you how a very basic flow can be configured and over the next few steps we will modify the code to make the function more helpful.
The function 'closes' the Lex box with a message displaying the values passed into the slot. If you look within the Javascript for the function named 'simpleResponse' you should be able to see it does very little other than return data back to the user.
- To make the application more useful we want to pass values into the function and retrieve data back from a data source (in this case an array but in production this could be a database).
If you modify the lambda function and look for the line 'return simpleResponse(intentRequest, callback);' and place '//' before this line and remove the '//' from the line below so it looks like:
//return simpleResponse(intentRequest, callback);
return balanceIntentError(intentRequest, callback);
//return balanceIntent(intentRequest, callback);
Save your lambda function and retry the Lex bot. First off all try by choosing 'Saving' and saying the 'PinNumber' is 1234. You should now get a nice response from lex telling you of your account balance. However, at the moment your Lex box will error if you enter an incorrect PinNumber which is not helpful to the user.
- Finally we are going to add some error handling and a feedback loop to the user until a correct PinNumber is entered. The code will check to see if there is an account match and if not will request the user tries again and resets the 'Slot'.
If you modify the lambda function and look for the line 'return balanceIntentError(intentRequest, callback);' and place '//' before this line and remove the '//' from the line below so it looks like:
//return simpleResponse(intentRequest, callback);
//return balanceIntentError(intentRequest, callback);
return balanceIntent(intentRequest, callback);
In this lab you have learned the basic operations to manage a Lex bot. First, you created a bot, then you defined intents and slot types. Finally you defined a Lambda function and attached it to your chatbot.
NOTE We will be continuing on from this Lab in the second Lab so please do not delete the Lex Bot you have created in this exercise.