Samyang Rasa Baru
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. One use case for these slots are basic user profile slots. Both the NLU-based system and CALM should likely be able to know whether a user is logged in or not, what their user name is, or what channel they are using. If you are storing this kind of data in slots you can annotate those slot definitions with the option shared_for_coexistence: True.
shared_for_coexistence: True
shared_for_coexistence: True
In the coexistence mode, if the option shared_for_coexistence is NOT set to true, it invalidates the reset_after_flow_ends: False property in the flow definition. In order for the slot value to be retained throughout the conversation, the shared_for_coexistence must be set to true.
Dynamic Form Behavior#
By default, Rasa will ask for the next empty slot from the slots listed for your form in the domain file. If you use custom slot mappings and the FormValidationAction, it will ask for the first empty slot returned by the required_slots method. If all slots in required_slots are filled the form will be deactivated.
You can update the required slots of your form dynamically. This is, for example, useful when you need to fill additional slots based on how a previous slot was filled or when you want to change the order in which slots are requested.
If you are using the Rasa SDK, we strongly recommend that you use the FormValidationAction and
override required_slots to fit your dynamic behavior. You must implement
a method extract_
from typing import Text, List, Optional
from rasa_sdk.forms import FormValidationAction
class ValidateRestaurantForm(FormValidationAction):
def name(self) -> Text:
return "validate_restaurant_form"
async def required_slots(
domain_slots: List[Text],
dispatcher: "CollectingDispatcher",
domain: "DomainDict",
additional_slots = ["outdoor_seating"]
if tracker.slots.get("outdoor_seating") is True:
additional_slots.append("shade_or_sun")
return additional_slots + domain_slots
If conversely, you want to remove a slot from the form's required_slots defined in the domain file under certain conditions, you should copy the domain_slots over to a new variable and apply changes to that new variable instead of directly modifying domain_slots. Directly modifying domain_slots can cause unexpected behaviour. For example:
from typing import Text, List, Optional
from rasa_sdk.forms import FormValidationAction
class ValidateBookingForm(FormValidationAction):
def name(self) -> Text:
return "validate_booking_form"
async def required_slots(
domain_slots: List[Text],
dispatcher: "CollectingDispatcher",
domain: "DomainDict",
updated_slots = domain_slots.copy()
if tracker.slots.get("existing_customer") is True:
updated_slots.remove("email_address")
Custom Slot Mappings#
The slots_mapped_in_domain argument provided to the required_slots method of FormValidationAction has been replaced by the domain_slots argument, please update your custom actions to the new argument name.
If none of the predefined Slot Mappings fit your use
case, you can use the
Custom Action validate_
If you're using the Rasa SDK we recommend you to extend the provided FormValidationAction. When using the FormValidationAction, three steps are required to extract customs slots:
In addition, you can override the required_slots method to add dynamically requested slots: you can read more in the Dynamic Form Behavior section.
If you have added a slot with a custom mapping in the slots section of the domain file which you only want to be validated within the context of a form by a custom action extending FormValidationAction, please make sure that this slot has a mapping of type custom and that the slot name is included in the form's required_slots.
The following example shows the implementation of a form which extracts a slot outdoor_seating in a custom way, in addition to the slots which use predefined mappings. The method extract_outdoor_seating sets the slot outdoor_seating based on whether the keyword outdoor was present in the last user message.
from typing import Dict, Text, List, Optional, Any
from rasa_sdk import Tracker
from rasa_sdk.executor import CollectingDispatcher
from rasa_sdk.forms import FormValidationAction
class ValidateRestaurantForm(FormValidationAction):
def name(self) -> Text:
return "validate_restaurant_form"
async def extract_outdoor_seating(
self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict
) -> Dict[Text, Any]:
text_of_last_user_message = tracker.latest_message.get("text")
sit_outside = "outdoor" in text_of_last_user_message
return {"outdoor_seating": sit_outside}
By default the FormValidationAction will automatically set the requested_slot to the first slot specified in required_slots which is not filled.
Calling Responses as Actions#
If the name of the response starts with utter_, the response can directly be used as an action, without being listed in the actions section of your domain. You would add the response to the domain:
- text: "Hey! How are you?"
You can use that same response as an action in your stories:
- action: utter_greet
When the utter_greet action runs, it will send the message from the response back to the user.
If you want to change the text, or any other part of the response, you need to retrain the assistant before these changes will be picked up.
The requested_slot slot#
The slot requested_slot is automatically added to the domain as a slot of type text. The value of the requested_slot will be ignored during conversations. If you want to change this behavior, you need to add the requested_slot to your domain file as a categorical slot with influence_conversation set to true. You might want to do this if you want to handle your unhappy paths differently, depending on what slot is currently being asked from the user. For example, if your users respond to one of the bot's questions with another question, like why do you need to know that? The response to this explain intent depends on where we are in the story. In the restaurant case, your stories would look something like this:
- story: explain cuisine slot
- intent: request_restaurant
- action: restaurant_form
- active_loop: restaurant
- requested_slot: cuisine
- action: utter_explain_cuisine
- action: restaurant_form
- story: explain num_people slot
- intent: request_restaurant
- action: restaurant_form
- active_loop: restaurant
- requested_slot: cuisine
- requested_slot: num_people
- action: utter_explain_num_people
- action: restaurant_form
Again, it is strongly recommended that you use interactive learning to build these stories.
Writing Stories / Rules for Unhappy Form Paths#
Your users will not always respond with the information you ask of them. Typically, users will ask questions, make chitchat, change their mind, or otherwise stray from the happy path.
While a form is active, if a user's input does not fill the requested slot, the execution of the form action will be rejected i.e. the form will automatically raise an ActionExecutionRejection. These are the specific scenarios in which a form will raise an ActionExecutionRejection:
To intentionally reject the form execution, you can also return an ActionExecutionRejected event as part of your custom validations or slot mappings.
To handle situations that might cause a form's execution to be rejected, you can write rules or stories that include the expected interruptions. For example, if you expect your users to chitchat with your bot, you could add a rule to handle this:
- rule: Example of an unhappy path
# Condition that form is active.
- active_loop: restaurant_form
# This unhappy path handles the case of an intent `chitchat`.
- action: utter_chitchat
# Return to form after handling the `chitchat` intent
- action: restaurant_form
- active_loop: restaurant_form
In some situations, users may change their mind in the middle of the form action and decide not to go forward with their initial request. In cases like this, the assistant should stop asking for the requested slots.
You can handle such situations gracefully using a default action action_deactivate_loop which will deactivate the form and reset the requested slot. An example story of such conversation could look as follows:
- story: User interrupts the form and doesn't want to continue
- intent: request_restaurant
- action: restaurant_form
- active_loop: restaurant_form
- action: utter_ask_continue
- action: action_deactivate_loop
It is strongly recommended that you build these rules or stories using interactive learning. If you write these rules / stories by hand you will likely miss important things.
Forms are fully customizable using Custom Actions.
Multiple Domain Files#
The domain can be defined as a single YAML file or split across multiple files in a directory. When split across multiple files, the domain contents will be read and automatically merged together. You can also manage your responses, slots, custom actions in Rasa Studio.
Using the command line interface, you can train a model with split domain files by running:
rasa train --domain path_to_domain_directory
Responses are templated messages that your assistant can send to your user. Responses can contain rich content like buttons, images, and custom json payloads. Every response is also an action, meaning that it can be used directly in an action step in a flow. Responses can be defined directly in the domain file under the responses key. For more information on responses and how to define them, see Responses.
Actions are the things your bot can do. For example, an action could:
All custom actions should be listed in your domain.
Rasa also has default actions which you do not need to list in your domain.
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.
If you decide to fill slots through response buttons where the payload syntax issues SetSlot command(s), note that the slot name must not include certain characters such as (, ), = or ,.
A text slot can take on any string value.
A boolean slot can only take on the values true or false. This is useful when you want to store a binary value.
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. For example, if you define a categorical slot with values low, medium, high, and Low, the value Low will be coerced to low and a warning will be issued.
A float slot can only take on floating point values. This is useful when you want to store a number with a decimal point.
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.
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.
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.
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.
If during message processing, the NLUCommandAdapter issues commands, then the following command generators in the pipeline such as LLM-based command generators will be entirely bypassed. As a consequence, LLM-based command generators will not be able to fill slots by issuing set slot commands at any point in the conversation flow. If the LLM-based command generator issues commands to fill slots with nlu-based predefined mappings, these set slot commands from LLM-based command generator are ignored. If no other commands were predicted for the same turn, then the assistant will trigger the cannot_handle conversation repair pattern.
Sometimes the user message may contain intentions that go beyond setting a slot. For example, the user message may contain an entity that fills a slot but also starts a digression that must be handled. In such cases, we recommend using NLU triggers to handle those specific intents within flows. Please refer to the Impact of slot mappings in different scenarios section for more details.
In a CALM assistant built with flows and using NLU components to process the message, the default action action_extract_slots will not run, because the slot set events are applied to the dialogue tracker during command execution. This ensures that this default action does not overwrite CALM set slot(./dialogue-understanding.mdx#set-slot) commands and does not duplicate SlotSet events that were already applied to the dialogue tracker.
In the case of coexistence, the action_extract_slots action will be executed only when the NLU-based system is active.
You can use the from_llm slot mapping type to fill slots with values generated by LLM-based command generators. This is the default slot mapping type if the mappings are not explicitly defined in the domain file.
In this example, the user_name slot will be filled with the value generated by the LLM-based command generator. The LLM-based command generator is allowed to fill this slot at any point in the conversation flow, not just at the corresponding collect step for this slot.
If you have defined additional NLU-based components in the config.yml pipeline, these components will continue to process the user message however they will not be able to fill slots. The NLUCommandAdapter will skip any slots with from_llm mappings and will not issue set slot commands to fill these slots. Please refer to the Impact of slot mappings in different scenarios section for more details.
Note that a slot must not have both from_llm and NLU-based predefined mappings or custom slot mappings. If you define a slot with from_llm mapping, you cannot define any other mapping types for that slot.
You can define conditions for slot mappings to be satisfied before the slot is filled. The conditions are defined as a list of conditions under the conditions key. Each condition can specify the flow id that must be active to the active_flow property.
This is particularly useful if you define several slots mapped to the same entity, but you do not want to fill all of them when the entity is extracted.
- active_flow: greet_user
- active_flow: issue_invoice
You can use the custom mapping type to define custom slot mappings for slots that should be filled by a custom action. The custom action must be specified in the action property of the slot mapping. You must also list the action in the domain file under the actions key.
- action_fill_user_name
action: action_fill_user_name
In this example, the user_name slot will be filled by the action_fill_user_name custom action. The custom action must return a SlotSet event with the slot name and value to fill the slot.
Note that if you're using the action_ask_
If you are using custom validation actions (using the validate_
If you are training with the --skip-validation flag and you have defined slots with custom slot mappings that do not
specify the action property in the domain file, nor do they have corresponding action_ask_
You can also run this check via the rasa data validate command.
This section clarifies which components in a CALM assistant built with flows and a NLU pipeline are responsible for filling slots in different scenarios when the flow is at either the collect step for slot name or at any other step.
Main takeaway is that the NLUCommandAdapter cannot fill slots with from_llm mappings at any point in the conversation.
You can provide an initial value for any slot in your domain file:
Channel-Specific Response Variations#
To specify different response variations depending on which channel the user is connected to, use channel-specific response variations.
In the following example, the channel key makes the first response variation channel-specific for the slack channel while the second variation is not channel-specific:
- text: "Which game would you like to play on Slack?"
- text: "Which game would you like to play?"
Make sure the value of the channel key matches the value returned by the name() method of your input channel. If you are using a built-in channel, this value will also match the channel name used in your credentials.yml file.
When your assistant looks for suitable response variations under a given response name, it will first try to choose from channel-specific variations for the current channel. If there are no such variations, the assistant will choose from any response variations which are not channel-specific.
In the above example, the second response variation has no channel specified and can be used by your assistant for all channels other than slack.
For each response, try to have at least one response variation without the channel key. This allows your assistant to properly respond in all environments, such as in new channels, in the shell and in interactive learning.
Slots and Conversation Behavior#
You can specify whether or not a slot influences the conversation with the influence_conversation property.
If you want to store information in a slot without it influencing the conversation, set influence_conversation: false when defining your slot.
The following example defines a slot age which will store information about the user's age, but which will not influence the flow of the conversation. This means that the assistant will ignore the value of the slot each time it predicts the next action.
influence_conversation: false
When defining a slot, if you leave out influence_conversation or set it to true, that slot will influence the next action prediction, unless it has slot type any. The way the slot influences the conversation will depend on its slot type.
The following example defines a slot home_city that influences the conversation. A text slot will influence the assistant's behavior depending on 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.
influence_conversation: true
As an example, consider the two inputs "What is the weather like?" and "What is the weather like in Bangalore?" The conversation should diverge based on whether the home_city slot was set automatically by the NLU. If the slot is already set, the bot can predict the action_forecast action. If the slot is not set, it needs to get the home_city information before it is able to predict the weather.
Storing true or false values.
If influence_conversation is set to true, the assistant's behavior will change depending on whether the slot is empty, set to true or set to false. Note that an empty bool slot influences the conversation differently than if the slot was set to false.
Storing slots which can take one of N values.
If influence_conversation is set to true, the assistant's behavior will change depending on the concrete value of the slot. This means the assistant's behavior is different depending on whether the slot in the above example has the value low, medium, or high.
A default value __other__ is automatically added to the user-defined values. All values encountered which are not explicitly defined in the slot's values are mapped to __other__. __other__ should not be used as a user-defined value; if it is, it will still behave as the default to which all unseen values are mapped.
Storing real numbers.
max_value=1.0, min_value=0.0
If influence_conversation is set to true, the assistant's behavior will change depending on the value of the slot. If the value is between min_value and max_value, the specific value of the number is used. All values below min_value will be treated as min_value, and all values above max_value will be treated as max_value. Hence, if max_value is set to 1, there is no difference between the slot values 2 and 3.5.
Storing arbitrary values (they can be of any type, such as dictionaries or lists).
Slots of type any are always ignored during conversations. The property influence_conversation cannot be set to true for this slot type. If you want to store a custom data structure which should influence the conversation, use a custom slot type.
Maybe your restaurant booking system can only handle bookings for up to 6 people. In this case you want the value of the slot to influence the next selected action (and not just whether it's been specified). You can do this by defining a custom slot class.
The code below defines a custom slot class called NumberOfPeopleSlot. The featurization defines how the value of this slot gets converted to a vector so Rasa machine learning model can deal with it. The NumberOfPeopleSlot has three possible “values”, which can be represented with a vector of length 2.
from rasa.shared.core.slots import Slot
class NumberOfPeopleSlot(Slot):
def feature_dimensionality(self):
def as_feature(self):
r = [0.0] * self.feature_dimensionality()
You can implement a custom slot class as an independent python module, separate from custom action code. Save the code for your custom slot in a directory alongside an empty file called "__init__.py" so that it will be recognized as a python module. You can then refer to the custom slot class by it's module path.
For example, say you have saved the code above in "addons/my_custom_slots.py", a directory relative to your bot project:
│ └── my_custom_slots.py
Your custom slot type's module path is then addons.my_custom_slots.NumberOfPeopleSlot. Use the module path to refer to the custom slot type in your domain file:
type: addons.my_custom_slots.NumberOfPeopleSlot
influence_conversation: true
Now that your custom slot class can be used by Rasa, add training stories that diverge based on the value of the people slot. You could write one story for the case where people has a value between 1 and 6, and one for a value greater than six. You can choose any value within these ranges to put in your stories, since they are all featurized the same way (see the featurization table above).
- story: collecting table info
# ... other story steps
- action: action_book_table
- story: too many people at the table
# ... other story steps
- action: action_explain_table_limit
As of 3.0, slot mappings are defined in the slots section of the domain. This change removes the implicit mechanism of setting slots via auto-fill and replaces it with a new explicit mechanism of setting slots after every user message. You will need to explicitly define slot mappings for each slot in the slots section of domain.yml. If you are migrating from an earlier version, please read through the migration guide to update your assistant.
Rasa comes with four predefined mappings to fill slots based on the latest user message.
In addition to the predefined mappings, you can define custom slot mappings. All custom slot mappings should contain a mapping of type custom.
Slot mappings are specified as a YAML list of dictionaries under the key mappings in the domain file. Slot mappings are prioritized in the order they are listed in the domain. The first slot mapping found to apply will be used to fill the slot.
The default behavior is for slot mappings to apply after every user message, regardless of the dialogue context. To make a slot mapping apply only within the context of a form see Mapping Conditions. There is one additional default limitation on applying from_entity slot mappings in the context of a form; see unique from_entity mapping matching for details.
Note that you can also define lists of intents for the optional parameters intent and not_intent.
The from_entity slot mapping fills slots based on extracted entities. The following parameters are required:
The following parameters are optional and can be used to further specify when the mapping applies:
not_intent: excluded_intent
There is an intentional limitation on applying from_entity slot mappings in the context of a form. When a form is active, a from_entity slot mapping will be applied only if one or more of the following conditions are met:
This limitation exists to prevent a form from filling multiple required slots with the same extracted entity value.
For example, in the example below, an entity date uniquely sets the slot arrival_date, an entity city with a role from uniquely sets the slot departure_city and an entity city with a role to uniquely sets the slot arrival_city, therefore they can be used to fit corresponding slots even if these slots were not requested. However, entity city without a role can fill both departure_city and arrival_city slots, depending which one is requested, so if an entity city is extracted when slot arrival_date is requested, it'll be ignored by the form.
Note that the unique from_entity mapping constraint will not prevent filling slots which are not in the active form's required_slots; those mappings will apply as usual, regardless of the uniqueness of the mapping. To limit applicability of a slot mapping to a specific form, see Mapping Conditions.
The from_text mapping will use the text of the last user utterance to fill the slot slot_name. If intent_name is None, the slot will be filled regardless of intent name. Otherwise, the slot will only be filled if the user's intent is intent_name.
The slot mapping will not apply if the intent of the message is excluded_intent.
not_intent: excluded_intent
To maintain the 2.x form behavior when using from_text slot mappings, you must use mapping conditions, where both active_loop and requested_slot keys are defined.
The from_intent mapping will fill slot slot_name with value my_value if user intent is intent_name. If you choose not to specify the parameter intent, the slot mapping will apply regardless of the intent of the message as long as the intent is not listed under not_intent parameter.
The following parameter is required:
The following parameters are optional and can be used to further specify when the mapping applies:
Note that if you choose not to define the parameter intent, the slot mapping will apply regardless of the intent of the message as long as the intent is not listed under the not_intent parameter.
not_intent: excluded_intent
The from_trigger_intent mapping will fill slot slot_name with value my_value if a form is activated by a user message with intent intent_name. The slot mapping will not apply if the intent of the message is excluded_intent.
- type: from_trigger_intent
not_intent: excluded_intent
To apply a slot mapping only within the context of a form, specify the name of the form in the conditions key of a slot mapping. Conditions list the form name(s) for which the mapping is applicable in the active_loop key.
Slot mappings can now specify null as the value of active_loop to indicate that the slot should only be filled when no form is active. Note that requested_slot cannot be used in conjunction with active_loop: null.
Conditions can also include the name of the requested_slot. If requested_slot is not mentioned, then the slot will be set if relevant information is extracted, regardless of which slot is being requested by the form.
- active_loop: your_form
requested_slot: slot_name
- active_loop: another_form
If conditions are not included in a slot mapping, the slot mapping will be applicable regardless of whether any form is active. As long as a slot is listed in a form's required_slots, the form will prompt for the slot if it is empty when the form is activated.
The requested_slot slot#
The slot requested_slot is automatically added to the domain as a slot of type text. The value of the requested_slot will be ignored during conversations. If you want to change this behavior, you need to add the requested_slot to your domain file as a categorical slot with influence_conversation set to true. You might want to do this if you want to handle your unhappy paths differently, depending on what slot is currently being asked from the user. For example, if your users respond to one of the bot's questions with another question, like why do you need to know that? The response to this explain intent depends on where we are in the story. In the restaurant case, your stories would look something like this:
- story: explain cuisine slot
- intent: request_restaurant
- action: restaurant_form
- active_loop: restaurant
- requested_slot: cuisine
- action: utter_explain_cuisine
- action: restaurant_form
- story: explain num_people slot
- intent: request_restaurant
- action: restaurant_form
- active_loop: restaurant
- requested_slot: cuisine
- requested_slot: num_people
- action: utter_explain_num_people
- action: restaurant_form
Again, it is strongly recommended that you use interactive learning to build these stories.