Custom Actions

Note

This tutorial will show you the pieces required to deploy an action server to Rasa Platform.

Goal

We will deploy an example action server to your running instance of the Platform. The recommended way of doing this is using Docker, but it’s not required. The only requirement is that your action server provides a webhook for executing actions, and supplying that url to Rasa Core. For the rest of this tutorial we will assume you are using Docker.

The easiest way to follow along is if you carry out these steps while in an ssh session on the server where you deployed the Platform. In the final section we discuss how to build and deploy to a remote server.

Clone and the Rasa platform-demo repository by running

git clone https://github.com/RasaHQ/platform-demo.git

Creating an action server and a Docker container

For custom actions written in python, creating a server is as simple as passing an actions.py file to the Rasa Core SDK:

python -m rasa_core_sdk.endpoint --actions actions -p 5001

This script runs a server that handles custom actions your bot should take. To run this as a docker container, you will need to build an image.

Building Your Docker Image

The platform-demo directory contains a Dockerfile that can be used to build an executable server. To build a docker image from your demo app, go to the root directory of the platform-demo repo and run:

docker build --build-arg CORE_SDK_VERSION=0.11.2 -t $IMAGE_TAG .

Note

You will need to supply a --build-arg specifying the version of Rasa Core SDK that should be used. We recommend you use version 0.12.0 of Rasa Core SDK with this version of Rasa Platform.

This will use the Dockerfile in your current directory. For now, you can use something like IMAGE_TAG=demobot:v1. If you are deploying to a remote server, you will want to push the image to a registry first. An image tag like IMAGE_TAG=username/demobot:v1 will allow you to push this tag to a docker registry like GCR or docker Hub. You can read more about docker tags here.

Start the action server

Once your docker image is built, you can start the container using the command below.

sudo docker pull $IMAGE_TAG
sudo docker stop app 2> /dev/null
sudo docker rm app 2> /dev/null
sudo docker run -d -p 5001:5001 --name app $IMAGE_TAG

Deploying that container as part of the Platform

To start your container as part of the Platform, you need to add it to the docker-compose specification. This will tell the docker runner to start your container as part of the Platform.

Instead of changing /etc/rasaplatform/docker-compose.yml we strongly recommend to create a file /etc/rasaplatform/docker-compose.override.yml with your changes. Any changes made to /etc/rasaplatform/docker-compose.yml might be overwritten by an update.

Here is an example docker-compose.override.yml that includes the changes necessary to start the above docker:

version: '3.4'
services:
  app:
    build:
      context: app
      dockerfile: Dockerfile
    container_name: "app"
    environment:
      # any env vars you need go here
    expose:
    - "5001"
    depends_on:
    - core

This assumes that your custom action server code is stored on the machine in /etc/rasaplatform/app and your docker file is located there as well /etc/rasaplatform/app/Dockerfile.

After creating this docker-compose.override.yml, you can use the following commands to restart the Platform with your action server:

cd /etc/rasaplatform
sudo docker-compose up --build -d

This setup will build the docker container directly on the server.

Alternatively, you can also build the docker container externally (e.g. on a continuous integration server like travis) and use that image in the docker compose:

version: '3'
services:
  app:
    image: my-private-docker-registry.com/platform-app:latest
    container_name: "app"
    environment:
      RASA_API_ENDPOINT_URL: "http://api:5002"
    expose:
    - "5001"
    depends_on:
    - core

In this case, you need to make sure to pull the image before starting up the server:

cd /etc/rasaplatform
sudo docker login -u USER -p PASSWORD https://my-private-docker-registry.com
sudo docker pull my-private-docker-registry.com/platform-app:latest

After the image is successfully pulled, make sure to login with the Rasa Platform credentials again and restart the Platform:

sudo docker login -u _json_key -p "$(cat /etc/rasaplatform/gcr-auth.json)" https://gcr.io
sudo docker-compose pull
sudo docker-compose up -d

Note

sudo docker-compose pull will also attempt to pull the latest image of your action server. This will fail because you’re logged in with the Rasa credentials, rather than your own. To update the Platform successfully, run sudo docker-compose pull --ignore-pull-failures

You can use the Dockerfile from the Platform demo as a basis for the custom action server of your own bot. The important part is that the Dockerfile starts the action server, and the easiest way to do this is with the following command (assuming your actions are in the actions.py file):

python -m rasa_core_sdk.endpoint --actions actions -p 5001