Conversational interfaces are transforming the way people interact with software applications and services. Increasingly, people are opting to interact with a bot when they need an answer to a question, to set a reminder, or to obtain a product or service.
With Amazon Lex, you can bring this same level of convenience to data. By allowing users to explore datasets by asking a series of questions, and maintaining a conversational context, you can provide a whole new experience and relationship with data.
In this lab, you will learn how to use Amazon Lex to implement a business intelligence (BI) chatbot, which we refer to as “BIBot”, although you can customize it to use a different name. BIBot can respond to user questions about data in a database, by converting the questions into backend database queries, and transforming the result sets into natural language responses. For example, the request “tell me the increase in inventory last month” could be translated to “select sum(item_qty) from inventory where month(received_date) = 10”.
- Login to your AWS Account and select us-west-2 region. This is the region where you will do rest of the labs.
- Create Cloud9 environment :
- Go to the Cloud9 console.
- Choose the Create environment button.
- On the Name environment page, for Name, enter
BuilderSession
. - (Optional) To add a description to your environment, enter it in Description.
- Choose Next step.
- On the Configure settings page, for Environment type, choose Create a new instance for environment (EC2).
- Leave rest of the values to default.
- Choose Next step.
- On the Review page, choose Create environment. Wait while AWS Cloud9 creates your environment. This can take several minutes.
- After AWS Cloud9 creates your environment, it displays the AWS Cloud9 IDE for the environment.
- Clone lab github repo in Cloud9 IDE:
- Go to Cloud9 environment you just created.
- On the bottom half of your IDE, you will see a termial window. Feel free to resize that window as you feel appropriate. Run following command to clone lab github repo.
git clone https://github.com/sshvans/amazon-lex-bi-bot.git
You have successfully setup a a cloud IDE and cloned the lab github repository.
Note: Replace YOUR_INITIALS with initials of your name.
- Go to AWS S3 console and create a bucket with name
YOUR_INITIALS-bibot-tickit-data
. This is the bucket where you will store a copy of the TICKIT sample data. - Create another S3 bucket called
YOUR_INITIALS-bibot-db-output
. This S3 bucket will be used for Athena to store output from queries. - Set environment variables:
- Go to your Cloud9 IDE, open file amazon-lex-bi-bot/export-env.sh.
- Provide the value for your Athena S3 bucket you created above.
- Replace
REPLACE-ME-ATHENA-BUCKET
with the bucket nameYOUR_INITIALS-bibot-tickit-data
- Replace
REPLACE-ME-OUTPUT-BUCKET
with the bucket nameYOUR_INITIALS-bibot-db-output
- Replace
- Save the file.
- In terminal window, run the following command to set the updated values.
cd amazon-lex-bi-bot && source export-env.sh
- In your Cloud9 IDE terminal window, run the following commands to copy TICKIT sample data files to your S3 bucket and build TICKIT database.
bash setup-db.sh
- You can go to Amazon Athena console and see tickit database.
You have successfully created a sample TICKIT database which will be used by your BIBot.
Note: Replace YOUR_INITIALS with initials of your name.
- From your Cloud9 IDE terminal, run following command to launch a cloudformation stack, which will create a Lambda execution IAM role.
aws cloudformation create-stack --stack-name YOUR_INITIALS-bbot-iam-roles --template-body file://iam-roles.yaml --region us-west-2 --capabilities CAPABILITY_IAM
- Make a note of the IAM role ARN:
- In a new tab, open the AWS CloudFormation console.
- From the list of stack, click the CloudFormation stack named YOUR_INITIALS-bbot-iam-roles. If the stack is still in-progress, wait for it to complete.
- Click Outputs tab.
- Make a note of the IAMRoleArn value.
You have created an IAM role which you will use with the Lambda function to give it a permission to perform actions on Amazon Athena, Amazon S3, and Amazon Lex.
You will use lambda functions which will fulfill the requests from Amazon Lex. It will perform queries on the Athena TICKIT sample database, and send response back to Amazon Lex.
We have created a script to expedite the process of creating Lambda function. Script need a Lambda IAM role ARN environment variable, to run successfully.
To set environment variable:
- In your Cloud9 IDE, open file export-env.sh.
- Provide the value for LAMBDA_ROLE_ARN. Replace
REPLACE-ME-LAMBDA-IAM-ROLE-ARN
with the IAM role ARN you copied in Step 2 above. - Save the file.
- In terminal window, run the following command to set the updated values for the LAMBDA_ROLE_ARN environment variable.
source export-env.sh
To create Lambda functions:
- In your terminal window run the following commands.
cd lambda bash ../create-lambda.sh
So far, you have successfully created:
- An Athena database with sample TICKIT database.
- An IAM role for the Lambda functions which gives permission to perform queries on Athena database and store its output on S3.
- Lambda functions for each intent
- In a new tab, open the Amazon Lex console at https://console.aws.amazon.com/lex/.
- If you are creating your first bot, choose Get Started. Otherwise, choose Bots, and then choose Create.
- On the Create your Lex bot page, choose Custom bot and provide the following information:
- App name: BIBot
- Output voice: Matthew
- Session timeout : 5 minutes.
- COPPA: Choose No.
- Choose Create.
Now, create the Count_Intent
intent , an action that the user wants to perform, with the minimum information needed. You add slot types for the intent and then configure the intent later.
To create an intent:
- In the Amazon Lex console, choose the plus sign (+) next to Intents, and then choose Create intent.
- In the Create intent dialog box, type the name of the intent as
Count_Intent
, and then choose Add.
Create the slot types, or parameter values, that the Count_Intent
intent uses.
To create slot types:
- In the left menu, choose the plus sign (+) next to Slot types. Choose Create slot type.
- In the Add slot type dialog box, add the following:
- Slot type name – cat_desc
- Description – Categories of events in the TICKIT database
- Choose Expand Values.
- Value – Type following values one at a time, and click + symbol to add.
- Musical theatre
- All opera and light opera
- All non-musical theatre
- All rock and pop music concerts
- Choose Add slot to intent.
- On the Intent page, keep Required checkbox unchecked. Change the name of the slot from
slotOne
tocat_desc
. Change the prompt toThe type of event
- Repeat Step 1 through Step 4 using the values in the following table:
Name | Description | Values | Slot name | Prompt |
---|---|---|---|---|
event_name | Name of events in TICKIT database | Joshua Radin, Rhinoceros | event_name | The name of the event |
- Add other slots to the intent, using the top row and values in the following table:
Name | Slot type | Prompt |
---|---|---|
event_month | AMAZON.Month | The month of the event |
venue_city | AMAZON.US_CITY | The city where the event takes place |
venue_state | AMAZON.US_STATE | The state where the event takes place |
venue_name | AMAZON.MusicVenue | The venue where the event takes place |
- Scroll down on the intent page and click Save.
Configure the Count_Intent
intent to fulfill a user's request.
To configure the intent, on the Count_Intent
configuration page (you should already be on the page), configure the intent as follows:
- Sample utterances – Type the following strings. The curly braces {} enclose slot names.
- How many tickets were sold
- Ticket sales for {event_name} in {venue_state}
- Count of tickets sold in {event_month} in {venue_city}
- Count of tickets sold in {venue_city} for {event_name}
- Tickets sold in {venue_state} in {event_month}
- Tickets sold in {venue_city} {venue_state} for {event_name}
- How many tickets were sold at {venue_name} in {event_month}
- How many tickets were sold for {cat_desc}
- Lambda initialization and validation – Leave the default setting.
- Confirmation prompt – Leave the default setting.
- Fulfillment – Perform the following tasks:
- Choose AWS Lambda function.
- Choose
BIBot_Count_Intent
. - If the Add permission to Lambda function dialog box is shown, choose OK to give the
Count_Intent
intent permission to call theBIBot_Count_Intent
Lambda function. - Leave None selected.
- Response - Perform the following tasks:
- Click + Add Message
- Type
via code hook
, and press Enter.
- Click Save Intent
Configure error handling for the BIBot
bot.
- Navigate to the
BIBot
bot. Choose Editor. and then choose Error Handling. - Use the Editor tab to configure bot error handling.
- In Clarification Prompts text box, enter
Come again?
and click + symbol.
- In Clarification Prompts text box, enter
- Click Save.
- To build the
BIBot
bot, choose Build. It can take some time to build. - To test the bot, in the Test Bot window, start communicating with your Amazon Lex bot. You can use any of the sample utterances to communicate with your bot. For eg.
- How many tickets were sold
- How many tickets were sold in boston?
- How many in chicago?
Go to your Cloud9 IDE and in the terminal window, run the following commands to create remaining Intents.
bash delete.sh
bash build-db.sh
bash build-bot.sh
- Go to Amazon Lex console at https://console.aws.amazon.com/lex/.
- You will see BIBot in BUILDING status. Wait for it to finish and go to READY status.
- Click Bots and then click BIBot.
- On the right, click Test Chatbot.
- In the Test Bot window, use following Sample Utterances:
- hello
- what were the top shows in houston in june
- how about chicago
Apply the AWS Credit to your AWS Account and follow the below steps to delete all the resources you created as part of this lab.
- Delete Lex bot, intents, slots, lambda functions and Athena database, by running the following command from your Cloud9 IDE.
bash delete.sh
- Delete Cloud9 IDE
- Delete S3 buckets
YOUR_INITIALS-bibot-tickit-data
YOUR_INITIALS-bibot-db-output
- Delete CloudFormation stack, created for IAM roles.