Intents, Entities, ContextWhen you have worked with chatbots or conversation services, then you already know that the key concepts are intents and entities. To start with, intents are the purpose or the goals the user asks for. Is it a weather- or a stock market-related question? Next, an entity represents a class of object or data type. Amazon, IBM, Google and Microsoft could be entities for the “company” the user wants the stock info for. San Francisco and Friedrichshafen could be cities and today, tomorrow or Sunday could be the dates for which I need the weather forecast. A special kind of entity are system entities. They are built-in and, once enabled, automatically detect numbers, persons, locations, dates and more in user input and the simplify the handling of those found values.
To understand what is going on, the context and context variables are used. They identify which user is chatting. Plus, they tell about the current step in a conversation. Even more, they carry application-related data throughout the dialog. In summary, they allow to build applications based on the Watson Conversation service and to pass vital information into and retrieve data from it (see the architecture diagram).
All three, intents, entities, and context can and should be used in dialog nodes. They enable decisions about the next step in a conversation, about which response to choose. Moreover, they allow to tailor responses to exactly that specific ongoing chat. Hence, using intents, entities, and context as part of responses are an important ingredient to a lively, human-like interaction in chatbots.
Conditions, Data Access, ExpressionsResponses for chatbots are defined in dialog nodes. In general, each dialog node has a name, optional conditions to check and one or more response. Conditions and responses both have access to the mentioned metadata and can include expressions. What are typical conditions?
- A check for an intent: Is the user goal to learn about weather, an insurance topic or a video of dancing cats?
- A test for an entity: Does it involve a location like San Francisco or Friedrichshafen, or an animal like cat or fish?
- An evaluation of a context variable to see whether the username was set, the temperature in the forecast is higher than 35°C or the list of suggested videos is empty.
- A logical combination of all the above: If it is a weather-related question (intent), the location is Friedrichshafen (entity) and the predicted temperature is high, then enter the dialog node and return a response with a variation of “It will be hot at Lake Constance”.
- The generic form consists of a JSON object or list with the type as name: intents, entities, context (and some more). Not going into details, an array-like access (entities[‘location’]) and a dot-notation (context.temperature) are possible.
- The shorthand uses “#” as prefix for a specific intent (#weather), the “@” for entities (@location) and “$” for context variables ($temperature).
Building Better ChatbotsOnce you have mastered the art of using expressions in conditions and responses, you probably want to write more sophisticated expressions. And yes, it is possible and you are allowed to. 🙂
The expression language in IBM Watson Conversation is based on SpEL, the Spring Expression Language. It allows to apply functions, methods and filters to the data. SpEL provides regular expressions. Its feature set has ternary operators, projections and selections on lists or collections, and much more. It is a powerful tool to explore. It is core to building better chatbots. Using SpEL helps to design chatbots that are situation- and data-driven, resulting in what I call “smart conversations”.
|Expression in Context Variable|
We will discuss the example above and other more complex expressions in another blog post. In the meantime, I would recommend heading over to this collection of sample expressions for the IBM Watson Conversation service. I created the GitHub repository to collect and explain samples with expressions like those discussed today.
If you have feedback, suggestions, or questions about this post, please reach out to me on Twitter (@data_henrik) or create an issue against the mentioned GitHub repository. Moreoever, check out the tool I created to manage conversation workspaces.
[Note that this blog entry is a pre-publish for https://www.ibm.com/blogs/bluemix/2017/06/building-chatbots-tips-tricks/]