Demystify AWS Lex BotsAsanka NissankaBlockedUnblockFollowFollowingJul 8Source : https://www.
com/chatbot-development/amazon-lex“Bot” is a popular and booming term these days and for sure an interesting technology for all with different knowledge backgrounds.
Unlike the old times where you had to build everything including the machine learning models, now there are lot of platforms and frameworks to ease and speed up the bot building process.
Depending on the business use case and requirement we have several options to choose from.
If you are running on the cloud, all major vendors like Amazon(Lex), Microsoft(Bot Framework), Google(Dialogflow), IBM(Watson Assistant) etc, have published their own bot platforms to be used as a service.
If you have limitations on data sharing and running on cloud you can use Rasa like platforms to build and host everything on-premise.
Recently I did some chat bot PoCs using couple of the above services and for the curiosity I replicated the same Bot using multiple services.
That was really an interesting and worthy effort since I could briefly evaluate and compare them with each other.
The good thing is that almost all follow a common terminology and if we dive deep in one service we can understand the others with little effort.
However in this article I am going to share the experience I got building chat bots using Amazon Lex.
Lex is one of Amazon’s managed service offering for machine learning which can be used to build conversational applications over voice and text.
It’s the same conversational engine that powers Amazon Alexa.
Before moving into details, there are few terms we need to know.
Components of a Lex BotThe best way to explain the components of a lex bot is via the intro image on AWS console.
Components of a Lex Bot (Source : AWS Console)Building a Lex BotThere are couple of approaches we can follow to build bots using Lex,Use AWS console only without any coding — For simple use cases like static informational botsUse AWS console and lambda function for the fulfillment functionality — For intermediate use cases where we want to respond with dynamic functionalityUse AWS console and lambda function(s) for both code hook and fulfillment functionality — For advance use cases where we need more control over the functionalityLet me explain bit more on the usage of lambda functions here.
Each intent of a lex bot has two main triggers, that can invoke lambda functions.
Initialization and validation code hook — Handle thebusiness logic required to validate the user’s inputFulfillment — Trigger once all the slots of an intent are filled.
We can complete(Fulfill) the intent here.
We can either configure the same lambda function for both triggers or configure two seperate lambda functions for each trigger.
In most cases we can use a single lambda function for both triggers across all intents.
How we build the business logic inside the lambda function is completely up to us, there are several approaches we can follow here.
The complexity of the logic depends on how you define your intents and the navigation between them (usually referred to as dialogue flow).
The most important thing to remember is don’t push the users to follow long predefined conversation flows to get the things done.
My recommendation is to break down the intents to the smallest possible unit and allow switching between them freely.
In programming terms instead of using complex and deep if.
statements, use switch.
Then your bot will be very natural for users to get things done easily.
Okay, so let the fun begin 🙂 AWS has documented everything we need to know about Lex lambda integration, there is no doubt with that.
However there are specific formats for the responses that we generate from the lambda functions, and even after working on several lex bot projects I still need to check on the documentations to find the exact format to return.
Anyways there is no point remembering these as well.
So to make my life easy with future lex bot projects, I implemented a node module that will simplify the response generation process.
This module follows the builder pattern to simply collect the necessary attributes via method calls and return the reponse object in the relevant format.
I assume this module is a common necessity for anyone working on a lex bot project, so I published it on NPM and made it open source.
You can find the NPM module from here and the source code from here.
I’ll try my best to keep this updated with the AWS Lex Documentation.
And I am more than happy to hear your feedback on this.
Few Additional ReadsIn case you need more details about the responses and types refer the links below.
SSML Support in Text ResponsesSSML Tags Supported by Amazon Polly.