AWS Machine Learning Blog
Smarter FAQ bots with Amazon Kendra
We often have questions when making a choice about a product or service. When was the last time you found yourself at the IT help desk at work? You probably had questions like “What time does the IT Help Desk open?” or “Can I get a temporary machine while you repair my laptop?” Quick and accurate responses to such queries improve customer satisfaction. Support staff can easily answer these questions, but it may not be the best use of their time. Such repetitive work is well suited for automation. Customers get a response right away, and the support staff can focus on solving problems.
To enable such a conversation with a bot, you could model each question as a separate intent. Sample utterances are designed to capture the user’s questions in natural language form, and the bot responds to provide the answers. This works well for a few questions. But as the number of questions grows, so does the number of intents, which leads to an ever-changing bot definition. This can be a bit inefficient if most of the responses are just static text. An alternate approach is to ingest the list of questions into a database, and query the database to answer each question. This is similar to what the QnABot bot does. With this design, you don’t have to add an intent for every question. However, if the natural language input varies from the database entry, the search has to be smart enough to accommodate linguistic variations. For example, it should provide the same answer for “How long does it take to fix a screen” and “What’s the turnaround time to fix a screen?”
With the recent launch of Amazon Kendra, you can now use natural language questions to get the answers you’re looking for, whether that is an FAQ, an answer in a document, or as a link to an entire document. Amazon Kendra can extract specific answers from unstructured data. You simply point Amazon Kendra at your content, and Amazon Kendra indexes the content to provide the answers. You can use the Amazon Kendra console or APIs to index your FAQs and documents to create a search index. Amazon Kendra uses the index to find the closest match to a search query, and returns the corresponding answer.
This post shows how you can integrate an Amazon Lex chatbot with Amazon Kendra, so that end users can query Amazon Kendra from messaging platforms such as Slack. The diagram below illustrates the idea:
Building an Amazon Lex chatbot
This post uses the following conversation to model a bot.
User: Where’s the IT Help Desk?
Agent: 2nd floor; room 221 (two rights after you get out of the elevators)
User: When does the Help Desk open?
Agent: 11am – 2pm.
User: Can I come in at noon?
Agent: Sure. What’s the nature of your problem?
User: I have a cracked laptop screen.
Agent: For verification purposes, can I get the last four digits of your employee ID?
User: 1234
Agent: Got it, we’ll see you then to take a look at your screen.
User: How long does it take to fix a screen?
Agent: Laptop screen repairs typically take 4 hours.
User: What do I need to bring with me for a screen repair?
Agent: For a laptop screen repair, please bring your employee badge, laptop, and VPN fob.
User: How do I access my documents while my laptop is being repaired?
Agent: You can use Amazon Workdocs to access your documents.
User: Can I access Amazon WorkDocs from my tablet?
Agent: Here is an excerpt from a document that might help: (displays relevant text from the document)
User: How do I use WorkDocs again?
Agent: Here are some documents I found… (displays list of relevant support documents)
User: Is there a coffee shop nearby?
Agent: There is on the 3rd floor and it’s open from 8 AM to 4 PM daily.
The first step is to build an Amazon Lex bot with intents to book an appointment and check appointment status. The MakeAppointment
intent elicits the information required to make an appointment, such as the date, time, employee ID, and the nature of the issue. The CheckAppointmentStatus
intent provides status of the appointment. When a user asks a question that the Lex bot can’t answer with these intents, you will use the built-in Fallback intent to connect to Amazon Kendra to search for an appropriate answer.
Deploying the sample Amazon Lex bot
To create the sample bot, perform the following steps. This creates an Amazon Lex bot called help_desk_bot
, and an AWS Lambda function called help_desk_bot_handler
.
- Download the Amazon Lex code here, and the Lambda code here.
- In the AWS Lambda console, choose Create function.
- Enter the function name
help_desk_bot_handler
. - Choose the latest Python runtime (for example, Python 3.8).
- For Permissions, choose Create a new role with basic Lambda permissions. We will update this role later to allow your Lambda function to access Amazon Kendra.
- Choose Create function.
- Once your new Lambda function is available, in the Function code section, choose Upload a .zip file, and for the Function package, select the
help_desk_bot_lambda_handler.zip
file that you downloaded. - Choose Save.
- In the Amazon Lex console, choose Actions, Import.
- Choose the file
help_desk_bot.zip
that you downloaded, and choose Import. - Choose the Lex bot
help_desk_bot
in the Amazon Lex console. - For each of the intents, including the fallback intent [help_desk_fallback], choose AWS Lambda function in the Fulfillment section, and select the
help_desk_bot_handler
function in the dropdown list. - For each intent, you will be prompted to Add permission to Lambda function. Choose OK.
- Once all of the intents have been updated, choose Build.
At this point, you should have a working Amazon Lex bot that is not yet connected to Amazon Kendra.
Creating an Amazon Kendra index
You are now ready to create a Kendra index for your documents and your FAQ. To create a Kendra index, complete the following steps:
- On the Amazon Kendra console, choose Launch Amazon Kendra.
- If you have existing Kendra indexes, choose Create index.
- Enter an Index name, such as “it-helpdesk,” and an optional Description, such as “IT Help Desk documents and FAQs”.
- For IAM role, choose “Create a new role” to create a role to allow Amazon Kendra to access CloudWatch Logs.
- For Role name, enter a name, such as “cloudwatch-logs”.
- Choose Create.
While Amazon Kendra creates your new index, upload your content to an S3 bucket.
- On the Amazon S3 console, create a new bucket, such as “kendra-it-helpdesk-docs-<account#>”
- Keep the default settings and choose Create bucket.
- Download the following sample files, and upload them to your S3 bucket:
Once your new Kendra index has been created, you can add your content.
- On the Amazon Kendra console, choose FAQs, and Add FAQ.
- For FAQ name, enter a name, such as “it-helpdesk-faq”
- For Description, enter an optional description, such as “FAQ for the IT Help Desk Lex bot”
- For S3, browse S3 to find your bucket, and select the “help-desk-faq.csv” file.
- For IAM role, choose “Create a new role” to allow Amazon Kendra to access your S3 bucket.
- For Role name, enter a name such as “s3-access” and choose Add.
- Stay on the page while Amazon Kendra creates your FAQ.
- On the Amazon Kendra console, choose Data sources, and under the S3 option, Add connector.
- For Data source name, enter a name, such as “it-helpdesk-documents”.
- For Description, enter a value, such as “IT Help Desk documents to be indexed by Kendra”.
- Choose Next.
- For Enter the data source location, choose Browse S3, and select your S3 bucket.
- For IAM role, choose “Create a new role” to allow Amazon Kendra to access your S3 bucket.
- For Role name, enter a name, such as “access-data-source”.
- For Set sync run schedule, select an option such as “Run on demand”.
- Choose Next, and then choose Create.
- Stay on the page while Kendra creates your data source.
- Once your data source has been created, you need to synchronize it. Choose Sync now. You can run this on demand whenever you add new documents, or on a specific schedule.
Once your data source is synchronized, you can try some searches on the Kendra Search console. For example:
- When is the help desk open?
- When does the help desk close?
- Where is the help desk?
- Can I access WorkDocs from my phone?
Now that you have a working Kendra index, you need to update the Amazon Lex bot code with the Kendra index ID.
- On the Amazon Kendra console, navigate to your new index.
- Copy the Index ID
- On the AWS Lambda console, choose the
help_desk_bot_handler
function. - In Environment variables, choose Edit, and Add environment variable.
- For Key, enter “KENDRA_INDEX”.
- For Value, paste the Kendra Index ID that you copied.
- Choose Save.
The last step is to allow your Lambda function to query Amazon Kendra.
- On the AWS Lambda console for your
help_desk_bot_handler
function, choose the Permissions tab.
- Choose the IAM role listed under Role name.
- Choose Add inline policy.
- Under Service, choose “Kendra”.
- Under Read, choose “Query”.
- Under Resources, choose Add ARN.
- For Region, choose Any.
- For Index Id, paste in your Kendra index Id.
- Choose Add.
- Choose Review policy.
- Enter a Name, such as “queryKendra”.
- Choose Create policy.
Now your Amazon Lex bot is able to execute Amazon Kendra queries. You can test this on the Amazon Lex console.
Deploying on a Slack channel
The final step is to connect your bot to a Slack channel.
To create a Slack channel association with your bot, complete the following steps:
- On the Amazon Lex console, choose Settings. You will need to publish a version of your bot to integrate it with Slack.
- Choose Publish, and in the Create an alias field, enter an alias name such as “test”. Choose Publish.
- Once your alias is published, choose the Channels
- Under Channels, choose Slack.
- Enter a Channel Name, such as “slack_HelpDeskBot”.
- Choose “aws/lex” from the KMS Key drop-down.
- Select your “test” Alias
- Provide the Client Id, Client Secret, and Verification Token for your Slack application.
- Choose Activate to generate the OAuth URL and Postback URL.
Use the OAuth URL and Postback URL on the Slack application portal to complete the integration. For more information about setting up a Slack application and integrating with Amazon Lex, see Integrating an Amazon Lex Bot with Slack.
Using a Lambda function to get answers from Amazon Kendra
When the user asks a question, the Lex bot’s fallback intent is triggered if the user’s input does not match any of the configured Lex intents. The fulfillment code in the Fallback intent can be used to query the Kendra index, and Amazon Kendra will surface a specific answer, which can be (1) a direct answer from an FAQ; (2) a relevant excerpt from an indexed document, along with a link to the document; or (3) a list of relevant documents for the user to choose from.
Here is the Lambda integration Python code for this blog post, which can be used for any Lex/Kendra integration:
Amazon Lex allows you to create a convenient, natural language chat experience that your users can interact with using a familiar interface, such as Slack. Amazon Lex allows you to curate specific types of interactions through intents, including transactional capabilities (such as scheduling an appointment with the Help Desk). By integrating Amazon Lex with Amazon Kendra, you can extend this experience with an additional natural language capability, enabling users to get answers to questions based on existing content in FAQs and documents. Amazon Kendra can surface direct answers from FAQs, relevant document excerpts, and links to relevant documents.
Conclusion
This post demonstrates how to integrate Amazon Lex and Amazon Kendra to answer queries received via messaging platforms such as Slack. Amazon Kendra provides an intuitive way to search using natural language, and returns accurate answers, so your users can discover information stored within vast amounts of content. Integrating with Amazon Kendra simplifies your bot design, and delivers natural language answers that can fit seamlessly into your automated conversations. For more information about incorporating these techniques into your bots, please see the Amazon Kendra documentation.
About the Authors
Brian Yost is a Senior Consultant with AWS Professional Services. In his spare time, he enjoys mountain biking, home brewing, and tinkering with technology.
As a Product Manager on the Amazon Lex team, Harshal Pimpalkhute spends his time trying to get machines to engage (nicely) with humans.
As a Solutions Architect, Niranjan Hira is often found near a white board helping our customers assemble the right building blocks to address their business challenges. In his spare time, he breaks things to see if he can put them back together.