Quick Start — Develop a Chat Bot with AWS Lex + Lambda: Part 1
Chatbots are the rage these days, and for good reason. Conversational interfaces are going to be kickass. It’s intuitive (and fun) to have a dialog with your computer. This is a guide to easily develop your own chat bot.
Contents
- Part 1: Create chat bot in lex + lambda
- Part 2: Advanced bot: Understanding the lex dialog model and sessions
Part 1: Create chat bot in lex + lambda
Let’s make a bot that finds the price of Bitcoin in 3 simple steps:
Step 1: Create a custom lex bot
We’ll start off by creating a custom bot.
Step 2: Building the language model
Here’s where we give our bot the ability to understand conversations. Some terms used here:
- Intent: Intent is a skill the bot has. Our bot has one skill for now i.e. FindBitcoinPrice.
- Utterances: Sentences to invoke an intent. For find bitcoin price intent, an example utterance would be “what is the price of bitcoin on {Date}?”
- Slots: Values user must supply to an intent. In this example, it is the “{Date}” on which the user wants bitcoin’s value. Amazon provides a lot of built-in slot types, we can use `AMAZON.DATE` for this slot.
Let’s fill these up and build our bot.
Build will take a couple of seconds. You can now test your bot by asking it a question in Test Chatbot section.
Try asking “what’s the price of bitcoin on first feb?”.
Step 3: Adding the logic
Now our bot is able to understand questions and get inputs, we need to add “the logic to perform the skill” i.e. getting the dollar value of bitcoin. We’ll be adding this logic through AWS Lambda.
AWS Lambda lets us run code without having to create & host a server. Read more about “serverless” computing.
Workflow with lambda:
Once lex understands user’s sentence, it produces an input JSON. This input JSON goes to the code performing logic. Lex expects an output JSON in return. Both the input and output JSON have specific formats described in detail here. First let’s create a lambda to add our logic.
Step 3.a Create a Python 3.6 lambda
On your AWS console, create a lambda with python 3.6 runtime. Choose a role that has access to Cloudwatch logs (useful for lambda function logs.)
Step 3.b Write python code for processing request
This is the input JSON we get from lex on asking “what is the price of bitcoin on first feb?”:
{
'messageVersion':'1.0',
'invocationSource':'FulfillmentCodeHook',
'userId':'zbf4r3orv6z2df6ryhcay9ulu97pd4uj',
'sessionAttributes':{},
'requestAttributes':None,
'bot':{
'name':'CryptoBot',
'alias':'$LATEST',
'version':'$LATEST'
},
'outputDialogMode':'Text',
'currentIntent':{
'name':'FindBitcoinPrice',
'slots':{
'Date':'2019-02-01'
},
'slotDetails':{
'Date':{
'resolutions':[],
'originalValue':'first feb'
}
},
'confirmationStatus':'None'
},
'inputTranscript':"what's the price of bitcoin on first feb?"
}
Lot of fields there, I’ve highlighted the ones useful for us. We’ll use CoinAPI to get the price of Bitcoin. Add this python code to the lambda:
Change the API key in the above code and save the lambda. You can use the above input JSON as a test event to the lambda.
Step 3.c Connect lambda to lex bot
On the lex console, add the above lambda in Fulfillment section.
Save and build the bot. Try it out by typing your question in the test bot section.
Congratulations on developing your own conversational bot! Tinker with it further, try adding new skills, integrate it with slack or do your own thing.
The second part of this article will talk about the conversational model of lex with advanced topics like multi-turn dialogs, handling sessions and so on. Use the Lex developer guide for any reference or to explore further. Cheers!