Build a Chatbot Using Amazon Lex

Author

Sudha Vankadara

Technical Manager

Published Apr 15 2019

Amazon Lex is an Amazon Web Service (AWS) that is used for building conversational interfaces (or chatbots) using voice and text. Lex is the same conversational engine that empowers Amazon Alexa and it is now available for developers to build chatbots. Creating chatbot with Lex is easy, quick and requires no deep learning expertise.

Before jumping on to building a chatbot, let’s familiarize with a 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 a descriptive name like ‘PlaceOrder’, ‘CheckOrderStatus’, etc.

Utterances: Utterances are sample phrases that users typically use to convey their intent. For example, a 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 a user says any of the utterances associated with an intent or similar phrases, Lex will understand the user’s intent. In above example, when the user says, ‘Please give me status of my order’, Lex can understand the user’s intent even though the phrase used by the user is not the exact utterance, which may be configured. Understanding similar phrases is the beauty of a chatbot and it does so with its natural language understanding (NLU) capability, which is the core of Lex.

Slot: Slot is a parameter that is associated with an intent to fulfill a user’s request. An intent can have zero or more slots. In ‘CheckOrderStatus’ intent, orderId will be one of the slots. During runtime, Lex prompts the user for specific slot values. A 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 slots which are the phrases users use to provide slot values.

Slot Type: Each slot has a type. There are built-in slot types that can be used or may help in creating a custom slot type. AMAZON.NUMBER built-in slot can be used for orderId slot. While creating a custom slot type, list of values for a slot has to be enumerated.

Prompt: Prompt is a phrase that is used by Lex during 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: Flow between user and a bot.

The sample conversation above is a flow between a user and a 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 the slot.

Lambda Initialization, Validation, Fulfillment: While Lex takes care of NLU and conversation flow, we require Lambda functions for business logic. Lex provides a pre-built integration with Lambda. Lambda can be used for initialization, validation, and fulfillment. Same function can be used for both functionalities or can have different functions, depending on complexity.

Sequential Procedure to Create a Chatbot:

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

Let’s first create a Lambda Function.

Sign in to the AWS Management Console and open AWS Lambda console at https://console.aws.amazon.com/lambda/. Choose ‘Create function’ and on create function page choose ‘Author from scratch’ to build a custom Lambda function.

Picture 2: Lambda Function

Provide a unique name for the function and choose Runtime. I chose Python. Create new role from template(s) and enter a new role name. Choose one or more policy templates depending on AWS services that Lambda has to access.

Picture 3: Lambda function code inline.

On the 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 the above example, for the sake of simplicity, validation logic is not mentioned and it’s just responding with Fulfilment message without actually changing any back-end state of the system.

Next step is to create Lex chatbot.

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

Picture 4: Creating Bots.

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

Picture 5: Custom Bot.

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

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

Picture 6: Create Intent.

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

Picture 7: Additional prompts.

I have created one more intent to check the order status.

Picture 8: Additional Intents.

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

Picture 9: Building test chatbot.

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