nemoguardrails.actions.action_dispatcher

View as Markdown

Module for the calling proper action endpoints based on events received at action server endpoint

Module Contents

Classes

NameDescription
ActionDispatcher-

Functions

NameDescription
is_action_fileHeuristics for determining if a Python file can have actions or not.

Data

log

API

class nemoguardrails.actions.action_dispatcher.ActionDispatcher(
load_all_actions: bool = True,
config_path: typing.Optional[str] = None,
import_paths: typing.Optional[typing.List[str]] = None
)
_registered_actions
Dict[str, Union[Type, Callable[..., Any]]] = {}
registered_actions

Gets the dictionary of registered actions. Returns: dict: A dictionary where keys are action names and values are callable action functions.

nemoguardrails.actions.action_dispatcher.ActionDispatcher._find_actions(
directory
) -> typing.Dict

Loop through all the subdirectories and check for the class with @action decorator and add in action_classes dict.

Parameters:

directory

The directory to search for actions.

Returns: Dict

Dictionary of found actions.

nemoguardrails.actions.action_dispatcher.ActionDispatcher._load_actions_from_module(
filepath: str
)
staticmethod

Loads the actions from the specified python module.

Parameters:

filepath
str

The path of the Python module.

Returns:

Dictionary of loaded actions.

nemoguardrails.actions.action_dispatcher.ActionDispatcher._normalize_action_name(
name: str
) -> str

Normalize the action name to the required format.

nemoguardrails.actions.action_dispatcher.ActionDispatcher.execute_action(
action_name: str,
params: typing.Dict[str, typing.Any]
) -> typing.Tuple[typing.Union[typing.Optional[str], typing.Dict[str, typing.Any]], str]
async

Execute a registered action.

Parameters:

action_name
str

The name of the action to execute.

params
Dict[str, Any]

Parameters for the action.

Returns: Tuple[Union[Optional[str], Dict[str, Any]], str]

Tuple[Union[str, Dict[str, Any]], str]: A tuple containing the result and status.

nemoguardrails.actions.action_dispatcher.ActionDispatcher.get_action(
name: str
) -> typing.Optional[typing.Callable]

Get the registered action by name.

Parameters:

name
str

The name of the action.

Returns: Optional[Callable]

The registered action.

nemoguardrails.actions.action_dispatcher.ActionDispatcher.get_registered_actions() -> typing.List[str]

Get the list of available actions.

Returns: List[str]

List[str]: List of available actions.

nemoguardrails.actions.action_dispatcher.ActionDispatcher.has_registered(
name: str
) -> bool

Check if an action is registered.

nemoguardrails.actions.action_dispatcher.ActionDispatcher.load_actions_from_path(
path: pathlib.Path
)

Loads all actions from the specified path.

This method loads all actions from the actions.py file if it exists and all actions inside the actions folder if it exists.

Parameters:

path
str

A string representing the path from which to load actions.

nemoguardrails.actions.action_dispatcher.ActionDispatcher.register_action(
action: typing.Callable,
name: typing.Optional[str] = None,
override: bool = True
)

Registers an action with the given name.

Parameters:

action
Callable

The action function.

name
Optional[str]Defaults to None

The name of the action. Defaults to None.

override
boolDefaults to True

If an action already exists, whether it should be overridden or not.

nemoguardrails.actions.action_dispatcher.ActionDispatcher.register_actions(
actions_obj: typing.Any,
override: bool = True
)

Registers all the actions from the given object.

Parameters:

actions_obj
any

The object containing actions.

override
boolDefaults to True

If an action already exists, whether it should be overridden or not.

nemoguardrails.actions.action_dispatcher.is_action_file(
filepath
)

Heuristics for determining if a Python file can have actions or not.

Currently, it only excludes the `init.py files.

nemoguardrails.actions.action_dispatcher.log = logging.getLogger(__name__)