Build a Chatbot with Amazon Lex
Let's set up your very own chatbot... in just one hour!
Introduction
⚡️ 30 second Summary
You can use Amazon Lex to build intelligent chatbots that understand natural language and help users get things done - like checking their bank balance or transferring money!
In today’s project, you’ll create a chatbot called BankerBot, designed for an imaginary banking service. This bot will greet users and be ready to handle conversations through voice or text.
The Lex Chatbot Series
This project is part ONE of our Lex Chatbot Series You’ll build a 5-part chatbot using Amazon Lex - no prior AI experience needed!
Want a complete demo of how to do this project, from start to finish? Check out our 🎬 walkthrough with Maya 🎬
If you're up for a bit of a challenge, quiz yourself on the key concepts up ahead in this project.
This project is part of a series:
Before we start...
It's always good to understand exactly what you're here to do.
Set up your Lex chatbot
Let's set up your own bot using an AWS service called Amazon Lex.
It's super simple and you'll get there in a few clicks - let's go!
In this step, get ready to:
- Create a new bot from scratch on Amazon Lex.
- Log in to the AWS console.
- Navigate to Amazon Lex (type Lex into the search bar of your Console).
- Check your URL in your web browser - does it say ...console.aws.amazon.com/lexv2/...?
- If you're not seeing "lexv2" in your URL, click on Switch to the new Lex V2 console link in your left-hand menu.
- Select Create bot.
- Select Create a blank bot.
Can't find the Create a blank bot option?
You might need to select the Traditional tab first to see it.
If you're stuck, ask the NextWork community!
- For Bot name, enter BankerBot
- For Description, enter Banker Bot to help customer check their balance and make transfers.
- Under IAM permissions, select Create a role with basic Amazon Lex permissions.
Why do we need Amazon Lex permissions?
Amazon Lex needs the permission to call other AWS services on your behalf, later in this project series you'll be integrating Lex with another service called Lambda!
- We'll be using it to call another service called Lambda later!
- Under Bot error logging, select Disabled.
What is bot error logging?
Bot error logging is a way for your chatbot to keep track of problems that happen during conversations. When something goes wrong, like the bot doesn’t understand a user or fails to respond, these issues are recorded in a log file. These logs are stored in services like AWS CloudWatch or S3, and can be encrypted for security. Reviewing error logs helps you find and fix problems, making your bot smarter and more reliable.
- Under Children’s Online Privacy Protection Act (COPPA), select No.
- Under Idle session timeout, keep the default of 5 minutes.
What does Idle session timeout mean?
Amazon Lex will only maintain a session for a set length of time. If the user is idle and doesn't add any input for 5 minutes, their session will end.
- Select Next.
- Now we're going to play around with your bot's voice.
- Keep the language as English so you can explore Lex's full set of features in this project.
- Under Voice interaction, click on the dropdown that says Danielle.
- Click around different voice options to choose your favorite one!
- Two favorites in the NextWork community are Gregory and Ruth :D
Fun fact
These voices are borrowed from Amazon Polly, which is another AWS service all about turning your text into speech!
- For Intent classification confidence score threshold, keep the default value of 0.40.
What is intent classification confidence score threshold?
When you're using Amazon Lex to build a chatbot, this threshold is like a minimum score for your chatbot to confidently understand what the user is trying to say.
Setting this to 0.4 means that your chatbot needs to be at least 40% confident that it understands what the user is asking to be able to give a response.
So if a user's input is ambiguous and your chatbot's confidence score is below 0.4, it'll throw an error message.
You'll see how this works in a bit!
- Select Done.
Create your first intent
Woah, you've just created a bot!
The bot's basic structure is now all set up for you, and you can now bring it to life.
Let's start by teaching our bot how to say hello 👋
In this step, get ready to:
- Train BankerBot to greet your users.
- When your bot is created, you will automatically see a page called Intent: NewIntent.
What are intents?
An intent is what the user is trying to achieve in their conversation with the chatbot. For example, checking a bank account balance; booking a flight; ordering food.
In Amazon Lex, you build your chatbot by defining and categorising different intents. If you set up different intents, one single chatbot can manage a bunch of requests that are usually related to each other.
- Let's change the name!
- Under Intent details, enter WelcomeIntent for the Intent name.
- Add the description Welcoming a user when they say hello.
- Scroll down to the Sample utterances panel.
- Click the Plain Text button.
- Copy the text below, which represent the user inputs (called utterances) that will trigger this intent, and paste it into the text window:
Hi
Hello
I need help
Can you help me?
- Click back to the Preview button to see these utterances in chat form.
- Scroll down to Closing response, and expand the arrow for Response sent to the user after the intent is fulfilled.
- In the Message field, enter the following message: Hi! I'm BB, the Banking Bot. How can I help you today?
- Choose Save intent.
- Choose Build, which is close to the top of the screen.
- This can take 30 seconds - while we wait...
- Choose Test.
- The following dialog will pop up, and you can interact with the bot by entering your opening message.
- Try various different phrases and see what comes up!
- The ones that you have literally defined in your Utterances section will definitely work.
- But what about other utterances?
- Since we have an intent classification confidence score of 0.40, other phrases with similar intents to the ones we've defined could work too.
- Test these:
Help me
Hiya
How are you
Good morning
How does my chatbot respond to these user inputs?
The first three are successfully recognized - Amazon Lex is able to use its ML techniques to match what you have said against your utterances.
But the last two fail, resulting in an Intent FallbackIntent is fulfilled response - meaning Amazon Lex doesn't quite recognize your utterance. We'll learn what FallbackIntent means in the next step.
- Try two of these utterances again, this time using voice!
- Click on the microphone icon at the left of the chat box, speak "Hello" and then click on the tick on the right.
- Now try another phrase!
- What's another way you like to greet someone or say hi?
Hmmm my chatbot didn't pick up what I was saying
Your chatbot has been built using the English (US) dialect, so some English accents may not be perfectly transcribed - this is why Amazon Lex actually supports multiple English dialects.
💡 Extra for Experts: Want to switch dialects, or add a completely new language?
- From the left hand sidebar, select Back to intents list.
- Your sidebar will update to show new options.
- Select All languages.
- Select Add language from the top right corner.
- Select Copy from an existing language.
- Under the Language details panel, select your New language.
- From Catalan, to Hindi, to German and Korean... there are lots of options to choose from!
Manage FallbackIntent
Hmmm so our chatbot will respond with "Intent FallbackIntent is not fulfilled" when it doesn't understand the user.
Sounds mysterious! Wonder what "FallbackIntent" actually means... let's find out 🤔
In this step, get ready to:
- Customise FallbackIntent to send user-friendly messages.
What is FallbackIntent?
Remember the intent classification confidence score threshold, and how it's been set to 0.4?
If your chatbot has a confidence score below 40% for all the intents you've defined (in our case, it's just the WelcomeIntent for now), the FallbackIntent is triggered.
Think of it as a custom error message that your chatbot will use to tell the user it doesn't understand their input.
- In your left hand navigation panel, choose FallbackIntent.
- The default FallbackIntent message you saw just now ("Intent FallbackIntent is fulfilled") can be a little confusing.
- Let's re-phrase that message so it's clearer to the user that your chatbot doesn't understand the user's request.
- Scroll down to Closing responses.
- Expand the arrow for Response sent to the user after the intent is fulfilled.
- In the Message field, add the following text: Sorry I am having trouble understanding. Can you describe what you'd like to do in a few words? I can help you find your account balance, transfer funds and make a payment.
What a detailed message!
It's often chatbot best practice to give a hint as to what kind of commands the bot can understand and respond to.
- You'll notice another arrow next to the label Variations - optional.
- Expand the arrow.
- Enter the following text: Hmm could you try rephrasing that? I can help you find your account balance, transfer funds and make a payment.
- Add another variation! What is another response that the chatbot could use to get clarification if it doesn't understand the user's intent?
What are variations?
Variations are literally variations of the same Message in the main Message box. When Amazon Lex needs to return a Fallback response, it will randomly choose a message from the group and return that.
Variations will give your users a dynamic range of responses, making them sound more conversational!
- Choose Save intent.
- Choose Build - time for another quick question!
- Choose Test.
- Let's test 2-3 message that failed in your last try - what do you see now?
I'm getting an error
Does your error say Missing required key 'sessionId' in params?
That usually means your bot needs a refresh or some time until it's ready for testing.
- Select the refresh button at the top of your chatbot's chat window.
- If you're still seeing the error, wait another minute before sending another message.
- If you're still seeing the error, close this window and select Build and Test again.
- Still seeing the error? Ask the NextWork community!
Secret mission
Welcome to your 🤫 exclusive 🤫 secret mission!
Your mission, should you choose to accept it, is to add an extra kick of excitement to BankerBot with initial responses.
💎 In this secret mission, get ready to:
- Spice up FallbackIntent with an initial response.
- Add variations to your initial response.
- Showcase your secret mission in your project documentation.
Add Initial Responses
Delete Your Resources
Delete Your Resources
Make sure you delete all your resources to avoid getting charged. This is a super important task for every single project you set up.
If you're doing you're doing Part Two of this chatbot series today
Did you know Part Two is going to pick up right where you left off in this project?
Yup, that means you'll be repeating all the steps in this project when you start Part Two from scratch again.
You don't have to delete your resources (so you won't have to repeat this project's steps) IF you're also doing Part Two's project today. Just make sure to:
- Complete all your tasks and
- Download today's documentation before heading to Part Two.
We challenge you to try to give this a go yourself 💪
Do you think you can delete the resources you've created today?
- Lex Bot
Yep, all done.
Fantastic! It's the home stretch!
I know, but also, I don't...
If you're feeling stuck (we've all been there!), here's a little guide:
- Head to your Amazon Lex console.
- Select Bots on the left-hand sidebar.
- Select the circle radio button next to BankerBot.
- Select Delete from your Action drop-down.
- Select Delete.
WOAH! Did you just complete a project?
WOAH! Did you just complete a project?
WOOOOOOOO that's another AWS project done ✅ and in the bag!
And there's much more to come - this project is part ONE of a five-part series.
In the next project, you'll ⬆️ level up ⬆️ your chatbot with another intent that helps users check their bank account balance, learning a new concept called slots along the way.
👉 Start project two of this chatbot series now
Let's recap, today you've learnt how to:
- 🎯 Define a basic intent: You configured a basic intent in Amazon Lex to control how your chatbot responds to user inputs.
- 📝 Create lists of utterances: You compiled a list of sample utterances that trigger different intents, which means your chatbot can recognize different user phrases and respond.
- 🛠️ Handle failures with FallbackIntent: You edited FallbackIntent to handle unrecognized or poorly understood user inputs.
- 🔁 Define variations to provide semi-random responses: You designed a MessageGroup of variations, which makes your chatbot sound more natural and conversational.
- 🧪 Build and test your bot: You tested your bot's setup in both text and voice formats. This comprehensive testing helps you confirm that your chatbot is ready for real-world interactions!
Are you ready to quiz yourself? 💪
Phew that's a pretty decent list, you've outdone yourself you champ!
Thanks for joining us in today's project, and we'll see you in the next part. 🤝
p.s. Does it say "Still tasks to complete!" at the bottom of the screen?
This means you still have screenshots left to upload, or questions left to answer!
- Press Ctrl+F (Windows) or Command+F (Mac) on your keyboard.
- Search for the text Return to later.
- Jump straight to your incomplete tasks!
- 🙋♀️ Still stuck? Ask the community!