Ramping up a Rasa-based chat-bot

Shahar Gino
Voice Tech Podcast
Published in
7 min readJul 8, 2020

--

RASA is one of the leading chat-bot solutions these days, mainly due to its cutting-edge engines (AI wise) and due to its emerging community.

A technical Quick-Start for applying Rasa Chat-Bot

A lot has already been written about RASA, either in Rasa official documents, in the various communities or in technical blogs, such as this one is. However, We still found it a bit tricky to ramp-up, since sometime it’s just hard to see the forest for all the trees. The purpose of this blog is therefore to act as a technical quick-start guide, from zero to a kicking chat-bot outcome.

We’ll be actually focusing on RASA-X, which is a is a browser-based GUI tool that allows training machine-learning model by using GUI based interactive mode. It’s an optional tool in RASA Software Stack, and it aims to learn from real conversations and improve the bot.

Goals

  1. Brief RASA summary
  2. Local ramp-up
  3. Server ramp-up, deployed over EC2 (Amazon) with Docker

Let’s start,

Brief RASA summary

  • Schematic illustration for RASA flow:
RASA 1.0 merges NLU and CORE into a single product (previously were separated products)
  • For every conversation that the user does with the bot, RASA NLU does intent classification and entity extraction, and RASA Core is responsible for what Bot will “utter” to the user:
RASA Flow — Illustration
  • A story is a representation of a conversation between a user and an AI assistant, converted into a specific format where user inputs are
    expressed as corresponding intents (and entities where necessary) while the responses of an assistant are expressed as corresponding action names.
  • Stories can be spread across multiple files and specify the folder containing the files for most of the scripts (e.g. training, visualization).
    The stories will be treated as if they would have been part of one large file.
  • RASA supports multi-languages

Local Ramp-Up

At the end of this section, you’ll be able to interact with your chat-bot locally, i.e. from your localhost machine.

Environment Ramp-Up

Start with installing Conda or MiniConda (https://docs.conda.io/en/latest/miniconda.html)

% conda install python=3.6
% conda create -n rasa python=3.6
% source activate rasa
% pip install rasa-x — extra-index-url https://pypi.rasa.com/simple

Create a new Project

rasa init --no-prompt

Do you magic (adjust the bot)

Important Rasa files

Training

Shall be called for updating (retraining) the NLU and the Core Model

The updated model will be stored in models/ directory

% rasa train

Launching Rasa-X locally

When running Rasa-X locally, all training data and stories are read from the files in the project (e.g. data/nlu.md), and any changes
in the UI are saved back to those files. Conversations and other data are stored in an SQLite database saved in a file called rasa.db.

% rasa x

Then open http://localhost:5002/talk

Custom Actions (optional)

Custom-Actions is a powerful feature of Rasa, which allows the developers to orthogonally design complex response paths of the bot in a friendly pythonic manner. The Custom-Actions are actually run on a different server and communicates with Rasa-X through a webhook.

Important files in that context are:

  • endpoints.yml → enable (uncomment) action_endpoint sectio
  • actions.py → implement the custom action class
  • nlu.md, stories.md, domain.yml → register the new intent and corresponding action

Launch the Custom-Actions server locally with:

% rasa run actions --actions actions

Then, launch Rasa-X with:

% rasa x --endpoints endpoints.yml

I’ve created a simple (public) project on my gitHub at: https://github.com/sgino209/rasabot_app.git

Build better voice apps. Get more articles & interviews from voice technology experts at voicetechpodcast.com

Server ramp-up, deployed over EC2 with Docker

At the end of this section, you’ll be able to interact with your chat-bot over the web, share it with others over a public URL.

Environment Ramp-Up

Start with creating an EC2 machine in Amazon. It’s recommended to pick a relatively strong machine with at least 8GB RAM and 100 GiB storage. We have picked a t2.xlarge machine with Ubuntu 18.04 LTS on it.

We assume you a DockerHub account. You can register a new account for free.

Log to your new EC2 machine over SSH and follow the below steps:

% curl -sSL -o install.sh https://storage.googleapis.com/rasa-x-releases/latest/install.sh% sudo bash ./install.sh% cd /etc/rasa% sudo docker-compose up -d% sudo python rasa_x_commands.py create --update admin me <PASSWORD>% wget -qO docker-compose.yml https://storage.googleapis.com/rasa-x-releases/latest/docker-compose.ce.yml
% wget -qO rasa_x_commands.py
https://storage.googleapis.com/rasa-x-releases/latest/rasa_x_commands.py
% sudo touch Dockerfile

Note: in case of a “command not found” error with the “sudo python” step, either of the two following workarounds may be applied:

% sudo python3 rasa_x_commands.py create --update admin me <PASSWORD>

OR

sudo env "PATH=$PATH" python rasa_x_commands.py create --update admin me <PASSWORD>

Populate the Dockerfile with the below content:

# Extend the official Rasa SDK image
FROM rasa/rasa-sdk:latest
# Use subdirectory as working directory
WORKDIR /app
# Copy any additional custom requirements, if necessary (uncomment next line)
# COPY actions/requirements-actions.txt ./
# Change back to root user to install dependencies
USER root
# Install extra requirements for actions code, if necessary (uncomment next line)
# RUN pip install -r requirements-actions.txt
# Copy actions folder to working directory
COPY ./actions /app/actions
# By best practices, don't run the code with root user
USER 1001

Custom Actions (Optional)

In that context, use the very same actions.py file as in Local mode:

% sudo mkdir actions
% cp actions.py actions/actions.py
% sudo touch actions/__init__.py

If you have special requirements (.txt), then populate then at:

actions/requirements-actions.txt

Prepare a docker override yml:

% sudo touch docker-compose.override.yml

Populate this docker override file with:

version: '3.4'
services:
app:
image: <username>/<repository_name>:<image_tag>

Prepare Docker Image

Build the Docker image and push it to your DockerHub account:

% sudo docker login% sudo docker build . -t <username>/<repository_name>:<image_tag>% sudo docker push <username>/<repository_name>:<image_tag>

Docker argument example: sgino209/rasabot_app:newt_actions

Start RASA-X

Custom-Actions will start inherently, if contained in the Docker image

% sudo docker-compose up -d

Notes

RASA can be taken off with the following shut-down command:

% sudo docker-compose down --remove-orphans

The following command glimpses into the docker log (useful for debug):

% sudo docker-compose logs --tail=100
% sudo docker-compose logs app
% sudo docker-compose logs rasa-x

Note: it’s important to keep an eye on Rasa compatibility matrix, when defining version in .env and in Dockerfile files.

RASA-X version update (EC2 with Docker)

First, SSH to the EC2 machine, then apply the following steps:

  1. Backup our old RASA in /etc/rasa.old:
% cd ~ 
% sudo mv /etc/rasa /etc/rasa.old

2. Fresh Start, e.g. updating to version 0.34.0:

% curl -sSL -o install.sh https://storage.googleapis.com/rasa-x-releases/0.34.0/install.sh
% sudo bash
% ./install.sh

3. Restore custom settings from our previous revision:

% sudo cp -r /etc/rasa.old/actions ./
% sudo cp /etc/rasa.old/Dockerfile ./
% sudo cp /etc/rasa.old/docker-compose* ./
% sudo cp /etc/rasa.old/credentials.yml ./
% sudo cp /etc/rasa.old/rasa_x_commands.py ./

4. Set admin password:

% sudo python rasa_x_commands.py create admin me
% sudo python rasa_x_commands.py create --update admin me q1w2e3r4

5. Build and Push docker images:

% sudo docker build . -t <username>/<repository_name>:<image_tag>
% sudo docker push <username>/<repository_name>:<image_tag>

Launch:

sudo docker-compose up -d

Following is a useful script which wraps up all of the above steps, assuming your previous version is available at /etc/rasa.old

#!/bin/bash
rasa_ver=${1:-latest}
rasa_docker=${2:-sgino209/rasabot_app:newt_actions}
# RASA Compatability Matrix
declare -A rasa_comp_matrix=(
["latest"]="2.1.2" \
["0.34.0"]="2.1.2" \
["0.33.0"]="2.0.2" \
["0.32.2"]="1.10.2" \
["0.31.5"]="1.10.2" \
["0.30.1"]="1.10.2" \
["0.29.3"]="1.10.2" \
["0.28.6"]="1.10.1" \
["0.27.8"]="1.9.0" \
["0.26.4"]="1.8.1" \
["0.25.3"]="1.7.0" \
["0.24.8"]="1.6.1" \
["0.23.6"]="1.5.2" \
["0.22.2"]="1.4.0" \
["0.21.5"]="1.3.3" \
["0.20.5"]="1.2.0"
)
rasa_sdk_ver="${rasa_comp_matrix[${rasa_ver}]}"
# Start fresh
cd /etc/rasa
sudo docker-compose down --remove-orphans
sudo docker system prune -a
cd -
sudo rm -rf /etc/rasa
# Install RASA-X
curl -sSL -o install.sh https://storage.googleapis.com/rasa-x-releases/${rasa_ver}/install.sh
sudo bash ./install.sh
cd /etc/rasa
# Import legacy files, if exists:
if [ -d "/etc/rasa.old" ]; then
sudo cp -r /etc/rasa.old/actions ./
sudo cp /etc/rasa.old/Dockerfile ./
sudo cp /etc/rasa.old/docker-compose.override.yml ./
sudo cp /etc/rasa.old/credentials.yml ./
sudo cat /etc/rasa.old/Dockerfile | sed "s/:latest/:${rasa_sdk_ver}/g" > ./Dockerfile
fi
# Create/Update a new user:
sudo python3 rasa_x_commands.py create admin me q1w2e3r4
sudo python3 rasa_x_commands.py create --update admin me q1w2e3r4
sudo python3 rasa_x_commands.py create --update admin me q1w2e3r4
# Prepare docker images:
sudo docker build . -t ${rasa_docker}
sudo docker push ${rasa_docker}
# Start RASA-X
sudo docker-compose up -d
# Display logs
sudo docker-compose logs
sudo docker-compose logs app | tail -100
echo "Done!"

Usage example (assuming script name is rasa_install.sh):

./rasa_install.sh 0.34.0 sgino209/rasabot_app:newt_actions2

Login to RASA-X url, and do the following (import from old RASA version):

  1. Upload NLU data
  2. Upload Story data
  3. Upload Domain data
  4. Update (copy/paste) Configurations
  5. Train!

Important: if you’ve upgraded from version 1.x to 2.x, you’ll have to follow Rasa Version Migration Guide, as the interface and formats have been changes, and not considered as backward-compatible (!)

Something just for you

--

--