Building and Using Custom Entities in Dialogflow
— Written by
In my previous article about Dialogflow, I’ve covered the basic setup of creating a telegram chatbot with a serverless backend. I have also showcased how we can extract data or information from the user’s message and send it to the backend for processing and coming up with the appropriate response for the message. In extracting the information, we will tag some of the elements/words in the message under the corresponding entities.
In this article, I’ll cover how we can create custom entities that cover your business requirements.
Before proceeding, I strongly recommend you to read my previous article in case if you missed it: https://www.skcript.com/svr/building-a-serverless-telegram-chatbot-with-dialogflow-and-firebase-functions/
What are the entities? a quick recap.
Entities as something similar to your data types in a programming language that helps you define what sort of data is stored in a variable. Here, entities in chatbot language are nothing but the pattern or the meaning we classify certain parts or elements of the conversation.
For example, in the below message, the user is asking for weather information in the city of Dubai. So, when configuring the chatbot we classify “Dubai” as the city, and post the training the bot will understand any city.
What is the limitation with inbuilt entities?
In Dialogflow, the inbuilt entities are trained based on the generic scenarios like date, city, and other standard units. But in most cases, we’ve to create our entities to satisfy the business requirements.
For an instance consider the below statement in an HR chatbot.
Here, we can easily train the bot to extract the start date and end date since it’s a very general information class. But the challenge comes when we try to extract the type of leave. The type of leave is information related to a specific requirement. Leave type could change between one organization to another.
These are the cases that can’t be solved with built-in entities and we need to depend on custom entities.
How do we create custom entities?
Let’s take our leave application use case for this article. To Dialogflow classify the leave type, we need to create a new entity called “Leave”.
Navigate to the Entities section in the sidebar of the project console.
Here you get to see all the custom entities that you created in your project, let’s create our first custom entity.
If you notice, the Dialogflow gives us the option to create the Entity and various items under that entity. This works more like your enum in programming as I mentioned earlier.
In our case, under the leave types, we’ve sick leave, annual leave, and vacation leave. So I create those under the entity items. Also providing more synonyms will help the bot to train for understanding better.
You can simply save after you created the required items for the entity. Post the training we can start using these new custom entities in the intent configuration page similar to system entities.
Now let’s ask the bot question to see if it can classify all the leaves properly.
Similar to this you can create any number of custom entities based on your requirements and use them together as well in the intent. Here is a small example from another chatbot project where I’ve used more than one custom entity in a statement.
Here both the facility attribute as well as facility entities are custom built for this hotel chatbot where the user asks for details of a facility in the hotel.
Up nextAsynchronous data fetching using ReactQuery - Part 1 (Queries)