Building a Chatbot using AWS Lex

Connexis
6 min readDec 9, 2020

With dramatic developments in artificial intelligence and machine learning, chatbots have immensely become popular across various sectors. As health services become patient-centric, offering personalized and satisfactory experience, healthcare providers started integrating advanced AI Chatbot into their system.

How chatbots are becoming game-changers in Healthcare and Nutrition

Artificial intelligence is making a huge impact on Healthcare and nutrition and it will increase over the coming years, some healthcare executives are already using AI such as predictive analysis/machine learning to solve various issues, and an increasing number of them are looking into chatbots.

Coming to chatbots, they are very useful in prioritizing and guiding the patients to receive appropriate help. I will be creating a chatbot/Healthcare assistant using AWS lex, AWS lambda, AWS RDS.

AWS uses a similar architecture that other cloud-based conversational AI providers does, we will need to create intents, responses, response cards and AWS lambda to give a personalized recommendation. Responses are based on user stories according to which we create the dialog flow.

Intents in chatbot

Lex allows us to create intents for which we can add a range of invocation phrases. As the name suggests, intents mean the intention of a user, that he can invoke by using one of the defined terms.

For example in Healthcare/nutrition bot intents can be :

  • How many calories should I eat today?
  • Track my meals
  • I am having a fever
  • How much should I exercise today
  • Track my blood glucose

For each intent, we need to train the bot with possible user utterances that a user can ask when invoking an intent.

Like here if a user wants to track his diet:

Here the NLP capability of Lex comes into the picture where based on these utterances it invokes the intent/phrase and the next step is to collect entities/slots, these are the values that lex requires from the user to fulfill his request.

Slots can be a numeric value, date of birth, Name, Age etc. For each slot created we can makes sure that bot is collecting that from the user before proceeding to the next slot by asking questions predefined by us, we can also prioritize the slots.

Response

Responses to most of the intents can be done through Lex console with confirmation prompt, follow up prompt, and response cards. We can also formulate personalized responses using AWS lambda python and send the response to lex.

For intents that do not require the lambda function to formulate response, we will add response from lex console.

For giving personalized responses to users which is very much required in a healthcare/nutrition bot. We will write a lambda function in python that receives a JSON payload from lex and we will formulate the response using RDS and send a JSON payload with necessary values to lex. Which is passed to the user in a form of text/speech.

Architecture of Bot

AWS lambda for personalised responses

Since the patients details will be stored in the RDS database we need to connect the lambda with RDS for that we will use the pymysql library, next we will package the library and upload to AWS lambda and then write out handler code based on intents.

Let’s say if a user is asking to “Analyse his diet “ then the lambda will have to collect the information about his diet like mealType, calories, food items etc from the RDS database, and then we will formulate a response accordingly.

Connecting to RDS is very easy, I used this line of code :
“”Connection = pymysql.connect(endpoint,user =username, passwd= password)”
Note: Make sure that AWS Lex, Lambda, and RDS are in the same region and lambda is also configured with the same VPC as the RDS database.

Once connected we can query the required table and access the necessary information, sometimes to query the required table we extract incoming json payload from lex for ex: userid, intentname.

Here is a sample input event :

{

"currentIntent": {

"name": "intent-name",

"nluIntentConfidenceScore": score,

"slots": {

"slot name": "value",

"slot name": "value"

},

"userId": "User ID specified in the POST request to Amazon Lex.",

"inputTranscript": "Text used to process the request",

"invocationSource": "FulfillmentCodeHook or DialogCodeHook",

"outputDialogMode": "Text or Voice, based on ContentType request header in runtime API request",

"messageVersion": "1.0",

.

.

.

Click here to view the whole input event.

Userid and intent name will be extracted from input response and query the data of that user respectively.

Lambda is a powerful tool that we can use to do calculations and perform any type of analytics. It can interact with other AWS services like sagemaker, Polly and comprehend to give intelligent responses.

Upon completing the lambda task we will formulate the response. Each response will be created based on the invoked intent. Here our intent was to analyze my diet so we will collect the data coming from RDS and computed data and feed them in a script creating a personalised response for the user.

In the above response ‘{}’ are the data points that we received from DB.

Response cards

There is also a field Response card which suggests to the users some related intents he can directly invoke without any need of typing, we can add multiple response cards and it makes it easy for users to interact with chatbot and spend more time with it. There are other fields in the response that are not covered in this article. Together with the response and response card formulated we can now send this response to Lex for displaying it to the user.

Conclusion

This is just the start, there are a ton of possibilities with AWS lex, and with the vast services that AWS has in store we can create an amazing healthcare assistant/ chatbot who will not only interact with the user on What to Eat, How to eat, how much calories to burn but also book an appointment with a doctor, guide on first aid and emergency care. We can also send reminders for eating medicine at the right time and many more.

In coming years, this will decrease the burden on healthcare executives by decreasing the amount of time they spend on each patient in managing and this is a huge plus for focusing on other important issues.

--

--