RASA - Creating your first chatbot
Tags: rasa, chatbot, nlp, ai
Published: 2021-03-04 19:14:38

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.
```yml
# 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
```
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](https://blog.rasa.com/introducing-dual-intent-and-entity-transformer-diet-state-of-the-art-performance-on-a-lightweight-architecture/) (which is an architecture that handles both intent classification and entity recognition), we will add another intent **mood_great**.
```yml
# 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
```
You can learn more about intents in the [documentation](https://rasa.com/docs/rasa/domain#intents).

## 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**.
```yml
# domain.yml
version: "2.0"

intents:
  - greet
  - mood_great
```

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.
```yml
# domain.yml
# ... previous content ...
responses:
  utter_greet:
    - text: Hey! How are you?
    - text: Hi, how's it going?
  utter_happy:
    - text: Great, carry on!
```
You can learn more about responses in the [documentation](https://rasa.com/docs/rasa/responses).

## 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:
```yml
# data/stories.yml
version: "2.0"

stories:
  - story: happy path
    steps:
      - intent: greet
      - action: utter_greet
      - intent: mood_great
      - action: utter_happy
```
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](https://rasa.com/docs/rasa/stories).

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w87x60vhcsmisrqj1euz.png)
Great! That went well.

## Testing our assistant
To make sure the assistant behaves as expected, we will write a test for it.
```yml
# 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
```
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fc9643wy1ep6u7c5gq2y.png)

You can learn more about testing the assistant in the [documentation](https://rasa.com/docs/rasa/testing-your-assistant).

In the next chapter, we will look at forms.

Repository for this tutorial:
{% github petr7555/rasa-dev-tutorial no-readme %}

You can checkout the state of the repository at the end of this tutorial by running:
```bash
git clone --branch 02-creating-chatbot git@github.com:petr7555/rasa-dev-tutorial.git
```