With Rasa Open Source 1.10 we released an experimental feature called Entity Roles and Groups. In this post I want to briefly explain what it is and how to use it. Feel free to share your feedback here with us.
What are Entity Roles and Groups?
Assigning custom entity labels to words, allow you to define certain concepts in the data. For example, we can define what a city is:
I want to fly from [Berlin](city) to [San Francisco](city).
However, sometimes you want to specify entities even further. Let’s assume we want to build an assistant that should book a flight for us. The assistant needs to know which of the two cities in the above example is the departure city and which is the destination city. “Berlin” and “San Francisco” are still cities, but they play a different role in our example. To distinguish between the different roles, you can assign a role label in addition to the entity label.
I want to fly from [Berlin]{"entity": "city", "role": "departure"} to [San Francisco]{"entity": "city", "role": "destination"}.
You can also group different entities by specifying a group label next to the entity label. The group label can, for example, be used to define different orders. In the following example we use the group label to reference what toppings goes with which pizza and what size which pizza has.
Give me a [small]{"entity": "size", "group": "1"} pizza with [mushrooms]{"entity": "topping", "group": "1"} and a [large]{"entity": "size", "group": "2"} [pepperoni]{"entity": "topping", "group": "2"}
As you may have notice the training data format looks slightly different. Check out the documentation for details on how to define entity roles and groups in your training data.
How to use Entity Roles and Groups?
Upgrade to Rasa 1.10.0 and update your training data to include some entities with role and/or group labels. Your pipeline should contain either the CRFEntityExtractor
or the DIETClassifier
as only those two entity extractors can detect role and group labels.
Most likely you want to fill a certain slot with an entity that has a specific role and/or group. To achieve this we recommend to use forms. However, you can also use a custom action for this.
Forms
Define a custom slot mapping to fill a slot with an entity that has a specific role and/or group label. Let’s go back to the example in which our assistants helped us to book a flight. We want to fill the two slots destination_city
and departure_city
. To achieve this you can use the function from_entity
in your slot mapping:
def slot_mappings(self) -> Dict[Text, Union[Dict, List[Dict]]]:
return {
“departure_city": [
self.from_entity(entity="city", role="departure"),
],
"destination_city": [
self.from_entity(entity="city", role="destination"),
]
}
If the form is activated and an entity of type city
is found that has the role departure
the slot departure_city
is filled. If the city
entity has the role destination
it is assigned to the slot destination_city
. Once the slots are filled you can use them, for example, in your custom action to book the flight.
Custom Actions
If you want to use a custom action, you can directly obtain the entity from the tracker using the method get_latest_entity_values
. You can pass a role label and/or group label next to the entity type to that function. For example:
tracker.get_latest_entity_values(entity_type=”city”, entity_role=”destination”)
Please, keep in mind that this feature is still experimental. We encourage you to try it out and give us feedback. However, the functionality might be changed or removed in the future.
Happy coding!