So far, you've learned how to add a simple conversation that just responds with a message.
However, a well functioning chatbot should be able to understand the key entities that it is dealing with, like Country, City, Car, Insurance etc.
This is very important when we want the chatbot to understand expressions like:
<user says>: "I want a car in London. I like Ford Focus"
Bot should understand:
There are different kinds of Entity Types. One of the simplest ones is Keyword
Static
type.
Using a Keyword Entity
allows you to train your chatbot with a list of values and synonyms. Your chatbot will look for these keywords in the user input. If it finds a match, it will be able to recognise it as the Entity Value.
When you are dealing with a small set of values that doesn't change too often, you can manually provide the values as a Static
list.
In the chatbot scenario, we need the chatbot to be able to recognise the names of the countries where our service operates.
It is time for you to create your first entity for Country:
Now you need to populate the Country entity with the values for the countries supported by our car rental company:
Here is the list of countries (with their synonyms) you should support:
Now you can use the Country entity to allow the user to tell the bot in which country they want the car.
At this point, you should have the rent-car
conversation ready. See homework from chapter two.
Now, you are going to add a question step to the rent-car
conversation. Question steps are used to capture specific information from the user. The question will ask the user for a Country and save the response for later.
rent-car
conversationsteps
array, which should have a message step in therestep
and select the step-question
snippet. This should output the following code:
{
"type": "question",
"entity": "entity-name",
"entity-type": "",
"messages": [
"How to ask for entity?"
]
}
entity
- is like a name of a variable where you want to store the result.
entity-type
- is the type of the entity, it can be one of the built-in entity types like Date, or it can be a type defined by you. It tells the chatbot what to look for to extract the value
messages
- contains message prompts asking the user to provide the value
"country"
"Country"
[ "Which country are you traveling to?" ]
The whole conversation should look like this:
"rent-car": {
"type": "goal",
"steps": [
{
"type": "message",
"messages": [
"Great, let me help you find a car for you."
]
},
{
"type": "question",
"entity": "country",
"entity-type": "Country",
"messages": [
"Which country are you traveling to?"
]
}
]
},
Now is a moment to test the new part of the conversation.
Open the test window and try the following conversations:
Understanding
after the last message in the Debug section. It should contain a Country
object with value
set to Germany, like this:
"Country": [
{
"value": "Germany",
"confidence": 1,
"_start": 14,
"_end": 21,
"_body": "Germany",
"_entity": "Country"
}
],
Understanding
after the last message in the console. Now the Country.value
should be FranceYou can also populate the entity values from a backend. This is done using a Keyword
Dynamic
Entity.
A Dynamic
entity allows you to train the chatbot based on a data returned from a REST call. So, instead of typing a value for each item, you get your data from a REST call.
For the Car Rental, we already have a list of office locations that can be accessed via an API.
You can retrieve the data by making a REST call with the following details:
{
"endpoint": "https://api.demo.nativechat.com/v1/car-rental/offices",
"method": "GET",
"headers": {
"Authorization": "Basic ZGVtby11c2VyOmRlbW8tcGFzc3dvcmQ="
}
}
Each record contains country, name, and localName (how the city is called in the local language, which can be used as an alias).
It is time for you to create your second entity for City:
Name
to: CityLookup strategy
as KeywordData Source
to DynamicEndpoint URL
: https://api.demo.nativechat.com/v1/car-rental/offices
Headers
:
key
: Authorization
value
: Basic ZGVtby11c2VyOmRlbW8tcGFzc3dvcmQ=
Value template
: {{name}}
Synonym templates
: {{localName}}
You might be wondering about the syntax used for the Value template: {{name}}
. NativeChat uses Mustache template system, which allows mixing text with values returned from an object.
For example, a template City: {{name}}
, would return an array of items like:
[ "City: Naples", "City: Lyon", "City: Gdansk", ...]
You can also use this with any message text returned by the chatbot. For example, you could have a message step like this:
{
"type": "message",
"messages": [
"You picked {{city}} in {{country}}"
]
}
Next, add another question step to the rent-car
conversation, which should ask the user to select the city. Use the following values:
city
City
,In which city are you looking for a car?
Save your changes.
You should test the new conversation part. Try these conversations:
Step by step
Understanding
for both the Country and the City in the console.All at once
Understanding
for both the Country and the City in the console.It wouldn't be surprising at all if the rental company decided to expand and add more cities to their offering. This would however mean that the chatbot wouldn't be able to recognise the new cities.
This is a quite common scenario where the dynamic data changes. When that happens you just need to retrain the chatbot with the new data.
You could really easily trigger the update process manually:
There are also scenarios where you want to be able to match to an entity like a driving license number or a car registration plate, but obviously you cannot list every single driving license number (plus this would make a very slow process of trying to match millions of potential values).
This can be done by defining an entity type as a regular expression.
The chatbot should be able to ask for the users Driving License Number. For the sake of simplicity, lets say that the license number is made of:
For example:
The full license # should be: STE1286DE
A regular expression for this would look like this: [A-Za-z]{3}[0-9]{4}[A-Za-z]{2}
You can learn more about regular expressions from MDN web docs.
Now you will add a third entity type to your chatbot training.
Name
to: DrivingLicenseNumberLookup strategy
to RegexPattern
to [A-Za-z]{3}[0-9]{4}[A-Za-z]{2}We don't need a question step to test if an Entity Type works. The chatbot engine always makes an attempt to match to any of the available Entity Types.
You can easily test the following expressions by typing them in the test console:
Even though the chatbot won't understand you now, expand the Understanding. If a match occurs, you should find an object like:
"DrivingLicenseNumber": [
{
"value": "EVA1093FR",
"confidence": 1,
"_start": 14,
"_end": 23,
"_body": "EVA1093FR",
"_entity": "DrivingLicenseNumber"
}
]
As a homework add a Dynamic Entity Type for a car. This is so that the chatbot could recognise expressions like: I want to rent a Ford KA.
Name
should be called Car
{ "endpoint": "
https://api.demo.nativechat.com/v1/car-rental/cars", "method": "GET", "headers": { "Authorization": "Basic ZGVtby11c2VyOmRlbW8tcGFzc3dvcmQ=" } }
value
should come from {{name}}
synonym
should come from {{short-name}}
Click test to try your changes before creating the entity type.
Add a question step to rent-car
in your Cognitive Flow to ask for a Car.
Get started today