Slots
Note: This is material for Rasa Open Source 3.x. If you're interested in the content for Rasa Open Source 2.x, please see the archived version of this lesson here.
Video
In Rasa, slots are your long term memory in a conversation. If there's any information you'd like to store for later use, you'd typically want to store it in a slot. It's important to understand that a slot is not the same thing as an entity. You could store any information in a slot, even if no entity has been detected. That said, it is very common to fill a slot value with an entity value.
If you want to define a slot, you'll need to define it in your domain.yml
file.
In general slots can be defined in two ways: using NLU or using a custom. If you're
using entity extraction via NLU models then you could extract a slot via
a configuration like:
entities: - destination
slots: destination: type: text influence_conversation: false mappings: - type: from_entity entity: destination
Alternatively, if you're using a custom action to set slots. This is discussed in the custom actions segment of this course.
Influencing the conversation
You'll notice that in these configurations that we've added a influence_conversation
tag.
The reason is that slots can influence a story. You might have something like this
in your stories.yml
file.
stories:- story: booking a flight ticket steps: - intent: book_a_ticket - slot_was_set: - destination: Toronto
If your slots are configured to influence the flow of the conversation, you have to
include them in your training stories. These slot_was_set
events will now be included
as training data for the machine learning pipelines in your assistant. Note that you're
able to declare multiple values for a slot in a story via the or
-operator.
stories:- story: booking a flight ticket steps: - intent: book_a_ticket - or: - slot_was_set: - destination: Toronto - slot_was_set: - destination: London
Slot Mappings
Slot mappings allow you to define how each slot will be filled
in. Slot mappings are applied after each user message. For example,
let's say that we have an entity called entity_name
that we only
want to store in a slot if the intent of the user is make_transaction
.
Then we might configure our slot via;
entities: - entity_name
slots: destination: type: any mappings: - type: from_entity entity: entity_name intent: make_transaction
Alternatively, we might also declare that we only want to detect the slot
value if the current intent is not check_transaction
. That could be
configured via:
entities: - entity_name
slots: destination: type: any mappings: - type: from_entity entity: entity_name not_intent: check_transaction
Slot mappings give you more control over when a slot is stored. This is great because it might prevent you from storing a value in a slot if the user is mentioning an entity out of context.
There are many different types of slot mappings. The most common ones are:
intent
: only applies the mapping when the intent is predictednot_intent
: does not apply the mapping when the intent is predictedrole
: only applies the mapping when the extracted entity has a specific rolegroup
: only applies the mapping when the extracted entity belongs to a specific group
It's also good to know that there are different methods of extracting slots. It's not just entities that are supported.
from_text
You can store the full text the user has message as a slot value. This is is done
via the from_text
slot mapping.
slots:
slot_name:
type: text
mappings:
- type: from_text
from_trigger_intent
The from_trigger_intent
mapping will fill a slot with a specific defined
value if a form is activated by a user message with a specific intent.
slots:
slot_name:
type: any
mappings:
- type: from_trigger_intent
value: some_value
intent: intent_name
custom
You can also use a custom action to set the slot value. If none of the predefined slot mappings fit your case, you can creat custom slot mappings using slot validation actions.
slots:
slot_name:
type: text
mappings:
- type: custom
action: action_calculate_day_of_week
Types
In Rasa, slots have types. This is useful, because certain types of information are better served as a number than a string of text.
Text Slots
Slot type text can be used to store any text information. It can influence the conversation based on whether or not the slot has been set.
slots: destination: type: text influence_conversation: true
Boolean Slots
Slot type boolean can be used to store information that can get the values True
or False
.
slots: authenticated: type: bool influence_conversation: true
Categorical Slots
Slot type categorical can be used to store values that can get one of a predefined set of possible values.
slots: price_range: type: categorical values: - low - medium - high
Float Slots
Slot type float can be used to store numerical values.
slots: radius: type: float min_value: 0 max_value: 100
List Slots
List slots can be used to store a list of values. When configured, only the presence of the slot can have influence on the flow of the conversation. The values themselves won't be taken into account.
slots: items: type: list
'Any' Slots
The "any" slot type can be used to store any arbitrary values. Slots of this type don’t have any influence on the conversation flow which means that the value and the presence of the slot doesn’t have any influence on how the conversation goes.
slots: shopping_items: type: any
Initial Values
Finally, it's good to know that you can also set a default value on a slot
by configuring the initial_value
parameter. The value will be assigned
to the slot from the beginning of the conversation and can be reset later
on by NLU or custom actions.
slots: current_account: type: float initial_value: 100
Links
Exercises
Try to answer the following questions to test your knowledge.
- When does a slot influence a conversation? When does it not?
- What's the difference between a slot and an entity?