Build engaging conversational chat-bot using context management in Dialogflow. Context is king!

Veer Khot
Veer Khot
Sep 5, 2018 · 4 min read

In recent years chatbots are trending, but there is a vacuum in the availability of sophisticated bots that understand context. Most chatbots are only capable of basic request — response conversations. They have a limited ability to understand the context of the conversation, as shown in the example below. The chatbot doesn’t understand I am asking for another joke when I say “one more please”.

Basically the bot doesn’t understand that the context of the conversation is not merely returning a joke but entertaining the user. This is important for making conversation more engaging and meaningful and not sound, for the better choice of a word, … robotic.

Steps for context management in dialogflow

Create intent

Intent means simply what the conversation is about. For example intent can be joke , small-talks, help, support, eating, watching-movie, etc. Name given to intent should be short and meaningful. To know more about creating intents. To know more about creating intents read our first article that walks you through the basics of Dialogflow.

Add some Training phrases

Training phrases are the question or request we will make to chatbots. For example “tell me a joke”, “entertain me”, etc. The larger the corpus of training phrases more accurate will the understanding of the bot be (E.g. “what is a joke” or “tell me something about a joke”). The creation of training phrases is explained in this article. The meaning of both these statements is the same. So add more and more diverse training phrases to make bot more understandable.

Add some responses

Responses are answers to the requests mentioned in training phrases. Add more than one responses and make them simple and meaningful. Here is an article that explains adding responses.

Add follow-up intent to our main intent

Here all context management happens. So basically if you ask chatbot “tell me a joke” it replies with joke and then you say “one more” then bot should know you are asking for another joke, that is context (asking for another joke). Context is added because bot should remember the purpose of chat and also what it said in previous sentences. Remembering previous sentences is called context management.

Adding follow-up intent means you are adding context. Adding output context remembers past four sentences of chat. It is very easy to change how much lines context should remember.

From above diagram jokesget-followup is name of context and the “5” is the number of statements context should remember. This number can be changed easily and set to any number developer wanted. But setting up context memory at “100” statement is not good idea because even human cannot remember what they said 2 years back. So setting up smaller context memory is good practice based on use of chatbot. Memory of context also called as lifespan.

Building a chatbot is easy these days but if context management is not that great then it is useless. Because most of the statements human use for chatting or conversations are small like “ok” , “yes” , “No” , “Naa..” , “bravo bravo” , etc. It is not possible for bot to reply on these statement without understanding the context. Bot will simply say “can you say that again?” or “I didn’t get that” which not as natural as human conversation. So having context in chat-bot then it will understand why user said statements like “wow”, “thank you”, “bye”,etc.

Sentiment analysis on context

Performing sentiment analysis on phrases like “Damm!” , “ok”, “bye” is not possible. So here rather performing sentiment analysis in individual phrase we can perform that on hole conversation happened in one lifespan of context.

For example assume that we have following conversation

Conversation A:
user1 : “you are too good in natural language processing”
user2 : “Dammm!”

Conversation B:
user1 : “you are worst in natural language processing”
user2 : “Dammm!”

If you see conversation A then “Dammm!” phrase used by user2 have positive sentiment because user1 is paying complement to the user2 and that is why user2 is excited and said “Dammm!”. But if you see conversation B then you can see same phrase have negative sentiment because user1 is simply being rude to user2. So using sentiment of hole context we can analyse the intention of user for using phrases like these. Also we can say that if hole context is have more negative or positive sentiment.

So conclusion is “context is king!!!”

Up Engineering

Incredible products have incredible stories. Every meticulously architected feature is a culmination of sweat, blood, tears, discovery and love. We’re the Up Your Game team. Come join us on our journey.

Veer Khot

Written by

Veer Khot

Data scientist.

Up Engineering

Incredible products have incredible stories. Every meticulously architected feature is a culmination of sweat, blood, tears, discovery and love. We’re the Up Your Game team. Come join us on our journey.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade