ValidationAction: the base class for custom actions extracting and validating slots.
ValidationAction class.
ValidationAction class
NLU-based assistants
This section refers to building NLU-based assistants. If you are working with Conversational AI with Language Models (CALM), this content may not apply to you. You can extend theValidationAction class in the Rasa SDK to define custom extraction and / or validation of slots.
How to subclass ValidationAction
Firstly, you must add the name of this action, action_validate_slot_mappings, to the domain actions list.
Note that you do not need to implement the name method in the custom action extending ValidationAction, since this
is already implemented. If you override the name method, the custom validation action will not run because the
original name is hardcoded in the call that the default action action_extract_slots makes to the action server.
You should create only one subclass of ValidationAction which should contain all extraction and validation
methods for different slots according to your use-case.
With this option, you do not need to specify the action key in the custom slot mapping,
since the default action action_extract_slots runs action_validate_slot_mappings
automatically if present in the actions section of the domain.
Validation of Slots with Predefined Mappings
To validate slots with a predefined mapping, you must write functions namedvalidate_<slot_name>.
In the following example, the value for slot location is capitalized only if the extracted value is of type string:
Extraction of Custom Slot Mappings
To define custom extraction code, write anextract_<slot_name> method for every slot with a
custom slot mapping.
The following example shows the implementation of a custom action that extracts the slot count_of_insults to keep
track of the user’s attitude.
ValidationAction class implementation
ValidationAction is a subclass of the Action Rasa SDK class and the abstract Python ABC class.
Therefore the class implements the name and run methods inherited from Action.
In addition, ValidationAction implements more specialized methods that will be called in the run method:
get_extraction_events: Extracts custom slots using availableextract_<slot name>methodsget_validation_events: Validates slots by calling availablevalidate_<slot name>methods for each slotrequired_slots: Returns slots which the validation action should fill
Methods
ValidationAction.name
Defines the action’s name: this must be hardcoded asaction_validate_slot_mappings.
- Returns: Name of action
-
Return type:
str
ValidationAction.run
run method executes the custom extraction code defined in extract_<slot name> methods by calling the
get_extraction_events method, then updates the tracker with the returned events.
The run method will also execute custom validation code defined in validate_<slot name> methods via the
get_validation_events method and add the returned events to the tracker.
Parameters
- dispatcher – the dispatcher which is used to
send messages back to the user. Use
dispatcher.utter_message()or any otherrasa_sdk.executor.CollectingDispatchermethod. See the documentation for the dispatcher - tracker – the state tracker for the current
user. You can access slot values using
tracker.get_slot(slot_name), the most recent user message istracker.latest_message.textand any otherrasa_sdk.Trackerproperty. See the documentation for the tracker. - domain – the bot’s domain
Returns
A list ofrasa_sdk.events.Event instances. See the documentation for events.
Return type
List[Dict[str, Any]]
ValidationAction.required_slots
required_slots method will return the domain_slots which is a list of all slot names mapped in the domain that
do not include any slot mapping with conditions. domain_slots is returned by the domain_slots method, which only takes
Domain as an argument.
Returns
A list of slot names of typeText.
ValidationAction.get_extraction_events
get_extraction_events method will gather the list of slot names via required_slots method call and then loop
through every slot name to run the extract_<slot name> method if available.
Returns
A list ofrasa_sdk.events.SlotSet instances. See the documentation for SlotSet events.
ValidationAction.get_validation_events
get_validation_events method will gather the list of slot names to validate via required_slots method call.
Then it will get a mapping of slots which were recently set and their values via tracker.slots_to_validate call.
Looping through this mapping of recently extracted slots, it will check if the slot is in required_slots then run the
validate_<slot name> method if available for that slot.
Returns
A list ofrasa_sdk.events.SlotSet instances. See the documentation for SlotSet events.
How to implement custom validation in CALM assistants
In CALM assistants, slot validation actions should be implemented like normal actions by subclassing the baseAction class.
Here is an example of a custom slot validation action in a CALM assistant: