Slots

Slots

Slots are your bot’s memory. They act as a key-value store which can be used to store information the user provided (e.g their home city) as well as information gathered about the outside world (e.g. the result of a database query).

Most of the time, you want slots to influence how the dialogue progresses. There are different slot types for different behaviors.

For example, if your user has provided their home city, you might have a text slot called home_city. If the user asks for the weather, and you don’t know their home city, you will have to ask them for it. A text slot only tells Rasa Core whether the slot has a value. The specific value of a text slot (e.g. Bangalore or New York or Hong Kong) doesn’t make any difference.

If the value itself is important, use a categorical or a bool slot. There are also float, and list slots. If you just want to store some data, but don’t want it to affect the flow of the conversation, use an unfeaturized slot.

How Rasa Uses Slots

The Policy doesn’t have access to the value of your slots. It receives a featurized representation. As mentioned above, for a text slot the value is irrelevant. The policy just sees a 1 or 0 depending on whether it is set.

You should choose your slot types carefully!

How Slots Get Set

You can provide an initial value for a slot in your domain file:

slots: name: type: text initial_value: "human"

There are multiple ways that slots are set during a conversation:

Slots Set from NLU

If your NLU model picks up an entity, and your domain contains a slot with the same name, the slot will be set automatically. For example:

# story_01
* greet{"name": "Ali"}
  - slot{"name": "Ali"}
  - utter_greet

In this case, you don’t have to include the - slot{} part in the story because it is automatically picked up.

To disable this behavior for a particular slot, you can set the auto_fill attribute to False in the domain file:

slots:
  name:
    type: text
    auto_fill: False

Slots Set By Clicking Buttons

You can use buttons as a shortcut. Rasa Core will send messages starting with a / to the RegexInterpreter, which expects NLU input in the same format as in story files, e.g. /choose{"color": "blue"} and /choose{"color": "red"}.

Slots Set by Actions

The second option is to set slots by returning events in custom actions. In this case, your stories need to include the slots.

Slot Types

Text Slot

text

Use For
User preferences where you only care whether or not they’ve been specified.

Example:

slots:
   cuisine:
      type: text

Boolean Slot

bool

Use For
True or False

Example:

slots:
   is_authenticated:
      type: bool

Categorical Slot

categorical

Use For
Slots which can take one of N values

Example:

slots:
   risk_level:
      type: categorical
      values:
      - low
      - medium
      - high

Float Slot

float

Use For
Continuous values

Example:

slots:
   temperature:
      type: float
      min_value: -100.0
      max_value:  100.0

List Slot

list

Use For
Lists of values

Example:

slots:
   shopping_items:
      type: list

Unfeaturized Slot

unfeaturized

Use For
Data you want to store which shouldn’t influence the dialogue flow

Example:

slots:
   internal_user_id:
      type: unfeaturized

Custom Slot Types

In the code below, we define a slot class called NumberOfPeopleSlot. The featurization defines how the value of this slot gets converted to a vector to our machine learning model can deal with.