Building a Chatbot using AWS Lex

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

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

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

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

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

Conclusion

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.

Connexis is a technologically driven company