/amazon-lex-v2-chatbot

Create and deploy chatbot using amazon lex v2

Primary LanguageJavaScriptMIT No AttributionMIT-0

Build an Amazon Connect Call Center

Lab Overview

In this workshop, you will initially build a very simple example of a flow for customer service chatbot for a fictitious FinTech company using Amazon Lex. Once you have built the chatbot you will integrate it with Amazon Connect to create a call center workflow.

Whilst it is possible to do Lab 1 without completing Lab 2 a large amount of the second lab is dependent on having completed the first lab.

Prerequisites

In order to complete this workshop you'll need an AWS Account with sufficient permission to create AWS IAM, Amazon Lex, Amazon Connect, AWS Lambda resources.

Please ensure you follow the instructions regarding the Regions to deploy your Amazon Lex code

The Labs

  • Lab 1 - Building your first Amazon Lex chatbot
  • Lab 2 - Creating your first call center using Amazon Connect

Questions and Contact

For questions on the AWS Lex Connect Workshop, or to contact the team, please leave a comment on GitHub.

Cleanup

To clean up all the resources created in this workshop:

  • If you have created an Amazon Connect instance, go to Amazon Connect console, select the instance and click Remove
  • Delete the Amazon Lex bot in the Amazon Lex Console

License

This sample code is made available under the MIT-0 license. See the LICENSE file.

Building Chatbots with Amazon Lex

Overview

Amazon Lex is a service for building conversational interfaces into any application using voice and text. Amazon Lex provides the advanced deep learning functionalities of automatic speech recognition (ASR) for converting speech to text, and natural language understanding (NLU) to recognize the intent of the text, to enable you to build applications with highly engaging user experiences and lifelike conversational interactions. With Amazon Lex, the same deep learning technologies that power Amazon Alexa are now available to any developer, enabling you to quickly and easily build sophisticated, natural language, conversational bots ("chatbots"). This lab is designed to demonstrate how to create a new bot including defining intents and slots. This lab will walk you through the following:

  • Creating a Lex bot
  • Adding intents
  • Adding slot types
  • Using AWS Lambda as the back-end logic for Lex

Step 1a: Create the bot

  1. Log in to the AWS console and navigate to the Amazon Lex service
  2. check the url, you will see 'lexv2' as part of the url if you are in the V2 console
  3. Please ensure you have selected North Virginia as the region in the top right (Amazon Connect is not available in all regions yet)
  4. If you have never created a bot, click "Create bot" and you will be presented with the following screen. Create bot screen
  5. select "Create a blank bot", give it a name and an optional description
  6. under "IAM permissions" select "Create a role with basic..."
  7. Choose "No" to the Children's Online Privacy Protection Act (COPPA) question
  8. Session timeout should be 5 minute
  9. Click "Next" and select the your preferred language
  10. Choose your preferred output voice
  11. leave "Intent classification confidence score threshold" at default 40% and click "Done"

1b Create an Intent

An intent, which represents an action that the user wants to perform. For example, we're going to create a "GetBalanceCheck" intent in this lab; (Afterwards you can follow the same process to create a separate intent for getting last transaction). It will allow user to check their account balance. This requires us to create custome slot to hold the account types. slot panel

  1. Create the custom slot for account types

    • On the Lex Console, select the bot you created and click on "Slot types" on the left panel.
    • Click on "Add slots type" and select Add blank slot type give it a name and click on Add
    • On the panel that opens, select Restrict to slot values under Slot value resolution
    • scroll down to Slot type values and add Saving and Current slot type
    • click on the Save slot type button to save your first custom slot
  2. Create the GetBalanceCheck intent

    • click on Intents on the left panel of the console, then click the Add intent button, select Add empty intent
    • add GetBalanceCheck as Intent name then click the Add button
    • on the panel that opens, add an optional description We now want to provide samples of what our user would type or say to perform this action (i.e. to activate this intent)
    • sroll down to Sample utterances, switch to Plain Text and paste in the following utterances
        how much money is in my account
        how much money do i have
        lexv2 accepts ulterance in the following formats:
        What’s the balance in my {accountType} account ?
        How much do I have in {accountType} ?
    
    • add additional ulterances as you chooses sample ulterances

    Next we define a slot which is information we need to process the users request. This information can be included in the utterance (query) that the user types or says, and if not included, Lex will prompt the user for the information. While Lex includes many built-in slot types (such as number, colour, city, food, etc), we will use the custom slot we created earlier.

    • Scroll down to the Slots pane and click on the Add slot button
    • ensure Require for this intent is checked
    • provide AccountType as the name
    • click on slot type and select AccountType custom slot you created
    • Enter "What type of account do you want to check (Current or Savings)? as prompt

    Add slot type

    • We are now going to ask a security follow up question and ask the user to enter their four digit pin number.
    • follow the steps again to create another slot that will use one of the amazon in-built slots
    • clicking the Add slot button, and this time use these values and then click the Add button:

    Name - PinNumber Slot type - AMAZON.Number Prompts - For verification purposes, what is your date of birth? slot It is worth noting as you build other intents you can modify the order of the Slot collection (ie what questions get asked in which order) and also whether or not the slot is "Required" before passing the values to our external function.

    • Scroll down and click "Save Intent"
  3. Let's build this simple Bot: Hit the grey Build button at the top right corner. You will be asked for confirmation to build. Click "Build".

The build process takes approximately a minute. Once complete, click on Test just beside Build. you can ask your bot a question as a way to test it. For example, you could type "what is my balance ?" in the chat window, or click the microphone symbol, speak your request and client it again to have Lex translate your speech to text. At this stage since we have not added in the backend Lambda function, the response will be intent fulfilled...

TestBot

Step 2: Fulfilling the bot

We are now in a position that we can transfer the answers to the 'slots' over to a function to perform logic and get a result for the user.

Here we will create a Lambda function that has some Javascript code to detect the intent name ('GetBalanceCheck' and return values based on the AccountType and if the Pin number was entered correctly.

In the Lambda function we have hard-coded an Array of data but in a real world example we would have authenticated the user and would use a database lookup for the account balances.

  1. Use the AWS Console to navigate to Lambda.
  2. Click on the orange 'Create a function' link under the 'Getting Started' section
  3. On the "Create function" page, click the "Author from scratch" button
  4. Let's give our function the name of "myPersonalBanker" and optionally provide a description
  5. Choose Node.js 14.x as the Runtime
  6. Click on Change default execution role to expand and select Create a new role from AWS policy templates
  7. We will "Create new role from template – give it a Lex-style role name (such as "LexRole")
  8. Under Policy templates, click and enter "Test Harness permissions" to select the policy template.

Author new Lambda function

  1. Hit "Create function" on the bottom right and you'll be taken to the "Configuration" window. We are not adding any additional triggers, nor are we using Lambda Layers, so scroll down to the "Function code" section
  2. Open the lambda function code myPersonalBanker_v2 Copy and paste the code into the inline editor – make sure that you overwrite any template code that is already in the code box.
  3. NOTE: If you used a different intent name apart from "GetBalanceCheck", go to line 131 of the code and replace 'GetBalanceCheck' to your intent name.
  4. Leave the rest unchanged, then hit the grey "Deploy" button at the top of the screen

Step 3: Link the bot with the Lambda function

  • Linking lex bot v2 to our lambda function is slightly different compared to v1.

In this step we will link the intents we created to the Lambda function. We do this by providing the Lambda function as the method that contains the business logic used to 'fulfill' the users requests. Once this is done (and the bot rebuilt), when a user specifies an intent (such as 'what is my checking account balance'), Lex will call our Lambda function and pass it the intent name ('GetAccountDetail') and the slot value ('checking').

To do this, we go back to the Lex Console.

  1. Click on PersonalBanker bot
  2. On the left-hand menu select the Aliases link. You will have this screen AliasesConsole
  3. Select the default TestBotAlias, then from the Languages panel select English(US)
  4. On the dialogbox select the lambda function we created as the source and $LATEST as seen below. sourceFunction

Enabling Lambda code hooks on the intents

  1. Navigate to your intent by clicking on the on the left menu

  2. Scroll down to the Fulfillment pane.

  3. Click on the arrow next to On successful fulfillment, then click on Advanced options button

  4. On the long list of the dialogbox go to the one marked Fulfillment Lambda code hook and click the checkbox, then click on the Update options button at the bottom of the panel.

Add Lambda permission

  1. Click "Save intent"
  2. Click "Build" and then click "Build" again on the confirmation screen.

Step 4: Running and debugging the bot

  1. If you now test the bot as you did at the bottom of Step 1 you will see a sample response back from the lambda function. This function has been setup to show you how a very basic flow can be configured and over the next few steps we will modify the code to make the function more helpful. bot response

The function 'closes' the Lex box with a message displaying the values passed into the slot. If you look within the Javascript for the function named 'simpleResponse' you should be able to see it does very little other than return data back to the user.

  1. To make the application more useful we want to pass values into the function and retrieve data back from a data source (in this case an array but in production this could be a database).

If you modify the lambda function and look for the line 'return simpleResponse(intentRequest, callback);' and place '//' before this line and remove the '//' from the line below so it looks like:

    //return simpleResponse(intentRequest, callback);
    return balanceIntentError(intentRequest, callback);
    //return balanceIntent(intentRequest, callback);

Save your lambda function and retry the Lex bot. First off all try by choosing 'Saving' and saying the 'PinNumber' is 1234. You should now get a nice response from lex telling you of your account balance. However, at the moment your Lex box will error if you enter an incorrect PinNumber which is not helpful to the user. Add Lambda permission

  1. Finally we are going to add some error handling and a feedback loop to the user until a correct PinNumber is entered. The code will check to see if there is an account match and if not will request the user tries again and resets the 'Slot'.

If you modify the lambda function and look for the line 'return balanceIntentError(intentRequest, callback);' and place '//' before this line and remove the '//' from the line below so it looks like:

    //return simpleResponse(intentRequest, callback);
    //return balanceIntentError(intentRequest, callback);
    return balanceIntent(intentRequest, callback);

Add Lambda permission

Conclusion

In this lab you have learned the basic operations to manage a Lex bot. First, you created a bot, then you defined intents and slot types. Finally you defined a Lambda function and attached it to your chatbot.

NOTE We will be continuing on from this Lab in the second Lab so please do not delete the Lex Bot you have created in this exercise.

STEP 5. Integrating your bot into your website.

Follow the steps in the documentation to integrate lex chat bot to your website using komunicate

STEP 6. Deploy your website on AWS Amplify

For a stet-by-step guild on how you can deploy your wesite on AWS Amplify, visit my other project in this repository