DEV Community

loading...
Cover image for RASA - Categorical slot

RASA - Categorical slot

petr7555 profile image Petr Janik Updated on ・3 min read

Remember, when creating a form, we have to specify the form slots, how they are filled and what their type is.

It looks like this:

# domain.yml
# ... previous content ...
forms:
  newsletter_form:
    email:
      - type: from_entity
        entity: email
    frequency:
      - type: from_text
        intent: inform_frequency

slots:
  email:
    type: text
    influence_conversation: false
  frequency:
    type: text
    influence_conversation: false
Enter fullscreen mode Exit fullscreen mode

Let's say that our newsletter is paid based on the frequency. We will make frequency a categorical slot and add two possible values. This does not change anything so far. Categorical slot does not provide a validation. Any value can still be assigned to it.
However, if we set influence_conversation to true, we can write a story or rule and change the flow based on the value of this slot.

# domain.yml
# ... previous content ...
slots:
  email:
    type: text
    influence_conversation: false
  frequency:
    type: categorical
    values:
      - once a week
      - twice a week
    influence_conversation: true
Enter fullscreen mode Exit fullscreen mode

After choosing the frequency, the user might ask for the price. Let's create an ask_price intent.

# data/nlu.yml
# ... previous content ..
  - intent: ask_price
    examples: |
      - how much will it be
      - what's the price
      - how much will it cost
      - How much will I pay?
Enter fullscreen mode Exit fullscreen mode

Add this intent to domain.yml

# domain.yml
# ... previous content ..
intents:
  # ... previous intents ..
  - ask_price
Enter fullscreen mode Exit fullscreen mode

There are two possible prices, one for each frequency. We will create a response for each.

# domain.yml
# ... previous content ...
responses:
  # ... previous responses ...
  utter_price_for_once_a_week:
    - text: Once a week subscription costs $5.
  utter_price_for_twice_a_week:
    - text: Twice a week subscription costs $10.
Enter fullscreen mode Exit fullscreen mode

Create two new stories:

# data/stories.yml
# ... previous content ...
  - story: once a week
    steps:
      - intent: subscribe
      - action: newsletter_form
      - active_loop: newsletter_form
      - slot_was_set:
          - frequency: once a week
      - intent: ask_price
      - action: utter_price_for_once_a_week
      - action: newsletter_form

  - story: twice a week
    steps:
      - intent: subscribe
      - action: newsletter_form
      - active_loop: newsletter_form
      - slot_was_set:
          - frequency: twice a week
      - intent: ask_price
      - action: utter_price_for_twice_a_week
      - action: newsletter_form
Enter fullscreen mode Exit fullscreen mode

The chatbot's answer is based on what frequency value is in the slot.
After the utter_price_for_[once|twice]_a_week action, the form is reactivated by - action: newsletter_form.

We also want to be able to ask for the price even after the form is finished. Add two new rules for this:

# data/rules.yml
# ... previous content ...
  - rule: once a week
    steps:
      - slot_was_set:
          - frequency: once a week
      - intent: ask_price
      - action: utter_price_for_once_a_week

  - rule: twice a week
    steps:
      - slot_was_set:
          - frequency: twice a week
      - intent: ask_price
      - action: utter_price_for_twice_a_week
Enter fullscreen mode Exit fullscreen mode

I have also added another form slot notification to demonstrate asking for the price during the form filling.

Let's try our assistant: rasa train && rasa shell.

Once a week

Chat with once per week subscription

Twice a week

Chat with twice per week subscription

We can see that the chatbot correctly answered both during the form filling and after the form has been completed.

You can learn more about categorical slot in the documentation.

At this point, I would like to make one of our tests more robust.
Change Newsletter form two interruptions test story in tests/test_stories.yml to the following:

# ... previous content ...
stories:
  # ... previous stories ...
  - story: Newsletter form two interruptions
    steps:
      - intent: subscribe
        user: |-
          I want to subscribe
      - action: newsletter_form
      - active_loop: newsletter_form
      - slot_was_set:
          - requested_slot: email
      - intent: chitchat
        user: |-
          How's the weather?
      - action: utter_ask_continue
      - intent: affirm
        user: |-
          Yes, I do
      - action: newsletter_form
      - slot_was_set:
          - requested_slot: email
      - intent: inform_email
        user: |-
          my email is [example@email.com](email)
      - slot_was_set:
          - email: example@email.com
      - action: newsletter_form
      - slot_was_set:
          - email: example@email.com
      - slot_was_set:
          - requested_slot: frequency
      - intent: chitchat
        user: |-
          I want to order a pizza
      - action: utter_ask_continue
      - intent: affirm
        user: |-
          Sure
      - action: newsletter_form
      - slot_was_set:
          - requested_slot: frequency
      - intent: inform_frequency
        user: |-
          twice a week
      - action: newsletter_form
      - slot_was_set:
          - frequency: twice a week
      - slot_was_set:
          - requested_slot: notification
      - intent: affirm
        user: |-
          yes
      - action: newsletter_form
      - slot_was_set:
          - notification: yes
      - slot_was_set:
          - requested_slot: null
      - active_loop: null
      - action: utter_subscribed
Enter fullscreen mode Exit fullscreen mode

This test was automatically generated by having a conversation with the chatbot in Rasa X. You have learned about Rasa X in the previous chapter.

In the next chapter, we will look at requested_slot.

Repository for this tutorial:

You can checkout the state of the repository at the end of this tutorial by running:

git clone --branch 09-categorical-slot git@github.com:petr7555/rasa-dev-tutorial.git
Enter fullscreen mode Exit fullscreen mode

Discussion (0)

pic
Editor guide