Forms

Forms

Warning: This document is for an old version of Rasa. The latest version is 1.10.26.

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}

In this story the user intent is request_restaurant, which is followed by the form action restaurant_form. With form{"name": "restaurant_form"} the form is activated and with form{"name": null} the form is deactivated again.

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.

Here’s an example for the restaurant bot:

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="number", intent=["inform", "request_restaurant"]
            ),
        ],
        "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. Here is an example of a validation method:

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. A typical scenario might look like this:

## chitchat
* request_restaurant
    - restaurant_form
    - form{"name": "restaurant_form"}
* stop
    - utter_ask_continue
* deny
    - action_deactivate_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, add it to your domain file as a categorical slot.

Debugging

Try to run your bot with the --debug flag for additional insights.

👋 I can help you get started with Rasa and answer your technical questions.