Creating a BankingBot on Amazon Lex V2 Console with support for English and Spanish
Amazon Lex is a service for building conversational interfaces into any application. The new Amazon Lex V2 Console and APIs make it easier to build, deploy, and manage bots. In this post, you will learn about about the 3 main benefits of Amazon Lex V2 Console and API, basic bot building concepts, and how to create a simple BankingBot on the Amazon Lex V2 Console.
The new Amazon Lex V2 Console and API have three main benefits:
- You can add a new language to a bot at any time and manage all the languages through the lifecycle of design, test, and deployment as a single resource. The new console dashboard allows you to quickly move between different languages to compare and refine your conversations.
- The Amazon Lex V2 API follows a simplified information architecture (IA) where intent and slot types are scoped to a specific language. Versioning is performed at the bot level so that resources such as intents and slot types don’t have to be versioned individually.
- Amazon Lex V2 Console and API provides additional builder productivity tools and capabilities that give you more flexibility and control of your bot design process. For example, you can now save partially completed work as you script, test, and tune your configuration. You can also use the Conversation flow section to view the utterances and slot types for each intent.
You can access new Amazon Lex V2 Console from the AWS Management Console, the AWS Command Line Interface (AWS CLI), or via APIs. With the enhanced console and revised APIs, you can expedite building virtual agents, conversational IVR systems, self-service chatbots, or informational bots.
Basic bot concepts
Amazon Lex enables you to add self-service, natural language chatbots to your applications or devices. You can build bots to perform automated tasks such as scheduling an appointment or to find answers to frequent customer queries such as return policies. Depending on your user base, you can also configure your bot to converse in multiple languages.
In this post, you learn the basic concepts needed to create a simple BankingBot
that can handle requests such as checking account balances, making bill payments, and transferring funds. When building conversational interfaces, you need to understand five main concepts:
- Intents – An intent represents an action that the user wants to perform. This enables the bot to understand and classify what task a user is trying to accomplish. Your bot can support one or more related intents and are scoped to individual languages. For this post, our
BankingBot
is configured to understand intents in English and Spanish, such asCheckBalance
, allowing your users to check the balance in their accounts, orTransferFunds
for paying bills. - Utterances – Utterances are phrases that are used to trigger your intent. Each intent can be trained by providing a set of sample utterances. Based on these utterances, Amazon Lex can identify and invoke an intent based on natural language user input.
- Slots and slot types – Slots are input data that a bot needs to complete an action or fulfill an intent. For the
CheckBalance
intent, the bot needs information regarding which account and date of birth to verify the user’s identity. This data is captured as slots, which is used to fulfill the intents. Amazon Lex has two types of slots:- Built-in slots – These slots provide a definition of how the data is recognized and handled. For example, Amazon Lex has the built-in slot type for
AMAZON.DATE
, which recognizes words or phrases that represent a date and converts them into a standard date format (for example, “tomorrow,” “the fifth of November,” or “22 December”). - Custom slots – These slots allow you to define and manage a custom catalog of items. You can define a custom slot by providing a list of values. Amazon Lex uses these values to train the natural language understanding model used for recognizing values for the slot. For example, you can define a slot type as
accountType
with values such asChecking
,Savings
, andCredit
. You can also add synonyms for each value, such as definingVisa
as a synonym for yourCredit
account.
- Built-in slots – These slots provide a definition of how the data is recognized and handled. For example, Amazon Lex has the built-in slot type for
- Prompts and responses – These are bot messages that can be used to get information, acknowledge what the user said earlier, or confirm an action with the user before completing a transaction.
- Fulfilling the user request – As part of fulfilling the user’s request, you can configure the bot to respond with a closing response. Optionally, you can enable code hooks such as AWS Lambda functions to run business logic.
Creating the bot
Now that you know about the basic building blocks of a bot, let’s get started. We configure the BankingBot
to interact and understand the five intents in English and four intents in Spanish. We start off with a basic Welcome intent and then increase the complexity of the intent confirmations by adding custom slots, Lambda functions, and context management. The following table provides an overview of our intents.
Intent | Built-in Slots |
Custom Slots |
Context | Prompts/ Responses |
App Integration |
Welcome |
x | ||||
CheckBalance |
x | x | Lambda | ||
FollowupCheckBalance* |
x | x | x | Lambda | |
TransferFunds |
x | x | x | ||
FallbackIntent |
x |
*As of this writing, context management is only supported in US English.
To create your bot, complete the following steps:
- On the Amazon Lex V2 Console, choose Bots. If you’re in the Lex V1 Console, click on Switch to the new Lex V2 Console located in the left hand menu.
- Choose Create bot.
- For Creation method, select Create.
- For Bot name, enter
BankingBot
. - Optionally, enter a description.
- For Runtime role, select Create a new role with basic Amazon Lex permissions.
- Because this bot is only for demo purposes, it’s not subject to COPPA, so select No.
- Leave the Idle session timeout and Advanced settings at their default.
- Choose Next.
Adding languages
This sample BankingBot
is configured for both US English and US Spanish. Let’s first add US English.
- For Select language, choose English (US).
If you’re building a voice-based bot, Amazon Lex comes pre-integrated with the neural speech-to-text voices from Amazon Polly. Try them out and see what voice fits your bot.
- Choose Add another language.
- For Select language, choose Spanish (US).
- Choose Done.
Congratulations, you have successfully created your BankingBot
! Now, let’s bring it to life.
Creating intents and slots
In this section, we walk you through how to create 5 intents and related slots for your bot.
Intent 1: Welcome
At this point, the console automatically takes you into the Intent editor page, where a NewIntent
is ready for you to configure. The BankingBot
is a friendly bot, so let’s start by creating a simple Welcome intent to greet users.
- Scroll to Intent details, for Intent name, replace
NewIntent
toWelcome
.
- Under Sample utterances, choose the Plain Text tab and add the following:
- Under Closing responses, for Message, enter:
- Choose Save intent.
- After your intent is saved, choose Build.
- Now that you’ve successfully built your first intent, choose Test and give it a try.
Intent 2: CheckBalance
Now let’s get a bit fancier with a CheckBalance
intent. This intent allows a user to check an account balance. The bot first validates the user by requesting their date of birth and then asks which account they want to check. This intent requires you to create a custom slot type, set up the intent and finally first set up a Lambda function for fulfillment.
Creating a custom slot
Now that you have added your Lambda function, you need to create a custom slot that can capture a user’s account types with valid values such as Checking
, Savings
, and Credit
before creating the intents. To create a custom slot, follow these steps:
- In the navigation pane, drill down to the English (US) version of your bot.
- Under English (US), choose Slot types.
- On the Add slot type menu, choose Add blank slot type.
- For Slot type name¸ enter
accountType
. - Choose Add.
- For Slot value resolution, select Restrict to slot values.
- Under Slot type values, add values for
Checking
,Savings
, andCredit
.
You’ve now created a custom slot type for accountType
. You can also add synonyms in the second column to help the bot recognize additional references to the Credit
slot, such as credit card
, Visa
, and Mastercard
.
- Choose Save slot type.
Congratulations! You now have your first custom slot type.
Creating the intent
Now let’s create the CheckBalance
intent. This intent allows a user to check an account balance. The bot first validates the user by requesting their date of birth and then asks which account they want to check. This intent uses the accountType
custom slot and a Lambda function for fulfillment.
- In the navigation pane, under English (US), choose Intents.
- Choose New intent.
- For Intent name, enter
CheckBalance
. - Choose Add.
- Under Intent details, for Description, add a description.
- Under Sample utterances, choose the Plain Text tab and enter the following utterances:
- Choose Save intent.
In the chatbot lifecycle, this component can be leveraged to expand the chatbot’s understanding of its users by providing additional utterances. The phrases don’t need to be an exact match for user inputs, but should be representative of real-world natural language queries.
- Under Slots, choose Add slot.
For the CheckBalance
intent, we set up two slots: account type and date of birth.
- For Name, enter
accountType
. - For Slot type, choose accountType.
- For Prompts, enter:
- Choose Add.
- Choose Add slot.
- For Name, enter
dateofBirth
. - For Slot type, choose AMAZON.Date.
- For Prompts, enter For verification purposes, what is your date of birth?
- Choose Add.
- Choose Save intent.
Preparing for intent 3: FollowupCheckBalance with context
Understanding the direction and context of an ever-evolving conversation is beneficial to building natural, human-like conversational interfaces. Being able to classify utterances as the conversation develops requires managing context across multiple turns. Consider when a user wants to follow up and check their account balance in a different account. You don’t want the bot to ask the user for their date of birth again. You want the bot to understand the context of the question and carry over the date of birth slot value from this intent into the follow-up intent.
To prepare for the third BankingBot
intent, FollowupCheckBalance
, you need to preserve this CheckBalance
context as an output for future use.
- Under Contexts, for Output contexts, choose New Context tag.
- For Context tag name, enter
contextCheckBalance
. - Choose Add.
Now your context is stored for future use.
- Under Code hooks, select Use a Lambda function for fulfillment. To create the Lambda function, please follow the instructions in Appendix B below.
- Choose Save Intent.
- Choose Build.
- After the bot building process is complete, you can test the intent by choosing Test.
You can also use the Conversation flow section to view the current state of your conversation flow and links to help you quickly get to that specific utterance, slot, or prompt.
You learn how to create an intent with prompts and closing responses in the fourth intent, TransferFunds
.
Intent 3: FollowupBalance
Next, we create a FollowupBalance
intent, where the user might ask what the balance is for a different account. With this intent, you want to use the context management feature and utilize the context that you set up earlier with the CheckBalance
intent.
- On the Intent editor page, under Intents, choose Add.
- Choose Add empty intent.
- For Intent name¸ enter
FollowupBalance
. - Choose Add.
- For Description, enter:
- In the Contexts section, for Input contexts, choose the context you just created in the
CheckBalance
intent.
- Under Sample utterances, on the Plain text tab, enter the following sample utterances:
- In the Slots section, choose Add slot.
- For Name, enter
accountType
. - For Slot type, choose accountType.
- For Prompts, enter:
- Choose Add.
Next, you create a second slot.
- Choose Add slot.
- For Name, enter
dateofBirth
. - For Slot type, choose AMAZON.Date.
- For Prompts, enter:
- Choose Add.
- In the Slots section, open the
dateofBirth
slot and choose Advanced options. - Under Default values, enter the context and slot value for the
CheckBalance
intent
(contextCheckBalance.dateofBirth
). - Choose Add default value.
- Choose Save.
- In the Code hooks section, select Use a Lambda function for fulfillment.
- Choose Save intent.
- Choose Build.
- When your
BankingBot
is built, choose Test and try theFollowupBalance
intent and can see if thedateofBirth
slot from theCheckBalance
intent is used.
Intent 4: TransferFunds
The TransferFunds
intent offers the functionality of moving funds from one account to a target account. In this intent, you learn how to create two different slots using the same slot type and how to configure confirmation prompts and declines.
- On the Intent editor page, under Intents, choose Add.
- Choose Add empty intent.
- For Name, enter
TransferFunds
. - Choose Add.
- For the intent description, enter:
- Under Sample Utterances, on the Plain Text tab, enter the following:
Next, we create the transferAmount
slot.
- Choose Add slot.
- For Name, enter
transferAmount
. - For Slot type, choose AMAZON.Number.
- For Prompts, enter:
- Choose Add.
Next, create the sourceAccountType
slot.
- Choose Add slot.
- For Name, enter
sourceAccountType
. - For Slot type¸ choose accountType.
- For Prompts¸ enter:
- Choose Add.
Next, create the targetAccountType
slot.
- Choose Add slot.
- For Name, enter
targetAccountType
. - For Slot type¸ choose accountType.
- For Prompts¸ enter:
- Choose Add.
- Under Prompts, for Confirmation prompts, enter:
- For Decline responses, enter:
- Under Closing responses, for Message, enter:
- Choose Save intent.
- Choose Build.
- Choose Test.
Intent 5: FallbackIntent
Your last intent is the fallback intent, which is used when the bot can’t understand or identify a specific intent. It serves as a catchall intent and can also be used to route the conversation to a human agent for more assistance.
- On the Intents list, choose FallbackIntent.
- Under Closing responses¸ for Message, enter:
- Choose Save intent.
- Choose Build.
Configuring the bot for Spanish
Amazon Lex V2 Console also allows you add multiple languages to a bot. Each language has an independent set of intents and slot types. Each intent follows the same structure as its English counterpart.
Intent 1: Welcome (Spanish)
To create the Welcome
intent in Spanish, complete the following steps:
- In the navigation pane, under Spanish (US), choose Intents.
- Choose NewIntent.
- For Intent name, enter
Welcome
. - Choose Add.
- Under Sample utterances, on the Plain text tab, enter the following:
- Under Closing responses, for Message, enter:
- Choose Save intent.
- Choose Build.
Intent 2: CheckBalance (Spanish)
To create the CheckBalance
intent, you first need to create the accountType
custom slot type as you did for the English bot.
- Under Spanish (US), choose Slot types.
- On the Add slot type menu, choose Add a blank slot type.
- For Slot type name, enter
accountType
. - Choose Add.
- In the Slot value resolution section, select Restrict to slot values.
- Add the Spanish slot values
Cheques
(Checking),Ahorro
(Savings), andCrédito
(Credit).
- Choose Save slot type.
You have now created the accountType
custom slot.
- In the navigation pane, under Spanish (US), choose Intents.
- On the Add intent menu, choose Add empty intent.
- For Intent name, enter
CheckBalance
. - Choose Add.
- For Description, enter:
- Under Sample utterances, on the Plain text tab, enter the following:
- Choose Add slot.
- For Name, enter
accountType
. - For Slot type, choose accountType.
- For Prompts, enter:
- Choose Add slot.
- For Name, enter
dateofBirth
. - For Slot type, choose AMAZON.Date.
- For Prompts, enter:
- Choose Add.
- Under Code hooks, select Use a Lambda function for fulfillment. For the Spanish version of your bot, you will need a new Lambda function for Spanish. To create a Lambda function, please follow the instructions in Appendix B below.
- Choose Save intent.
- Choose Build.
Intent 3: TransferFunds (Spanish)
Like the English version, the TransferFunds
intent offers the functionality of moving funds from one account to another. This intent allows you to work with two slots of the same type and configure confirmations and prompts.
- Create a new intent and name it
TransferFunds
. - For the intent description, enter:
- Under Sample utterances, on the Plain Text tab, enter the following:
Next, create the transferAmount
slot.
- Choose Add slot.
- For Name, enter
transferAmount.
- For Slot Type, choose AMAZON.Number.
- For Prompts,
- Choose Add.
Next, create the sourceAccountType
slot.
- Choose Add slot.
- For Name, enter
sourceAccountType
. - For Slot type, choose accountType.
- For Prompts, enter
- Choose Add.
Next, create the targetAccountType
slot.
- Choose Add slot.
- For Name, enter
targetAccountType
. - For Slot type, choose accountType.
- For Prompts, enter
- Choose Add.
- Under Prompts, for Confirmation prompts, enter:
- For Decline responses, enter:
- Under Closing responses, for Message, enter:
- Choose Save intent.
- Choose Build.
Conclusion
Congratulations! You have successfully built a BankingBot
that can check balances, transfer funds, and properly greet a customer. You also seen how easy it is to add and manage new languages. Additionally, the Conversation flow section lets you view and jump to different parameters of the conversation as you build and refine the dialogue for each of your intents.
To learn more about Amazon Lex V2 Console and APIs, check out the following resources:
- Amazon Lex Introduces an Enhanced Console Experience and new V2 APIs
- Amazon Lex V2 Console Developer Guide
- How to deliver natural conversations with Amazon Lex streaming APIs
Also, you could give your bot the ability to reply to natural language questions by integrating it with Amazon Kendra. For more information, see Integrate Amazon Kendra and Amazon Lex using a search intent.
Appendix A: Bot configuration
This example bot contains five intents that allow a user to interact with the financial institution and perform the following tasks:
- Welcome – Intent to greet users
- CheckBalance – Intent to check balance in the specified account
- FollowupBalance – Intent to provide detail of expenses made for an account over a period of time
- TransferFunds – Intent to transfer funds between checking and savings accounts
- FallbackIntent – Default intent to respond when no other intent matches user input
Intents details: English
- Welcome configuration
- Description: Intent to greet users
- Sample utterances:
- Hi
- Hello
- I need help
- Can you help me?
- Closing response: Hi! I’m BB, the BankingBot. How can I help you today?
- CheckBalance configuration
- Description: Intent to check balance in the specified account
- Sample utterances:
- What’s the balance in my account?
- Check my account balance
- What’s the balance in {accountType}?
- How much do I have in {accountType}?
- I want to check the balance
- Can you help me with account balance?
- Balance in {accountType}
- Slots:
- accountType:
- Custom slot type: accountType
- Prompt: For which account would you like to check the balance?
- dateofBirth:
- Built-in slot type: AMAZON.Date
- Prompt: For verification purposes, what is your date of birth?
- accountType:
- Context tag:
- contextCheckBalance
- Output contexts
- contextCheckBalance
- Closing response: Response comes from the fulfillment by the Lambda function.
- FollowupBalance configuration
- Description: Intent to provide detail of expenses made for an account over a period of time.
- Sample utterances:
- How about my {accountType} account
- What about {accountType}
- And in {accountType}?
- how about {accountType}
- Slots:
- dateofBirth:
- Built-in slot type: AMAZON.Date (default: #CheckBalance.dateOfBirth)
- Prompt: For verification purposes. What is your date of birth?
- accountType:
- Custom slot type: accountType
- Prompt: Which account do you need the balance details for? (default:
#contextCheckBalance.dateofBirth
)
- dateofBirth:
- Closing response: Response comes from the fulfillment Lambda function.
- TransferFunds configuration
- Description: Intent to transfer funds between checking and savings accounts.
- Sample utterances:
- I want to transfer funds
- Can I make a transfer?
- I want to make a transfer
- I’d like to transfer {transferAmount} from {sourceAccountType} to {targetAccountType}
- Can I transfer {transferAmount} to my {targetAccountType}
- Would you be able to help me with a transfer?
- Need to make a transfer
- Slots:
- sourceAccountType:
- Custom slot type: accountType
- Prompt: Which account would you like to transfer from?
- targetAccountType:
- Custom slot type: accountType
- Prompt: Which account are we transferring to?
- transferAmount:
- Built-in slot type: AMAZON.Number
- Prompt: What amount are we transferring today?
- sourceAccountType:
- Confirmation prompt: Got it. So we are transferring {transferAmount} dollars from {sourceAccountType} to {targetAccountType}. Can I go ahead with the transfer?
- Decline response: Sure. The transfer has been cancelled.
- Closing response: The transfer is complete. {transferAmount} should now be available in your {targetAccountType} account.
- FallbackIntent configuration
- Description: Default intent to respond when no other intent matches user input.
- Closing response: Sorry I am having trouble understanding. Can you describe what you’d like to do in a few words? I can help you with account balance, transfer funds and payments.
Intent details: Spanish
- AccountBalance configuration
- Description: Intent to check balance in the specified account.
- Sample utterances:
- Cuál es el balance en mi cuenta?
- Verificar balance en mi cuenta
- Cuál es el balance en la cuenta {accountType}
- Cuál es el balance en {accountType}
- Cuánto hay en {accountType}
- Quiero verificar el balance
- Me podría ayudar con el balance de mi cuenta?
- Balance en {accountType}
- Slots:
- accountType:
- Custom slot type: Restrict values to Cheques, Ahorros, and Crédito
- Prompt: Por supuesto. De qué cuenta le gustaría conocer el balance?
- dateofBirth:
- Built-in slot type: AMAZON.Date (default: #CheckBalance.dateOfBirth)
- Prompt: Por supuesto. Por motivos de verificación. Podría por favor compartir su fecha de nacimiento?
- accountType:
- Closing response: Response comes from the fulfillment Lambda function.
- TransferFunds configuration
- Description: Intent to transfer funds between checking and savings accounts.
- Sample utterances:
- Quisiera transferir fondos
- Puedo realizar una transferencia?
- Necesito hacer una transferencia.
- Quisiera transferir {transferAmount} desde {sourceAccountType} hacia {targetAccountType}
- Puedo transferir {transferAmount} hacia {targetAccountType} ?
- Necesito ayuda con una transferencia.
- Me ayudaría a realizar una transferencia?
- Necesito realizar una transferencia.
- Slots:
- sourceAccountType:
- Custom slot type: Restrict Values to Checking, Savings, and Credit
- Prompt: Desde qué cuenta desea iniciar la transferencia?
- targetAccountType:
- Custom slot type: Restrict Values to Checking, Savings, and Credit
- Prompt: Hacia qué cuenta desea realizar la transferencia?
- transferAmount:
- Built-in slot type: AMAZON.Number
- Prompt: Qué monto desea transferir?
- sourceAccountType:
- Confirmation prompt: Entendido. Usted desea transferir {transferAmount} dólares desde la cuenta {sourceAccountType} hacia la cuenta {targetAccountType}. Puedo realizar la transferencia?
- Decline response: No hay problema. La transferencia ha sido cancelada.
- Closing response: La transferencia ha sido realizada. {transferAmount} deberían estar disponibles en su cuenta {targetAccountType}
- FallbackIntent configuration
- Description: Default intent to respond when no other intent matches user input
- Closing response: Lo siento, no he entendido. En pocas palabras, podría describer que necesita hacer? Puedo ayudarlo con balance de cuenta, transferir fondos y pagos.
Appendix B: Creating a Lambda function
In Amazon Lex V2 Console, you use a single Lambda function as a fulfillment mechanism for all of your intents. This function is defined in the Alias language support page. You will need to create a separate Lambda function for each language you have specified for your bot. For this example bot, you will need to create a unique Lambda function for the English US and Spanish US language in your bot.
- In the top left corner, click on the Services drop down menu and select Lambda in the Compute section.
- On the Lambda console, choose Functions.
- Choose Create function.
- Select Author from scratch.
- For Function name, enter a name. For the English version, enter
BankingBotEnglish
for the Function name. For the Spanish version, enterBankingBotSpanish
. - For Runtime, choose Python 3.8.
- Choose Create function.
- In the Function code section, choose lambda_function.py.
- Download the Lambda BankingBotEnglish or BankingBotSpanish code for the specific language and open it in a text editor.
- Copy the code and replace the current function code with the Lambda
BankingBotEnglish
code orBankingBotSpanish
code for the respective language. - Choose Deploy.
Adding the Lambda function to your language
Now you have set up your Lambda function. In Amazon Lex V2 Console, Lambda functions are defined at the bot alias level. Follow these steps to set up your bot to use a Lambda function:
- On the Amazon Lex V2 Console, in the navigation pane, under your bot, choose Aliases.
- Choose TestBotAlias.
- For Languages, select English (US) or Spanish (US) depending on which fulfillment Lambda function you are creating.
- For Source, choose
BankingBotEnglish
orBankingBotSpanish
as your source depending on which language you are configuring. - For Lambda function version or alias, choose your function.
- Choose Save.
Now your Lambda function is ready to work with your BankingBot intents.
About the Author
Juan Pablo Bustos is an AI Services Specialist Solutions Architect at Amazon Web Services, based in Dallas, TX. Outside of work, he loves spending time writing and playing music as well as trying random restaurants with his family.
As a Product Manager on the Amazon Lex team, Harshal Pimpalkhute spends his time trying to get machines to engage (nicely) with humans.
Esther Lee is a Product Manager for AWS Language AI Services. She is passionate about the intersection of technology and education. Out of the office, Esther enjoys long walks along the beach, dinners with friends and friendly rounds of Mahjong.
Tags: Archive
Leave a Reply