Python API

Apart from running Rasa NLU as a HTTP server you can use it directly in your python program. Rasa NLU supports python 3.5, 3.6 and 3.7 (supported for python 2.7 up until version 0.14).

Training a Model

For creating your models, you can follow the same instructions as non-python users. Or, you can train directly in python with a script like the following (using spacy):

from rasa_nlu.training_data import load_data
from rasa_nlu.model import Trainer
from rasa_nlu import config

training_data = load_data('data/examples/rasa/demo-rasa.json')
trainer = Trainer(config.load("sample_configs/config_pretrained_embeddings_spacy.yml"))
trainer.train(training_data)
model_directory = trainer.persist('./projects/default/')  # Returns the directory the model is stored in

Using a Model to Make Predictions

You can call Rasa NLU directly from your python script. To do so, you need to load the metadata of your model and instantiate an interpreter. The metadata.json in your model dir contains the necessary information to reconstruct your model:

from rasa_nlu.model import Interpreter

# where model_directory points to the model folder
interpreter = Interpreter.load(model_directory)

You can then use the loaded interpreter to parse text:

interpreter.parse(u"The text I want to understand")

which returns the same data as the /parse endpoint of the HTTP API .

Reducing Memory Use When Loading Multiple Models

If multiple models are created, it is reasonable to share components between the different models. E.g. the 'SpacyNLP' component, which is used by every pipeline that wants to have access to the spacy word vectors, can be cached to avoid storing the large word vectors more than once in main memory. To use the caching, a ComponentBuilder should be passed when loading and training models.

Here is a short example on how to create a component builder, which can be reused to train and run multiple models. To train a model:

from rasa_nlu.training_data import load_data
from rasa_nlu import config
from rasa_nlu.components import ComponentBuilder
from rasa_nlu.model import Trainer

builder = ComponentBuilder(use_cache=True)      # will cache components between pipelines (where possible)

training_data = load_data('data/examples/rasa/demo-rasa.json')
trainer = Trainer(config.load("sample_configs/config_pretrained_embeddings_spacy.yml"), builder)
trainer.train(training_data)
model_directory = trainer.persist('./projects/default/')  # Returns the directory the model is stored in

The same builder can be used to load a model (can be a totally different one). The builder only caches components that are safe to be shared between models. Here is a short example on how to use the builder when loading models:

from rasa_nlu.model import Interpreter
from rasa_nlu import config

# For simplicity we will load the same model twice, usually you would want to use the metadata of
# different models

interpreter = Interpreter.load(model_directory, builder)     # to use the builder, pass it as an arg when loading the model
# the clone will share resources with the first model, as long as the same builder is passed!
interpreter_clone = Interpreter.load(model_directory, builder)

Important Classes

Config

rasa_nlu.config.load(filename: Union[str, NoneType] = None, **kwargs) → rasa_nlu.config.RasaNLUModelConfig
class rasa_nlu.config.RasaNLUModelConfig(configuration_values=None)
__init__(configuration_values=None)

Create a model configuration, optionally overridding defaults with a dictionary configuration_values.

Interpreter

class rasa_nlu.model.Interpreter(pipeline: List[rasa_nlu.components.Component], context: Union[typing.Dict[str, typing.Any], NoneType], model_metadata: Union[rasa_nlu.model.Metadata, NoneType] = None)

Use a trained pipeline of components to parse text messages.

static load(model_dir: str, component_builder: Union[rasa_nlu.components.ComponentBuilder, NoneType] = None, skip_validation: bool = False) → rasa_nlu.model.Interpreter

Create an interpreter based on a persisted model.

Parameters:
  • skip_validation – If set to True, tries to check that all required packages for the components are installed before loading them.
  • model_dir – The path of the model to load
  • component_builder – The ComponentBuilder to use.
Returns:

An interpreter that uses the loaded model.

Metadata

class rasa_nlu.model.Metadata(metadata: Dict[str, Any], model_dir: Union[str, NoneType])

Captures all information about a model to load and prepare it.

static load(model_dir: str)

Loads the metadata from a models directory.

Parameters:model_dir (str) – the directory where the model is saved.
Returns:A metadata object describing the model
Return type:Metadata

ComponentBuilder

class rasa_nlu.components.ComponentBuilder(use_cache: bool = True)

Creates trainers and interpreters based on configurations.

Caches components for reuse.

load_component(component_meta: Dict[str, Any], model_dir: str, model_metadata: Metadata, **context) → rasa_nlu.components.Component

Tries to retrieve a component from the cache, else calls load to create a new component.

Parameters:
  • component_meta (dict) – the metadata of the component to load in the pipeline
  • model_dir (str) – the directory to read the model from
  • model_metadata (Metadata) – the model’s rasa_nlu.models.Metadata
Returns:

the loaded component.

Return type:

Component

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.