Product and Engineering

Maintaining Context in Chatbots

One of the harder problems that chatbot developers face is, how to maintain the context of conversation. While all the popular frameworks provide an opinionated take on how to maintain this context, none of them seem to be either simple or complete.

At ThoughtSpot we built Spot, a bot to get insights into your data. We used a reactive approach to maintain the conversation context.

 

This can be summarised as:

  • Whenever the bot send a message, set(push) <a matcher method m(), a context callback> on the stack.
  • On receiving a message, match the message using the matcher methods in FIFO order. Call the matched context callback.

The elegance of this approach is that the pushed callback captures the state of variables as a part of its closure context. When the callback is called, this state is retained and we can time travel back to the state when the context was actually set.

Let me illustrate with an example:

This was a simple example where we used context stack to match the user intent. Things become more interesting when we are able to use the closure variables in the called callback.

Bot-context is a simple yet powerful way to manage conversation context in your shiny smart chatbot. Its open source, you are just an npm installaway from using it.

Let us see the complete example using bot-context:

Thats it! The above code is all you need to handle all permutations and combinations of the answers the user may give. Please visit the github of the bot-context project for more details and discussion.

×