PolarSPARC

Hands-on Primer on Rasa - Part 1


Bhaskar S 11/08/2024


Overview

In order to drive operational efficiencies as well as engage customers, the modern Enterprises are increasingly deploying and making use of intelligent Chatbot assistants (or agents) that have human-like conversations with the customers using natural language processing and machine learning techniques.

Rasa is a popular, open source framework that enables one to build custom, task oriented, chatbot assistant system, which facilitates a two-way conversation between a human user and a system, and over a period of time the chatbot system learns from the interactions.

The Rasa platform consits of the following two important sub-systems:

In the first part of this primer, we will demonstrate how one can effectively setup and run the Rasa platform using a Docker image.


Installation and Setup

The installation and setup will be on a Ubuntu 22.04 LTS based Linux desktop. Ensure that Docker is installed and setup on the desktop (see instructions).

Also, ensure that the Python 3.x programming language is installed. In addition, ensure the cli utilities curl and jq are also installed.

We will setup the required directory structure by executing the following command in a terminal window:


$ mkdir -p $HOME/.rasa/actions

To pull and download the current version of the docker image for Rasa (3.10.8 at the time of this article), execute the following command in a terminal window:


$ docker pull rasa/rasa-pro:3.10.8

The following should be the typical output:


Output.1

3.10.8: Pulling from rasa/rasa-pro
7646c8da3324: Pull complete 
0de6c9c5bbc0: Pull complete 
4f4fb700ef54: Pull complete 
f43a6fa70b0a: Pull complete 
7e962051ba34: Pull complete 
8bbc3839fc25: Pull complete 
Digest: sha256:607c1c12365b2086d657544d7bf974334d07b982dda3fd99487c64fe83e09b57
Status: Downloaded newer image for rasa/rasa-pro:3.10.8
docker.io/rasa/rasa-pro:3.10.8

This completes all the system installation and setup for the Rasa hands-on demonstration.


Hands-on with Rasa

Before one can get started with the hands-on experiments, one should request a free Rasa Pro developer License Key and assign the key to the environment variable RASA_PRO_LICENSE.

To test the Rasa platform, execute the following command in the terminal window:


$ docker run --rm --name rasa-pro -e RASA_PRO_LICENSE=${RASA_PRO_LICENSE} rasa/rasa-pro:3.10.8 --help


The following should be the typical output:


Output.2

usage: rasa [-h] [--version]
        {init,run,shell,train,interactive,telemetry,test,visualize,data,export,x,evaluate,llm,studio,license,markers,inspect}
        ...

Rasa command line interface. Rasa allows you to build your own conversational
assistants. The 'rasa' command allows you to easily run most common commands
like creating a new bot, training or evaluating models.

positional arguments:
{init,run,shell,train,interactive,telemetry,test,visualize,data,export,x,evaluate,llm,studio,license,markers,inspect}
                    Rasa commands
init                Creates a new project, with example training data,
                    actions, and config files.
run                 Starts a Rasa server with your trained model.
shell               Loads your trained model and lets you talk to your
                    assistant on the command line.
train               Trains a Rasa model using your NLU data and stories.
interactive         Starts an interactive learning session to create new
                    training data for a Rasa model by chatting.
telemetry           Configuration of Rasa Pro telemetry reporting.
test                Tests Rasa models using your test NLU data and
                    stories.
visualize           Visualize stories.
data                Utils for the Rasa training files.
export              Export conversations using an event broker.
x                   Run a Rasa server in a mode that enables connecting to
                    Rasa Enterprise as the config endpoint.
evaluate            Tools for evaluating models.
llm                 Commands related to LLMs.
studio              Rasa Studio commands.
license             Displays licensing information.
markers             Rasa Studio commands.
inspect             Loads your trained model and lets you talk to your
                    assistant in the browser.

In order to use the Rasa chatbot platform, one needs to first initialize a new project.

To initialize and setup a new Rasa chatbot instance, execute the following command in the terminal window:


$ docker run --rm --name rasa-pro -u $(id -u $USER):$(id -g $USER) -v $HOME/.rasa/:/app -e RASA_PRO_LICENSE=${RASA_PRO_LICENSE} rasa/rasa-pro:3.10.8 init --no-prompt


The following should be the typical trimmed output:


Output.3

2024-11-02 18:43:25 INFO     root  - copying /opt/venv/lib/python3.10/site-packages/rasa/cli/project_templates/default/domain.yml -> .
2024-11-02 18:43:25 INFO     root  - creating data
2024-11-02 18:43:25 INFO     root  - copying /opt/venv/lib/python3.10/site-packages/rasa/cli/project_templates/default/data/rules.yml -> ./data
2024-11-02 18:43:25 INFO     root  - copying /opt/venv/lib/python3.10/site-packages/rasa/cli/project_templates/default/data/nlu.yml -> ./data
2024-11-02 18:43:25 INFO     root  - copying /opt/venv/lib/python3.10/site-packages/rasa/cli/project_templates/default/data/stories.yml -> ./data
2024-11-02 18:43:25 INFO     root  - creating tests
2024-11-02 18:43:25 INFO     root  - copying /opt/venv/lib/python3.10/site-packages/rasa/cli/project_templates/default/tests/test_stories.yml -> ./tests
2024-11-02 18:43:25 INFO     root  - copying /opt/venv/lib/python3.10/site-packages/rasa/cli/project_templates/default/credentials.yml -> .
2024-11-02 18:43:25 INFO     root  - copying /opt/venv/lib/python3.10/site-packages/rasa/cli/project_templates/default/endpoints.yml -> .
2024-11-02 18:43:25 INFO     root  - copying /opt/venv/lib/python3.10/site-packages/rasa/cli/project_templates/default/config.yml -> .
2024-11-02 18:43:25 INFO     root  - copying /opt/venv/lib/python3.10/site-packages/rasa/cli/project_templates/default/actions/__init__.py -> ./actions
2024-11-02 18:43:25 INFO     root  - copying /opt/venv/lib/python3.10/site-packages/rasa/cli/project_templates/default/actions/actions.py -> ./actions
2024-11-02 18:43:25 INFO     root  - creating actions/__pycache__
2024-11-02 18:43:25 INFO     root  - copying /opt/venv/lib/python3.10/site-packages/rasa/cli/project_templates/default/actions/__pycache__/__init__.cpython-310.pyc -> ./actions/__pycache__
2024-11-02 18:43:25 INFO     root  - copying /opt/venv/lib/python3.10/site-packages/rasa/cli/project_templates/default/actions/__pycache__/actions.cpython-310.pyc -> ./actions/__pycache__
[ ... SNIP ... ]
Created project directory at '/app'.
Finished creating project structure.
Training an initial model...
The configuration for policies and pipeline was chosen automatically. It was written into the config file at 'config.yml'.
Epochs: 100% |||||||||| 100/100 [00:13<00:00,  7.39it/s, t_loss=1.14, i_acc=1]   
2024-11-02 18:43:44 INFO     rasa.engine.training.hooks  - Finished training component 'DIETClassifier'.
2024-11-02 18:43:44 INFO     rasa.engine.training.hooks  - Starting to train component 'EntitySynonymMapper'.
2024-11-02 18:43:44 INFO     rasa.engine.training.hooks  - Finished training component 'EntitySynonymMapper'.
2024-11-02 18:43:45 INFO     rasa.engine.training.hooks  - Starting to train component 'ResponseSelector'.
2024-11-02 18:43:45 INFO     rasa.nlu.selectors.response_selector  - Retrieval intent parameter was left to its default value. This response selector will be trained on training examples combining all retrieval intents.
2024-11-02 18:43:45 INFO     rasa.engine.training.hooks  - Finished training component 'ResponseSelector'.
Processed story blocks: 100% |||||||||| 3/3 [00:00<00:00, 3637.73it/s, # trackers=1]
Processed story blocks: 100% |||||||||| 3/3 [00:00<00:00, 1485.59it/s, # trackers=3]
Processed story blocks: 100% |||||||||| 3/3 [00:00<00:00, 350.85it/s, # trackers=12]
Processed story blocks: 100% |||||||||| 3/3 [00:00<00:00, 88.47it/s, # trackers=39]
Processed rules: 100% |||||||||| 2/2 [00:00<00:00, 5548.02it/s, # trackers=1]
2024-11-02 18:43:45 INFO     rasa.engine.training.hooks  - Starting to train component 'MemoizationPolicy'.
Processed trackers: 100% |||||||||| 3/3 [00:00<00:00, 3318.28it/s, # action=12]
Processed actions: 12it [00:00, 16666.11it/s, # examples=12]
2024-11-02 18:43:45 INFO     rasa.engine.training.hooks  - Finished training component 'MemoizationPolicy'.
2024-11-02 18:43:45 INFO     rasa.engine.training.hooks  - Starting to train component 'RulePolicy'.
Processed trackers: 100% |||||||||| 2/2 [00:00<00:00, 2789.69it/s, # action=5]
Processed actions: 5it [00:00, 26579.87it/s, # examples=4]
Processed trackers: 100% |||||||||| 3/3 [00:00<00:00, 3615.78it/s, # action=12]
Processed trackers: 100% |||||||||| 2/2 [00:00<00:00, 2888.64it/s]
Processed trackers: 100% |||||||||| 5/5 [00:00<00:00, 2022.33it/s]
2024-11-02 18:43:45 INFO     rasa.engine.training.hooks  - Finished training component 'RulePolicy'.
2024-11-02 18:43:45 INFO     rasa.engine.training.hooks  - Starting to train component 'TEDPolicy'.
Processed trackers: 100% |||||||||| 120/120 [00:00<00:00, 2776.05it/s, # action=30]
Epochs: 100%|||||||||| 100/100 [00:07<00:00, 13.36it/s, t_loss=2.29, loss=2.13, acc=0.967]
2024-11-02 18:43:53 INFO     rasa.engine.training.hooks  - Finished training component 'TEDPolicy'.
[ ... SNIP ... ]
2024-11-02 18:44:05 INFO     rasa.model_training  - {"event_info": "Your Rasa model is trained and saved at 'models/20241102-181642-avocado-bright.tar.gz'.", "event": "model_training.train.finished_training", "level": "info"}
If you want to speak to the assistant, run 'rasa shell' at any time inside the project directory.

Post the initialization, the default Rasa chatbot deployed is a very basic chatbot that identifies if the user is happy or unhappy and takes the appropriate actions.

To test the deployed chatbot, execute the following command in the terminal window:


$ docker run -it --rm --name rasa-pro -u $(id -u $USER):$(id -g $USER) -v $HOME/.rasa/:/app -e RASA_PRO_LICENSE=${RASA_PRO_LICENSE} rasa/rasa-pro:3.10.8 shell


The following should be the typical trimmed output:


Output.4

[ ... SNIP ... ]
2024-11-03 01:39:04 INFO     root  - Starting Rasa server on http://0.0.0.0:5005
2024-11-03 01:39:10 INFO     rasa.core.processor  - Loading model models/20241102-181642-avocado-bright.tar.gz...
[ ... SNIP ... ]
2024-11-03 01:39:30 INFO     root  - Rasa server is up and running.
Bot loaded. Type a message and press enter (use '/stop' to exit): 
Your input ->

Notice that the chatbot is waiting for user input on the prompt Your input ->.

Type Hello! at the chatbot prompt.

The following should be the typical output:


Output.5

Hey! How are you?

Next, type feel bad at the chatbot prompt.

The following should be the typical output:


Output.6

Here is something to cheer you up:
Image: https://i.imgur.com/nGF1K8f.jpg
Did that help you?


!!! ATTENTION !!!

Looks like the link for the above image is broken - should have been a picture of a cute baby tiger !!!

Next, type feel good at the chatbot prompt.

The following should be the typical output:


Output.7

Great, carry on!

Finally, type /stop at the chatbot prompt and the chatbot will exit.

We will now start the Rasa chatbot in the server mode and interact with the chatbot via APIs.

Assuming that the ip address of the desktop is 192.168.1.25, to start the Rasa chatbot server, execute the following command in the terminal window:


$ docker run -it --rm --name rasa-pro --network="host" -u $(id -u $USER):$(id -g $USER) -v $HOME/.rasa/:/app -e RASA_PRO_LICENSE=${RASA_PRO_LICENSE} rasa/rasa-pro:3.10.8 run --enable-api --cors “*” -i 192.168.1.25


The following should be the typical trimmed output:


Output.8

2024-11-03 01:51:53 INFO     rasa.tracing.config  - No endpoint for tracing type available in endpoints.yml,tracing will not be configured.
2024-11-03 01:51:55 INFO     root  - Starting Rasa server on http://192.168.1.25:5005
[ ... SNIP ... ]
2024-11-03 01:51:59 INFO     rasa.core.processor  - Loading model models/20241102-181642-avocado-bright.tar.gz...
[ ... SNIP ... ]
2024-11-03 01:52:20 INFO     root  - Rasa server is up and running.
[2024-11-03 01:52:20 +0000] [1] [INFO] Starting worker [1]
2024-11-03 01:52:20 INFO     sanic.server  - Starting worker [1]

To check the version of the Rasa chatbot instance, execute the following command in a terminal window:


$ curl -s http://192.168.1.25:5005/version | jq


The following should be the typical output:


Output.9

{
  "version": "3.10.8",
  "minimum_compatible_version": "3.10.0rc1"
}

To send the Hello message to the Rasa chatbot, execute the following command in a terminal window:


$ curl -s http://192.168.1.25:5005/webhooks/rest/webhook -X POST -d '{"sender": "user", "message": "hello"}' | jq


The following should be the typical output:


Output.10

[
  {
    "recipient_id": "user",
    "text": "Hey! How are you?"
  }
]

To send the feel bad message to the Rasa chatbot, execute the following command in a terminal window:


$ curl -s http://192.168.1.25:5005/webhooks/rest/webhook -X POST -d '{"sender": "user", "message": "feel bad"}' | jq


The following should be the typical output:


Output.11

[
  {
    "recipient_id": "user",
    "text": "Here is something to cheer you up:"
  },
  {
    "recipient_id": "user",
    "image": "https://i.imgur.com/nGF1K8f.jpg"
  },
  {
    "recipient_id": "user",
    "text": "Did that help you?"
  }
]

Finally, to send the feel good message to the Rasa chatbot, execute the following command in a terminal window:


$ curl -s http://192.168.1.25:5005/webhooks/rest/webhook -X POST -d '{"sender": "user", "message": "feel good"}' | jq


The following should be the typical output:


Output.12

[
  {
    "recipient_id": "user",
    "text": "Great, carry on!"
  }
]

WALLA - we have successfully tested the deployed basic Rasa chatbot !

Before we proceed any further, let us dive into some core concepts of the Rasa chatbot platform. The following are some of the core concepts to understand:

The following is the structure and contents of the directory $HOME/.rasa/ post initialization. Note that the directory was empty when we first started:


Directory Structure
Figure.1

We will now navigate the directory structure shown above and outline the purpose of most important files in the following section:

Given that we have covered the setup, testing, core concepts, and main platform files, we conclude Part-1 of this series !!!


References

Rasa Pro Documentation



© PolarSPARC