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.
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
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:- destinationslots:destination:type: textinfluence_conversation: falsemappings:- type: from_entityentity: 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
The reason is that slots can influence a story. You might have something like this
stories:- story: booking a flight ticketsteps:- 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
stories:- story: booking a flight ticketsteps:- intent: book_a_ticket- or:- slot_was_set:- destination: Toronto- slot_was_set:- destination: London
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
Then we might configure our slot via;
entities:- entity_nameslots:destination:type: anymappings:- type: from_entityentity: entity_nameintent: 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
entities:- entity_nameslots:destination:type: anymappings:- type: from_entityentity: entity_namenot_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 predicted
not_intent: does not apply the mapping when the intent is predicted
role: only applies the mapping when the extracted entity has a specific role
group: 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.
You can store the full text the user has message as a slot value. This is is done
from_text slot mapping.
slots: slot_name: type: text mappings: - type: from_text
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
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
In Rasa, slots have types. This is useful, because certain types of information are better served as a number than a string of text.
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: textinfluence_conversation: true
Slot type boolean can be used to store information that can get the values
slots:authenticated:type: boolinfluence_conversation: true
Slot type categorical can be used to store values that can get one of a predefined set of possible values.
slots:price_range:type: categoricalvalues:- low- medium- high
Slot type float can be used to store numerical values.
slots:radius:type: floatmin_value: 0max_value: 100
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.
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.
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: floatinitial_value: 100
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?