def local_action_factory(action_classes, action_names, utter_templates): # type: (List[Text], List[Text], List[Text]) -> List[Action] """Converts the names of actions into class instances.""" def _action_class(action_name): # type: (Text) -> Action """Tries to create an instance by importing and calling the class.""" try: cls = utils.class_from_module_path(action_name) return cls() except ImportError as e: raise ValueError( "Action '{}' doesn't correspond to a template / action. " "Remember to prefix actions that should utter a template " "with `utter_`. Error: {}".format(action_name, e)) except (AttributeError, KeyError) as e: raise ValueError( "Action '{}' doesn't correspond to a template / action. " "Module doesn't contain a class with this name. " "Remember to prefix actions that should utter a template " "with `utter_`. Error: {}".format(action_name, e)) actions = [] for name in action_classes: if name in utter_templates: actions.append(UtterAction(name)) else: actions.append(_action_class(name)) return actions
def ask_for_action(self, action_name, action_endpoint): if action_name not in self.agent.domain.action_names: logger.warning("action not found") return None defaults = {a.name(): a for a in action.default_actions()} if action_name in defaults and action_name not in self.agent.domain.user_actions: return defaults.get(action_name) elif action_name.startswith("utter_"): return UtterAction(action_name) else: return RemoteAction(action_name, action_endpoint)
def action_from_name(name: Text, action_endpoint: Optional[EndpointConfig], user_actions: List[Text]) -> 'Action': """Return an action instance for the name.""" defaults = {a.name(): a for a in default_actions()} if name in defaults and name not in user_actions: return defaults.get(name) elif name.startswith("utter_"): return UtterAction(name) elif name.startswith("line_"): return LineAction(name) elif name.startswith("custom_"): return get_custom_action(name) else: return RemoteAction(name, action_endpoint)
def local_action_factory(action_classes, action_names, utter_templates): # type: (List[Text], List[Text], List[Text]) -> List[Action] """Converts the names of actions into class instances.""" def _action_class(action_name): # type: (Text) -> Action """Tries to create an instance by importing and calling the class.""" try: cls = utils.class_from_module_path(action_name) return cls() except ImportError as e: if len(e.args) > 0: erx = re.compile("No module named '?(.*?)'?$") matched = erx.search(e.args[0]) if matched and matched.group(1) in action_name: # we only want to capture exceptions that are raised by the # class itself, not by other packages that fail to import raise ValueError( "Action '{}' doesn't correspond to a template / " "action. Remember to prefix actions that should " "utter a template with `utter_`. " "Error: {}".format(action_name, e)) else: # raises the original exception again raise except (AttributeError, KeyError) as e: raise ValueError( "Action '{}' doesn't correspond to a template / action. " "Module doesn't contain a class with this name. " "Remember to prefix actions that should utter a template " "with `utter_`. Error: {}".format(action_name, e)) actions = [] for name in action_classes: if name in utter_templates: actions.append(UtterAction(name)) else: actions.append(_action_class(name)) return actions
def snips_action_factory(action_classes, action_names, utter_templates): # type: (List[Text], List[Text], List[Text]) -> List[Action] """Converts the names of actions into class instances.""" print('SNIPSACTIONFACTORY') def _action_class(action_name): # type: (Text) -> Action """Tries to create an instance by importing and calling the class.""" try: cls = utils.class_from_module_path(action_name) return cls() except ImportError as e: raise ValueError( "Action '{}' doesn't correspond to a template / action. " "Remember to prefix actions that should utter a template " "with `utter_`. Error: {}".format(action_name, e)) except (AttributeError, KeyError) as e: raise ValueError( "Action '{}' doesn't correspond to a template / action. " "Module doesn't contain a class with this name. " "Remember to prefix actions that should utter a template " "with `utter_`. Error: {}".format(action_name, e)) actions = [] for name in action_classes: if name.startswith('ask_') or name.startswith('askslot_') or name.startswith('choose_') or name.startswith('capture_') or name.startswith('say_'): actions.append(SnipsMqttAction(name)) if not name.startswith('say_'): pass # actions.append(ActionListen()) elif name in utter_templates: #pass actions.append(UtterAction(name)) else: actions.append(_action_class(name)) print(actions) return actions
def local_action_factory(action_names, utter_templates): # type: (List[Text]) -> List[Action] """Converts the names of actions into class instances.""" from rasa_core.domain import Domain def _action_class(action_name): # type: (Text) -> Action """Tries to create an instance by importing and calling the class.""" try: cls = utils.class_from_module_path(action_name) return cls() except ImportError as e: raise ValueError( "Action '{}' doesn't correspond to a template / action. " "Remember to prefix actions that should utter a template " "with `utter_`. Error: {}".format(action_name, e)) except (AttributeError, KeyError) as e: raise ValueError( "Action '{}' doesn't correspond to a template / action. " "Module doesn't contain a class with this name. " "Remember to prefix actions that should utter a template " "with `utter_`. Error: {}".format(action_name, e)) default_actions = {a.name(): a for a in Domain.DEFAULT_ACTIONS} actions = [] for name in action_names: if name in default_actions: actions.append(default_actions[name]) elif name in utter_templates: actions.append(UtterAction(name)) else: actions.append(_action_class(name)) # TODO: double check that action names are unique? return actions
def test_text_format(): assert "{}".format(ActionListen()) == \ "Action('action_listen')" assert "{}".format(UtterAction("my_action_name")) == \ "UtterAction('my_action_name')"