Capture information with entities

In the Choo Choo bot, we can define a new intent that tells us that the user wants to book a train ticket by training it with expressions like:

  • I want a train ticket

  • I need a ticket

  • Can I book a train ticket here?

But what happens when a user says something like:

  • I want a train ticket to Amsterdam

  • I need to go to Antwerp tomorrow

  • Can I book a train ticket to Brussels please?

Those expressions contain valuable information. We want to make sure we capture the destination and store it as a variable. In this tutorial, we will see how to store data that was mentioned in intents using entities. In the next tutorial, you will see how we can ask explicitly for missing variables.

Creating entities

Not all users will immediately give the destination, so let's make sure we train our intent without entities as well:

  • Go to NLP > Intents

  • Add a new intent called book train ticket

  • Add some simple expressions, like:

    • I want a train ticket

    • I need a ticket

    • Can I book a train ticket, please?

If you have trouble doing this, feel free to have a look back at the previous tutorial.

Next, it's time to add an entity.

  • Click on + Add expression

  • Enter an expression that contains an entity, like:

I want to book a ticket from Brussels to Paris

  • Select Brussels in this sentence.

  • Brussels is the location the user wants to depart from so we will name this entity origin.

  • Press on Enter-key to create the origin entity

  • Do the same thing for Paris as a destination entity.

  • Add more expressions that contain the entities origin and destination.

Chatlayer recommends adding at least 30 expressions per entity, to guarantee the quality of the entity detection.

After adding sufficient expressions and updating your NLP model, Chatlayer can automatically suggest entities based on your expressions. Click the 'suggest entities' in the expression dialog.

  • Make sure you retrain the model by clicking the Update NLP button.

Testing entities

After we have retrained the model, let's see if the model is good enough to recognize the destination entity.

  • Go to NLP >Test to open the testing console.

  • Write 'I would like to go to Brussels from Amsterdam' as the expression to be tested

  • Click on Test.

You see that the entity gets recognized with a 89.72% confidence. Your results will be different based on your training set. If the entity is not recognized correctly, you can add it here as a training expression immediately.

Make sure you retrain the model before testing newly added expressions.

Using variables in messages

When a user says something with an entity, and the entity is successfully detected, Chatlayer will automatically store it as a variable for that specific user.

To reuse the variable later in the conversation, you can put it in between curly brackets like this: {variable_name}. When writing this message to the users, Chatlayer.ai will automatically substitute {variable_name} with the value of the variable. If the variable is empty, an empty string will be printed.

  • In Bot dialogs, create a bot message book train ticket and link the book train ticket intent to it in the NLP tab.

  • Add a new text message with the text 'So you want to go to {destination}, I can help you with that!'

Variables can be used everywhere throughout the platform, for example in API calls, list templates and button labels.

Not only entities can be variables. Later on you will learn how to use variables to build flows.

Testing entities in the emulator

Now that we have linked everything, we are ready to test if everything is configured correctly by using the emulator.

  • Open the Emulator

  • Enter "I want to go to Amsterdam" and submit

  • Open the debugger

In the 'NLP Result' tab you can now see the entity was extracted correctly.

All entities are stored in the variables list automatically. Under the 'Debugger' tab, you can find a list of all known variables under 'User Session'. Since the entity was recognized in the previous message, this contains the variable 'destination'.

If your entity is recognised by the NLP but does not show up in the User Session, it did not pass the threshold. By default the threshold is set to 80%. You can lower it in the NLP tab, but better still is to add more expressions to your bot so that the model becomes more robust and will work better in the future.

Multiple entities

You can add as many entities as you want to an expression. It can sometimes make sense to prepare for a lot of entities and add expressions like

  • I want to go from Antwerp to Brussels tomorrow at 9am in first class

with entities

  • departure location: Antwerp

  • arrival location: Brussels

  • date: tomorrow

  • time: 9am

  • class: first

Performance

Keep in mind that NLP techniques are probabilistic in nature. When you try to capture 5 expressions in one sentence, it might not recognize all of them correctly. As a general rule of thumb, you can start to expect reasonable results for one entity when the NLP was given 30 expression to learn from.

Depending on how important it is, the Chatlayer team can help you write custom parsers to improve entity detection.

If an entity was not provided by the user or recognized correctly, you can always ask for it later in the conversational flow.

Missing entities

Not every user will input all the entities you might need. In the next tutorial, we will see how we can check if a user has already provided information, and ask for everything that is missing.