Custom NLU Components

These docs are for version 1.x of Rasa Open Source.

User Guide

NLU

Core

Conversation Design

API Reference

Migrate from (beta)

Reference

Custom NLU Components

You can create a custom component to perform a specific task which NLU doesn’t currently offer (for example, sentiment analysis). Below is the specification of the rasa.nlu.components.Component class with the methods you’ll need to implement.

Note: There is a detailed tutorial on building custom components here.

You can add a custom component to your pipeline by adding the module path. So if you have a module called sentiment containing a SentimentAnalyzer class:

pipeline:
- name: "sentiment.SentimentAnalyzer"

Also be sure to read the section on the Component Lifecycle.

To get started, you can use this skeleton that contains the most important methods that you should implement:

<br>import typing<br>from typing import Any, Optional, Text, Dict, List, Type<br>from rasa.nlu.components import Component<br>from rasa.nlu.config import RasaNLUModelConfig<br>from rasa.nlu.training_data import Message, TrainingData<br>if typing.TYPE_CHECKING:<br> from rasa.nlu.model import Metadata<br>class MyComponent(Component):<br> """A new component"""<br> @classmethod<br> def required_components(cls) -> List[Type[Component]]:<br> return []<br> defaults = {}<br> supported_language_list = None<br> not_supported_language_list = None<br> def __init__(self, component_config: Optional[Dict[Text, Any]] = None) -> None:<br> super().__init__(component_config)<br> def train(<br> self,<br> training_data: TrainingData,<br> config: Optional[RasaNLUModelConfig] = None,<br> **kwargs: Any,<br> ) -> None:<br> pass<br> def process(self, message: Message, **kwargs: Any) -> None:<br> pass<br> def persist(self, file_name: Text, model_dir: Text) -> Optional[Dict[Text, Any]]:<br> pass<br> @classmethod<br> def load(<br> cls,<br> meta: Dict[Text, Any],<br> model_dir: Optional[Text] = None,<br> model_metadata: Optional["Metadata"] = None,<br> cached_component: Optional["Component"] = None,<br> **kwargs: Any,<br> ) -> "Component":<br> if cached_component:<br> return cached_component<br> else:<br> return cls(meta)<br>

Note: If you create a custom tokenizer you should implement the methods of rasa.nlu.tokenizers.tokenizer.Tokenizer. The train and process methods are already implemented and you simply need to overwrite the tokenize method. train and process will automatically add a special token __CLS__ to the end of list of tokens, which is needed further down the pipeline.

Note: If you create a custom featurizer you should return a sequence of features. E.g. your featurizer should return a matrix of size (number-of-tokens x feature-dimension). The feature vector of the __CLS__ token should contain features for the complete message.