Amazon Lex introduction and demo with built-in chatbot bot

What is a Chatbot?

A chatbot is a computer program which conducts a conversation in natural language via speech or text, understands the intent of the user, and sends a response based on business rules and data of the organisation. So when you talk to chatbot, it understands what you're trying to say and either it will reply with the relevant message or it will complete the desired task for you. Using this chatbot, you can do online shopping, book tickets, news reports, order food etc.

What is Amazon Lex?

Amazon Lex is a service for building conversational interfaces into any application using voice and text. Using this we can create a chatbot for human like conversation without knowing any complex technology.

Use Cases-

  1. Build powerful chat interfaces to use in mobile applications.

  2. Build highly interactive and conversational user experiences for connective devices in Internet of Things.

  3. Build enterprise chatbots to check sales data, marketing performance and much more.

Benefits of Amazon Lex-

  1. Offers an easy-to-use console and predefined bots.
  2. Employs advanced deep learning functionalities.
  3. Provides seamless deploying and scaling.
  4. Offers built-in integration with AWS platform.
  5. Cost effective platform to create bots.

How Amazon Lex operates?

Chatbot receives user input. It can reply with an answer, perform actions or ask for more input. To do this, it triggers a lambda function which integrates with other services like dynamoDB, SNS, SES etc and performs necessary action by providing desired results to the user.

The basic steps to follow when working with Amazon Lex are-

  1. Create a chatbot and configure it with intents, slots and utterances.
  2. Test the bot on text window slide provided by lex Console.
  3. Publish a version and create an alias.
  4. Deploy the bot on suitable platforms.

Some Terminologies-

1. Amazon Bot: An artificial intelligence program that simulates interactive conversation.

2. Intent: An intent represents an action that user wants to perform. Every intent includes a name, utterances, intent fulfil. For example, if you order a pizza, then ordering a pizza is an intent and 'can i order a pizza please?' will be the utterance.

3. Slots: Slots are parameters that an intent may require. For example, while ordering a pizza, we have to specify pizza size, number of pizzas etc; these are the slots.

4. Slot type: Every slot has a type. Can create built-in or custom slot types. For example, if we consider pizza crust size as a slot, then thick and thin will be the slot types.

Creating a simple chatbot-

Search for Amazon Lex in the services and click on get started. You can then create either a custom bot on your own or use sample bots provided by AWS. Today we are going to make a sample BookTrip bot. Follow the parameters as shown in the pictures below.

We can see here that, there are predefined intent and slots and the sentences which tells in which way we can interact with the user.

It also offers a confirmation prompt option where you can tell if the reservation is confirmed then display the confirm message, or else display the cancel message.

Now, click on Build tab on the top right corner and wait for few seconds.

After the build is successful, you can try testing your bot on the right hand side.

But, if you'll give incorrect driver age(<18) or invalid date etc, it will still accept the answer. To make the bot more precise, we have to use lambda function. Go on to the services and search for lambda. Click on create function and then on blueprints. Search for lex-booktrip-python and click on it.

Now give your function a name and click on use an existing role.

The lambda function is made and a code is generated. Let's explore the code a little. First things which happens when a lambda function is created is that an event is generated. The event handler starts working as soon as you select an intent. In the code, you have two handlers for both bookhotel and bookcar intent but we created just one. So let's say you have selected bookcar intent, then that intent will be dispatched to the respective handler.

Similarly you can find respective functions to validate the slots. The conditions written inside are easy to understand.

Click on Test- configure test events and save. It should return a successful message after the completion.

Now let's go back to amazon lex and click on fulfilment- AWS lamnda function and then click on build. Also tick on Initialisation and validation code hook under Lambda initialization and validation.

Finally click on save intent and build. Try testing the bot with the correct parameters.

You can try making another intent, and try reading amazon documentation. I hope you understood the basic features of Amazon Lex. Try experimenting a little and do comment in case of any doubts.

Happy Learning!!