Introduction to Chatbots With Rasa & Python

Not Necessary to Know Machine Learning, Just Focus on Practical Side

Baysan
CodeX

--

Hi. Last weekend, I was hanging around on Coursera to find good quality content to consume. And I came across with “Create Your First Chatbot with Rasa and Python” guided project. Of course, I consumed it and took some notes. In this story, we will try to understand what Rasa is and how to use Rasa. We will be creating a chatbot to find out the time zone of the place we are targeting. Actually, you can find a similar project in the Coursera guided project. But, we will create the project which I extended a little bit. The bot we will create, get inputs, and show the timezone of the input region.

Photo by Yuyeung Lau on Unsplash

What is Rasa?

We can say Rasa is a framework to create conversational applications. We can create our own Virtual Assistant by using Rasa.

First Steps in Rasa

If we want to use Rasa, firstly we need to install it on our locals. You can install it by the code below.

pip install rasa

Now we can create a project template by using Rasa CLI.

rasa init
Image by Author

I said “Yes” for the question the CLI asked me “do you want to train an initial model?”. We will get a base template with a trained model by Rasa.

I am going to the myfirstbot folder I typed as default working director in the CLI.

cd myfirstbot

We can run the chatbot and start a conversation by executing this command:

rasa shell
Image by Author

What is NLU?

NLU; Natural Language Understanding. We will set sentences (intents) to make the bot understand what we are typing and it will understand from the context we typed it and will generate a response by using our sentences.

You can get more information about NLU from Wikipedia by using this link.

Basic Rasa Terms

Intents

These are terms to be understood from the bot. We will set them in data/nlu.yml file. Also, we will write terms and sentences about the related intents. By using these examples, the bot will understand what we are typing about. For example, we see a default set intent below.

Image by Author

The intent is written automatically by Rasa. When the bot gets inputs like “hey”, “hello”, “hello there” etc. which are under the examples variable it will understand that we are greeting it.

Each intent has to be defined in the domain.yml file.

Image by Author

Responses

We can define responses in the domain.yml file. We see the default set responses automatically by Rasa below.

Image by Author

For an instance, when we want to have the bot say “Bye”, we need to use utter_goodbye variable.

Stories

We can define conversation flows in the stories.yml file.

Image by Author

We see the automatically added stories (flows) by Rasa above. I want to dive a little bit into this term.

  • We start to define a flow by — story.
  • Then, we define its steps under the steps.
  • We can set the aimed intents by intent.
  • We can set the action the bot will get through the aimed intent by action.

Pay attention here, we have to write these all parameters (intents, actions, responses) in the domains.yml file.

Rules

We can define general flows in rules.yml file. We see the default added rules by Rasa below. For example, when the bot got some input related to goodbye intent, always will take the utter_goodbye action.

Image by Author

Start to Improve Our First Bot

Now, we can improve the bot by using the information above. Actually, still we have to learn some basic terms about Rasa, but I want to go step by step. I’m going to remove all responses and intents other thangreet and goodbye. The files will be like below.

domain.yml
rules.yml
stories.yml
nlu.yml

As aforementioned, our bot will show the timezone of the input region. To simulate our scenario, I’m going to add 3 intents to the nlu.yml file.

nlu.yml

When the bot gets the sentences something like from example sections, will understand the intent of humans who interacted with the bot. And, I am going to add some responses to the domain.yml file. Also, pay attention here. We have to add the new intents we created above in this file.

domain.yml

Create a story to finish the scenario in the stories.yml file for now.

In this story we simulated this flow:

  • A human asks for the time zone
  • The bot asks about the time zone of which city
  • A human types about the region
  • The bot searches the time zone and shows it
  • A human says thanks
  • The bot welcomes the human
stories.yml

If you execute direct rasa shell code to start a conversation with the bot, you will interact with the old bot. We have to execute rasa train to update the bot after every update.

rasa train && rasa shell

Now we can continue to learn basic Rasa terms.

Continuing to Learn Basic Rasa Terms

Entities

We use entities if we want to extract data from the intents. We need to use these in the nlu.yml file like this: [VARIABLE Name](variable_name). And we need to define all entities in the domain.yml file.

entities:
- entitiy_one
- entity_two

Slots

We use the slots to store entities . Rasa stores the slots as key-value pairs. We have to define the slots in the domain.yml file. We can see an example of defining the slots below.

slots:
slot_one:
type: text
mappings:
- type: from_entity
entity: entity_one

We can use these values like this:

f"The time zone is {tz_slot} in {entity_one}"

Actions

We used just default actions to return strings from the bot until now. We can create custom actions. To create custom actions, we need to edit actions/actions.py file. We need to follow these steps to create a custom action:

  • Update both of these files: domain.yml and stories.yml
  • Create custom action in action.file file
  • Before starting the chatbot, we need to start the action server to create communication between the bot and actions.
  • rasa run actions starts the actions server

We can see an example action below. Actually, we will use this action in our project.

Image by Author

The name method helps us to register that action in the domain.yml file.The run method helps us to define the process to execute by using this action. We can get the stored entities by using tracker instance. We need to update domain.yml file like this:

actions:
- custom_action

And we need to do the last thing to activate the actions. I’m going to uncomment the action_endpoint variable in the endpoints.yml file. By doing this, the bot and actions can communicate.

endpoints.yml

Continuing to Improve Our Bot

I’m going to add an entity and a slot to the domain.yml file.

I created an entity called target_timezone. I stored the entity in the target_timezone slot.

I’m going to update our city_info intent.

nlu.yml

And then update the story. I added action_find_timezone.

stories.yml

Now I’ll create the action in the actions.py file.

actions.py

To finish the bot, I’ll uncomment the endpoint in the endpoints.yml file.

endpoints.yml

Now, we can try our chatbot. I’m going to update the bot model and re-run it.

rasa train && rasa shell

And, I’m going to execute the code below in a new terminal simultaneously.

rasa run actions

We can start the conversation with our chatbot ^^

Image by Author

Finally

Hopefully, you enjoyed reading it. I myself enjoyed writing and coding. We took over a step from the Rasa’s door. Now, we have the basic knowledge to start to improve ourselves on Rasa.

Also, you can get the code we coded above by the link below.

Kind regards.

--

--

Baysan
CodeX
Writer for

Lifelong learner & Developer. I use technology that helps me. mebaysan.com