diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 23bbd620c79..4c2cb6fd826 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -22,6 +22,7 @@ Added the intent right - Additional checks in PolicyEnsemble to ensure that custom Policy classes' load function returns the correct type +- Entries for tensorflow and sklearn versions to the policy metadata Removed ------- diff --git a/rasa_core/policies/ensemble.py b/rasa_core/policies/ensemble.py index eb9dc0317ed..469de7c1617 100644 --- a/rasa_core/policies/ensemble.py +++ b/rasa_core/policies/ensemble.py @@ -1,13 +1,14 @@ -import sys -from collections import defaultdict - +import importlib import json import logging -import numpy as np import os +import sys +from collections import defaultdict from datetime import datetime from typing import Text, Optional, Any, List, Dict, Tuple +import numpy as np + import rasa_core from rasa_core import utils, training, constants from rasa_core.actions.action import ACTION_LISTEN_NAME @@ -26,6 +27,8 @@ class PolicyEnsemble(object): + versioned_packages = ["rasa_core", "tensorflow", "sklearn"] + def __init__(self, policies: List[Policy], action_fingerprints: Optional[Dict] = None) -> None: @@ -97,12 +100,22 @@ def _create_action_fingerprints(training_events): action_fingerprints[k] = {"slots": slots} return action_fingerprints + def _add_package_version_info(self, metadata: Dict[Text, Any]) -> None: + """Adds version info for self.versioned_packages to metadata.""" + + for package_name in self.versioned_packages: + try: + p = importlib.import_module(package_name) + metadata[package_name] = p.__version__ + except ImportError: + pass + def _persist_metadata(self, path: Text, dump_flattened_stories: bool = False) -> None: """Persists the domain specification to storage.""" - # make sure the directory we persist to exists + # make sure the directory we persist exists domain_spec_path = os.path.join(path, 'policy_metadata.json') training_data_path = os.path.join(path, 'stories.md') utils.create_dir_for_file(domain_spec_path) @@ -117,7 +130,6 @@ def _persist_metadata(self, metadata = { "action_fingerprints": action_fingerprints, - "rasa_core": rasa_core.__version__, "python": ".".join([str(s) for s in sys.version_info[:3]]), "max_histories": self._max_histories(), "ensemble_name": self.__module__ + "." + self.__class__.__name__, @@ -125,6 +137,8 @@ def _persist_metadata(self, "trained_at": self.date_trained } + self._add_package_version_info(metadata) + utils.dump_obj_as_json_to_file(domain_spec_path, metadata) # if there are lots of stories, saving flattened stories takes a long @@ -132,7 +146,8 @@ def _persist_metadata(self, if dump_flattened_stories: training.persist_data(self.training_trackers, training_data_path) - def persist(self, path: Text, dump_flattened_stories: bool = False) -> None: + def persist(self, path: Text, + dump_flattened_stories: bool = False) -> None: """Persists the policy to storage.""" self._persist_metadata(path, dump_flattened_stories) @@ -173,13 +188,12 @@ def _ensure_loaded_policy(cls, policy, policy_cls, policy_name: Text): if policy is None: raise Exception( "Failed to load policy {}: " - "load returned None" - .format(policy_name)) + "load returned None".format(policy_name)) elif not isinstance(policy, policy_cls): raise Exception( "Failed to load policy {}: " "load returned object that is not instance of its own class" - .format(policy_name)) + "".format(policy_name)) @classmethod def load(cls, path: Text) -> 'PolicyEnsemble': diff --git a/rasa_core/utils.py b/rasa_core/utils.py index a4671c60b08..7de19c1dbb1 100644 --- a/rasa_core/utils.py +++ b/rasa_core/utils.py @@ -96,7 +96,7 @@ def class_from_module_path(module_path: Text) -> Any: def module_path_from_instance(inst: Any) -> Text: - """Return the module path of an instances class.""" + """Return the module path of an instance's class.""" return inst.__module__ + "." + inst.__class__.__name__