Are Slots and Entities the same?. Building Chatbots with Rasa — part II | by Aniruddha Karajgi | Towards Data Science

I’m been working with Rasa for the past few months and was initially pretty confused with the difference between slots and entities.
In this post, we’ll discuss how slots and entities are alike, and more importantly, how they are different.
Table of Contents- Defining Entities and Slots- Are slots and entities interchangeable?- When to use one over the other- Setting Slots- Extracting Entities
Quick Note
This is the next post in my series on Rasa. You can check out the previous post below.
towardsdatascience.com
There, we covered some of the building blocks of Rasa and chatbots in general and if you’re new to Rasa, going through that article first would help.
A quick refresher: this is how you could define these terms.
Slots are the bot's memory. Anywhere you need persistence in saving values, you use slots.
Rasa gives you a lot of flexibility when it comes to defining and using slots. You can give them a type — text, boolean, and even custom types — and you can also choose whether a slot should or shouldn’t affect how the conversation goes.
Entities are key pieces of information that the bot extracts from user messages, like a user’s contact information.
The extraction itself is done by the NLU part of your chatbot using an entity extractor.
At first glance, they seem to be pretty similar — both store information, albeit one doing it for longer and more importantly, they seem to be interchangeable.
They aren’t. They are related, but only to an extent.
Both store information
Both can affect how the conversation flows
Entities are more abstract and are extracted by NLU components called entity extractors.
Slots cannot be populated by “extraction” directly — only through already extracted entities or custom actions.
Entities can have more complex structuring and grouping using features like roles, groups, synonyms and regex, while slots do not.
Slots have types and rasa lets you configure them to a great extent.
This is a really important question to ask when designing your bot. In theory, you could map every entity to a slot, and have everything persist on your tracker. But that isn’t always necessary.
Remember that slots are required only when you want the information to persist, like the name of a user. Otherwise, entities themselves are sufficient.
Entities can often have slots associated with them since slots can be considered as “more persistent” entities. But think of cases where you need to extract something temporarily and that piece of information vital for the bot to appropriately respond to the user’s query.
A simple example? Take a “calculator” bot. The user mentions simple arithmetic queries, and the bot calculates the answer.
The user says something like:
Could you add 1223239 and 190239?
The required info can be extracted as entities: operand (which the two operands would fall into) and operator (which is “add” in this case).
Since this information is useful only temporarily — that is, before the next question— storing them in slots is not a great idea, though you could do that if you wanted to.
This one’s a lot easier to think of a use case for. If you need a hint, slots don’t need to be filled via entities. You could use actions as well.
Slots can be used just like global variables. You could maybe maintain a slot called is_existing which keeps track of whether a user is an existing user or not.
Here, it wouldn’t make sense to have a corresponding entity called is_existing as well, since this information can’t be provided by the user and hence, can’t be extracted— it must be verified by a database query.
Slots are defined under the slots key, located in a file called the domain. Rasa let’s organize your data in many ways — for smaller projects, you can choose to dump everything into a single file, or you could keep everything separate.
A defined slot looks like this:
slots:  email:    type: text    initial_value: null    auto_fill: true    influence_conversation: true
You can decide the type, set a default value, ensure that any corresponding entity (having the same name as your slot) automatically populates it and also decide whether it can affect your conversation or not.
Slots can be set in two ways:
through entities with the same name (when autofill and store_entities_as_slots are set, which — by default — they are)
Through custom actions.
When entities and slots have the same name, it's possible to automatically populate slots with their corresponding extracted entities. This can be done with the help of two properties that are True by default.
auto_fill (set to True by default for each slot). Looks something like this:
slots:  email:    type: text    auto_fill: <true|false>
store_entities_as_slots (set as a config parameter in your domain file)
config:  store_entities_as_slots: false
Slots can also be set through custom actions. A custom action is defined in a file as a class, with a name and a run method. It inherits from Rasa’s Action class.
Slots are set by returning SlotSet objects in a list, like this:
return [SlotSet("my_slot", value), .. ]
Remember that custom actions can be used to query databases, make API calls and also respond to a user using the dispatcher .
So, a custom action to check if a user has already signed up for a newsletter could be implemented something like this:
Action action_check_if_existing_user does the following:
reads the user’s email from email slot
passes it to a helper function that queries a database
gets the response and sets the is_existing boolean slot
class ActionCheckIfExistingUser(Action):    def name(self):        return "action_check_if_existing_user"    def run(self, dispatcher, tracker, domain):        email = tracker.get_slot('email')        # some helper method that queries the database        action_utils = ActionUtils()        is_existing = action_utils.check_if_user_exists(email)        if is_existing:            dispatcher.utter_message(text="You're signed up.")        else:            dispatcher.utter_message(text="You aren't signed up.")            return [SlotSet("is_existing", is_existing)]
Entities are extracted by EntityExtractors. These are components you mention in the pipeline of your config file.
There are several existing ones, which you can find in Rasa’s documentation, and you can always design custom components.
We’ll cover the details of entity extraction in a future post.
This was a feature released last year, that gives you a little more flexibility in defining entities.
Roles
Think of role as a way to add a more nuanced meaning to a particular entity.
Say you’re building a bot to help users trade in their old mobile devices. You have an entity modelthat, well, extracts smartphone models.
Now, both the old device and the one the user wants to buy are technically smartphone models, but distinguishing between them is important for the bot.
So, we can add a role to the model entity. You can add training data like this:
My existing device is an [iphone 11]{"entity": "model", "role": "existing_device"} and I want to buy an [iphone 13]{"entity": "model", "role": "new_device"}.
Groups
Groups let you group entities together so that they make more sense. Let’s say you have two entities — name and age , and a user says something like:
My friend John is 20 while David is 25.
Here, the bot needs to identify that there’s a grouping between the two sets of names and ages. John’s age must be associated with his name, and likewise for David. This is where groups come in.
You can do something like this:
My friend [John]{"entity": "name", "group": "1"} is [20]{"entity": "age", "group": "1"} while [David]{"entity": "name", "group": "2"} is [25]{"entity": "age", "group": "2"}
Links to pages in the Rasa documentation
Entities
Entity extractors
Slots
Slot behaviour in affecting conversation
Part I: Building a Chatbot with Rasa
Part III: Handling Chatbot Failure
Part IV: How do Chatbots understand?
We discussed slots and entities in this post, specifically talking about how they are related, but not interchangeable. The goal of this post is to make it easier to understand what each of these is, and which ones to use in which scenarios.
This is just the tip of the iceberg when it comes to what Rasa offers. I’ll be adding more parts to this series.
Hope it helped!
20.3.2022
Add links to the other posts in the series