.._section_customcomponents:
Custom 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.
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | from rasa_nlu.components import Component
import typing
from typing import Any, Optional, Text, Dict
if typing.TYPE_CHECKING:
from rasa_nlu.model import Metadata
class MyComponent(Component):
"""A new component"""
# Defines what attributes the pipeline component will
# provide when called. The listed attributes
# should be set by the component on the message object
# during test and train, e.g.
# ```message.set("entities", [...])```
provides = []
# Which attributes on a message are required by this
# component. e.g. if requires contains "tokens", than a
# previous component in the pipeline needs to have "tokens"
# within the above described `provides` property.
requires = []
# Defines the default configuration parameters of a component
# these values can be overwritten in the pipeline configuration
# of the model. The component should choose sensible defaults
# and should be able to create reasonable results with the defaults.
defaults = {}
# Defines what language(s) this component can handle.
# This attribute is designed for instance method: `can_handle_language`.
# Default value is None which means it can handle all languages.
# This is an important feature for backwards compatibility of components.
language_list = None
def __init__(self, component_config=None):
super(MyComponent, self).__init__(component_config)
def train(self, training_data, cfg, **kwargs):
"""Train this component.
This is the components chance to train itself provided
with the training data. The component can rely on
any context attribute to be present, that gets created
by a call to :meth:`components.Component.pipeline_init`
of ANY component and
on any context attributes created by a call to
:meth:`components.Component.train`
of components previous to this one."""
pass
def process(self, message, **kwargs):
"""Process an incoming message.
This is the components chance to process an incoming
message. The component can rely on
any context attribute to be present, that gets created
by a call to :meth:`components.Component.pipeline_init`
of ANY component and
on any context attributes created by a call to
:meth:`components.Component.process`
of components previous to this one."""
pass
def persist(self,
file_name: Text,
model_dir: Text) -> Optional[Dict[Text, Any]]:
"""Persist this component to disk for future loading."""
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':
"""Load this component from file."""
if cached_component:
return cached_component
else:
return cls(meta)
|
Component¶
-
class
rasa_nlu.components.
Component
(component_config: Union[typing.Dict[str, typing.Any], NoneType] = 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_packages
() → List[str]¶ Specify which python packages need to be installed to use this component, e.g.
["spacy"]
.This list of requirements allows us to fail early during training if a required package is not installed.
-
classmethod
create
(component_config: Dict[str, Any], config: rasa_nlu.config.RasaNLUModelConfig) → rasa_nlu.components.Component¶ Creates this component (e.g. before a training is started).
Method can access all configuration parameters.
-
provide_context
() → Union[typing.Dict[str, typing.Any], NoneType]¶ Initialize this component for a new pipeline
This function will be called before the training is started and before the first message is processed using the interpreter. The component gets the opportunity to add information to the context that is passed through the pipeline during training and message parsing. Most components do not need to implement this method. It’s mostly used to initialize framework environments like MITIE and spacy (e.g. loading word vectors for the pipeline).
-
train
(training_data: TrainingData, cfg: rasa_nlu.config.RasaNLUModelConfig, **kwargs) → None¶ Train this component.
This is the components chance to train itself provided with the training data. The component can rely on any context attribute to be present, that gets created by a call to
components.Component.pipeline_init()
of ANY component and on any context attributes created by a call tocomponents.Component.train()
of components previous to this one.
-
process
(message: Message, **kwargs) → None¶ Process an incoming message.
This is the components chance to process an incoming message. The component can rely on any context attribute to be present, that gets created by a call to
components.Component.pipeline_init()
of ANY component and on any context attributes created by a call tocomponents.Component.process()
of components previous to this one.
-
persist
(file_name: str, model_dir: str) → Union[typing.Dict[str, typing.Any], NoneType]¶ Persist this component to disk for future loading.
-
prepare_partial_processing
(pipeline: List[_ForwardRef('Component')], context: Dict[str, Any]) → None¶ Sets the pipeline and context used for partial processing.
The pipeline should be a list of components that are previous to this one in the pipeline and have already finished their training (and can therefore be safely used to process messages).
-
partially_process
(message: Message) → Message¶ Allows the component to process messages during training (e.g. external training data).
The passed message will be processed by all components previous to this one in the pipeline.
-
classmethod
can_handle_language
(language: collections.abc.Hashable) → bool¶ Check if component supports a specific language.
This method can be overwritten when needed. (e.g. dynamically determine which language is supported.)
-
classmethod
Have questions or feedback?¶
We have a very active support community on Rasa Community Forum that is happy to help you with your questions. If you have any feedback for us or a specific suggestion for improving the docs, feel free to share it by creating an issue on Rasa NLU GitHub repository.