Build an Amazon Lex Chatbot with Microsoft Excel
This is a guest post by AWS Community Hero Cyrus Wong.
Our institution (IVE) here in Hong Kong has begun experimenting with Amazon Lex in teaching, research, and healthcare. We have many non-technical employees, such as English teachers in IVE and therapists from IVE Childcare, Elderly and Community Services Discipline, who don’t have the technical know-how to build natural language conversational bots (chatbots) in the Amazon Lex console. We completed a few trial projects to build Amazon Lex chatbots for non-technical users. The non-technical users filled in an Excel spreadsheet with their questions, and then developers copied their questions to the Amazon Lex console. Any time a user wanted to change anything in their chatbot they had to go through the same copy and paste process.
We then thought to ourselves: why don’t we use Excel directly to build a chatbot and stop the repeated copy and paste work?
All our employees know how to use Microsoft Excel, so we decided to build a project called “ExcelLexBot” which lets them use Excel to create chatbots with minimum programming skills.
ExcelLexBot is a serverless application that converts an Excel file (xlsx) in predefined format into an Amazon Lex chatbot. The ExcelLexBot we’ve built can be used in many ways, all of which are designed to be built quickly and with minimal development skills. Teachers, such as myself, can use ExcelLexBot to create interactive chat-like tests and assignments. Students use it to add chatbot functionality into their final year-end projects, and they can do it fast: 80 students from the Higher Diploma in Cloud and Data Centre Administration created 80 chatbots within one hour.
In this post, I’ll explain how ExcelLexBot works and how to deploy and use it.
Amazon Lex concepts
Amazon Lex is a service that allows developers to build conversational interfaces for voice and text into applications. With Amazon Lex, the same deep learning technologies that power Amazon Alexa are now available to any developer, so you can quickly and easily build sophisticated chatbots.
An Amazon Lex chatbot consists of three types of components:
- Bot – A bot contains all of the components of a conversation.
- Intent – An intent represents an action that the bot’s user wants to achieve (scheduling an appointment, ordering flowers, and so forth).
- Slot Type – a slot type defines how data in the slot is recognized and handled. Amazon Lex supports built-in slot types from the Alexa Skills Kit. You can create custom slot types in your intents.
For more details, please refer to Amazon Lex – Build Conversational Voice & Text Interfaces in Amazon Lex Concepts.
Architecture and design
ExcelLexBot uses the AWS Serverless Application Model (AWS SAM) to create an AWS SAM Amazon Lex application connecting to Excel. AWS SAM is a specification for describing Lambda-based applications. It offers a syntax designed specifically for expressing serverless resources.
ExcelLexBot builder stack
It consists of an Amazon S3 bucket and an ExcelLexBuilder Lambda event hook function. When a user uploads an Excel file, the ExcelLexBuilder Lambda function performs the following actions:
- Parses the Excel file.
- Generates an ExcelLexBot SAM template.
- Creates a Lambda code package.
- Uploads the Lambda code package to S3
- Creates changeset from the ExcelLexBot SAM template.
- Deploys the chatbot stack.
ExcelLexBuilder reads the Excel file and generates the ExcelLex SAM Template and Deployment Package. It creates one JSON file per worksheet as currently, and I use AWS Lambda-backed custom resources to execute the JSON file to create the chatbot. You can check the details of using JSON to create the Amazon Lex Bot with Exercise 1: Create an Amazon Lex Bot (AWS CLI).
For the basic Lex terminology (for example, what is Intent), please refer to Amazon Lex – Build Conversational Voice & Text Interfaces on the AWS blog.
Lex Dispatcher is the default code hook that captures both the dialog and fulfillment event. It performs the following tasks:
- Forwards requests to other Lambda functions based on naming convention. There are three naming rules.
- The first rule “Capture All,” forwards both Dialog and Fulfillment event, if there is a Lambda function with the same name as the intent, for example MakeAppointmentIntent.
- The second rule, “Capture Dialog,” forwards the Dialog event to a Lambda function with name end with “_dialogCodeHook” for example, “MakeAppointmentIntent_dialogCodeHook”.
- The third rule, “Capture Fulfillment,” forwards the Dialog event to a Lambda function with a name ending with “_fulfillmentActivity” for example, “MakeAppointmentIntent_fulfillmentActivity”.
You can use Lambda functions to perform initialization and validation, fulfillment, or both in your intent configuration.
- If you defined “Capture All”, then you should not define “Capture Dialog” or “Capture Fulfillment”. If you haven’t defined “Capture All”, you must define both “Capture Dialog” and “Capture Fulfillment” at the same time.
- When the fulfillmentActivity event happens, it saves the intent information into an Amazon DynamoDB table with the same name of the intent. And, all tables have Auto Scaling for Amazon DynamoDB enabled with throughput range from 1 to 50 capacity units.
- When the fulfillmentActivity event happens and the user provides email addresses in Excel, it will publish the intent information to Amazon SNS and let Amazon SNS send emails to those email addresses.
This design can handle the common tasks such as saving the intent information and emailing it to the user, and decoupling the custom code hook from chatbot stack.
If you want to migrate your existing Amazon Lex chatbot to ExcelLexBot, you need to fill in the Excel worksheet and redeploy all Lambda function code hooks according to the naming convention. There isn’t any special magic behind the Lex Dispatcher function. It just gets the current intent name, appends a proper suffix, and calls another Lambda function.
When the “FulfillmentCodeHook” event happens, it saves and sends email before calling another Lambda function.
With an ExcelLexBot you define the codehook as usual. You just need to define the code hook as usual. Amazon Lex Chatbot as Lex Dispatcher does not modify anything and it just forwards the event to the original code hook. You can check out Amazon Lex and AWS Lambda Blueprints to learn how to write code hooks.
The Excel format follows the Convention Over Configuration Principle:
- The worksheet name will be the Amazon Lex component name.
- The type of component maps to the suffix of the worksheet name and there are three types of components: Bot, Intent, and Type. The following diagram for ExcelLexBot illustrates this, and it can map to the original Amazon Lex as well.
A Bot can have 1 or more Intent. Each Intent can have 1 or more Question (Slot). Each Question has to set a Type (SlotType). Type can be Lex Built-in or Custom Slot Type (user defines in worksheet). For Custom Slot Type, there are one or more enumeration value (Possible Value). Each enumeration value can have synonyms.
I have reformatted the JSON data into Excel data so you can refer to the AWS blueprints for the detailed meaning of most fields.
A few things to note:
- The Excel file name only contains English characters. For example, ChatBot- Copy.xlsx or ChatBot2.xlsx are not valid.
- We renamed “slot” into “question”, “slotType” into “type”, and “slotConstraint” into “Constraint” to use more familiar terms.
- Sample Utterances, Receiver Email, Question and Possible Value allow multiple line inputs. For example:
That represents the JSON Array in the original Lex JSON Receiver Email is the only additional field that does not exist in the Lex JSON schema.
- Max Attempts in the intent worksheet will populate to the “maxAttempts” of the slot (question) as users don’t care about the number of retry per question.
- The content cell value only supports ASCII characters, and it will remove non-ASCII
- Description – A description of the bot.
- Abort Statement – If Amazon Lex can’t understand the user’s input in context, it tries to elicit the information a few times through user prompts. After that, Amazon Lex sends the message defined in Abort Statement to the user, and then terminates the conversation.
- Clarification Prompt – When Amazon Lex doesn’t understand the user’s intent, it uses this message to get clarification.
- The name of the bot is the name of this worksheet, that is, ExcelLexBot and it will include all intents defined in this Excel workbook. These are OrderFlowersIntent and MakeAppointmentIntent.
For more details, please refer to the put-bot cli command.
This snippet is the top section of MakeAppointmentIntent worksheet.
- Description – A description of the intent.
- Max Attempts – The number of times to prompt the user for information.
- Confirmation Prompt – Prompts the user to confirm the intent before fulfilling it.
- Sample Utterances – Each line is a sample utterance, and it converts to an array of sample utterances that are configured for the intent.
- Receiver Email – Each line is an email address. . A record indicating intent fulfillment will be sent to this email address via SNS..
- The name of the intent is the name of this worksheet.
This snippet is the question section of MakeAppointmentIntent worksheet.
I learned that users find it easier to understand making a questionnaire than the idea of Intent. For developers or IT students, the idea is just to create an online form for a set of variables in strong type language, for example, AMAZON.DATE Date = 10/5/2017. Amazon Lex just helps you to replace any complicated web form with data type validation. My students feel that using ExcelLexBot to build a chatbot is easier than creating a web form.
- Name – The name of the question (slot). It will be the attribute name in Amazon DynamoDB and JSON data for code hook.
- Description – A description of the question (slot).
- Question – Each line is a question text. The original AWS Lex JSON uses valueElicitationPrompt with the list of messages in string. All of our users don’t know SSML so ExcelLexBot does not support SSML.
- Type – The type of the question (slot), either a custom slot type that you defined or one of the built-in slot types.
- Constraint – Specifies whether the slot is required or optional.
- Priority – Directs Lex to follow the order in which to elicit this slot value from the user. For example, if the intent has two questions (slots) with priorities 1 and 2, Amazon Lex first elicits a value for the question (slot) with priority 1. If multiple questions (slots) share the same priority, the order in which Lex elicits values is arbitrary.
- Sample Utterances – If you know a specific pattern with which users might respond to an Amazon Lex request for a question (slot) value, you can provide those utterances to improve accuracy. This is optional. In most cases, Amazon Lex is capable of understanding user utterances.
For more details, please refer to the put-intent cli command.
Output is at ChatBot_ExcelLexBot.json, and here is the partial output.
- Description – A description of the Type (slot type).
- Value Selection Strategy – Determines the slot resolution strategy that Amazon Lex uses to return slot type values. The field can be set to one of the following values:
ORIGINAL_VALUE – Returns the value entered by the user, if the user value is similar to the slot value.
TOP_RESOLUTION – If there is a resolution list for the slot, return the first value in the resolution list as the slot type value. If there is no resolution list, null is returned.
- Possible Values – A list of Possible Values (EnumerationValue) string that defines the values that the slot type can take. You can create synonyms by newline.
For more details, please refer to the put-slot-type cli command.
To deploy the application, perform the following steps:
- Create an Amazon S3 source bucket in the us-east-1 AWS Region.
- Download the two files in the Deployment folder on GitHub: excellexbot.yaml and lex_builder_function.zip.
- Upload the source packages that you get from the Deployment folder, lex_builder_function.zip, into the S3 source bucket. If you are not familiar with the Amazon S3 console, please follow this tutorial: How Do I Upload Files and Folders to an S3 Bucket?
- In the AWS CloudFormation console, choose Create Stack.
- Select Upload a template to Amazon S3, choose yaml, then choose Next.
- Specify the following parameters, and choose Next.
- Stack name: excellexbot (A unique name for the stack in your AWS region).
- SourceBucket: The name of the S3 source bucket that you created in Step 1.
- ExcelBucketName: The name of the S3 bucket that you want to create. The bucket name must be unique. When you delete the AWS CloudFormation stack, this bucket remains.
- On the Options page, choose Next.
- Select all acknowledgment boxes, and choose Create Change Set.
- When the change set has been created, choose Execute.
- Wait while the AWS CloudFormation stack is created.
You can reference “Analyze Emotion in Video Frame Samples Using Amazon Rekognition on AWS” Deploying the “HowWhoFeelInVideo” section and those deployment steps are similar.
Try your deployment
- Download the sample excel file ChatBot.xlsx from GitHub.
- In the S3 console, upload a ChatBot.xlsx into the bucket for ExcelBucketName.
- Open the AWS CloudFormation console.
Wait until the chatbot stack status changes into CREATE_COMPLETE. The stack name is the lowercase version of the excel file name.
- (Optional) If you input email for the Intent, you will receive an email to confirm your subscription and click on the Confirm subscription
- Open the Amazon Lex console, and choose ExcelLexBot
- Choose OK. Amazon Lex will update the resource permission to the Lex Dispatcher Lambda function. AWS CloudFormation already granted the permission to Amazon Lex, but if you don’t click on add the permission here, it will pop up every time when you are testing the chatbot.
- Choose the Test Chatbot bar on the left side.
- You can test the Chatbot from the Amazon Lex console.
- This deployment contains Example Bot: ScheduleAppointment and you can test by following that example.
- Open the Amazon DynamoDB console and the table ChatBotMakeAppointmentIntent will contain a new item.
- (Optional) If you input email for the Intent, you will receive an email with the intent information.
Add a code hook
In this section, you update the configuration of the MakeAppointment intent to use the Lambda function as a code hook for validation and fulfillment activities. For the details about this code hook, read Step 3: Update the Intent: Configure a Code Hook.
- Download the two files in the deployment folder on GitHub: codehook.yaml and codehook.zip.
- Repeat the deployment step in the previous Deploying ExcelLexBot section.
- Specify stack name and parameter:
- Stack name: codehook (A unique name for the stack in your AWS Region.)
- SourceBucket: The name of the S3 source bucket that you created in Deploying ExcelLexBot Step 1.
- Wait for the code hook step status changes to CREATE_COMPLETE.
- You can test the chatbot again and it will have the same features as Example Bot: ScheduleAppointment.
Update and delete a chatbot
If you delete the Excel file from the S3 bucket, it will delete the chatbot stack. Warning! If you delete the chatbot stack, it will also delete the DynamoDB table, so you have to back up the data before you delete the stack.
To make updates, you need to delete the Excel and wait for the chatbot stack completely delete. Then, you upload the updated Excel into Amazon S3.
- The AWS Management Console can validate input immediately, but ExcelLexBot cannot validate the input.
- Lex allows reusing intents, slot-types across bots – but the Excel based solution assumes independence. Basically, downplay the limitation.
- Amazon Lex protecting itself by throttling aggressive calls, so you cannot delete multiple Excel files or chatbot stacks at the same time as Lex will return “There is a conflicting operation in progress” if there are too many “delete Bot API calls” at the same time. However, you can create multiple chatbots at the same time by uploading several Excel files to Amazon S3.
Non-technical users are sometimes hesitant to learn new tools. However, if you send them a few example Excel files, and ask them to fill in their questions in a spreadsheet, most of them can do it without hesitation.
ExcelLexBot can help build chatbots through Excel easily. IVE teachers can just copy assignments or assessments from Word to Excel, then we can adopt Amazon AI services to enhance the IVE teaching experience.
This project has been developed in collaboration with four of my students from the IT114115 Higher Diploma in Cloud and Data Centre Administration: LI, Ka Wing, LAU, Tik Haang, HON, Ho Yuk, and MA, Shi Sum. Also, thanks to the AWS Academy curriculum, which helps my students learn how to use AWS services in depth!
Learn how to build better bots with Amazon Lex.
About the Author
Cyrus Wong is a Data Scientist at the Hong Kong Institute of Vocational Education (Lee Wai Lee) Cloud Innovation Centre and an AWS Community Hero. He has achieved all 7 AWS Certifications and enjoys sharing his AWS knowledge with others through open-source projects, blog posts, and events.