Codementor Events

Build Chatbot using Amazon Lex

Published Mar 23, 2019

Amazon released Lex which is an AWS service for building conversational interfaces also known as chatbots using voice and text. Lex is the same conversational engine that powers Amazon Alexa and is now available for developers to build chatbots. Creating chatbot with Lex is easy and quick and no deep learning expertise is necessary.

Before jumping on to building a chatbot, let’s familiarize with few concepts of Lex.

Intent: An intent represents an action that the user wants to perform. A bot will have one or more related intents. For example, an ecommerce bot will have intents to place an order and check order status. Each intent will have descriptive name like PlaceOrder, CheckOrderStatus etc.

Utterances: Utterances are sample phrases user typically uses to convey the intent. For example, user might say ‘Check my order status’ or ‘What is the status of my order’ etc. An intent can have one or more utterances associated with it. The more number of utterances associated with intent the better it is.
When user says any of the utterances associated with an intent or similar phrases Lex understands user’s intent. In above example when user says, ‘Please, give me status of my order’, Lex can understand user’s intent, though phrase used by user is not exact utterance that’s configured. Understanding similar phrases is the beauty of chatbot and is possible with its NLU capability which is the core of Lex.

Slot: Slot is a parameter associated with an intent which is required to fulfil user’s request. An intent can have zero or more slots. In CheckOrderStatus intent, orderId will be one of the slots. At runtime Lex prompts the user for specific slot values. Slot can be configured as required or optional. User must provide values for all required slots before fulfilling an intent. Utterances are also configured with slot which are the phrases users use to provide slot values.

Slot Type: Each slot has a type. There are built in slot types which can be used or create a custom slot type if required. AMAZON.NUMBER built in slot type can be used for orderId slot. While creating custom slot type, list of values for slot has to be enumerated.

Prompt: Prompt is the phrase used by Lex at runtime to get slot values for an intent. Each slot can have one or more prompts associated with it. Lex randomly picks up one of them. orderId slot can have prompts like – ‘Please provide your order Id’, ‘What is your order Id’ etc.

Picture1.png

Above is sample conversation flow between user and bot for CheckOrderStatusIntent. User starts the conversation with the phrase – ‘Hi, what’s the status of my order?’. Based on the utterances associated with intent, Lex recognizes user’s intent and prompts the user for order Id. OrderId is the required slot field for this intent. ‘May I know your orderId’ is one of the prompts associated with order Id slot and ‘My order id is {orderId}’ is one of the utterances associated with slot.

Lambda Initialization, Validation, Fulfillment: While NLU and conversation flow is taken care by Lex, we need Lambda functions for business logic. Lex provides pre-built integration with Lambda. Lambda function can be used for Initialization, Validation and Fulfillment. Same function can be used for both functionalities or can have different functions depending on complexity.

Step by Step procedure to create a chatbot:

Let’s take a look at how to create chatbot from AWS Management Console. Bot can be created from AWS CLI too. Refer to Lex Guide on how to create bot from CLI. AWS account is prerequisite for creating Bot using Lex. Create an account if you don’t have one.

Let’s first create a Lambda Function.
Sign into the AWS Management Console and open the AWS Lambda console at https:// console.aws.amazon.com/lambda/. Choose Create function and on create function page choose Author from scratch, as we are building custom lambda function.

Picture1.png

Provide a unique name for function and choose Runtime. I chose Python. For role, create new role from template(s) and enter a new role name. Depending on other AWS services, lambda function has to access, choose one or more policy templates.
Picture1.png
On function page, choose ‘Edit code inline’ in Function code section and write python code to process requests from Lex and save it. Check Lex Guide on request and response formats. In above example, for the sake of simplicity validation logic is not there and it’s just responding with Fulfilment message without actually changing any backend state of the system.
Next step is creating Lex chatbot.

Login to AWS Management console and go to Amazon Lex under Machine Learning. In Amazon Lex page by default Bots will be selected. Click on Create to start creating a bot.

Picture1.png

Lex provides few pre-built bot templates. In this guide, let’s create a custom bot. Fill all required details for custom bot and choose Create.

Picture1.png

I have provided Bot Name as ‘MyStore’. Output voice will be used when interacting with chatbot through voice. Session timeout is how long the session lasts when bot is left idle. IAM role will be created by default. I selected No for COPPA.

Next step is creating intent. Click on Create Intent. Give a unique name for the intent and Click Add.

Picture1.png

I have given PlaceOrder as my intent name and provided few utterances. Associated productId as the required slot with built in AMAZON.NUMBER slot type. And productId slot has one Prompt message – ‘Provide product Id’. Provided Lambda function which I created in previous section as Fulfillment code hook. Additional prompts for a slot can be configured by clicking on Settings icon of Slot.

Picture1.png

I have created one more intent to check order status.

Picture1.png

Next step is to build and test chatbot. Build chatbot by clicking Build button at top right corner. Once build is successful, it will be ready to test. Bot can be tested from console by clicking ‘Test bot’ section in right pane.
Picture1.png

Above screenshot shows how conversation flow happens between user and our bot. Please refer Lex documentation on how to publish and deploy bot to one of the messaging channels.

Discover and read more posts from Sudha Vankadara
get started
post commentsBe the first to share your opinion
Show more replies