Home / 

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 a chatbot with Lex is easy, quick, and requires no deep learning expertise.

Before jumping on to building a chatbot, let’s get familiarized 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 e-commerce 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 intent or similar phrases, Lex will understand the user’s intent. In the above example, when the user says, ‘Please give me the 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. Users 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, a 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.

Flow between user and a bot

Picture1: Flow between the user and a bot.

The sample conversation above is a flow between a user and a bot for CheckOrderStatusIntent. The user starts the conversation with the phrase – ‘Hi, what’s the status of my order?’. Based on the utterances associated with intent, Lex recognizes the 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 the orderID 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. The 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 the AWS Management console. The bot can be created from AWS command-line interface (CLI) too. Refer to Lex Guide on how to create a 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. Choose ‘Create function’ and on the create function page choose ‘Author from scratch’ to build a custom Lambda function.

Lambda Function

Picture 2: Lambda Function

Provide a unique name for the function and choose Runtime. I chose Python. Create a 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.

Lambda function code inline

Picture 3: Lambda function code inline.

On the function page, choose ‘Edit code inline’ in the 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.

The next step is to create Lex chatbot.

Login to AWS Management Console and go to Amazon Lex under Machine Learning. In the Amazon Lex page, bots are selected by default. Click on Create to start creating a bot.

Creating Bots

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’.

Custom Bot

Picture 5: Custom Bot.

I have used the bot name as ‘MyStore’. The 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.

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

Create Intent

Picture 6: Create an 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 the Fulfillment code hook. Additional prompts for a slot can be configured by clicking on the Settings icon of Slot.

Additional prompts

Picture 7: Additional prompts.

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

Additional Intents

Picture 8: Additional Intents.

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

Building test chatbot

Picture 9: Building test chatbot.

The 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.