Getting to know Rasa 3 – creating a simple greeting chatbot

Tram Ho

Rasa is an open source framework for building chatbots – using the Python programming language.

There are many introductions about Rasa already, so I won’t repeat it again. The main purpose of this series of articles is to share my practice with Rasa 3, hopefully it can help some of you who are trying to learn Rasa 3 like me to approach faster.

Note: At the time of writing this article, with the latest Rasa version 3.3.1, Rasa 3 can only be installed on Python version 3.7.x and 3.8.x only, older or newer can’t, so you guys Note where this version of Python avoids wasting time installing errors that do not know why.

Install and initialize Rasa 3

For more details, you can see the official Rasa guide for Windows, Ubuntu, and MacOS

Below I will summarize the installation steps on Windows.

Open a Terminal window, navigate to the folder you want to contain the source and perform the following steps:

Step 1: Create a virtual environment

I will create a virtual environment to install Rasa, the purpose is that when playing around and installing things that will not affect other projects, you can skip this step if you find it unnecessary.

I named the new venv as venvRasa, type the following command:

When this time in the source will be generated a directory venvRasa, type additional commands to point to the venv you want to use:

The result is that the comment command with the name venv in parentheses as shown in the image below is correct rasa3venv.png

Step 2: Install Rasa 3

Just type a single command like this:

By default, the latest version will be installed, if you want to specify the version, add the following version:

Wait a little while.

After the installation is complete, check the Rasa version with the command:

Result as shown below:

rasa_version.png

Step 3: Create a new Rasa project

Type command:

image.png

The question will appear: ?Please enter a path where the project will be created [default: current directory]

This step is that Rasa requires specifying the directory to create the Rasa project, if you want to create it in another directory, enter the path, the default is to create the current directory. Note that the created directory needs to be an empty directory because before initializing rasa, it will delete all the files in the directory.

I want to create in a subfolder named rasa3, so type “rasa3” and then press Enter to continue.

Because my rasa3 folder has not been created yet, Rasa asked me if I wanted to create a new folder? Press Y to confirm consent.

Wait for a while, you will see that Rasa has created the “rasa3” folder in your machine, the initialization step here is considered complete.

Next Rasa will ask: ?Do you want to train an initial model?

I choose Y to let Rasa train the model with default data, so that after initialization, the chatbot is available for testing.

(If you find it unnecessary, press “n” and go to the “Create a super basic greeting chatbot in Vietnamese” section.)

When the training is over, Rasa will ask: ? Do you want to speak to the trained assistant on the command line?

I press Y to continue.

Wait for a while, the command line “Your input ->”, please test the greetings in English to see how Rasa responds. image.png

To exit the test space, type /stop and press Enter.

Create a super simple basic greeting chatbot in Vietnamese

The purpose of this section is to get used to using rasa train chatbot because by default Rasa already has trained hello and goodbye scripts for chatbots, but in English, now I will customize it so that Rasa can Hello in Vietnamese. The newly created Rasa 3 source structure will look like the image below:

rasa_truct.png

In the immediate future, to serve the request to create a basic greeting chatbot, we are only interested in the files in the data directory and the domain.yml file, the other files in the more in-depth articles we will learn later. .

Step 1: Define the intents in the file /data/nlu.yml

The file /data/nlu.yml will contain sample examples to teach the bot to recognize the intent of the chat person. For example, if you want to teach a bot: When a user chats with “goodbye” or “good night”, the intention is to say goodbye to end the chat, then declare the following:

The more examples you teach the bot, the smarter the bot will be.

By default, there are a few intents in the nlu file, I don’t need to use it, so I will delete all of them, leaving only 2 intents: greet and goodbye.

The name of the intent you want is fine (as long as it follows the standard of variable naming in python), I keep it as greet and goodbye.

Insert additional sentence patterns corresponding to each intent as follows:

Step 2: Define bot responses in domain.yml

The domain.yml file has the effect of declaring objects and configuring the chatbot to use.

To serve the super simple bot in this article, I’m only interested in 2 declarations of intents and responses.

instents is the place to declare the instents you have defined in the file /data/nlu.yml. With 2 intents, greet and goodbye, I declare as follows:

responses is where the bot’s dialogues are declared. To teach the chat bot to say goodbye, we define the following:

In the declaration above, utter_goodbye is the name of the speech that I teach the bot, you can put it any way you want (as long as it follows the standard of variable naming in python).

The text part is the verbatim speech you want the bot to respond to. In order for the bot to have many different answers, you declare many different texts, the bot will randomly speak in the list of sentences you define.

Rasa has support to teach bots to say more flexible sentences, but this article I just stop at answering in a specific sentence pattern.

In summary, I will have the content of the domain.yml file as follows:

Step 3: Specify the behavior of the bot in the file /data/rules.yml

The file /data/rules.yml is used to teach bots how to react to chat messages.

As I want to teach the bot: when I determine that the user intends to greet with the intent of greet, then reply with the response of utter_greet, then I enter the information in the file /data/rules.yml as follows:

Same with intent goodbye. Finally, I have the content of the file /data/rules.yml as follows:

In addition to specifying behavior in the Rasa rule, it also supports teaching the bot to behave flexibly and seem smarter by defining behavioral contexts in the file /data/stories.yml, which I will demonstrate in another article. more complex requirements.

There is also a place to configure the language in the config.yml file, but I don’t think it’s necessary in this post, so I’ll skip it.

Now that the definition is done, the next step is to ask Rasa to teach the chatbot the definitions I entered and see the results.

Step 4: Run the command train and test the results

In the Terminal window, go to the directory containing the source rasa and run the following command:

The training process takes a while depending on your computer configuration. After the training is complete, run the following command to test the results:

The result will be as follows:

image.png

The above are the 4 simplest steps to teach the Rasa chatbot to recognize the intent of the conversationalist and respond according to the sample text.

Good luck!

If any of you follow this article and encounter errors or have any suggestions, please comment below.

References:

https://rasa.com/docs/rasa/

Share the news now

Source : Viblo