DEV Community

Cover image for RASA - Creating your first chatbot
Petr Janik
Petr Janik

Posted on

RASA - Creating your first chatbot

Let's start with a very simple example.
We will have a chatbot that will respond to our greeting.

To start fresh, delete the contents of data/nlu.yml, data/rules.yml, data/stories.yml and domain.yml

Intents

Our greeting is called an intent. Think of an intent as of a meaning of our message.
This intent can be expressed in many ways, for example by saying "Hi!". We will create this intent in the data/nlu.yml file. In the examples below the intent, we will list a couple of phrases the user might say to express the greet intent. The suggested number of examples is at least 5.

# data/nlu.yml
version: "2.0"

nlu:
  - intent: greet
    examples: |
      - hey
      - hello
      - hi
      - hello there
      - good morning
      - good evening
      - moin
      - hey there
      - let's go
      - hey dude
      - goodmorning
      - goodevening
      - good afternoon
Enter fullscreen mode Exit fullscreen mode

Intents and their examples are used as training data for the assistant's Natural Language Understanding (NLU) model. They do not have to match exactly what the user might say, as the model learns to generalize.
Since we need at least 2 different intents to train DIETClassifier (which is an architecture that handles both intent classification and entity recognition), we will add another intent mood_great.

# data/nlu.yml
# ... previous content ...
  - intent: mood_great
    examples: |
      - perfect
      - great
      - amazing
      - feeling like a king
      - wonderful
      - I am feeling very good
      - I am great
      - I am amazing
      - I am going to save the world
      - super stoked
      - extremely good
      - so so perfect
      - so good
      - so perfect
Enter fullscreen mode Exit fullscreen mode

You can learn more about intents in the documentation.

Domain

Now let's look at the domain.yml file.
The domain defines the universe in which your assistant operates.

We need to list all our intents from data/nlu.yml. We had two, called greet an mood_great.

# domain.yml
version: "2.0"

intents:
  - greet
  - mood_great
Enter fullscreen mode Exit fullscreen mode

Next, we define responses our chatbot might say.
Name of a response must start with utter_.
We will create two. First one, called utter_greet, which will be a response to our greet intent, and a second one utter_happy, which will be a response to a user saying they feel happy. We specify the text of the responses under each response's name.
If a response has multiple text options, one of these options will be chosen at random whenever that response is predicted.

# domain.yml
# ... previous content ...
responses:
  utter_greet:
    - text: Hey! How are you?
    - text: Hi, how's it going?
  utter_happy:
    - text: Great, carry on!
Enter fullscreen mode Exit fullscreen mode

You can learn more about responses in the documentation.

Stories

Stories are example conversations that train an assistant to respond correctly depending on what the user has said previously in the conversation. The story format shows the intent of the user message followed by the assistant's action or response.
Let's add our first story:

# data/stories.yml
version: "2.0"

stories:
  - story: happy path
    steps:
      - intent: greet
      - action: utter_greet
      - intent: mood_great
      - action: utter_happy
Enter fullscreen mode Exit fullscreen mode

The name of the story, in this case happy path is just for readability and debugging purposes.
In this story the user and the assistant exchange greetings, the user says they feel happy and the assistant responds that it's great.

You can learn more about stories in the documentation.

Chatting with the assistant

Now when we've specified what the assistant should do, we will train it by running rasa train.
Once the training is done, run rasa shell.
You can talk to your assistant now.
Chat session
Great! That went well.

Testing our assistant

To make sure the assistant behaves as expected, we will write a test for it.

# tests/test_stories.yml
version: "2.0"

stories:
  - story: happy path 1
    steps:
      - user: |
          hi
        intent: greet
      - action: utter_greet
      - user: |
          good
        intent: mood_great
      - action: utter_happy
Enter fullscreen mode Exit fullscreen mode

In this test we are saying that when a user types hi, it should be classified as intent greet and followed by an action utter_greet.
Then, when the user answers good, it should be classified as intent mood_great and followed by an action utter_happy.
Run this test by executing rasa test.
You should see the following results:
Test results

You can learn more about testing the assistant in the documentation.

In the next chapter, we will look at forms.

Repository for this tutorial:

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

git clone --branch 02-creating-chatbot git@github.com:petr7555/rasa-dev-tutorial.git
Enter fullscreen mode Exit fullscreen mode

Oldest comments (0)