Forms

These docs are for version 1.x of Rasa Open Source.

User Guide

NLU

Core

Conversation Design

API Reference

Migrate from (beta)

Reference

Versions

viewing: 1.9.3
tags
1.10.26 1.10.25 1.10.24 1.10.23 1.10.22 1.10.21 1.10.20 1.10.19 1.10.18 1.10.17 1.10.16 1.10.15 1.10.14 1.10.13 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 1.10.7 1.10.6 1.10.5 1.10.4 1.10.3 1.10.2 1.10.1 1.10.0 1.9.7 1.9.6 1.9.5 1.9.4 1.9.3 1.9.2 1.9.1 1.9.0 1.8.3 1.8.2 1.8.1 1.8.0 1.7.4 1.7.3 1.7.2 1.7.1 1.7.0 1.6.2 1.5.3 1.4.6 1.3.10 1.2.9

Forms

Note

There is an in-depth tutorial here about how to use Rasa Forms for slot filling.

One of the most common conversation patterns is to collect a few pieces of information from a user in order to do something (book a restaurant, call an API, search a database, etc.). This is also called slot filling.

If you need to collect multiple pieces of information in a row, we recommended that you create a FormAction. This is a single action which contains the logic to loop over the required slots and ask the user for this information. There is a full example using forms in the examples/formbot directory of Rasa Core.

Example of a FormAction

When you define a form, you need to add it to your domain file. If your form’s name is restaurant_form, your domain would look like this:

forms:
  - restaurant_form
actions:
  ...

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.

If we take the example of the restaurant bot, this single story describes all of the happy paths.

## happy path
* request_restaurant
    - restaurant_form
    - form{"name": "restaurant_form"}
    - form{"name": null}

The FormAction will only request slots which haven’t already been set. If a user starts the conversation with I’d like a vegetarian Chinese restaurant for 8 people, then they won’t be asked about the cuisine and num_people slots.

Note that for this story to work, your slots should be unfeaturized. If any of these slots are featurized, your story needs to include slot{} events to show these slots being set. In that case, the easiest way to create valid stories is to use Interactive Learning.

Required Methods in FormAction

You need to define three methods:

def name(self) -> Text:
    return "restaurant_form"
@staticmethod
def required_slots(tracker: Tracker) -> List[Text]:
    return ["cuisine", "num_people", "outdoor_seating", "preferences", "feedback"]
def submit(
    self,
    dispatcher: CollectingDispatcher,
    tracker: Tracker,
    domain: Dict[Text, Any],
) -> List[Dict]:
    dispatcher.utter_message(template="utter_submit")
    return []

Handling Unhappy Paths

Of course 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. The way this works with forms is that a form will raise an ActionExecutionRejection if the user didn’t provide the requested information.

Example of Chitchat Handling

You could add a story like this:

## chitchat
* request_restaurant
    - restaurant_form
    - form{"name": "restaurant_form"}
* chitchat
    - utter_chitchat
    - restaurant_form
    - form{"name": null}

Validating User Input

After extracting a slot value from user input, the form will try to validate the value of the slot. Any required slots that were filled before the initial activation of a form are validated upon activation as well.

Example Validation

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}

Debugging

Run your bot with the debug flag to aid in testing. One of the guiding principles behind Rasa Core is:

Learning from real conversations is more important than designing hypothetical ones

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