Enhance a chatbot conversation with Context Variables and System Entities

5 min read

Enhance a chatbot conversation with Context Variables and System Entities

Imagine, you are in a conversation with a chatbot and you feel that the human angle is completely missing because the bot starts it’s dialog with a usual “Hi” or “Hello.” You may want to personify the conversation by adding the name of the person (who’s logged in) to the boring “Hi” or “Hello.” Ever thought of this? It’s not just personification. How about wishing appropriately based on the time of the day someone invokes your chat application? Also, how about passing values back and forth during a conversation between the nodes or from application to a node?

You may ask a question that my dialog is stateless. How can acheive this?

Your dialog is not stateless anymore. Using Context variables, the application can pass information to the dialog, and the dialog can update this information and pass it back to the application.

A context variable is a variable that you define in a node, and optionally specify a default value for. Other nodes or application logic can subsequently set or change the value of the context variable.

You can define context variables with a default value on “Dialog” tab of your Watson Conversation service on IBM Cloud. You can test your dialog with context variables and system entities even before incorporating it into your application.

Defining Context Variables on Conversation Dashboard

Defining Context Variables on Conversation Dashboard

To understand the various building blocks of conversation, refer to this blog post.

Passing context from the application

Pass information from the application to the dialog by setting a context variable and passing the context variable to the dialog.

For example, your application can set a $time_of_day context variable, and pass it to the dialog which can use the information to tailor the greeting it displays to the user.

Passing context from the application

In this example, the dialog knows that the application sets the variable to one of these values: morningafternoon, or evening. It can check for each value, and depending on which value is present, return the appropriate greeting. If the variable is not passed or has a value that does not match one of the expected values, then a more generic greeting is displayed to the user.

Passing context from node to node

The dialog can also add context variables to pass information from one node to another or to update the values of context variables. As the dialog asks for and gets information from the user, it can keep track of the information and reference it later in the conversation.

For example, in one node you might ask users for their name, and in a later node address them by name.

Passing context from node to node

In this example, the system entity @sys-person is used to extract the user’s name from the input if the user provides one. In the JSON editor, the username context variable is defined and set to the @sys-person value. In a subsequent node, the $username context variable is included in the response to address the user by name.

What are System Entities?

The Conversation service provides a number of system entities, which are common entities that you can use for any application. Enabling a system entity makes it possible to quickly populate your workspace with training data that is common to many use cases.

System entities can be used to recognize a broad range of values for the object types they represent. For example, the @sys-number system entity matches any numerical value, including whole numbers, decimal fractions, or even numbers written out as words.

System entities are centrally maintained, so any updates are available automatically. You cannot modify system entities.

System Entities

System Entities

Defining a Context Variable and using @sys-person

You can either edit an existing dialog node or create a new node to define a context variable on the dialog. 

  • On your “Dialog” tab, navigate to an existing node and Click to open the node.

  • Under “Then respond with”, Click on the three vertical dots and “Open JSON editor.”

  • Just above output, you will add context as shown below

    "context": {
    "username": "@sys-person.literal"


  • Close JSON editor.

Whenever a name is encountered, @sys-person entity automatically recognises it and assigns it to the username context variable. You can use it by calling $username anywhere in your dialog.

You can test it by adding the $username to the response condition in your dialog node.


Dialog with Context Variable and System Entity

What we have seen till now is just a drop in the ocean, You can further enhance your chatbot experience with advanced options as mentioned here. (Treat this as a recommended read.)

If you any questions, drop a comment below or reach out to me @VidyasagarMSC.

Happy Coding!!!

Be the first to hear about news, product updates, and innovation from IBM Cloud