Building a Rasa Assistant in Docker
These docs are for version 1.x of Rasa Open Source.
Building a Rasa Assistant in Docker
If you don’t have a Rasa project yet, you can build one in Docker without having to install Rasa Open Source on your local machine. If you already have a model you’re satisfied with, see Deploying Your Rasa Assistant to learn how to deploy your model.
Installing Docker
If you’re not sure if you have Docker installed, you can check by running:
docker -v # Docker version 18.09.2, build 6247962
If Docker is installed on your machine, the output should show you your installed versions of Docker. If the command doesn’t work, you’ll have to install Docker. See Docker Installation for details.
Setting up your Rasa Project
Just like in the tutorial, you’ll use the rasa init command to create a project. The only difference is that you’ll be running Rasa inside a Docker container, using the image rasa/rasa. To initialize your project, run:
docker run -v $(pwd):/app rasa/rasa:1.10.18-full init --no-prompt
What does this command mean?
-v $(pwd):/appmounts your current working directory to the working directory in the Docker container. This means that files you create on your computer will be visible inside the container, and files created in the container will get synced back to your computer.rasa/rasais the name of the docker image to run. ‘1.10.18-full’ is the name of the tag, which specifies the version and dependencies.the Docker image has the
rasacommand as its entrypoint, which means you don’t have to typerasa init, justinitis enough.
Running this command will produce a lot of output. What happens is:
a Rasa project is created
an initial model is trained using the project’s training data.
To check that the command completed correctly, look at the contents of your working directory:
ls -1
The initial project files should all be there, as well as a models directory that contains your trained model.
Talking to Your Assistant
To talk to your newly-trained assistant, run this command:
docker run -it -v $(pwd):/app rasa/rasa:1.10.18-full shell
This will start a shell where you can chat to your assistant.
Note that this command includes the flags -it, which means that you are running Docker interactively, and you are able to give input via the command line.
Training a Model
If you edit the NLU or Core training data or edit the config.yml file, you’ll need to retrain your Rasa model. You can do so by running:
docker run -v $(pwd):/app rasa/rasa:1.10.18-full train --domain domain.yml --data data --out models
Here’s what’s happening in that command:
-v $(pwd):/app: Mounts your project directory into the Docker container so that Rasa can train a model on your training data.- rasa/rasa:1.10.18-full: Use the Rasa image with the tag ‘1.10.18-full’.
train: Execute therasa traincommand within the container. For more information see Command Line Interface.
In this case, we’ve also passed values for the location of the domain file, training data, and the models output directory to show how these can be customized.
Customizing your Model
Choosing a Tag
All rasa/rasa image tags start with a version number. The current version is 1.10.18. The tags are:
{version}{version}-full{version}-spacy-en{version}-spacy-de{version}-mitie-en
The {version}-full tag includes all possible pipeline dependencies, allowing you to change your config.yml as you like without worrying about missing dependencies. The plain {version} tag includes all the dependencies you need to run the default pipeline created by rasa init.
Adding Custom Components
If you are using a custom NLU component or policy in your config.yml, you have to add the module file to your Docker container. You can do this by either mounting the file or by including it in your own custom image (e.g. if the custom component or policy has extra dependencies). Make sure that your module is in the Python module search path by setting the environment variable PYTHONPATH=$PYTHONPATH:<directory of your module>.
Adding Custom Actions
To create more sophisticated assistants, you will want to use Custom Actions. Continuing the example from above, you might want to add an action which tells the user a joke to cheer them up.
Start by creating the custom actions in a directory actions in your working directory:
mkdir actions > mv actions.py actions/actions.py > # Rasa SDK expects a python module. > # Therefore, make sure that you have this file in the directory. > touch actions/__init__.py
Then build a custom action using the Rasa SDK by editing actions/actions.py, for example:
import requests import json from rasa_sdk import Action class ActionJoke(Action): def name(self): return "action_joke" def run(self, dispatcher, tracker, domain): request = requests.get('http://api.icndb.com/jokes/random').json() # make an api call joke = request['value']['joke'] # extract a joke from returned json response dispatcher.utter_message(text=joke) # send the message back to the user return []
In data/stories.md, replace utter_cheer_up with the custom action action_joke to tell your bot to use this new action.
In domain.yml, add a section for custom actions, including your new action:
actions: - action_joke
After updating your domain and stories, you have to retrain your model:
docker run -v $(pwd):/app rasa/rasa:1.10.18-full train
Your actions will run on a separate server from your Rasa server. First create a network to connect the two containers:
docker network create my-project
You can then run the actions with the following command:
docker run -d -v $(pwd)/actions:/app/actions --net my-project --name action-server rasa/rasa-sdk:1.10.3
To instruct the Rasa server to use the action server, you have to tell Rasa its location. Add this endpoint to your endpoints.yml, referencing the --name you gave the server:
action_endpoint: url: "http://action-server:5055/webhook"
Now you can talk to your bot again via the shell command:
docker run -it -v $(pwd):/app -p 5005:5005 --net my-project rasa/rasa:1.10.18-full shell
Deploying your Assistant
Work on your bot until you have a minimum viable assistant that can handle your happy paths. After that, you’ll want to deploy your model to get feedback from real test users. To do so, you can deploy the model you created with Rasa X via one of our recommended deployment methods or do a Rasa-only deployment in Docker Compose.