Cloud AI - Connect a chatbot with Lambda (part-3)

yyounos shaikyyounos shaik
8 min read

DIFFICULTY : Easy TIME: 60 MINS COST: 0$

WHAT YOU’LL NEED:

AWS SERVICES :

  • Amazon Lex

  • Amazon Lamda


Overview

In the part one and two we have seen how we :

  • Define intents

  • Provide variations in your bot's responses

  • Set up a custom slot type

  • Build and test your bot using text and speech

Now we will use the AWS Lamda to return a random bank balance…

Let’s Get Started…

Step 1 : Setting up a new chatbot

  • Create a new chatbot looking into part one and two projects.

  • Once you have finished, you should have:

    • A new Lex chatbot called BankerBot.

    • WelcomeIntent set up.

    • FallbackIntent customised, i.e., your BankerBot says a customised error message if it doesn't understand the user's input.

    • CheckBalance set up with your custom slot type called accountType.

If you are having trouble looking into the previous two parts then let’s dive into the brief steps to create a chatbot here!

  • Log in to your AWS Account.

  • Create a blank bot:

    • Name: BankerBot

    • Description: Banker Bot to help customer check their balance and make transfers.

    • IAM permissions: Create a role with basic Amazon Lex permissions.

    • Children’s Online Privacy Protection Act (COPPA): No

    • Idle session timeout: 5 minutes

    • Intent classification confidence score threshold: 0.40

Set Up WelcomeIntent

  • Configure a new intent:

    • Intent name: WelcomeIntent

    • Description: Welcoming a user when they say hello.

  • Add the following 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?

  • Add a closing response to the user after the intent is fulfilled:

    • Message: Hi! I'm BB, the Banking Bot. How can I help you today?
  • Save and build your bot.

  • Test these user inputs:

    • Help me

    • Hiya

    • How are you

    • Good morning

Manage FallbackIntent

  • In your left hand navigation panel, choose FallbackIntent.

  • Change FallbackIntent's closing response to:‍
    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.

  • Add variations to your closing response:

    • 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?

  • Go through the Save intent / Build / Test cycle again, and test your bot!

  • Let's re-test 2-3 message that failed in your last try - what do you see now?

Create the accountType Custom Slot

  • Add a new blank slot type.

    • Name: accountType

    • Select the option to Restrict to slot values.

  • Now let's add the three account types!

    1. Checking

    2. Savings

    3. Credit

  • Add a few synonyms to Credit

    • credit card

    • visa

    • mastercard

    • amex

    • american express

Create the CheckBalance intent

  • Create a new intent:

    • Name: CheckBalance

    • Description: Intent to check the balance in the specified account type.

    • Utterances:

      • What’s the balance in my account?

      • Check my account balance

      • What’s the balance in my {accountType} account?

      • How much do I have in {accountType} ?

      • I want to check the balance

      • Can you help me with account balance?

      • Balance in {accountType}

  • Add a slot for the accountType.

    • Name: accountType

    • Prompts: For which account would you like your balance?

  • Add a slot for the user's birthday.

    • Name: dateOfBirth

    • Slot type: AMAZON.Date

    • Prompts: For verification purposes, what is your date of birth?

  • Go through the Save intent / Build / Test cycle again, and test your bot!

  • Pass on an account type and birth date to your chatbot.

  • Check that your chatbot records two slot values -accountType and dateOfBirth - with information it now knows about you.

  • Ask your chatbot for another account's balance - make sure the accountType is already in your input.

  • This time, Amazon Lex will only prompt you for your date of birth, as it already knows the account type it should check.

  • Nice - you've just validated that your chatbot is set up and ready for more!

Step 2: Set up AWS Lambda function

What is AWS Lambda?

AWS Lambda is a service that lets you run code without provisioning or managing servers.

Lambda runs your code only when needed and scales automatically, from a few requests per day to thousands per second - all you need to do is supply your code in one of the languages that Lambda supports.

  • Head to Lambda in your AWS Management Console.

  • Choose Author from scratch.

  • Function name - BankingBotEnglish

  • Runtime - Python 3.12, or a later version of Python3 if v3.12 is not available.

  • Select Create function.

  • Scroll down to the Function code section.

  • Use the following source code file:
    Lambda.py

    What might be in the code file?

    This Python script helps your chatbot give users quick answers about their account balances.

    When someone asks about their account balance to your chatbot, Lex will ask your Lambda function to run this code, which will pick a random number to pretend it's the balance.

    Lambda will pass this random number to Lex, who will then push the bank balance figure to the user through your chatbot.

  • Copy the downloaded code, and paste it in your text editor - it should completely replace any placeholder code that was in the example code fragment!

  • Deploy your code.

Step 3 : Connect AWS Lambda with Amazon Lex

  • Head back to your Amazon Lex console.

  • Select BankerBot.

  • On the left-hand menu, choose Aliases.

    What is meant by Aliases?

    Think of an alias in Amazon Lex as a pointer for a specific version of your bot.

    So when you're connecting Lex with other AWS services or your custom applications, those external resources will connect to an alias, which will point to the specific version of your bot that you want to use.

    Now, instead of always updating your apps to connect to the newest version of the bot, you can just update the alias to point to that new version. All your apps will automatically start using the updated bot without needing any changes on their end - this saves developers a TON of time and reduces the risk of errors!

  • Choose the default TestBotAlias.

    What is TestBotAlias?

    TestBotAlias is a default version of your bot that's made for testing or development.

    This is the playground version of your bot that you'll use to make sure everything works smoothly before rolling out changes!

  • From the Languages panel, select English (US).

  • Ooo how perfect - a Lambda function panel pops up. This panel lets you associate a Lambda function to this TestBotAlias version of your bot.

  • For Source, choose your Lambda function BankingBotEnglish.

  • Leave the Lambda function version or alias field at the default $LATEST.

    What is $Latest?

    Using the $LATEST version means you're directing your alias to always use the most up to date version of this Lambda function. This setup is a time saver that lets you immediately test any changes in your function.

  • Choose Save.

Step 4 : Connect your CheckBalance intent with your Lambda function

The Lambda function is now ready to work on the BankingBot intents , but we still have to tell Amazon Lex which intent will actually use the Lambda function.

Time to make a direct connection between a specific intent with the Lambda function!

  • Navigate to your CheckBalance intent.

  • Scroll down to Fulfilment panel.

    What is Fulfilment?

    In Amazon Lex, fulfilment means completing the intent.
    With your BankingBot, after a user tells your bot:

    1. The account they want to check, and

    2. Their birthday for verification

    The bot has all the information it needs and moves to fulfilment. This is where it will use the Lambda function to get the account balance and pass it back to the user.

  • Expand the On successful fulfilment bubble.

  • Choose Advanced options.

  • Under the Fulfilment Lambda code hook panel, check the checkbox next to Use a Lambda function for fulfilment.

    What are code Hooks?

    Code hooks help you connect your chatbot to custom Lambda functions for doing specific tasks during a conversation.

    They're used to handle more complex actions that the basic chatbot setup can't do on its own, like checking data from a database or making decisions based on past conversations.

  • Choose Update options.

  • Choose Save intent.

  • Choose Build - time for another stretch!

  • Choose Test.

  • Ask for the balance of any of your accounts - your bot should now be able to return (random) bank balance figures!

  • Done!! we have successfully connected the lambda function with the Amazon Lex.

Summary

Nice Work!! We have accomplished to set up the chatbot using the Lambda function and Amazon Lex. In the Next final part we will save the user information into the chatbot so that it can verfiy our details everytime we access it. So stay tuned…..

0
Subscribe to my newsletter

Read articles from yyounos shaik directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

yyounos shaik
yyounos shaik

An Aspring Cloud Engineer