Custom NLU Components

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:

```
import typing
from typing import Any, Optional, Text, Dict, List, Type
from rasa.nlu.components import Component
from rasa.nlu.config import RasaNLUModelConfig
from rasa.nlu.training_data import Message, TrainingData
if typing.TYPE_CHECKING:
from rasa.nlu.model import Metadata  

class MyComponent(Component):
"""A new component"""
@classmethod
def required_components(cls) -> List[Type[Component]]:
return []
defaults = {}
supported_language_list = None
not_supported_language_list = None
def init(self, component_config: Optional[Dict[Text, Any]] = None) -> None:
super().init(component_config)
def train(
self,
training_data: TrainingData,
config: Optional[RasaNLUModelConfig] = None,
**kwargs: Any,
) -> None:
pass
def process(self, message: Message, **kwargs: Any) -> None:
pass
def persist(self, file_name: Text, model_dir: Text) -> Optional[Dict[Text, Any]]:
pass
@classmethod
def load(
cls,
meta: Dict[Text, Any],
model_dir: Optional[Text] = None,
model_metadata: Optional["Metadata"] = None,
cached_component: Optional["Component"] = None,
**kwargs: Any,
) -> "Component":
if cached_component:
return cached_component
else:
return cls(meta)
|
# A few more structure examples or notes # can go here as needed.


**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.

**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.

## Component

_class_`rasa.nlu.components.Component`( _component_config=None_)

A component is a message processing unit in a pipeline.

Components are collected sequentially in a pipeline. Each component is called one after another. This holds for initialization, training, persisting and loading the components. If a component comes first in a pipeline, its methods will be called first.

E.g. to process an incoming message, the `process` method of each component will be called. During the processing (as well as the training, persisting and initialization) components can pass information to other components. The information is passed to other components by providing attributes to the so called pipeline context. The pipeline context contains all the information of the previous components a component can use to do its own processing. For example, a featurizer component can provide features that are used by another component down the pipeline to do intent classification.

_classmethod_`required_components`()

Specify which components need to be present in the pipeline.  
Returns the list of class names of required components.

**Returns**  
`List`[`Type`[`Component`]]

_classmethod_`required_packages`()

Specify which python packages need to be installed.  
E.g. `[