def get_component_class(component_name: Text) -> Type["Component"]: """Resolve component name to a registered components class.""" if component_name not in registered_components: if component_name not in old_style_names: try: return class_from_module_path(component_name) except AttributeError: # when component_name is a path to a class but the path does not contain # that class module_name, _, class_name = component_name.rpartition(".") raise Exception("Failed to find class '{}' in module '{}'.\n" "".format(component_name, class_name, module_name)) except ImportError as e: # when component_name is a path to a class but that path is invalid or # when component_name is a class name and not part of old_style_names is_path = "." in component_name if is_path: module_name, _, _ = component_name.rpartition(".") exception_message = "Failed to find module '{}'. \n{}".format( module_name, e) else: exception_message = ( "Cannot find class '{}' from global namespace. " "Please check that there is no typo in the class " "name and that you have imported the class into the global " "namespace.".format(component_name)) raise ModuleNotFoundError(exception_message) else: # DEPRECATED ensures compatibility, remove in future versions logger.warning("DEPRECATION warning: your nlu config file " "contains old style component name `{}`, " "you should change it to its class name: `{}`." "".format(component_name, old_style_names[component_name])) component_name = old_style_names[component_name] return registered_components[component_name]
def _load_from_module_string( endpoint_config: EndpointConfig, domain: Domain ) -> "NaturalLanguageGenerator": """Initializes a custom natural language generator. Args: domain: defines the universe in which the assistant operates endpoint_config: the specific natural language generator """ try: nlg_class = common.class_from_module_path(endpoint_config.type) return nlg_class(endpoint_config=endpoint_config, domain=domain) except (AttributeError, ImportError) as e: raise Exception( f"Could not find a class based on the module path " f"'{endpoint_config.type}'. Failed to create a " f"`NaturalLanguageGenerator` instance. Error: {e}" )
def load_tracker_from_module_string( domain: Domain, store: EndpointConfig, event_broker: Optional[EventChannel] = None, ) -> "TrackerStore": """ Initializes a custom tracker. Args: domain: defines the universe in which the assistant operates store: the specific tracker store event_broker: an event broker to publish events Returns: custom_tracker: a tracker store from a specified database InMemoryTrackerStore: only used if no other tracker store is configured """ custom_tracker = None try: custom_tracker = common.class_from_module_path(store.type) except (AttributeError, ImportError): warnings.warn(f"Store type '{store.type}' not found. " f"Using InMemoryTrackerStore instead.") if custom_tracker: init_args = common.arguments_of(custom_tracker.__init__) if "url" in init_args and "host" not in init_args: warnings.warn( "The `url` initialization argument for custom tracker stores is deprecated. Your " "custom tracker store should take a `host` argument in ``__init__()`` instead.", FutureWarning, ) store.kwargs["url"] = store.url else: store.kwargs["host"] = store.url return custom_tracker( domain=domain, event_broker=event_broker, **store.kwargs, ) else: return InMemoryTrackerStore(domain)
def load(cls, path: Text) -> "PolicyEnsemble": """Loads policy and domain specification from storage""" metadata = cls.load_metadata(path) try: cls.ensure_model_compatibility(metadata) except UnsupportedDialogueModelError as e: logger.warning(e.message) return None policies = [] for i, policy_name in enumerate(metadata["policy_names"]): policy_cls = registry.policy_from_module_path(policy_name) dir_name = f"policy_{i}_{policy_cls.__name__}" policy_path = os.path.join(path, dir_name) policy = policy_cls.load(policy_path) cls._ensure_loaded_policy(policy, policy_cls, policy_name) policies.append(policy) ensemble_cls = class_from_module_path(metadata["ensemble_name"]) fingerprints = metadata.get("action_fingerprints", {}) ensemble = ensemble_cls(policies, fingerprints) return ensemble
def _load_from_module_string( domain: Domain, store: EndpointConfig, event_broker: Optional[EventBroker] = None ) -> "TrackerStore": """Initializes a custom tracker. Defaults to the InMemoryTrackerStore if the module path can not be found. Args: domain: defines the universe in which the assistant operates store: the specific tracker store event_broker: an event broker to publish events Returns: a tracker store from a specified type in a stores endpoint configuration """ try: tracker_store_class = class_from_module_path(store.type) init_args = arguments_of(tracker_store_class.__init__) if "url" in init_args and "host" not in init_args: raise_warning( "The `url` initialization argument for custom tracker stores is " "deprecated. Your custom tracker store should take a `host` " "argument in its `__init__()` instead.", DeprecationWarning, ) store.kwargs["url"] = store.url else: store.kwargs["host"] = store.url return tracker_store_class( domain=domain, event_broker=event_broker, **store.kwargs ) except (AttributeError, ImportError): raise_warning( f"Tracker store with type '{store.type}' not found. " f"Using `InMemoryTrackerStore` instead." ) return InMemoryTrackerStore(domain)