DEV Community

Cover image for RASA - Unhappy paths
Petr Janik
Petr Janik

Posted on • Edited on

RASA - Unhappy paths

When users communicates with the chatbot, they don't always behave the way we would like them to.
When filling a newsletter form, they might jump to a chitchat or change their mind and not fill the form at all.

We will handle those two cases now.

Chitchat

It might happen, that user starts talking about something completely irrelevant when filling the newsletter form. In this case, we want to provide a way for the user to go back on track and finish the form.

Create a new intent that will mean that user went out of scope. Add a few training examples to this intent.

# data/nlu.yml
# ... previous contents ...
  - intent: chitchat
    examples: |
      - that's not what I want to do
      - wait stop
      - you're no help
      - this is no help at all
      - how old are you
      - I want to order a pizza
      - tell me the weather
      - this isn't working
      - I already told you that
      - don't like that
      - I don't want to tell you that
      - none of your business
      - that's not right
      - stop asking
      - nevermind
      - I want to do something else
      - I changed my mind
Enter fullscreen mode Exit fullscreen mode

Add another two intents affirm and deny.

# data/nlu.yml
# ... previous contents ...
  - intent: affirm
    examples: |
      - yes
      - indeed
      - of course
      - that sounds good
      - correct
      - sure
      - ok
      - why not
      - ok great
      - yes I did
      - yeah, great
      - pretty good
      - fine
      - You bet
      - I sure did
      - more or less
  - intent: deny
    examples: |
      - no
      - never
      - I don't think so
      - no way
      - not really
      - none
      - absolutely not
      - no thanks
      - didn't do any
      - no I didn't
      - could have been better
      - not great
      - nope
Enter fullscreen mode Exit fullscreen mode

As all other intents, list those in domain.yml file too.

# domain.yml
# ... previous contents ...
intents:
  # ... previous intents ...
  - chitchat
  - affirm
  - deny
Enter fullscreen mode Exit fullscreen mode

If our chatbot notices chitchat intent, it will ask if the user wants to continue. Let's create an utterance for this.

# domain.yml
# ... previous contents ...
  utter_ask_continue:
    - text: Sorry, I don't quite understand. Do you want to continue?
Enter fullscreen mode Exit fullscreen mode

Now let's put those intents and responses together in a story.

# data/stories.yml
# ... previous contents ...
  - story: newsletter form continue
    steps:
      - intent: subscribe
      - action: newsletter_form
      - active_loop: newsletter_form
      - intent: chitchat
      - action: utter_ask_continue
      - intent: affirm
      - action: newsletter_form
Enter fullscreen mode Exit fullscreen mode

In this story, if chitchat intent is recognized, the chatbot will ask if the user wants to continue. If so, the form is reactivated.

Now run rasa train && rasa test.
Let's test our assistant.
Chat with chitchat during email question

Looks good, now let's try chitchat during the second form question.

Chat with chitchat during frequency question handled poorly

What happened? Why does it consider our pizza order an email frequency? To answer this, I will introduce new – very useful – rasa command: rasa interactive.
This will train our model and start an interactive learning session to create new training data for a Rasa model by chatting. We will, however, use this to debug our assistant.

During the interactive chat, you see chatbot's decisions about what intent was expressed:

Subscribe intent during interactive chat

what action should follow:

newsletter_form action during interactive chat

and which entities have been recognized:

Recognized email entity

You can modify those decisions.

We can see, that the chatbot recognized our pizza order properly as a chitchat intent.

Chitchat intent recognized during interactive chat

So where is the problem?
The problem is, that no matter the intent, the frequency slot is always filled from_text.

Current slots

We need to tell the chatbot, that the frequency slot should be filled only when the intent is inform_frequency. There is an easy way to do this in the domain.yml file.

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

Now frequency slot will be filled from_text only if the intent was inform_frequency.

You can learn more about this in the documentation.

Now it is right!

Chat with chitchat during frequency question handled properly

Leaving the form

The second unhappy path that might occur is user not wanting to continue the form at all. In cases like this, the assistant should stop asking for the requested slots.
Here's a story for this.

# data/stories.yml
# ... previous contents ...
  - story: newsletter form stop
    steps:
      - intent: subscribe
      - action: newsletter_form
      - active_loop: newsletter_form
      - intent: chitchat
      - action: utter_ask_continue
      - intent: deny
      - action: action_deactivate_loop
      - active_loop: null
Enter fullscreen mode Exit fullscreen mode

We handle the situation using a default action action_deactivate_loop which will deactivate the form and reset the requested slot.

Cancelling the form

We see that the chatbot stopped asking questions after the user cancelled the form.

You can learn more about unhappy paths in the documentation.

In the next chapter, we will look at testing those unhappy paths.

Repository for this tutorial:

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

git clone --branch 05-unhappy-paths git@github.com:petr7555/rasa-dev-tutorial.git
Enter fullscreen mode Exit fullscreen mode

Top comments (0)