notice

This is unreleased documentation for Rasa Open Source Documentation Master/Unreleased version.
For the latest released documentation, see the latest version (2.8.x).

Version: Master/Unreleased

rasa.engine.graph

SchemaNode Objects

@dataclass
class SchemaNode()

Represents one node in the schema.

GraphSchema Objects

@dataclass
class GraphSchema()

Represents a graph for training a model or making predictions.

as_dict

def as_dict() -> Dict[Text, Any]

Returns graph schema in a serializable format.

Returns:

The graph schema in a format which can be dumped as JSON or other formats.

from_dict

@classmethod
def from_dict(cls, serialized_graph_schema: Dict[Text, Any]) -> GraphSchema

Loads a graph schema which has been serialized using schema.as_dict().

Arguments:

  • serialized_graph_schema - A serialized graph schema.

Returns:

A properly loaded schema.

Raises:

  • GraphSchemaException - In case the component class for a node couldn't be found.

target_names

@property
def target_names() -> List[Text]

Returns the names of all target nodes.

minimal_graph_schema

def minimal_graph_schema() -> GraphSchema

Returns a new schema where all nodes are a descendant of a target.

GraphComponent Objects

class GraphComponent(ABC)

Interface for any component which will run in a graph.

create

@classmethod
@abstractmethod
def create(cls, config: Dict[Text, Any], model_storage: ModelStorage, resource: Resource, execution_context: ExecutionContext) -> GraphComponent

Creates a new GraphComponent.

Arguments:

  • config - This config overrides the default_config.
  • model_storage - Storage which graph components can use to persist and load themselves.
  • resource - Resource locator for this component which can be used to persist and load itself from the model_storage.
  • execution_context - Information about the current graph run.
  • Returns - An instantiated GraphComponent.

load

@classmethod
def load(cls, config: Dict[Text, Any], model_storage: ModelStorage, resource: Resource, execution_context: ExecutionContext, **kwargs: Any, ,) -> GraphComponent

Creates a component using a persisted version of itself.

If not overridden this method merely calls create.

Arguments:

  • config - The config for this graph component. This is the default config of the component merged with config specified by the user.
  • model_storage - Storage which graph components can use to persist and load themselves.
  • resource - Resource locator for this component which can be used to persist and load itself from the model_storage.
  • execution_context - Information about the current graph run.
  • kwargs - Output values from previous nodes might be passed in as kwargs.

Returns:

An instantiated, loaded GraphComponent.

get_default_config

@staticmethod
def get_default_config() -> Dict[Text, Any]

Returns the component's default config.

Default config and user config are merged by the GraphNode before the config is passed to the create and load method of the component.

Returns:

The default config of the component.

supported_languages

@staticmethod
def supported_languages() -> Optional[List[Text]]

Determines which languages this component can work with.

Returns: A list of supported languages, or None to signify all are supported.

not_supported_languages

@staticmethod
def not_supported_languages() -> Optional[List[Text]]

Determines which languages this component cannot work with.

Returns: A list of not supported languages, or None to signify all are supported.

required_packages

@staticmethod
def required_packages() -> List[Text]

Any extra python dependencies required for this component to run.

GraphNodeHook Objects

class GraphNodeHook(ABC)

Holds functionality to be run before and after a GraphNode.

on_before_node

@abstractmethod
def on_before_node(node_name: Text, execution_context: ExecutionContext, config: Dict[Text, Any], received_inputs: Dict[Text, Any]) -> Dict

Runs before the GraphNode executes.

Arguments:

  • node_name - The name of the node being run.
  • execution_context - The execution context of the current graph run.
  • config - The node's config.
  • received_inputs - Mapping from parameter name to input value.

Returns:

Data that is then passed to on_after_node

on_after_node

@abstractmethod
def on_after_node(node_name: Text, execution_context: ExecutionContext, config: Dict[Text, Any], output: Any, input_hook_data: Dict) -> None

Runs after the GraphNode as executed.

Arguments:

  • node_name - The name of the node that has run.
  • execution_context - The execution context of the current graph run.
  • config - The node's config.
  • output - The output of the node.
  • input_hook_data - Data returned from on_before_node.

ExecutionContext Objects

@dataclass
class ExecutionContext()

Holds information about a single graph run.

GraphNode Objects

class GraphNode()

Instantiates and runs a GraphComponent within a graph.

A GraphNode is a wrapper for a GraphComponent that allows it to be executed in the context of a graph. It is responsible for instantiating the component at the correct time, collecting the inputs from the parent nodes, running the run function of the component and passing the output onwards.

__init__

def __init__(node_name: Text, component_class: Type[GraphComponent], constructor_name: Text, component_config: Dict[Text, Any], fn_name: Text, inputs: Dict[Text, Text], eager: bool, model_storage: ModelStorage, resource: Optional[Resource], execution_context: ExecutionContext, hooks: Optional[List[GraphNodeHook]] = None) -> None

Initializes GraphNode.

Arguments:

  • node_name - The name of the node in the schema.
  • component_class - The class to be instantiated and run.
  • constructor_name - The method used to instantiate the component.
  • component_config - Config to be passed to the component.
  • fn_name - The function on the instantiated GraphComponent to be run when the node executes.
  • inputs - A map from input name to parent node name that provides it.
  • eager - Determines if the node is instantiated right away, or just before being run.
  • model_storage - Storage which graph components can use to persist and load themselves.
  • resource - If given the GraphComponent will be loaded from the model_storage using the given resource.
  • execution_context - Information about the current graph run.
  • hooks - These are called before and after execution.

parent_node_names

def parent_node_names() -> List[Text]

The names of the parent nodes of this node.

__call__

def __call__(*inputs_from_previous_nodes: Tuple[Text, Any]) -> Tuple[Text, Any]

Calls the GraphComponent run method when the node executes in the graph.

Arguments:

  • *inputs_from_previous_nodes - The output of all parent nodes. Each is a dictionary with a single item mapping the node's name to its output.

Returns:

The node name and its output.

from_schema_node

@classmethod
def from_schema_node(cls, node_name: Text, schema_node: SchemaNode, model_storage: ModelStorage, execution_context: ExecutionContext, hooks: Optional[List[GraphNodeHook]] = None) -> GraphNode

Creates a GraphNode from a SchemaNode.