Running Rasa with docker-compose

Hello community,

I have build my rasa assistant with a custom component in nlu pipeline and now I want to run my chatbot using docker-compose up so I can have all my server up and running together. from documentation here in this link : [Building a Rasa Assistant in Docker] says if you have custom component you must include PYTHONPATH=$PYTHONPATH: to the environment of docker container.

which I followed this by having a Dockerfile as below for both rasa-sdk (used for action server) and rasa:

FROM rasa/rasa-sdk:3.0.0


COPY actions/requirements.txt ./

COPY actions/service_key.json ./

USER root

COPY ./actions /app/actions

CMD ["start", "--actions", "actions"]

RUN pip install -r requirements.txt



USER 1000

FROM rasa/rasa:3.0.3-full


COPY wordEmbeddingClassifier/requirements.txt ./

USER root

ENV PYTHONPATH "${PYTHONPATH}:/app/wordEmbeddingClassifier/keras_embedding_intent_classifier"

RUN pip install -r requirements.txt


USER 1000

after building the above images I am trying to put them in a docker-compose file as below:

version: "3.9"
    image: rasa/rasa:3.0.3-full
    - "5005:5005"
    - ./:/app
      - shell
      - --debug
      - action_server
    image: rasa/rasa-sdk:3.0.0
    -  ./actions:/app/actions
      - "5055:5055"
    image: rasa/duckling:latest
    - "8000:8000"

however when I am testing the chatbot with docker-compose up and running the rasa shell… the program is not able to get a response from the custom component process function. it is working without running with docker however with docker it is not working… anyone knows how I can fix this???

Hello @maralbahari, did you update the credentials.yml.


  url: 'http://localhost:5055/webhook'


  url: 'http://action_server:5055/webhook'

Please do refer to this thread, I hope this will help or share the error’s if you encounter. Dockerizing my rasa chatbot application that has botfront - #11 by nik202

yes I changed the endpoint for action server. the docker compose is running all the ports however when I am testing the chatbot the rasa image is not finding my custom component in nlu

@maralbahari is your file is in the actions folder?

You can inspect the actions_server container and see the content

docker exec -u root -t -i actions_server /bin/bash


docker-compose up

Always mention @ and nik202 for fast reply.

@nik202 no my file is in different directory. I am using this custom component as nlu pipeline component inside config.yml file. the documentation says when you have custom nlu component or policies you must include this: PYTHONPATH=$PYTHONPATH:<directory of your module> . to docker environment

however I am not sure where and how to do it I have posted both of my Dockerfile and docker-compose

@maralbahari right, can you share the config.yml ? or your custom component is .py file? did you mentioned all the requirements in requirements.txt.

@maralbahari are you mentioning this link ? Custom Graph Components

here is my component it is in a python package

from __future__ import annotations
import logging
import typing
import warnings
from typing import Any, Dict, List, Optional, Text, Tuple, Type

import numpy as np
import re
import string

import as io_utils
import sklearn
from rasa.engine.graph import GraphComponent, ExecutionContext
from import DefaultV1Recipe
from import Resource
from import ModelStorage
from rasa.shared.constants import DOCS_URL_TRAINING_DATA_NLU
from rasa.nlu.classifiers import LABEL_RANKING_LENGTH
from rasa.shared.nlu.constants import TEXT,METADATA_EXAMPLE
from rasa.nlu.classifiers.classifier import IntentClassifier
from rasa.shared.nlu.training_data.training_data import TrainingData
from rasa.shared.nlu.training_data.message import Message
from sklearn.preprocessing import LabelEncoder
from rasa.nlu.classifiers.diet_classifier import DIETClassifier
logger = logging.getLogger(__name__)

if typing.TYPE_CHECKING:
    import keras
    from keras.preprocessing.sequence import pad_sequences
    from keras.models import Sequential
    from keras.layers import Dense
    from keras.layers import GlobalMaxPooling1D
    import nltk
    from nltk.tokenize import word_tokenize
    from sklearn.model_selection import train_test_split
    from keras.layers.embeddings import Embedding

    DefaultV1Recipe.ComponentType.INTENT_CLASSIFIER, is_trainable=True
class KerasEmbeddingIntentClassifier(GraphComponent, IntentClassifier):
    def required_components(cls) -> List[Type]:
        """Components that should be included in the pipeline before this component."""
        return []

    def get_default_config() -> Dict[Text, Any]:
        """The component's default config (see parent class for full docstring)."""
        return {

            "max_len" : 1000,
            "embedding_dim" : 200,
            "trunc_type" : 'post',
            "padding_type" : 'post',
            "num_threads": 1

    def __init__(
            config: Dict[Text, Any],
            model_storage: ModelStorage,
            resource: Resource,
            clf: "keras.models.Sequential" = None,
            le: "sklearn.preprocessing.LabelEncoder" = None,
    ) -> None:
        from keras.preprocessing.text import Tokenizer
        from sklearn.preprocessing import LabelEncoder
        self.component_config = config
        self._model_storage = model_storage
        self._resource = resource
        if fe is not None:
  "feature tokenizer is not none:{self.fe.word_index}")
        if le is not None:
            self.le = le
            self.le = LabelEncoder()
        self.clf = clf
    def create(
        config: Dict[Text, Any],
        model_storage: ModelStorage,
        resource: Resource,
        execution_context: ExecutionContext,
    ) -> KerasEmbeddingIntentClassifier:
        """Creates a new untrained component (see parent class for full docstring)."""
        return cls(config, model_storage, resource)

    def required_packages(cls) -> List[Text]:
        """Any extra python dependencies required for this component to run."""
        return ["keras","sklearn","nltk"]

    def transform_labels_str2num_seq(self, labels: List[Text]) -> np.ndarray:
        """Transforms a list of strings into numeric label representation.

        :param labels: List of labels to convert to numeric representation
        return  self.le.fit_transform(labels)

    def transform_nlu_examples_str2num(self,X)-> list:
        return self.fe.texts_to_sequences(X)
    def pad_nlu_examples(self,X)-> np.ndarray:
        from keras.preprocessing.sequence import pad_sequences
        return pad_sequences(X,padding=self.component_config["padding_type"],maxlen=self.component_config['max_len'])

    def clean_text(self,text):
        import nltk
        from nltk.tokenize import word_tokenize
        import string
        text = text.lower()
        # tokenize the text
        tokens = word_tokenize(text)
        tokens = [w.lower() for w in tokens]
        # remove puntuations
        table = str.maketrans('', '', string.punctuation)
        stripped = [w.translate(table) for w in tokens]
        return ' '.join(stripped)

    def train(self, training_data: TrainingData) -> Resource:
        from sklearn.model_selection import train_test_split
        from keras.callbacks import ModelCheckpoint
        from nltk.tokenize import word_tokenize
        num_threads = self.component_config["num_threads"]

        labels = [e.get("intent") for e in training_data.intent_examples]

        examples=[e.get(TEXT) for e in training_data.intent_examples]"examples{examples}")
        cleaned_x=[self.clean_text(t) for t in examples]"cleaned:{cleaned_x}")
        tokenized_cleaned_x=[word_tokenize(t) for t in cleaned_x]"tokenized cleaned{tokenized_cleaned_x}")

        if len(set(labels)) < 2:
                "Can not train an intent classifier as there are not "
                "enough intents. Need at least 2 different intents. "
                "Skipping training of intent classifier.",
            return self._resource

        y = self.transform_labels_str2num_seq(labels)


        trainX, testX, trainY, testY = train_test_split(X_padded, y, test_size=0.2, random_state=42)
        self.clf = self._create_classifier(num_threads, y)
        with warnings.catch_warnings():

            checkpoint = ModelCheckpoint('model-{epoch:03d}-{val_accuracy:03f}.h5', verbose=1, monitor='val_acc',
                                         save_best_only=True, mode='auto')

        return self._resource
    def glove_embedding_matrix(self):
        import os
        embedding_index = {}
        glove_dir = "C:\\Users\\maral\\PycharmProjects\\FSKTMBot\\wordEmbeddingClassifier\\pretrained_glove"
        f = open(os.path.join(glove_dir, 'glove.twitter.27B.200d.txt'), encoding="utf-8")
        for line in f:
            values = line.split()
            word = values[0]
            coeff = np.asarray(values[1:], dtype='float32')
            embedding_index[word] = coeff
        embedding_matrix = np.zeros((len(self.fe.word_index)+1, self.component_config["embedding_dim"]))
        for word, i in self.fe.word_index.items():
            embedding_vector = embedding_index.get(word)
            if embedding_vector is not None:
                embedding_matrix[i] = embedding_vector
        return embedding_matrix
    def _create_classifier(
            self, num_threads: int, y: np.ndarray
    ) -> "keras.models.Sequential":
        from keras.models import Sequential
        from keras.layers import Dense,GlobalMaxPooling1D,Dropout
        from keras.layers.embeddings import Embedding

        model = Sequential()
                            self.component_config["embedding_dim"],weights=[embedding_matrix], input_length=self.component_config["max_len"],trainable=False))
        model.add(Dense(18, activation=self.component_config["activation_2"]))
        # compile the model
                      loss=self.component_config['loss'], metrics=self.component_config['metrics'])
        return model
    def predict_prob(self, X: np.ndarray) -> np.ndarray:
        """Given a bow vector of an input text, predict the intent label.

        Return probabilities for all labels.

        :param X: bow of input text
        :return: vector of probabilities containing one entry for each label.
        return self.clf.predict(X)

    def predict(self, X: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
        """Given a bow vector of an input text, predict most probable label.

        Return only the most likely label.

        :param X: bow of input text
        :return: tuple of first, the most probable label and second,
                 its probability.
        pred_result = self.predict_prob(X)
        # sort the probabilities retrieving the indices of
        # the elements in sorted order
        sorted_indices = np.fliplr(np.argsort(pred_result, axis=1))"sorted_indicies_type:{type(sorted_indices)}")
        return sorted_indices, pred_result[:, sorted_indices]

    def transform_labels_num2str(self,intent_ids)->np.ndarray:
        return self.le.inverse_transform(intent_ids[0])
    def process(self, messages: List[Message]) -> List[Message]:
        """Return the most likely intent and its probability for a message."""
        from nltk.tokenize import word_tokenize
        for message in messages:
            if not self.clf:
                # component is either not trained or didn't
                # receive enough training data
                intent = None
                intent_ranking = []
                msg =str(message.get(TEXT))
                msg=[word_tokenize(w) for w in msg]
                intent_ids, probabilities = self.predict(padded_X)

                intents = self.transform_labels_num2str(intent_ids)

                # `predict` returns a matrix as it is supposed
                # to work for multiple examples as well, hence we need to flatten
                probabilities = probabilities.flatten().astype('float64')
      "flatten prob:{probabilities}")

                if intents.size > 0 and probabilities.size > 0:
                    ranking = list(zip(list(intents), list(probabilities)))[

                    intent = {"name": intents[0], "confidence": probabilities[0]}
                    intent_ranking = [
                        {"name": intent_name, "confidence": score}
                        for intent_name, score in ranking
                    intent = {"name": None, "confidence": 0.0}
                    intent_ranking = []

  "probabilites item type:{type(probabilities[0])}")
            message.set("intent", intent, add_to_output=True)
            message.set("intent_ranking", intent_ranking, add_to_output=True)
        return messages
    def persist(self) -> None:
        """Persist this model into the passed directory."""
        with self._model_storage.write_to(self._resource) as model_dir:
            file_name = self.__class__.__name__
            classifier_file_name = model_dir / f"{file_name}_classifier.h5"
            encoder_file_name = model_dir / f"{file_name}_labels_encoder.pkl"

            if self.clf and self.le:
                io_utils.json_pickle(encoder_file_name, self.le.classes_)

    def load(
            config: Dict[Text, Any],
            model_storage: ModelStorage,
            resource: Resource,
            execution_context: ExecutionContext,
            **kwargs: Any,
    ) -> KerasEmbeddingIntentClassifier:
        """Loads trained component (see parent class for full docstring)."""
        from keras.preprocessing.text import Tokenizer
        from keras.models import load_model
        from sklearn.preprocessing import LabelEncoder
            with model_storage.read_from(resource) as model_dir:
                file_name = cls.__name__
                classifier_file = model_dir / f"{file_name}_classifier.h5"

                if classifier_file.exists():

                    classifier = load_model(classifier_file,compile=True)
                    encoder_file = model_dir / f"{file_name}_labels_encoder.pkl"
                    features_encoder_file=model_dir / f"{file_name}_features_encoder.pkl"
                    labels = io_utils.json_unpickle(encoder_file)
                    labels_encoder = LabelEncoder()


          "features index:{features_encoder.word_index}")

                    return cls(config, model_storage, resource, classifier, labels_encoder,features_encoder)
        except ValueError:
                f"Failed to load '{cls.__name__}' from model storage. Resource "
                f"'{}' doesn't exist."
        return cls(config, model_storage, resource)

@nik202 here is config

# Configuration for Rasa NLU.
recipe: "default.v1"
language: "en"

# # No configuration for the NLU pipeline was provided. The following default pipeline was used to train your model.
# # If you'd like to customize it, uncomment and adjust the pipeline.
# # See for more information.
   - name: "WhitespaceTokenizer"
     intent_tokenization_flag: True
     intent_split_symbol: "_"
   - name: "RegexFeaturizer"
     case_sensitive: False
   - name: "RegexEntityExtractor"
   - name: "DucklingEntityExtractor"
     url: "http://localhost:8000"
     dimensions: ["email"]
   - name: "LexicalSyntacticFeaturizer"
   - name: "wordEmbeddingClassifier.keras_embedding_intent_classifier.KerasEmbeddingIntentClassifier"

# Configuration for Rasa Core.
# # No configuration for policies was provided. The following default policies were used to train your model.
# # If you'd like to customize them, uncomment and adjust the policies.
# # See for more information.
   - name: "RulePolicy"
     core_fallback_treshold: 0.3
   - name: "MemoizationPolicy"
   - name: "TEDPolicy"
     max_history: 4
     epochs: 100

@maralbahari what is the file name of this code? custom-component its OR ?

@nik202 it’s a python package in this order wordEmbeddingClassifier

  • here is an image of my project window

@maralbahari on local machine how you running this I guess just mentioned in the configuration or any other means?

sorry I dont understand your question :frowning:

if you mean I used it anywhere else no I didn’t it is predicting intents in my rasa nlu

@maralbahari did you mount the wordEmbeddingClassifier folder path in /app ?

@maralbahari confirm me that you were able to run on your local machine?

I am able to run the action server separately by rasa run actions and the rasa shell --debug and bot is working perfectly fine however when I am using docker the rasa-full image does not find my custom component to retrieve the task (which is intent detection) and it throws run time error.

but this component is not for action server it’s for nlu

@maralbahari sorry, you right, me confused. Is all your project folder mounted in docker container?

@maralbahari can you check the logs of docker-compose, there you will see the issue or I guess there can be the path issue for sure, container not able to get the path of component.

@nik202 I think I just fixed it

it shows my warning that I put in the component def process() that it is predicting for message intent however it stops and throws run time error

rasa_1           | 2021-12-17 15:56:54 DEBUG    rasa.engine.graph  - Node 'run_LexicalSyntacticFeaturizer4' running 'LexicalSyntacticFeaturizer.process'.
rasa_1           | 2021-12-17 15:56:54 DEBUG    rasa.engine.graph  - Node 'run_wordEmbeddingClassifier.keras_embedding_intent_classifier.KerasEmbeddingIntentClassifier5' running 'KerasEmb
rasa_1           | /opt/venv/lib/python3.8/site-packages/rasa/shared/utils/ UserWarning: message:hello
rasa_1           | 2021-12-17 15:56:54 DEBUG    rasa.core.lock_store  - Deleted lock for conversation 'a74816ed7fe246e299060c412ff141a4'.

here is the error from docker-compose up:

    raise asyncio.TimeoutError from None

this is my warning to debug if it is getting the message from my custom component which is finding

however in the process it needs some dependencies like nltk which I think it does not exist in the rasa server image that’s why it is not being processed anymore

any suggestion how to include the requiretments.txt with the rasa server? @nik202

here is my new Dockerfile for rasa server and action server

FROM rasa/rasa-sdk:3.0.0


COPY actions/requirements.txt ./

COPY actions/service_key.json ./

USER root

COPY ./actions /app/actions
COPY ./endpoints.yml /app/

CMD ["run","start", "--actions", "actions"]

RUN pip install -r requirements.txt



USER 1000

FROM rasa/rasa:3.0.3-full


COPY wordEmbeddingClassifier/requirements.txt ./

USER root

ENV PYTHONPATH "${PYTHONPATH}:/app/wordEmbeddingClassifier"

RUN pip install -r requirements.txt


USER 1000

which actually the rasa action server now is not working any more the error says:

action_server_1  | ./ line 17: exec: actions: cannot execute: Is a directory