Ejemplo n.º 1
0
    def __call__(
            self, *inputs_from_previous_nodes: Tuple[Text,
                                                     Any]) -> Tuple[Text, Any]:
        """Calls the `GraphComponent` run method when the node executes in the graph.

        Args:
            *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.
        """
        received_inputs: Dict[Text, Any] = dict(inputs_from_previous_nodes)

        kwargs = {}
        for input_name, input_node in self._inputs.items():
            kwargs[input_name] = received_inputs[input_node]

        input_hook_outputs = self._run_before_hooks(kwargs)

        if not self._eager:
            constructor_kwargs = rasa.shared.utils.common.minimal_kwargs(
                kwargs, self._constructor_fn)
            self._load_component(**constructor_kwargs)
            run_kwargs = {
                k: v
                for k, v in kwargs.items() if k not in constructor_kwargs
            }
        else:
            run_kwargs = kwargs

        logger.debug(f"Node '{self._node_name}' running "
                     f"'{self._component_class.__name__}.{self._fn_name}'.")

        try:
            output = self._fn(self._component, **run_kwargs)
        except InvalidConfigException:
            # Pass through somewhat expected exception to allow more fine granular
            # handling of exceptions.
            raise
        except Exception as e:
            if not isinstance(e, RasaException):
                raise GraphComponentException(
                    f"Error running graph component for node {self._node_name}."
                ) from e
            else:
                logger.error(
                    f"Error running graph component for node {self._node_name}."
                )
                raise

        self._run_after_hooks(input_hook_outputs, output)

        return self._node_name, output
Ejemplo n.º 2
0
 def _run_after_hooks(self, input_hook_outputs: List[Dict],
                      output: Any) -> None:
     for hook, hook_data in zip(self._hooks, input_hook_outputs):
         try:
             logger.debug(f"Hook '{hook.__class__.__name__}.on_after_node' "
                          f"running for node '{self._node_name}'.")
             hook.on_after_node(
                 node_name=self._node_name,
                 execution_context=self._execution_context,
                 config=self._component_config,
                 output=output,
                 input_hook_data=hook_data,
             )
         except Exception as e:
             raise GraphComponentException(
                 f"Error running after hook for node '{self._node_name}'."
             ) from e
Ejemplo n.º 3
0
    def _load_component(self, **kwargs: Any) -> None:
        logger.debug(
            f"Node {self._node_name} loading "
            f"{self._component_class.__name__}.{self._constructor_name} "
            f"with config: {self._component_config}, and kwargs: {kwargs}.")

        constructor = getattr(self._component_class, self._constructor_name)
        try:
            self._component: GraphComponent = constructor(  # type: ignore[no-redef]
                config=self._component_config,
                model_storage=self._model_storage,
                resource=self._get_resource(kwargs),
                execution_context=self._execution_context,
                **kwargs,
            )
        except Exception as e:
            raise GraphComponentException(
                f"Error initializing graph component for node {self._node_name}."
            ) from e
Ejemplo n.º 4
0
 def _run_before_hooks(self, received_inputs: Dict[Text,
                                                   Any]) -> List[Dict]:
     input_hook_outputs = []
     for hook in self._hooks:
         try:
             logger.debug(
                 f"Hook '{hook.__class__.__name__}.on_before_node' "
                 f"running for node '{self._node_name}'.")
             hook_output = hook.on_before_node(
                 node_name=self._node_name,
                 execution_context=self._execution_context,
                 config=self._component_config,
                 received_inputs=received_inputs,
             )
             input_hook_outputs.append(hook_output)
         except Exception as e:
             raise GraphComponentException(
                 f"Error running before hook for node '{self._node_name}'."
             ) from e
     return input_hook_outputs