Running RASA 2 chatbot with GCP

Samrudha Kelkar
tech-that-works
Published in
5 min readMar 27, 2021

--

Have you built your first rasa bot?

Is it running only on your machine and you are eager to show it to the world?

If yes, this is the blog for you!

Long back, when RASA was a new cool thing in the town, I had written about hosting the rasa bot on AWS. Now that there have been significant changes within RASA in the last two years, it's time for a new post to host the latest bot on a cloud, this time on GCP. Let's dive straight into the code..

Get the bot running on local

RASA has made this step extremely easy with its new CLI-based interface.

Get a fresh folder, a fresh python virtual environment

Create your environment

This will install the latest open source rasa version. Now just type rasa init to create a project structure of your bot.

Test your bot by typing rasa shell. This will create a sample bot that comes with a prebuilt NLU and domain. It will interact on basic chitchat like hi, bye.

Dockerized bot image

Shell is good enough to test your bot but let's create a proper Docker image for the bot to deploy it systematically. In the same project folder create an empty file with the name Dockerfile (note that it's not Dockerfile.txt)

Add below content in the same.

We can quickly check if the image is built correctly.

docker build . -t <rasa bot tag>

Now create an empty app.yaml file in the same folder and add below content

I referred to this blogpost to create this YAML file. Note that the extension of the file is not .yml, it's .yaml

Add the entire code now to your Github by creating a fresh repository. Read here if you have never done that.

Host a bot on GCP

Now that we have docker image and App engine descriptions for our bot, we will host the same in GCP. We will be using App Engine service by GCP which lets you build scalable apps without worrying much about server management. All configurations can be just specified in app.yaml file and the App Engine will take care of deployments. You can read more about it here

However, just be aware of the cost. Everything simple comes at a cost. My bot which ran for about 2 days continuously came with bill of INR 400 (~5 USD)

Let's see how to deploy App Engine now:

  1. Create a new project in GCP
  2. Go to App-engine service and enable it for the project

3. Open a cloud shell symbol in the right top which looks like this

4. Open a terminal and clone the repository in which you have committed your bot code.

git clone <your_repository_name>

(you will be asked your Github username and the password. This way of password authentication will soon be deprecated by Github. However, there is a way-out which I will explain below)

5 Navigate to the folder of the repository and type

gcloud app deploy -v <your_bot_deployment_version>

6. This will deploy your bot. The hosted link address can be extracted from gcloud app browse

If you go to the link from the above step, you should see the same message that we saw when we ran Docker on local.

Hello from Rasa: 2.4.0

Auto build and push the updates:

It's not a good idea to login to cloud shell every time you change something in your bot code. To avoid that you can hook a trigger.

7. Search for triggers in the GCP console and click on the create trigger button as shown above

8. Give some name, description, and tag. That's for your reference.

9. In the event section click on push to a branch

10. In the Source section click on connect new repository part.

It will open a side tab like below:

Here select Github

Authenticate your Github account

Select the repository which has bot code. I recommend ONLY choosing one repository that has code and not others associated with your account

10. Since we have already had Dockerfile in the repository, we will select the Dockerfile option in the configuration. GCP also has cloudbuild option for building your app if it is not dockerized already.

11. The last thing is to assign permission to our trigger to redeploy App-engine when you change the code. For that go to the Settings part of your Trigger, and enable App-engine permissions.

Interact with the bot service:

Now that we have built a backend service, we can use any channel to interact with the bot. I will stick to the web-based REST API calls like my last blog

Here is the snippet from my flask app which sends user text to the bot service in GCP and returns back the responses

--

--