Create your chatbot using Rasa and deploy it on AWS

Vishnu Sivan
Coinmonks
Published in
14 min readAug 8, 2022

--

Chatbots have become a critical part of customer service automation as it helps customers to get quick responses to their queries. After the covid outbreak, 80% of marketers plan to start using a chatbot to improve customer support. According to Forbes, the chatbot market is forecasted to reach $1.25 billion by 2025.

A chatbot is a computer program that simulates human conversation through text or voice interactions. It brings cost savings and boosts operational efficiency of the business as they can reduce the need for human interaction by automating FAQs and quick replies.

Rasa is an open-sourced framework to build custom AI chatbots using Python and NLU. Chatbots built using Rasa can be deployed on multiple social platforms like Whatsapp, Facebook, slack etc, websites etc…

In this section, we will learn to create a custom chatbot using Rasa and deploy it on AWS VM.

Getting Started

Table of contents

Part A: Rasa chatbot creation and integration to the frontend

Prerequisites

Download Python 3.8.4 from the official site and install it in your machine. Note that, you have selected the Add to path and Install for all users option while installing it.

Rasa Installation

This article is focused on creating rasa chatbot on windows operating system. Rasa has a well defined documentation if you are using other operating systems then please refer the following link.

  • Open command prompt in the preferred directory and create a virtual environment to isolate your python project
python3 -m venv ./venv
  • Activate the virtual environment
venv\Scripts\activate
  • Upgrade the pip package before installing Rasa using the following command
pip install --upgrade pip
or
python -m pip install --upgrade pip
  • Install rasa using the following command
pip install rasa

Create first Rasa project

Create yor first chatbot using rasa by creating a rasa project. You can do that by running rasa init command in the terminal. Select your preferences on the questions that asked on the initial step of rasa project creation. After model training, you get a question that ‘Do you want to speak to the assistant on the command line’. If you say yes then you can interact to your first chatbot in the command line.

rasa init

Understand the project structure

The rasa init command creates necessary project files that Rasa needs and trains a simple bot on sample data.

  • __init__.py — Empty file that helps to find the actions
  • actions.py — Used to write your custom actions. You can define custom actions to call external server via REST API. Also, we can create multiple scripts for implementing custom actions.
  • config.yml — Contains configuration of your NLU and Core models. It mainly used to define language, pipelines and policies of the chatbot.
  • credentials.yml — Contains informations for connecting to other services. You can use this file to connect with various social media platforms such as Whatsapp, Facebook Messenger etc… as well as integrate the bot with websites.
  • data/nlu.yml— Contains your NLU training data and defines the user response. You can define your chatbot Intents here. For example: Greeting, Order items, Show maps etc. For that, you have to add related sentences on the nlu file.
  • data/stories.md — Contains your stories which are required for Rasa Core to set the flow of the conversation between you and the chatbot. It is an essential part to make your chatbot perfect on various paths.
  • domain.yml — Contains different Intents which chatbot can detect and list of Bot replies.
  • endpoints.yml — Contains the different endpoints your bot can use like FB messenger. It is useful for production.
  • models/<timestamp><random-name>.tar.gz — Your initial model

Customize your chatbot

You will get a perfectly running basic chatbot after executing the rasa init command. The file structure and the basic intent format can be understoos from the documentation provided by the rasa team. Therefore, by referring these documentation you can easily customize your chatbot.

It is very important to put the right version of Rasa used for proper operation and customization of the bot without which the bot can’t be trained. For example, for rasa version 2.7.1, the version is 2.0. Similarly, for rasa version 3.2.5, the version is 3.1. This version should be specified in nlu.yml, stories.yml, rules.yml and domain.yml.

  • Let’s begin with adding a new intent into the nlu.yml file. Add the following code to the file for querying the bot name. We are using the yml format. So, ensure that the tab spacing is correct. Otherwise, it will throw some errors while training the model.
- intent: bot_name
examples: |
- what is your name?
- what are you called?
- how can i address you?
  • Add the bot response in the domain.yml file. Here we are going to add a new utter named utter_bot_name and a text response as “Enthiran 3.0”.
utter_bot_name:
- text: "Enthiran 3.0"
  • Define a new set of rules for mapping the intent with the bot response in rules.yml file. Here, we are going to map the intent bot_name with the bot action utter_bot_name. For that, add the following code to the rules.yml file,
- rule: Say 'I am a bot' anytime the user challenges
steps:
- intent: bot_name
- action: utter_bot_name
  • Add a new path in the stories.yml file to create a flow for your custom chatbot query and the bot response.
- story: path 4
steps:
- intent: bot_name
- action: utter_bot_name

We have added all the information to the specified files for creating a custom intent to ask the bot name and get a response like “Enthiran 3.0”. We need to train the model again to map the newly added intent to the model. Execute anyone of the following command to train the model again.

rasa train --config config.yml --domain domain.yml --data data/

Now we can tryout our modified chatbot on the terminal to verify the changes by executing the following command.

rasa shell

Hurray! we have created our first customized chatbot.

Rasa visualize

We can visualize stories in a graph using the rasa visualize command. It is very helpful to generate a visual representation of your stories. You can locate the results in the graph.html file located under the root folder after the rasa visualize command execution.

rasa visualize

Rasa test

Rasa is enabled with a test option to test a trained Rasa model. You can test the model using the following command,

rasa test

For more on rasa commands: Rasa cheat sheet

Frontend integration

Running the chatbot on the terminal does not look appealing and is less interactive. We can integrate our chatbot on a frontend site to make the interaction better. The Rasa team has provided the basic steps to integrate with the website. Refer the following link to know more on how to integrate a chatbot on the website.

  • You must enable the socket.io credentials in the credentials.yml file to create a channel between the rasa backend and the frontend plugin. For that, just uncomment the socket.io credentials and make the changes as mentioned below,
socketio:
user_message_evt: user_uttered
bot_message_evt: bot_uttered
session_persistence: true
  • Create a html file named index.html in the root folder and add the following content to it.
<!DOCTYPE html>
<html>
<body>
<div
id="rasa-chat-widget"
data-websocket-url="http://localhost:5005/"
data-height=475>
</div>
<script src="https://unpkg.com/@rasahq/rasa-chat" type="application/javascript"></script>
</body>
</html>

Don’t forget to change the data-websocket-url with the public dns address if you are trying to host it in AWS (data-websocket-url="http://54.183.218.201:5005/").

  • Run the rasa backend using the following command
rasa run -m models --enable-api --cors "*"
rasa run -m models --enable-api --cors "*" --debug
  • Load the index.html file on your browser after the “Rasa server is up and running” message in the terminal.

We have integrated our chatbot in the frontend side (website) as well.

Version control

It is advised to apply any version control on your code to track the changes on it. It enables us to roll back our codes, track changes, manage versions and all. Github is one of the best version control tools available to do that.

Create a respository on GitHub and follow the steps to push your codes to a remote repository.

Open a terminal on the project folder and execute the following commands to push your code to the remote repository.

git init
git add .
git commit -m "initial commit"
git remote add origin <origin url>
git push origin master

You can check the GitHub page to get the origin url.

Part B: Deploy the chatbot on AWS EC2

So far, we have learned to create a custom chatbot using rasa. Now, we can try to deploy the basic chatbot we have created on a AWS EC2 instance.

VM instance creation

Create a AWS account if you don’t have one yet.

  • As a first step, sign in to the AWS account and select the EC2 instance dashboard form the search window.
  • Click the Launch Instances button to get the instance creation window. Provide an instance name in the name and tags field.
  • Select the Ubuntu Server 20.04 LTS (HVM), SSD Volume Type as AMI.
  • Select the t2.micro as Instance type.
  • Create a key pair to access the VM via SSH. Ensure that you have kept the key file in a secure folder and don’t loose it as it is the only way to authenticate the VM access. Click on the Create new key pair and provide a key pair name, key pair type and key file format (keep it as pem) in the popup window.
  • Check Allow SHH traffic from Anywhere and Allow HTTP traffic from the internet in the network settings. Check the HTTP traffic checkbox else it won’t allow access to the http port (80) on your VM.
  • Provide the storage size as 16 (default is 8) because we need more than 8 GB for installing the rasa dependencies. After that, click the Launch Instance button to launch the VM.
  • Click the instance id to get the basic information about the VM.

Connect to the VM via SHH

  • Click the Connect button to get the connection related information.
  • Select the SHH tab and copy the command provided below to connect to the VM via SHH on your terminal.
  • Before connecting to the VM via SHH, we have to restrict the key file access. If you are using a ubuntu machine then execute chmod 400 key-file-name.pem . If you are using a windows machine then try the following steps to restrict the pem file access.
  • Select the pem file and open the properties window. Choose the security tab and click on the Advanced button. Click on the Disable inheritance button and select Remove all inherited permissions from this object option.
  • Now, all users are removed from the list. Click the Add button to get a popup window for adding the user information. Add the current user by clicking on the Select a principal text. Type the username in the given input list and press the Check Names button.
  • All done. We can connect the VM using the pem file via SHH. Open the terminal in the folder where the pem file is located and execute the following command (provided as an example in the SSH tab).
ssh -i "vbot.pem" ubuntu@ec2-54-193-138-236.us-west-1.compute.amazonaws.com

Install dependencies

Prerequisites

  • Python3.7 or 3.8 (preferred python 3.8.10)
  • pip3 22 or higher version
  • venv
  • tensorflow 2.7.0
  • nginx

Before installing the dependencies, update your VM using the following command,

sudo apt-get update

Install pip3 to the VM

sudo apt-get install python3-pip

Install python virtual environment to the VM

sudo apt-get install python3.8-venv

Install nginx to host your frontend using the following command,

sudo apt install nginx

Rasa installation

The rasa installation in EC2 instance is a bit different than the normal installation. AWS won’t allow us to install tensorflow directly in the VM. So, we have to install it seperately before installing rasa in the VM.

  • Create a virtual environment on the desired folder by executing the following command on the terminal.
python3 -m venv ./venv
  • Activate the virtual environment
source ./venv/bin/activate
  • Upgrade pip package because rasa requires updated pip
pip3 install -U pip
  • Install tensorflow in the VM
pip3 install tensorflow==2.7.0 --no-cache-dir
  • Install rasa using the following command and verify the version
pip3 install rasa
rasa --version

Clone the code

We have installed the required dependencies and the rasa framework in the VM. Now, we can clone the code that you have pushed to the git.

git clone <repo-url>
  • Navigate to the cloned project folder. Sometimes the model needs to be retrained. Delete the previous models from the models folder and then retrain the latest model.
  • Test the chatbot on the terminal using the following command
rasa shell

Frontend deployment

We have to make some changes on the firewall and the VM network inbound rules to deploy our frontend site in the VM.

  • Click the security group id listed under the security tab in the instance dashboard.
  • Add a new inbound rule to allow the socket.io access. Click the Add rule button. Set type to Custom HTTP, port to 5005 and source to Anywhere.
  • Enable access to the ufw firewall services. Execute the following command to allow nginx, SHH, tcp access in the VM.
sudo ufw allow ssh
sudo ufw allow 'Nginx Full'
sudo ufw enable
sudo ufw status numbered
  • It is necessary to enable the 5005 port access to communicate with the rasa backend via socket.io from the frontend site. Execute the following code to allow 5005 port access in ufw.
sudo ufw allow 5005/tcp
  • You can move the frontend files to the default nginx folder and backup the default index file available in the directory. Execute the following commands to do that.
cd /var/www/html/
sudo mv index.nginx-debian.html index.html.bckp
cd ~/vbot-the-rasa-chatbot/
sudo cp index.html /var/www/html/index.html
sudo cp bg.png /var/www/html/bg.png
  • Restart the nginx server to reflect the file changes
sudo systemctl restart nginx
sudo systemctl status nginx
  • Finally, run the rasa backend using the nohup package. nohup enables to run the command even if the terminal is closed.
nohup rasa run -m models --enable-api --cors "*" &

You can view the site on the browser by opening the public IPv4 address. You can get the public IPv4 address from the dashboard. Note that, if you are not using any SSL certificate then manually provide http:// as the prefix of the URL (http://54.183.218.201/).

Output

Don’t forget to change the data-websocket-url with the public dns address if you are trying to host it in AWS (data-websocket-url="http://54.183.218.201:5005/").

If you are using AWS free account then the VM access might expire after 3 months. You might not able to access the chatbot after that period.

Thanks for reading this article.

Thanks Gowri M Bhatt for reviewing the content.

If you enjoyed this article, please click on the clap button 👏 and share to help others find it!

The article is also available on Dev.

The full source code for this tutorial can be found here,

Join Coinmonks Telegram Channel and Youtube Channel learn about crypto trading and investing

Also, Read

Vishnu Sivan
Coinmonks

Try not to become a man of SUCCESS but rather try to become a man of VALUE