Introduction to Forms

⚠️

Note: This is material for Rasa 2.x. The syntax has updated slightly in Rasa 3.0 so we recommend new users to check the new course found here.

In this segment we will teach you how to make custom forms. Rasa Forms are a way to create collect slot values from the user in a structured way.

Video


Code

There are some important changes that we've made to our domain.yml file. You can see them highlighted in the code block below.

version: '2.0'
session_config:
session_expiration_time: 60
carry_over_slots_to_new_session: true
intents:
- greet
- deny
- request_names
- goodbye
- affirm
- mood_great
- mood_unhappy
- bot_challenge
forms:
name_form:
first_name:
- type: from_text
last_name:
- type: from_text
slots:
first_name:
type: text
influence_conversation: true
last_name:
type: text
influence_conversation: true
responses:
utter_greet:
- text: Hey! How are you?
utter_cheer_up:
- text: 'Here is something to cheer you up:'
image: https://i.imgur.com/nGF1K8f.jpg
utter_did_that_help:
- text: Did that help you?
utter_happy:
- text: Great, carry on!
utter_goodbye:
- text: Bye
utter_iamabot:
- text: I am a bot, powered by Rasa.
utter_ask_first_name:
- text: What is your first name?
utter_ask_last_name:
- text: What is your last name?
utter_submit:
- text: Ok. Thanks!
utter_slots_values:
- text: I will remember that your name is {first_name} {last_name}!

Form Definition

First, the domain file has a form defined. It's defined in this part:

forms:
  name_form:
    first_name:
    - type: from_text
    last_name:
    - type: from_text

We're declaring that our form has two slots that we'd like to fill; first_name and last_name.

Slot Definition

Second, the domain file has slots defined. Note that the names of these slots correspond with the names in our form definition.

slots:
  first_name:
    type: text
    influence_conversation: true
  last_name:
    type: text
    influence_conversation: true

Note that these two slots have types attached. In this case we're using strings but Rasa allows for many different slot types.

Utterance Definition

Finally, we also add some utterances in the domain.yml file. These are used in our stories.

  utter_ask_first_name:
  - text: What is your first name?
  utter_ask_last_name:
  - text: What is your last name?
  utter_submit:
  - text: Ok. Thanks!
  utter_slots_values:
  - text: I will remember that your name is {first_name} {last_name}!

You may have also noticed that some utterances follow a specific naming convention utter_ask_<slotname>. These utterances will be picked up by our form to ask for a slot value. This is explored in more detail in the next video.

The utter_slots_values utterance uses slot values to generate a response. This allows us to confirm what the user has provided as names.

Stories

The dialogue in the video is recorded via rasa interactive and it will produce a story in our stories.yml file. Although it's not shown in the video, we wanted to share the story here.

- story: interactive_story_1
steps:
- intent: greet
- action: utter_greet
- intent: request_names
- action: name_form
- active_loop: name_form
- slot_was_set:
- requested_slot: first_name
- slot_was_set:
- first_name: vincent
- slot_was_set:
- requested_slot: last_name
- slot_was_set:
- last_name: vincent-mcvincent
- slot_was_set:
- requested_slot: null
- active_loop: null
- action: utter_submit
- action: utter_slots_values

Links

Exercises

Try to answer the following questions to test your knowledge.

  1. Can you use slots in responses defined in the domain file?
  2. How can you indicate that a slot was set in the stories.yml file?
  3. When slots appear in a stories.yml file, do they influence the next action prediction? If so, do they do it all the time?

2016-2024 © Rasa.