Deploying Rasa Powered Chatbot on Google Cloud Platform.

Muhammad Hassaan Rafique
7 min readSep 1, 2019

Chatbots are in high demand and they will continue to grow in the near future. According to Gartner report:

“By 2022, 70% of white-collar workers will interact with conversational platforms on a daily basis”

There are a lot of API’s available that you can use to build chatbots like DialogFlow by Google, Amazon Lex and other chat bot services by Facebook, Microsoft are available you only need to hook your website with these API’s. But when you are building a product which you are going to sell it becomes difficult to use these chatbot services due to some copy right issues. You cannot sell it as your own product and add to that the paid services. This is where Rasa comes in to play as it is an open-source frame work for building chatbots.

On Rasa Website they say:

“Rasa is an open source machine learning framework for automated text and voice-based conversations. Understand messages, hold conversations, and connect to messaging channels and APIs.”

It is very easy to train your own data on the rasa-chatbot itself and there documentation is very easy to follow so if you want to learn more about the framework you can visit the link

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

Rasa-Stack

So quick intro about Rasa we have something called Rasa-Stack which includes Rasa-Core and Rasa-NLU. Where Rasa-NLU is an open-source natural language processing tool for intent classification and entity extraction in chatbots. And Rasa-Core is a machine learning-based dialogue framework which helps you manage context in a conversation.

You can read more about Rasa-Core here: https://rasa.com/docs/rasa/core/about/

Let’s build our first basic chatbot using RASA then deploy on GCP using App-Engine and hook it up with facebook messenger.

Lets first start with a basic folder structure for our chatbot.

first_chat_bot/
--- data/
--- models/

--- __init__.py
--- actions.py
--- app.yaml
--- config.yaml
--- credentials.yaml
--- DockerFile
--- domain.yaml
--- endpoints.yaml
--- out.log

[/data] folder contains two files currently nlu.md and stories.md. nlu.md contains all of your intents in markdown format. While stories.md contains the actual conversations you will be having with your user.

[/models] contains the model which is going to be trained on nlu.md and stories.md.

We won’t be going into details in each and every file. For our current scenario we only require app.yaml, credentials.yaml and the DockerFile.

As we will be deploying our chatbot application on GCP APP-ENGINE lets go over what exactly is APP-ENGINE.

APP-ENGINE is a web-frame work which helps you deploy your applications without worrying about the scaling and load-balancing it lets developers focus on what they best do coding.

There are two environments from which you can select from:
Standard-Environment
Flexible-Environment

According to GCP Documentation:

Standard-Environment

The standard environment is optimal for applications with the following characteristics:

  • Source code is written in specific versions of the supported programming languages:
  • Python 2.7, Python 3.7
  • Java 8, Java 11 (beta)
  • Node.js 8, Node.js 10
  • PHP 5.5, PHP 7.2, and PHP 7.3 (beta)
  • Ruby 2.5 (beta)
  • Go 1.9, Go 1.11, and Go 1.12
  • Intended to run for free or at very low cost, where you pay only for what you need and when you need it. For example, your application can scale to 0 instances when there is no traffic.
  • Experiences sudden and extreme spikes of traffic which require immediate scaling.

Flexible-Environment

Application instances run within Docker containers on Compute Engine virtual machines (VM).

Applications that receive consistent traffic, experience regular traffic fluctuations, or meet the parameters for scaling up and down gradually.

The flexible environment is optimal for applications with the following characteristics:

  • Source code that is written in a version of any of the supported programming languages:
    Python, Java, Node.js, Go, Ruby, PHP, or .NET
  • Runs in a Docker container that includes a custom runtime or source code written in other programming languages.
  • Uses or depends on frameworks that include native code.
  • Accesses the resources or services of your Google Cloud Platform project that reside in the Compute Engine network.

We will be using Flexible environment for deploying our chatbot as we will be using Rasa base docker image to run our chatbot server.

So let’s get started:

First lets configure our credentials.yaml file for hooking up with Facebook messenger:

For Facebook messenger connection you can follow the tutorial here: https://rasa.com/docs/rasa/user-guide/connectors/facebook-messenger/

In the end your credentials.yaml will include these credentials which you will get from facebook developer account

facebook:
verify: “you can give any custom token here”
secret: “the secret you will get from facebook developer account”
page-access-token: "the access token also which you will get from facebook developer account"

Now when deploying your application you need to provide App-Engine an app.yaml file where we define the type of environment we will be using the number of instances, CPU, memory and storage.

I used the following configuration you can use the same for tutorial purposes later you can experiment with different configurations:

runtime: custom
env: flex
runtime_config:
python_version: 3
manual_scaling:
instances: 1
resources:
cpu: 1
memory_gb: 4
disk_size_gb: 20

Create an app.yaml file and copy paste the above configuration.

When we deploy our application on App-Engine it expects a docker-file which it will use to deploy your application as a Docker-Container. Create a DockerFile and copy pase the below code:

FROM rasa/rasa
ENV BOT_ENV=production
COPY . /var/www
WORKDIR /var/www
RUN pip install rasa==1.3.0a1
RUN rasa train
ENTRYPOINT [ “rasa”, “run”, “-p”, “8080”]

The above file we are using first takes rasa as base image then using COPY command to copy all of the current files and folder into /var/www then switches to directory /var/www where the code was copied using WOKRDIR command. To make sure our rasa is version 1.3.0a we run the command RUN pip install rasa==1.3.0a1 and to train our model using Run rasa train command. Our entry point will be rasa run with port 8080 for the container as App-Engine expects your application to be running on port 8080.

First lest test the docker image we will create locally:

Comment out the below code in credentials.yaml

# rasa:
# url: “http://localhost:5002/api"

From endpoints.yaml comment out the below code:

# action_endpoint:
# url: “http://localhost:5055/webhook"

Now build the docker image using the command below:

docker build -t hassaanseeker/chatbot .

After the image is built and ready use the below command to run the container:


docker run -d -p 8080:8080 hassaanseeker/chatbot

Use docker-machine ip command to get the IP of your docker-machine:

The IP you get will be usually 192.168.99.100. So you can check for your server running on:

192.168.99.100:8080 you will get the below response.

Hello from Rasa: 1.3.0a1

Now that we have tested and our server is running locally we can now deploy our application on GCP using App-Engine.

You can create a google account initially you will get 300 dollars worth of credit for free. You can use those credits to experiment with GCP.

Go to this address: https://cloud.google.com/

Create your account and login. Then on the upper right corner press the Console button.

You will need to create a project before all of this go to this link follow it and create a project:
https://cloud.google.com/resource-manager/docs/creating-managing-projects#creating_a_project

GCP dashboard will appear on the top left there is a navigation menu click and from the drop down list select App-Engine from the Dashboard.

From there on the top right you will see a button called Activate cloud shell press it and the cloud shell will open. We will be using code editor on the top right of cloud shell press code editor button. A new window for the code editor will open.

Go to command line and clone the following repository:

git clone https://github.com/hassaanseeker/Rasa-GCP.git

cd into Rasa-GCP folder and copy paste the command:

gcloud app deploy -v chatbotv1

It will take some time and voila your chatbot is ready and deployed. To get a https link you can use the below command

gcloud app browse

After you go to the link you will see the below text

Hello from Rasa: 1.3.0a1

Our Rasa application is now live and deployed on GCP with just a few commands.

To setup facebook messenger app with Rasa just follow the details mentioned in the Below link:

https://rasa.com/docs/rasa/user-guide/connectors/facebook-messenger/

Now in order for Rasa Server to communicate with facebook-messenger we need to setup webhooks. The Callback URL will be:

For example as below:

https://[YOUR-PROJECT-ID].appspot.com/webhooks/facebook/webhook

And the verification token will be the verify field you entered in credentials.yaml.

Now you can go to the messenger and chat with the chatbot and hopefully you will hear back.

Rasa-Chatbot on Messenger

Conclusion

In this article you learned how to deploy a simple Rasa powered chatbot on Google Cloud Platform. Using Docker and APPENGINE.

Thank you for reading. I hope you enjoyed the article. You can follow me on Medium and on Twitter.

References:

--

--