Forms
Forms
Note
There is an in-depth tutorial here about how to use Rasa Forms for slot filling.
Configuration File
To use forms, you also need to include the FormPolicy in your policy configuration file. For example:
policies:
- name: "FormPolicy"
Form Basics
Using a FormAction, you can describe all of the happy paths with a single story. By “happy path”, we mean that whenever you ask a user for some information, they respond with the information you asked for.
## happy path
* request_restaurant
- restaurant_form
- form{"name": "restaurant_form"}
- form{"name": null}
Custom slot mappings
If you do not define slot mappings, slots will be only filled by entities with the same name as the slot that are picked up from the user input. Some slots, like cuisine, can be picked up using a single entity, but a FormAction can also support yes/no questions and free-text input. The slot_mappings method defines how to extract slot values from user responses.
def slot_mappings(self) -> Dict[Text, Union[Dict, List[Dict]]]:
return {
"cuisine": self.from_entity(entity="cuisine", not_intent="chitchat"),
"num_people": [
self.from_entity(entity="num_people", intent=["inform", "request_restaurant"]),
self.from_entity(entity="number"),
],
"outdoor_seating": [
self.from_entity(entity="seating"),
self.from_intent(intent="affirm", value=True),
self.from_intent(intent="deny", value=False),
],
"preferences": [
self.from_intent(intent="deny", value="no additional preferences"),
self.from_text(not_intent="affirm"),
],
"feedback": [self.from_entity(entity="feedback"), self.from_text()],
}
Validating user input
After extracting a slot value from user input, the form will try to validate the value of the slot. By default, validation only checks if the requested slot was successfully extracted from the slot mappings. If you want to add custom validation, for example to check a value against a database, you can do this by writing a helper validation function with the name validate_{slot-name}.
def validate_cuisine(
self,
value: Text,
dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any],
) -> Dict[Text, Any]:
if value.lower() in self.cuisine_db():
return {"cuisine": value}
else:
dispatcher.utter_message(template="utter_wrong_cuisine")
return {"cuisine": None}
Handling unhappy paths
Of course your users will not always respond with the information you ask of them. The way this works with forms is that a form will raise an ActionExecutionRejection if the user didn’t provide the requested information. You need to handle events that might cause ActionExecutionRejection errors in your stories.
## chitchat
* request_restaurant
- restaurant_form
- form{"name": "restaurant_form"}
* chitchat
- utter_chitchat
- restaurant_form
- form{"name": null}
The requested_slot slot
The slot requested_slot is automatically added to the domain as an unfeaturized slot. If you want to make it featurized, you need to add it to your domain file as a categorical slot.
Summary
The FormAction will only request slots which haven’t already been set. Once all the slots are filled, the submit() method is called. The predefined functions help to define how to extract and validate values dynamically.