Slots | Rasa Documentation
Slots
Slots are your assistant'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).
Slots are defined in the slots section of your domain with their name, type and default value. Different slot types exist to restrict the possible values a slot can take.
Note: If you decide to fill slots through response buttons where the payload syntax issues
SetSlotcommand(s), note that the slot name must not include certain characters such as(,),=or,.
Slot Types
Text Slot
A text slot can take on any string value.
- Example
slots:
cuisine:
type: text
- Allowed Values
Any string
Boolean Slot
A boolean slot can only take on the values true or false. This is useful when you want to store a binary value.
- Example
slots:
confirmation:
type: bool
- Allowed Values
true or false
Categorical Slot
A categorical slot can only take on values from a predefined set. This is useful when you want to restrict the possible values a slot can take.
If the user provides a value where the casing does not match the casing of the values defined in the domain, the value will be coerced to the correct casing. For example, if the user provides the value LOW for a slot with values low, medium, high, the value will be converted to low and stored in the slot.
If you define a categorical slot with a list of values, where multiple of the values coerce to the same value, a warning will be issued and you should remove one of the values from the set in the domain.
- Example
slots:
risk_level:
type: categorical
values:
- low
- medium
- high
Float Slot
A float slot can only take on floating point values. This is useful when you want to store a number with a decimal point.
- Example
slots:
temperature:
type: float
Any Slot
This slot type can take on any value. This is useful when you want to store any type of information, including structured data like dictionaries.
- Example
slots:
shopping_items:
type: any
List Slot
A list slot can take on a list of values. Note that the list slot type is only supported in custom actions when building an assistant with CALM. List slots cannot be filled with flows in either the collect or set_slots flow step types.
Resetting a slot
To reset a slot in a flow, you can set it to null using the set_slots step:
- set_slots:
slot_name: null
If you want to reset a slot in a custom action, set its value to None.
Slots that are empty are not eligible for correction.
CALM Slot Mappings
New in 3.9.0
When building an assistant with CALM, you can configure slot filling to either use nlu-based predefined slot mappings or the newly introduced from_llm slot mapping type.
NLU-based predefined slot mappings
You can continue using the nlu-based predefined slot mappings such as from_entity or from_intent when building an assistant with CALM. In addition to including tokenizers, featurizers, intent classifiers, and entity extractors to your pipeline, you must also add the NLUCommandAdapter to the config.yml file. The NLUCommandAdapter will match the output of the NLU pipeline (intents and entities) against the slot mappings defined in the domain file. If the slot mappings are satisfied, the NLUCommandAdapter will issue set slot commands to fill the slots.
Recommendations
- We recommend adding the
FallbackClassifierto the nlu pipeline to guard against low confidence scores for intents when these are used infrom_intentslot mappings. - We recommend setting
ask_before_filling: trueat thecollectflow steps for slots that can be filled by the same entity in the same flow. This prevents the assistant from greedily filling all the slots with the same entity at the same time, when only one of the slots was requested.
Initial slot values
You can provide an initial value for any slot in your domain file:
slots:
num_fallbacks:
type: float
initial_value: 0
Persistence of Slots during Coexistence
In Coexistence of NLU-based and CALM systems the action action_reset_routing resets all slots and hides events from featurization for the NLU-based system policies to prevent them from seeing events that originated while CALM was active. However, you might want to share some slots that both CALM and the NLU-based system should be able to use.
Real-Time Slot validation
New in 3.12
You can now define validation rules that are strictly independent of business logic directly in the domain file. These rules enforce constraints on slot values when they are collected during the conversation in real time.
You can now validate slot values in real-time as they are collected at any point during a conversation. This can be achieved by adding a validation key to the slot definition in the domain file.
slots:
phone_number:
type: text
mappings:
- type: from_llm
validation:
rejections:
- if: not (slots.phone_number matches "^\([0-9]{3}\) [0-9]{3}-[0-9]{4}$")
utter: utter_invalid_phone_number
- if: not (slots.phone_number matches "^\d+$")
utter: utter_invalid_phone
refill_utter: "utter_refill_phone_number"
Allowed Validation Types
The following validation checks can be defined in the domain file using the pypred library:
- Regex Matching: Validate inputs against specific patterns (e.g email addresses, phone numbers, zip codes, registration numbers, etc.)
- Length Validation: Ensure input text meets minimum and maximum length requirements (e.g usernames, passwords, IDs)
- Data Type Validation: Ensure inputs conform to specific type categories (integers only, numerical values, alphanumeric strings)
- Range Checks: For numerical inputs, verify that values fall within a specified range (e.g 18-65 for age, 1-100 for quantity, minimum/maximum thresholds)
- Date Format Validation: Validate date inputs against specific formats and logical constraints (e.g YYYY-MM-DD, no future birth dates)
- List or Enumeration Matching: Check if inputs match predefined valid options (e.g colors, sizes, categories)
- Prefix/Suffix Checks: Verify inputs begin or end with required characters or strings (e.g product codes, reference numbers)
- Case Sensitivity Checks: Ensure inputs follow case requirements (e.g lowercase usernames, uppercase codes)
- Whitespace Validation: Check for improper spacing patterns in inputs (e.g unwanted leading, trailing, or excessive internal spaces)
- Special Character Filtering: Restrict or validate special characters to maintain data integrity and security