Design and Algorithm that makes Amazon Lex to process any random input text from the conversations
Background about Amazon Lex:
As per the Amazon documentation, Amazon Lex is a service for building conversational interfaces into any application using voice and text. Amazon Lex provides the advanced deep learning functionalities of automatic speech recognition (ASR) for converting speech to text, and natural language understanding (NLU) to recognize the intent of the text, to enable you to build applications with highly engaging user experiences and lifelike conversational interactions.
Working of Amazon Lex:
Amazon Lex is a chatbot that facilitates dynamic conversations based on the pre-defined patterns of topics called as Intent and fields to capture from the conversations that are more relevant to the topic called as Slots. Both Intent and Slots are the building blocks to this chatbot framework. Each Intent can have multiple slots, as the conversation progress all the slots are captured and Intent is fulfilled. An AWS lambda can be triggered to take some action once the Intent is fulfilled. Depending on the business need an action can be anything that can be coded in AWS lambda i.e. calling an API, persisting data about the conversations or triggering some processing/parsing logic, etc.
A lambda can also be triggered to perform some initialization and input validation for each slot that is invoked in an Intent. And this is where I found a solution to solve the problem of capturing and processing random input text.
Problem Statement:
Amazon Lex resolves the slot values in an utterance only to the values that are defined as expected values, or it expands the resolution to related or similar values. Although the list of values is used to train the machine learning model to recognize values for a slot, it still won't allow some values (utterances) that can be valid or just allow random text to be processed further.
I had a business case were this random text needs to be processed, captured and stored in the database. Scenario was Lex Bot will ask some questions which user may answer in a sentence(utterance) that will less likely fit into any of the defined slot values. Bot will not allow those random values and keep retrying/asking the same question/slot again and again, and will eventually go for a fallback Intent.
Recommended by LinkedIn
Solution:
I found a work around that will allow Lex to process these random values and capture them in the session attributes. Once all the slots are captured in the session attribute, I am just invoking a logic to persist the conversation to a database. This algorithm allows to move to the next slot by capturing previous slot values in the session attribute and force the move to the next slot by chaining the slots in an ordered list. In short, it just traverse the ordered slot list that is defined for a particular intent.
Python Code:
class RoleAndWork:
slot_list =
["WorkingOn", "TeamForumName", "ProductName", "CustomerGroupSupported"]
@staticmethod
def process(event):
if event['invocationSource'] == 'DialogCodeHook':
return RoleAndWork.validation(event)
else:
return RoleAndWork.fulfillment(event)
@staticmethod
def validation(event):
session_attributes = Utility.get_session_attributes(event)
slots = Utility.get_slots(event)
for index, slot in enumerate(RoleAndWork.slot_list):
if not session_attributes.get(slot):
session_attributes[slot] = event['inputTranscript']
if index < (len(RoleAndWork.slot_list)-1):
return {
'sessionState': {
'sessionAttributes': session_attributes,
'dialogAction': {
'type': 'ElicitSlot',
'slotToElicit': RoleAndWork.slot_list[index+1]
},
'intent': {
'name': 'RoleAndWork'
}
},
'sessionId': event['sessionId'],
'requestAttributes': event['requestAttributes'] if 'requestAttributes' in event else None
}
else:
# Capture session attributes in ddb
return RoleAndWork.fulfillment(event)
return {
"sessionState": {
"sessionAttributes": session_attributes,
"dialogAction": {
"type": "Delegate",
"slots": slots
}
}
}
@staticmethod
def fulfillment(event):
# All the fulfillment code
Some utility functions that are used here are very simple, it returns the session attributes and slots from the event that the lambda function receives.
I hope you find this solution simple enough to move through all the slots in an intent without restricting its values and flexible enough to call fulfillment logic to close the intent as needed.
Happy Coding ! 😊
Good read Amruta!