Image for post
Image for post

Building contextual assistants with Rasa Forms

Justina Petraityte
Jan 10, 2019 · 10 min read

A contextual assistant that goes beyond simple FAQ-style interactions requires more than just an algorithm and a prayer. A conversational assistant needs to have collected important details needed to answer user questions in the right context. Otherwise, no happy path. Simple enough, and known as slot filling. But how do you gather and define the details that matter before taking action, or providing a response?

Slot filling is made easy with our new addition of FormPolicy. This is a fresh feature that implements slot filling in an easy and effective way. How? FormPolicy allows you to cover all the happy paths with a single story. Forms also let you alter logic within a happy path, without needing to change the training data.

So, how do you implement this new technique? Glad you asked. Here’s how to get FormPolicy working for you.


  1. Introduction to Slot Filling
  2. Building a restaurant search assistant using the new Forms:
  • Step 1: Extracting details from user inputs using Rasa NLU
  • Step 2: Training the dialogue model: handling the happy path with forms
  • Step 3: Defining the domain
  • Step 4: Defining the FormAction
  • Step 5: Expanding the FormAction to handle more advanced cases
  • Step 6: Handling the deviations from the happy path
  • Step 7: Testing the restaurant search assistant

3. Summary

4. Useful resources

Introduction to Slot Filling

Slot filing is a process of collecting important pieces of information in order to fulfil the user’s request. It’s all about having relevant data on hand that may be useful in a given conversation.

Let’s take a restaurant search assistant as an example to illustrate it. Before the assistant can actually perform a restaurant search action, it has to know users preferences like cuisine, price range, location, etc, in order to make a useful suggestion.

To store such information, Rasa uses slots. In simple cases, you can implement slot filling by only using slots, but things quickly become complicated once the complexity of the conversations grows. With more details comes more possible dialogue turns, which creates larger and larger training data requirements. This is where FormAction comes to the rescue. It allows you to enforce strict logic on the information collection process, and considerably reduce the amount of training data needed to build a good dialogue model.

Building a restaurant search assistant using Rasa Forms

In the remaining part of this tutorial, you will learn how to use the new FormPolicy in practice. This tutorial is based on a restaurant search assistant called . You can find all the code inside the Rasa GitHub repository:

By following this tutorial and using the code inside the repo, you will build a fun assistant, capable of suggesting restaurants based on user preferences like cuisine, number of people, preferred seating, and other possible requirements. An example conversation between the user and the assistant will look as follows:

Image for post
Image for post

Step 1: Extracting details from user inputs using Rasa NLU

Before storing important information as slots, the assistant has to extract them from user inputs.

To enable your assistant to do that, it is necessary to train the NLU model which will classify the intent of the user inputs and extract important details as entities.

Formbot example already comes with training examples which you can use to train the NLU model. Using the provided training examples you can teach your assistant to understand inputs like greeting, restaurant search queries, inputs to supply requested information, etc, and then extract entities like cuisine, number of people, additional requirements, etc. You can find the NLU training examples inside the data/ file of the formbot example.

To train the model, run the command below. This command will call the Rasa NLU train function, pass the training data and model configuration files, and save the model inside the models directory of your working directory:

rasa train nlu

Note: if you are new to Rasa NLU and would like to learn more about it, make sure to check out the Rasa NLU documentation.

Step 2: Training the dialogue model: handling the happy path with forms

Once the assistant is capable of understanding the user inputs, it’s time to build a dialogue management model. When using Forms for Slot Filling, it’s best to get started by training the model to handle the happy paths — situations where the user provides all the required information and lets the assistant drive the conversation.

The best part about the new forms is that the assistant learns to handle all the happy paths from one single training story. Check out the video below for the illustration:

How does it work?

Once the form action gets predicted, the assistant keeps asking for necessary details until all required slots are set. There are no restrictions on how the user should provide the details - if a user specifies all preferences in the initial restaurant request, for example, ‘Book me a table for two at the Chinese restaurant’, the assistant will skip the questions about the cuisine and number of people.

If the user does not specify any of these details in an initial request, then the assistant asks all the details in follow-up questions until all of them are provided. Both of these situations represent two different conversations (there can be more than two), but by using FormsPolicy they both will be learned using the same, single story. Below is a snippet of the training story used to model all the happy paths in a formbot example:

## happy path 
* greet
- utter_greet
* request_restaurant
- restaurant_form
- form{"name": "restaurant_form"}
- form{"name": null}
- utter_slots_values
* thankyou
- utter_noworries

Check out the data/ file of the Formbot example to investigate the training stories in more detail.

Step 3: Defining the domain

In order to train the dialogue management model with Rasa, you also need to define the domain. This is where you can specify which extracted details should be stored as slots.

There are three important things you should consider when defining the domain for an assistant with slot filling:

  1. In Rasa, different slot types have a different influence on the predictions of the next action. When using FormAction to fill the slots, you are enforcing strict rules which tell your assistant what information it should ask for next. This way, you allow FormAction to handle all the happy paths with on single story as it checks which requested slots are populated and which ones are still missing. For this to work, the requested slots in your domain file should be defined as unfeaturized.
  2. The names of the templates which will be used to ask for the missing required slots should follow the format utter_ask_{slotname}. This is important for FormAction to know which template to use for which slot.
  3. In addition to all the usual parts of the domain (intents, entities, templates, actions and slots), you will have to include one additional section called forms. This section has to include the names of all form actions your assistant should be able to predict based on the stories defined in a training data file.

Below is a snippet of the domain used in a formbot example:

- cuisine
- num_people
- number
- feedback
- seating
type: unfeaturized
auto_fill: false
type: unfeaturized
auto_fill: false
type: unfeaturized
auto_fill: false
type: unfeaturized
auto_fill: false
type: unfeaturized
- restaurant_form

Step 4: Defining the FormAction

The next step is to actually implement the FormAction which, once predicted, will handle the slot filling. You can implement all FormActions in the file where you would implement any other custom action you would like your assistant to handle. Let’s define the FormAction for the slot filling of the restaurant assistant step-by-step (check out the file of the formbot example to see a full code):

  • First, define the form action class. Note, that form actions, in contrast to regular custom actions, inherit from FormAction class:
  • The first function which you should define in a form action class is called name which simply defines the name of the action (the same one you defined in the domain file). In a restaurant search example it is defined as restaurant_form:
  • Next, a function called required_slots is used to define a list of slots which the assistant needs to fill in before responding to the user’s request:

Pro tip: required_slots function is a great place to introduce some custom slot logic. For example, it might make sense to include the outdoors seating option only for the restaurants of a specific cuisine. You can achieve this by introducing simple logic like in the example below, where outdoor_seating slot will only be required when the users ask for restaurants which serve Greek food:

  • The final building block of a simple form action is a function called submit which defines what should happen once all required slots are populated. In a restaurant search assistant case, once all the slots are populated, the assistant will execute the template utter_submit which, based on how it’s defined in a domain.yml file, will confirm that the assistant has all the information it needs to proceed:

And there you have it — a simple implementation of the FormAction. There is so much more you can add to enable your assistant to handle even more advanced cases. Let’s look into that in the next step of this tutorial.

Step 5: Handling the advanced cases with FormAction

Some necessary slots can come from very different user inputs. For example, the user could answer the question ‘Would you like to sit outside?’ with the following possible answers:

  • ‘Yes’
  • ‘No’
  • ‘I prefer sitting indoors’ (or similar direct answer)

Each of these responses corresponds to different intents or has different important entities, but since they provide a viable answer to the question, an assistant must be able to accept it and set a slot to move forward.

This is where the slot_mappings function in a FormAction comes in play — it defines how to extract slot values from possible user responses and maps them to a specific slot. Below is an example of the slot_mappings function for the previously discussed slot. Based on the defined logic, the slot will be populated using either:

  • value ‘True’ if the user responds with ‘affirm’ intent to the question
  • value ‘False’ if the user responds with ‘deny’ intent to the question
  • value of the extracted entity

You can find more examples of slot mapping inside the slot_mappings function of the file of the formbot example.

Another useful thing you can do with FormAction is slot validation. For example, before allowing your assistant to move on with questions, you may want to check the slot value against possible values in your database or check if the value is in the right format. You can achieve that by creating a function called validate in your FormAction class. By default, it checks if the requested slot was extracted, but you can add as much additional logic as you need. Below is an example of the validation function which first checks if the requested slot was populated and later checks if the provided number of people is in the right format: if the number is an integer, the assistant will use the provided value, if not it will respond with a message that the format of the slot value is invalid, set the slot to None and ask for it again.

You can find more examples of the slot validation (checking against the database, string matching) in the file of the formbot example.

Step 6: Handling the deviations from the happy path

The main idea behind the slot filling with FormAction is that strict logic is applied to collect the important pieces of information and handle the happy path while the regular machine learning is used to gracefully handle the deviations from the happy path. These deviations could be some chit-chat messages in the middle of the form action session or the situations where the users refuse to provide all the necessary details. In order to handle situations like this, you have to write the stories which represent such conversation turns.

For example, the story below represents the situation where the user decided to stop providing the information in the middle of the form action session but later came back to provide the required details:

## stop but continue path
* request_restaurant
- restaurant_form
- form{"name": "restaurant_form"}
* stop
- utter_ask_continue
* affirm
- restaurant_form
- form{"name": null}
- utter_slots_values
* thankyou
- utter_noworries

To enable your assistant to handle even more advanced cases, you need to collect more stories like this to cover different dialogue turns. In some situations, you might want to handle unhappy paths differently depending on what slot is currently being asked for from the user. To achieve that, inside the domain file you should set to categorical. This will allow the predictions of the next response to be influenced depending on which slot is currently requested.

Check out the file data/ of the formbot example to see more possible unhappy path stories.

Step 7: Testing the restaurant search assistant

By now you have learned a lot about implementing the FormAction and have defined all the necessary bits of the dialogue management model. Now it’s time for the most exciting part — testing the assistant!

First, train the dialogue management model using the following command which will call the Rasa train function, pass the domain and data files to it, and store the trained model inside the models directory of your working directory:

rasa train

Once the model is trained, time to test how the restaurant search bot performs!

First, in a new terminal, start a duckling server by running the following command:

docker run -p 8000:8000 rasa/duckling

Launch your assistant by executing the following command which will spin up the local server for custom actions and load the assistant for you to chat:

rasa run actions&
rasa shell -m models --endpoints endpoints.yml

To best see how the FormAction works, spend some time testing how the assistant performs on different happy and unhappy paths.


Building good contextual assistants is not easy. Combining FormAction with traditional ML allows you to build assistants that can handle deeper conversations without requiring you to write loads of training stories to handle the happy path. In addition to this, FormAction makes it a lot easier to make changes to the code and dialogue of your assistant as requested slots are not used in the training stories directly.

Test the new forms on your own datasets and share your feedback with us by joining the discussion on this thread of the Rasa Community Forum!


Originally published at on January 10, 2019.

Rasa Blog

Open source conversational AI

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store