5 Essential elements to build highly successful chatbot (Conversational AI)
By: Niwratti Kasture
Tags: Machine Learning, Chatbots, NLP, Conversational Ai, Artificial Intelligence

Conversation should touch everything, but should concentrate itself on nothing. — Oscar Wilde

Using AI to offer more personalized customer experiences at scale is one of the top priorities for companies (products, services) across the globe, and conversational AI plays a pivotal role in this space.

Our interaction with technology touch points have increased exponentially in daily life. It starts from getting morning news updates and ends with setting an alarm and reminder for next morning by giving a voice command to the Google assistant. Similarly there are more such instances during the day for ex. booking a cab, checking weather, interacting with bank or calling someone with a voice command etc.. it is all done through various mobile applications or sites. Each of these technology points are conversation with a virtual assistant which tries to give you a personalized experience based on your usage history, available options etc…

While the ability of a machine or application to have conversation as fluent and intelligent as the AI assistant named “Jarvis” from famous Marvel movie series, is still a goal to be achieved in next few years but this phenomena is increasing.

Having conversation is a basic human quality that has started and developed with thousand years of evolution. It is basic human requirement to be able to converse with other people and communicate about your thoughts, exchange ideas, express emotions, information and so on.

This natural requirement is giving birth to many new use cases of virtual assistant. With technology advancements virtual assistants have grown primarily in 2 categories :

General Purpose virtual assistants such as Google assistant, Amazon Alexa, Microsoft Cortana, Apple Siri etc.. which can respond to variety of input as well as take some action. Domain specific virtual assistants or chat bots for ex. Bank chatbot, Restaurant or food delivery chat bot, Customer Service, Improve Workplace Productivity (HR assistant), Booking Agent, Gaming Expert, Weather Forecaster, News Reporter, Job Hunter, Marketer, Finance Adviser, Teacher, Legal adviser etc…

To understand how chat bot really work and are able to mimic human conversation capability, we need to focus on few fundamental elements. In rest of this article, i will use domain specific chat bot as an example to explain the process of creating a chat bot.

A contextual chatbot understands the context of the conversation by observing the pattern of communication between itself and the user. It not only keeps track of the current state of the conversation, but also of what has been said before which allows them to suggest next response or action.

This capability differentiates from traditional programming method in terms of using ML model instead of relying on a series of if/else statements.

The entire functioning of chatbot rely on following factors

Framework Natural Language Understanding (NLU) layer and it’s training Pipeline selection for NLU training Dialogue management (Core or Brain of Chat bot) and it’s training Domain configuration Framework — For every conversational chatbot, the basic requirement is to first select a conversational framework as foundation. There are many options available such Google Dialog flow, Amazon Lex, Microsoft Azure bot service etc.. In this article the steps and examples are explained with an open source framework called Rasa to build a restaurant search chat bot for an imaginary food delivery start-up.

Every conversational framework (Rasa, Lex, Dialogflow etc..) has 2 main elements

Natural Language Understanding, or NLU layer

Dialogue Management System layer or Core

When humans do conversation, primarily these body parts and senses are involved — Ear (to listen), ability to speak (to respond) and Human brain (to decide action or response, to keep track of context, history of ongoing conversation).

Similarly in terms of conversational framework, the NLU layer manages the listening and speaking ability and the dialogue management system is the brain of the chatbot.

In Rasa, these two components are called Rasa NLU and Rasa Core, respectively.

2. Natural Language Understanding Layer (Rasa NLU) — First layer of a conversational system. It is responsible for intent classification and entity extraction. It interprets the free text provided by the user. It basically takes an unstructured text phrase or sentence, understands what the user probably intends to say, extracts entities from the text phrase or sentence, and converts it into structured data. for ex. if a user has asked a query to a chatbot — “Search for a restaurant in Nagpur.”

Intent: Restaurant search; Entities: [location=Nagpur]

The NLU layer explained above will be able to perform it’s task based on the learning from training data. In general, the training data for Rasa NLU is structured into three parts :

Example Data— Most important component since it contains all training examples. The performance of NLU layer depends on the volume and variety of training examples . Each example further has three components— text, intent and entities.

Text : It is an example of what would be submitted for parsing. for ex. what is the average budget for 2 people at MTR JP Nagar ?

: It is an example of what would be submitted for parsing. for ex. what is the average budget for 2 people at MTR JP Nagar ? Intent is the objective that should be associated with the text. for ex. in above query the intent is “restaurant search”.

is the objective that should be associated with the text. for ex. in above query the intent is “restaurant search”. Entities are specific parts of the text that need to be identified. for ex. in above query following entities are identified. Restaurant — MTR, location — JP Nagar

2. Synonyms — These are helpful to add those terms which are referring to a common term and generally used as a part of a conversation. for ex. City — Bangalore is referred as Bengaluru or misspelt as Bangaluru. The synonym field allows to capture possible combination to refer to an entity.

3. Regex features — With regular expression, you can cover a broader range of values where lot of unique elements are available and follows a certain pattern. for ex. City Pin codes are unique and large in values.

3. Selecting pipeline for NLU — Rasa framework allows to customize NLU model by configuring specific pipeline.

supervised_embeddings

pretrained_embeddings_spacy

The biggest difference is that the pretrained_embeddings_spacy pipeline, uses pre-trained word vectors whereas the supervised_embeddings pipeline feeds specifically on your data set and doesn’t use any pre-trained word vectors. It is generally recommended that you use the pretrained_embeddings_spacy pipeline if you have less than 1,000 total training examples, and there is a spaCy model for your language. You need to specify the pipeline in config.yml as shown below :

4. Dialogue Management with Rasa Core — The next important aspect of the conversational framework is the Dialogue Management Model. A dialogue management model will predict the response or action that the chatbot should take based on the stage of the conversation. These actions/responses could be to fetch data, send a mail to the user or simply say “Good bye”. For ex. if a user asks “What’s the current temperature in Bengaluru ?”. In this case the bot should fetch the results from a weather database and display it on the screen.

Rasa Core takes structured input in the form of intents and entities (i.e., the output of Rasa NLU) and decides the next actions. It accomplishes the task of learning to take the correct action based on the stage of the conversation.

However, the correct action or response is highly dependent on the training data that is supplied to Rasa Core (Dialogue management ). These inputs in training data are supplied in the form of ‘conversational stories’. A story represents one training example, and it is simply a conversation between a user and an AI assistant, expressed in a particular format. Rasa core trains a neural network model on the stories. Rasa Core uses Tensorflow, a library for training neural networks, at the back end. More specifically, it uses LSTM neural networks implemented in Keras.

In the story examples, user inputs are expressed as corresponding intents (and entities where necessary), while the responses of the assistant are expressed as corresponding action names.

5. Domain.yml — The domain defines the universe in which your assistant operates. It specifies the intents, entities, slots, templates and actions that your bot should know about.

Slots: These are ‘objects’ that you want to ‘keep track of’ during a conversation. Suppose a user says “Book a table for 2 at MTR Lalbagh.” Throughout this conversation, the bot needs to remember that ‘MTR’ is the value of the entity restaurant_name and ‘2’ is the value of the entity number_of_seats so it can be used to query a database such as Zomato.

Slots in domain.yml

Intents (NLU layer) : These are strings (such as ‘greet’, ‘restaurant_search’), which describe what the user intends to say.

: These are strings (such as ‘greet’, ‘restaurant_search’), which describe what the user intends to say. Entities (NLU Layer): As part of NLP, entities are extracted using models such as CRF, spaCy, etc. In most cases, entities are stored in slots; in cases when an entity is irrelevant for the dialogue flow, you don’t need to assign it a slot.

As part of NLP, entities are extracted using models such as CRF, spaCy, etc. In most cases, entities are stored in slots; in cases when an entity is irrelevant for the dialogue flow, you don’t need to assign it a slot. Templates: Templates define the way the bot will utter a statement. For example, if the bot wants to ask for cuisine preference, then a template can be defined so that bot can ask for it: “What kind of cuisine would you like?”, “Please specify your cuisine preference”, etc. All these can be specified in the template section of the domain file. A random response is picked from the template so that your bot doesn’t sound robotic or static.

Templates in domain.yml

Actions: The ‘actions’ component enlists all the actions that the bot can perform — uttering a text message such as “Hi”, looking up a database, making an API call, asking the user a question, etc. For example, an action named ‘action_check_cuisine’ can check validity and availability of a particular cuisine.

Action module in domain.yml

There are two types of actions a bot can take:

Utterance action: The bot just sends a text message to the user. For example: replying with a greet message, asking the location, etc

The bot just sends a text message to the user. For example: replying with a greet message, asking the location, etc Custom actions: Actions such as querying the database, sending emails to the user, etc. If you want the bot to perform a custom action, you need to add that action in your actions file. for ex. below custom action for sending email is included in action.py file.

Aforementioned all the components together complete the functioning of a conversational chat bot.

Conversation about the weather is the last refuge of the unimaginative. — Oscar Wilde

In case of building chat bot human imagination has no boundary. There are multiple possibilities that are still unexplored and the technology advancements in AI (Particularly in Machine learning and NLP) are allowing to getting into those unknown territories such as Health care chat bot, insurance chat bot etc…

Happy Chatting !!