Exemplo n.º 1
0
async def load_agent_on_start(
    model_path: Text,
    endpoints: AvailableEndpoints,
    remote_storage: Optional[Text],
    app: Sanic,
    loop: Text,
):
    """Load an agent.

    Used to be scheduled on server start
    (hence the `app` and `loop` arguments)."""
    from rasa.core import broker

    try:
        unpacked_model_context = get_model(model_path)
        if unpacked_model_context:
            with unpacked_model_context as unpacked_model:
                _, nlu_model = get_model_subdirectories(unpacked_model)
                _interpreter = NaturalLanguageInterpreter.create(
                    nlu_model, endpoints.nlu
                )
        else:
            raise RuntimeError("No model found at '{}'.".format(model_path))

    except Exception:
        logger.debug("Could not load interpreter from '{}'.".format(model_path))
        _interpreter = None

    _broker = broker.from_endpoint_config(endpoints.event_broker)
    _tracker_store = TrackerStore.find_tracker_store(
        None, endpoints.tracker_store, _broker
    )

    model_server = endpoints.model if endpoints and endpoints.model else None

    app.agent = await load_agent(
        model_path,
        model_server=model_server,
        remote_storage=remote_storage,
        interpreter=_interpreter,
        generator=endpoints.nlg,
        tracker_store=_tracker_store,
        action_endpoint=endpoints.action,
    )

    if not app.agent:
        logger.warning(
            "Agent could not be loaded with the provided configuration. "
            "Load default agent without any model."
        )
        app.agent = Agent(
            interpreter=_interpreter,
            generator=endpoints.nlg,
            tracker_store=_tracker_store,
            action_endpoint=endpoints.action,
            model_server=model_server,
            remote_storage=remote_storage,
        )

    return app.agent
Exemplo n.º 2
0
    def update_model(
        self,
        domain: Optional[Domain],
        policy_ensemble: Optional[PolicyEnsemble],
        fingerprint: Optional[Text],
        interpreters: Optional[Dict[str, NaturalLanguageInterpreter]] = None,
        model_directory: Optional[Text] = None,
    ) -> None:
        self.domain = self._create_domain(domain)
        self.policy_ensemble = policy_ensemble

        if interpreters:
            self.interpreters = {}
            for lang in interpreters.keys():
                self.interpreters[lang] = NaturalLanguageInterpreter.create(
                    interpreters[lang])

        self._set_fingerprint(fingerprint)

        # update domain on all instances
        self.tracker_store.domain = domain
        if hasattr(self.nlg, "templates"):
            self.nlg.templates = domain.templates if domain else {}

        self.model_directory = model_directory
Exemplo n.º 3
0
async def load_agent_on_start(
    model_path: Text,
    endpoints: AvailableEndpoints,
    remote_storage: Optional[Text],
    app: Sanic,
    loop: Text,
):
    """Load an agent.

    Used to be scheduled on server start
    (hence the `app` and `loop` arguments)."""
    import rasa.core.brokers.utils as broker_utils

    # noinspection PyBroadException
    # bf mod
    try:
        with model.get_model(model_path) as unpacked_model:
            _, nlu_models = model.get_model_subdirectories(unpacked_model)
            _interpreters = {}
            for lang, nlu_model_path in nlu_models.items():
                _interpreters[lang] = NaturalLanguageInterpreter.create(
                    nlu_model_path, endpoints.nlu)
    except Exception:
        logger.debug(f"Could not load interpreter from '{model_path}'.")
        _interpreters = {}
    # /bf mod

    _broker = broker_utils.from_endpoint_config(endpoints.event_broker)
    _tracker_store = TrackerStore.find_tracker_store(None,
                                                     endpoints.tracker_store,
                                                     _broker)
    _lock_store = LockStore.find_lock_store(endpoints.lock_store)

    model_server = endpoints.model if endpoints and endpoints.model else None

    app.agent = await agent.load_agent(
        model_path,
        model_server=model_server,
        remote_storage=remote_storage,
        interpreters=_interpreters,
        generator=endpoints.nlg,
        tracker_store=_tracker_store,
        lock_store=_lock_store,
        action_endpoint=endpoints.action,
    )

    if not app.agent:
        warnings.warn(
            "Agent could not be loaded with the provided configuration. "
            "Load default agent without any model.")
        app.agent = Agent(
            interpreters=_interpreters,
            generator=endpoints.nlg,
            tracker_store=_tracker_store,
            action_endpoint=endpoints.action,
            model_server=model_server,
            remote_storage=remote_storage,
        )

    return app.agent
Exemplo n.º 4
0
async def load_agent_on_start(
    model_path: Text,
    endpoints: AvailableEndpoints,
    remote_storage: Optional[Text],
    app: Sanic,
    loop: AbstractEventLoop,
):
    """Load an agent.

    Used to be scheduled on server start
    (hence the `app` and `loop` arguments)."""

    # noinspection PyBroadException
    try:
        with model.get_model(model_path) as unpacked_model:
            _, nlu_model = model.get_model_subdirectories(unpacked_model)
            _interpreter = NaturalLanguageInterpreter.create(endpoints.nlu or nlu_model)
    except Exception:
        logger.debug(f"Could not load interpreter from '{model_path}'.")
        _interpreter = None

    _broker = EventBroker.create(endpoints.event_broker)
    _tracker_store = TrackerStore.create(endpoints.tracker_store, event_broker=_broker)
    _lock_store = LockStore.create(endpoints.lock_store)

    model_server = endpoints.model if endpoints and endpoints.model else None

    try:
        app.agent = await agent.load_agent(
            model_path,
            model_server=model_server,
            remote_storage=remote_storage,
            interpreter=_interpreter,
            generator=endpoints.nlg,
            tracker_store=_tracker_store,
            lock_store=_lock_store,
            action_endpoint=endpoints.action,
        )
    except Exception as e:
        rasa.shared.utils.io.raise_warning(
            f"The model at '{model_path}' could not be loaded. " f"Error: {e}"
        )
        app.agent = None

    if not app.agent:
        rasa.shared.utils.io.raise_warning(
            "Agent could not be loaded with the provided configuration. "
            "Load default agent without any model."
        )
        app.agent = Agent(
            interpreter=_interpreter,
            generator=endpoints.nlg,
            tracker_store=_tracker_store,
            action_endpoint=endpoints.action,
            model_server=model_server,
            remote_storage=remote_storage,
        )

    logger.info("Rasa server is up and running.")
    return app.agent
Exemplo n.º 5
0
def _load_and_set_updated_model(agent: "Agent", model_directory: Text,
                                fingerprint: Text) -> None:
    """Load the persisted model into memory and set the model on the agent.

    Args:
        agent: Instance of `Agent` to update with the new model.
        model_directory: Rasa model directory.
        fingerprint: Fingerprint of the supplied model at `model_directory`.
    """
    logger.debug(f"Found new model with fingerprint {fingerprint}. Loading...")

    core_path, nlu_path = get_model_subdirectories(model_directory)

    try:
        # interpreter = _load_interpreter(agent, nlu_path)
        interpreter = NaturalLanguageInterpreter.create(nlu_path)  # bf
        domain, policy_ensemble = _load_domain_and_policy_ensemble(core_path)

        agent.update_model(domain, policy_ensemble, fingerprint, interpreter,
                           model_directory)

        logger.debug("Finished updating agent to new model.")
    except Exception as e:
        logger.exception(
            f"Failed to update model. The previous model will stay loaded instead. "
            f"Error: {e}")
Exemplo n.º 6
0
async def load_agent_on_start(core_model, endpoints, nlu_model, app, loop):
    """Load an agent.

    Used to be scheduled on server start
    (hence the `app` and `loop` arguments)."""
    from rasa.core import broker
    from rasa.core.agent import Agent

    _interpreter = NaturalLanguageInterpreter.create(nlu_model, endpoints.nlu)
    _broker = broker.from_endpoint_config(endpoints.event_broker)

    _tracker_store = TrackerStore.find_tracker_store(None,
                                                     endpoints.tracker_store,
                                                     _broker)

    if endpoints and endpoints.model:
        from rasa.core import agent

        app.agent = Agent(interpreter=_interpreter,
                          generator=endpoints.nlg,
                          tracker_store=_tracker_store,
                          action_endpoint=endpoints.action)

        await agent.load_from_server(app.agent, model_server=endpoints.model)
    else:
        app.agent = Agent.load(core_model,
                               interpreter=_interpreter,
                               generator=endpoints.nlg,
                               tracker_store=_tracker_store,
                               action_endpoint=endpoints.action)

    return app.agent
Exemplo n.º 7
0
    def __init__(
            self,
            domain: Union[Text, Domain] = None,
            policies: Union[PolicyEnsemble, List[Policy], None] = None,
            interpreter: Optional[NaturalLanguageInterpreter] = None,
            generator: Union[EndpointConfig, 'NLG', None] = None,
            tracker_store: Optional['TrackerStore'] = None,
            action_endpoint: Optional[EndpointConfig] = None,
            fingerprint: Optional[Text] = None
    ):
        # Initializing variables with the passed parameters.
        self.domain = self._create_domain(domain)
        if self.domain:
            self.domain.add_requested_slot()
        self.policy_ensemble = self._create_ensemble(policies)
        if not self._is_form_policy_present():
            raise InvalidDomain(
                "You have defined a form action, but haven't added the "
                "FormPolicy to your policy ensemble."
            )

        self.interpreter = NaturalLanguageInterpreter.create(interpreter)

        self.nlg = NaturalLanguageGenerator.create(generator, self.domain)
        self.tracker_store = self.create_tracker_store(
            tracker_store, self.domain)
        self.action_endpoint = action_endpoint
        self.conversations_in_processing = {}

        self._set_fingerprint(fingerprint)
Exemplo n.º 8
0
def _load_interpreter(
    interpreter_path: Optional[Text],
) -> Optional[NaturalLanguageInterpreter]:
    if interpreter_path:
        return NaturalLanguageInterpreter.create(interpreter_path)

    return None
Exemplo n.º 9
0
def main():
    from rasa.core.agent import Agent
    from rasa.core.interpreter import NaturalLanguageInterpreter
    from rasa.core.utils import AvailableEndpoints, set_default_subparser
    import rasa.nlu.utils as nlu_utils
    import rasa.core.cli
    from rasa.core import utils

    loop = asyncio.get_event_loop()

    # Running as standalone python application
    arg_parser = create_argument_parser()
    set_default_subparser(arg_parser, "default")
    cmdline_arguments = arg_parser.parse_args()

    logging.basicConfig(level=cmdline_arguments.loglevel)
    _endpoints = AvailableEndpoints.read_endpoints(cmdline_arguments.endpoints)

    if cmdline_arguments.output:
        nlu_utils.create_dir(cmdline_arguments.output)

    if not cmdline_arguments.core:
        raise ValueError(
            "you must provide a core model directory to evaluate using -d / --core"
        )
    if cmdline_arguments.mode == "default":

        _interpreter = NaturalLanguageInterpreter.create(
            cmdline_arguments.nlu, _endpoints.nlu
        )

        _agent = Agent.load(cmdline_arguments.core, interpreter=_interpreter)

        stories = loop.run_until_complete(
            rasa.core.cli.train.stories_from_cli_args(cmdline_arguments)
        )

        loop.run_until_complete(
            test(
                stories,
                _agent,
                cmdline_arguments.max_stories,
                cmdline_arguments.output,
                cmdline_arguments.fail_on_prediction_errors,
                cmdline_arguments.e2e,
            )
        )

    elif cmdline_arguments.mode == "compare":
        compare(
            cmdline_arguments.core, cmdline_arguments.stories, cmdline_arguments.output
        )

        story_n_path = os.path.join(cmdline_arguments.core, "num_stories.json")

        number_of_stories = utils.read_json_file(story_n_path)
        plot_curve(cmdline_arguments.output, number_of_stories)

    logger.info("Finished evaluation")
Exemplo n.º 10
0
def test_create_interpreter(parameters, trained_nlu_model):
    obj = parameters["obj"]
    if obj == "trained_nlu_model":
        _, obj = get_model_subdirectories(get_model(trained_nlu_model))

    interpreter = NaturalLanguageInterpreter.create(obj, parameters["endpoint"])

    assert isinstance(interpreter, parameters["type"])
Exemplo n.º 11
0
def test_core(
    model: Optional[Text] = None,
    stories: Optional[Text] = None,
    endpoints: Optional[Text] = None,
    output: Text = DEFAULT_RESULTS_PATH,
    kwargs: Optional[Dict] = None,
):
    import rasa.core.test
    import rasa.core.utils as core_utils
    import rasa.model
    from rasa.core.interpreter import RegexInterpreter, NaturalLanguageInterpreter
    from rasa.core.agent import Agent

    _endpoints = core_utils.AvailableEndpoints.read_endpoints(endpoints)

    if kwargs is None:
        kwargs = {}

    if output:
        io_utils.create_directory(output)

    try:
        unpacked_model = rasa.model.get_model(model)
    except ModelNotFound:
        print_error(
            "Unable to test: could not find a model. Use 'rasa train' to train a "
            "Rasa model and provide it via the '--model' argument."
        )
        return

    core_path, nlu_path = rasa.model.get_model_subdirectories(unpacked_model)

    if not core_path:
        print_error(
            "Unable to test: could not find a Core model. Use 'rasa train' to train a "
            "Rasa model and provide it via the '--model' argument."
        )

    use_e2e = kwargs["e2e"] if "e2e" in kwargs else False

    _interpreter = RegexInterpreter()
    if use_e2e:
        if nlu_path:
            _interpreter = NaturalLanguageInterpreter.create(_endpoints.nlu or nlu_path)
        else:
            print_warning(
                "No NLU model found. Using default 'RegexInterpreter' for end-to-end "
                "evaluation."
            )

    _agent = Agent.load(unpacked_model, interpreter=_interpreter)

    kwargs = utils.minimal_kwargs(kwargs, rasa.core.test, ["stories", "agent"])

    loop = asyncio.get_event_loop()
    loop.run_until_complete(
        rasa.core.test(stories, _agent, out_directory=output, **kwargs)
    )
Exemplo n.º 12
0
def test_core(
    model: Optional[Text] = None,
    stories: Optional[Text] = None,
    endpoints: Optional[Text] = None,
    output: Text = DEFAULT_RESULTS_PATH,
    kwargs: Optional[Dict] = None,
):
    import rasa.core.test
    import rasa.core.utils as core_utils
    from rasa.nlu import utils as nlu_utils
    from rasa.model import get_model
    from rasa.core.interpreter import NaturalLanguageInterpreter
    from rasa.core.agent import Agent

    _endpoints = core_utils.AvailableEndpoints.read_endpoints(endpoints)

    if kwargs is None:
        kwargs = {}

    if output:
        nlu_utils.create_dir(output)

    unpacked_model = get_model(model)
    if unpacked_model is None:
        print_error(
            "Unable to test: could not find a model. Use 'rasa train' to train a "
            "Rasa model."
        )
        return

    core_path, nlu_path = get_model_subdirectories(unpacked_model)

    if not os.path.exists(core_path):
        print_error(
            "Unable to test: could not find a Core model. Use 'rasa train' to "
            "train a model."
        )

    use_e2e = kwargs["e2e"] if "e2e" in kwargs else False

    _interpreter = RegexInterpreter()
    if use_e2e:
        if os.path.exists(nlu_path):
            _interpreter = NaturalLanguageInterpreter.create(nlu_path, _endpoints.nlu)
        else:
            print_warning(
                "No NLU model found. Using default 'RegexInterpreter' for end-to-end "
                "evaluation."
            )

    _agent = Agent.load(unpacked_model, interpreter=_interpreter)

    kwargs = minimal_kwargs(kwargs, rasa.core.test, ["stories", "agent"])

    loop = asyncio.get_event_loop()
    loop.run_until_complete(
        rasa.core.test(stories, _agent, out_directory=output, **kwargs)
    )
Exemplo n.º 13
0
    def load(
        cls,
        model_path: Text,
        interpreter: Optional[NaturalLanguageInterpreter] = None,
        generator: Union[EndpointConfig, NaturalLanguageGenerator] = None,
        tracker_store: Optional[TrackerStore] = None,
        lock_store: Optional[LockStore] = None,
        action_endpoint: Optional[EndpointConfig] = None,
        model_server: Optional[EndpointConfig] = None,
        remote_storage: Optional[Text] = None,
        path_to_model_archive: Optional[Text] = None,
    ) -> "Agent":
        """Load a persisted model from the passed path."""
        try:
            if not model_path:
                raise ModelNotFound("No path specified.")
            elif not os.path.exists(model_path):
                raise ModelNotFound(f"No file or directory at '{model_path}'.")
            elif os.path.isfile(model_path):
                model_path = get_model(model_path)
        except ModelNotFound:
            raise ValueError(
                "You are trying to load a MODEL from '{}', which is not possible. \n"
                "The model path should be a 'tar.gz' file or a directory "
                "containing the various model files in the sub-directories 'core' "
                "and 'nlu'. \n\nIf you want to load training data instead of "
                "a model, use `agent.load_data(...)` instead.".format(
                    model_path))

        core_model, nlu_model = get_model_subdirectories(model_path)

        if not interpreter and nlu_model:
            interpreter = NaturalLanguageInterpreter.create(nlu_model)

        domain = None
        ensemble = None

        if core_model:
            domain = Domain.load(os.path.join(core_model, DEFAULT_DOMAIN_PATH))
            ensemble = PolicyEnsemble.load(core_model) if core_model else None

            # ensures the domain hasn't changed between test and train
            domain.compare_with_specification(core_model)

        return cls(
            domain=domain,
            policies=ensemble,
            interpreter=interpreter,
            generator=generator,
            tracker_store=tracker_store,
            lock_store=lock_store,
            action_endpoint=action_endpoint,
            model_directory=model_path,
            model_server=model_server,
            remote_storage=remote_storage,
            path_to_model_archive=path_to_model_archive,
        )
Exemplo n.º 14
0
def test_core(
    model: Optional[Text] = None,
    stories: Optional[Text] = None,
    endpoints: Optional[Text] = None,
    output: Text = DEFAULT_RESULTS_PATH,
    model_path: Optional[Text] = None,
    kwargs: Optional[Dict] = None,
):
    import rasa.core.test
    import rasa.core.utils as core_utils
    from rasa.nlu import utils as nlu_utils
    from rasa.model import get_model
    from rasa.core.interpreter import NaturalLanguageInterpreter
    from rasa.core.agent import Agent

    _endpoints = core_utils.AvailableEndpoints.read_endpoints(endpoints)

    if kwargs is None:
        kwargs = {}

    if output:
        nlu_utils.create_dir(output)

    if os.path.isfile(model):
        model_path = get_model(model)

    if model_path:
        # Single model: Normal evaluation
        loop = asyncio.get_event_loop()
        model_path = get_model(model)
        core_path, nlu_path = get_model_subdirectories(model_path)

        if os.path.exists(core_path) and os.path.exists(nlu_path):
            _interpreter = NaturalLanguageInterpreter.create(nlu_path, _endpoints.nlu)

            _agent = Agent.load(core_path, interpreter=_interpreter)

            kwargs = minimal_kwargs(kwargs, rasa.core.test, ["stories", "agent"])

            loop.run_until_complete(
                rasa.core.test(stories, _agent, out_directory=output, **kwargs)
            )
        else:
            logger.warning(
                "Not able to test. Make sure both models, core and "
                "nlu, are available."
            )

    else:
        from rasa.core.test import compare, plot_curve

        compare(model, stories, output)

        story_n_path = os.path.join(model, "num_stories.json")

        number_of_stories = core_utils.read_json_file(story_n_path)
        plot_curve(output, number_of_stories)
Exemplo n.º 15
0
def _interpreter_from_previous_model(
    old_model_zip_path: Optional[Text],
) -> Optional[NaturalLanguageInterpreter]:
    if not old_model_zip_path:
        return None

    with model.unpack_model(old_model_zip_path) as unpacked:
        _, old_nlu = model.get_model_subdirectories(unpacked)
        return NaturalLanguageInterpreter.create(old_nlu)
Exemplo n.º 16
0
 def __init__(
     self,
     model_directory: Dict[Text, Optional[Text]],
     config_file: Optional[Text] = None,
     lazy_init: bool = False,
 ):
     self.lazy_init = lazy_init
     self.config_file = config_file
     self.interpreters = {
         lang: NaturalLanguageInterpreter.create(model_path)
         for lang, model_path in model_directory.items()
     }
Exemplo n.º 17
0
async def train_agent_on_start(args, endpoints, additional_arguments, app,
                               loop):

    _interpreter = NaturalLanguageInterpreter.create(args.get("nlu"),
                                                     endpoints.nlu)

    model_directory = args.get("out", tempfile.mkdtemp(suffix="_core_model"))

    _agent = await train(args.get("domain"), args.get("stories"),
                         model_directory, _interpreter, endpoints,
                         args.get("dump_stories"),
                         args.get("config")[0], None, additional_arguments)
    app.agent = _agent
Exemplo n.º 18
0
    def load(
        cls,
        unpacked_model_path: Text,
        interpreter: Optional[NaturalLanguageInterpreter] = None,
        generator: Union[EndpointConfig, NaturalLanguageGenerator] = None,
        tracker_store: Optional[TrackerStore] = None,
        action_endpoint: Optional[EndpointConfig] = None,
        model_server: Optional[EndpointConfig] = None,
        remote_storage: Optional[Text] = None,
    ) -> "Agent":
        """Load a persisted model from the passed path."""
        if not os.path.exists(unpacked_model_path) or not os.path.isdir(
            unpacked_model_path
        ):
            raise ValueError(
                "You are trying to load a MODEL from "
                "('{}'), which is not possible. \n"
                "The persisted path should be a directory "
                "containing the various model files in the "
                "sub-directories 'core' and 'nlu'. \n\n"
                "If you want to load training data instead of "
                "a model, use `agent.load_data(...)` "
                "instead.".format(unpacked_model_path)
            )

        core_model, nlu_model = get_model_subdirectories(unpacked_model_path)

        if not interpreter and os.path.exists(nlu_model):
            interpreter = NaturalLanguageInterpreter.create(nlu_model)

        domain = None
        ensemble = None

        if os.path.exists(core_model):
            domain = Domain.load(os.path.join(core_model, DEFAULT_DOMAIN_PATH))
            ensemble = PolicyEnsemble.load(core_model) if core_model else None

            # ensures the domain hasn't changed between test and train
            domain.compare_with_specification(core_model)

        return cls(
            domain=domain,
            policies=ensemble,
            interpreter=interpreter,
            generator=generator,
            tracker_store=tracker_store,
            action_endpoint=action_endpoint,
            model_directory=unpacked_model_path,
            model_server=model_server,
            remote_storage=remote_storage,
        )
Exemplo n.º 19
0
def _load_interpreter(agent: "Agent",
                      nlu_path: Optional[Text]) -> NaturalLanguageInterpreter:
    """Load the NLU interpreter at `nlu_path`.

    Args:
        agent: Instance of `Agent` to inspect for an interpreter if `nlu_path` is
            `None`.
        nlu_path: NLU model path.

    Returns:
        The NLU interpreter.
    """
    if nlu_path:
        return NaturalLanguageInterpreter.create(nlu_path)

    return agent.interpreter or RegexInterpreter()
Exemplo n.º 20
0
def _load_and_set_updated_model(agent: "Agent", model_directory: Text,
                                fingerprint: Text):
    """Load the persisted model into memory and set the model on the agent."""

    logger.debug(
        "Found new model with fingerprint {}. Loading...".format(fingerprint))

    # bf mod
    core_path, nlu_models = get_model_subdirectories(model_directory)

    interpreters = {}
    # If NLU models exist then create interpreters from them
    if len(nlu_models):
        for lang, model_path in nlu_models.items():
            interpreters[lang] = NaturalLanguageInterpreter.create(
                os.path.join(model_directory, model_path))
    # If no NLU models exist, then associate a RegexInterpreter to the language code found in the fingerprints,
    # that should correspond to the default language in Botfront. This is to make sure an interpreter is available
    # when training stories without NLU
    else:
        from rasa.model import fingerprint_from_path, FINGERPRINT_CONFIG_NLU_KEY
        models_fingerprint = fingerprint_from_path(model_directory)
        if len(models_fingerprint.get(FINGERPRINT_CONFIG_NLU_KEY).keys()):
            interpreters = {
                list(
                    models_fingerprint.get(FINGERPRINT_CONFIG_NLU_KEY).keys())[0]:
                RegexInterpreter()
            }
    # /bf mod

    domain = None
    if core_path:
        domain_path = os.path.join(os.path.abspath(core_path),
                                   DEFAULT_DOMAIN_PATH)
        domain = Domain.load(domain_path)

    try:
        policy_ensemble = None
        if core_path:
            policy_ensemble = PolicyEnsemble.load(core_path)
        agent.update_model(domain, policy_ensemble, fingerprint, interpreters,
                           model_directory)
        logger.debug("Finished updating agent to new model.")
    except Exception:
        logger.exception("Failed to load policy and update agent. "
                         "The previous model will stay loaded instead.")
Exemplo n.º 21
0
def test_core(model: Text,
              stories: Text,
              endpoints: Text = None,
              output: Text = DEFAULT_RESULTS_PATH,
              model_path: Text = None,
              **kwargs: Dict):
    import rasa.core.test
    import rasa.core.utils as core_utils
    from rasa_nlu import utils as nlu_utils
    from rasa.model import get_model
    from rasa.core.interpreter import NaturalLanguageInterpreter
    from rasa.core.agent import Agent

    _endpoints = core_utils.AvailableEndpoints.read_endpoints(endpoints)

    if output:
        nlu_utils.create_dir(output)

    if os.path.isfile(model):
        model_path = get_model(model)

    if model_path:
        # Single model: Normal evaluation
        loop = asyncio.get_event_loop()
        model_path = get_model(model)
        core_path, nlu_path = get_model_subdirectories(model_path)

        _interpreter = NaturalLanguageInterpreter.create(
            nlu_path, _endpoints.nlu)

        _agent = Agent.load(core_path, interpreter=_interpreter)

        kwargs = minimal_kwargs(kwargs, rasa.core.test)
        loop.run_until_complete(
            rasa.core.test(stories, _agent, out_directory=output, **kwargs))

    else:
        from rasa.core.test import compare, plot_curve

        compare(model, stories, output)

        story_n_path = os.path.join(model, 'num_stories.json')

        number_of_stories = core_utils.read_json_file(story_n_path)
        plot_curve(output, number_of_stories)
Exemplo n.º 22
0
    def update_model(self,
                     domain: Union[Text, Domain],
                     policy_ensemble: PolicyEnsemble,
                     fingerprint: Optional[Text],
                     interpreter: Optional[NaturalLanguageInterpreter] = None
                     ) -> None:
        self.domain = domain
        self.policy_ensemble = policy_ensemble

        if interpreter:
            self.interpreter = NaturalLanguageInterpreter.create(interpreter)

        self._set_fingerprint(fingerprint)

        # update domain on all instances
        self.tracker_store.domain = domain
        if hasattr(self.nlg, "templates"):
            self.nlg.templates = domain.templates or []
Exemplo n.º 23
0
    def __init__(
        self,
        domain: Union[Text, Domain, None] = None,
        policies: Union[PolicyEnsemble, List[Policy], None] = None,
        interpreter: Optional[NaturalLanguageInterpreter] = None,
        generator: Union[EndpointConfig, NaturalLanguageGenerator, None] = None,
        tracker_store: Optional[TrackerStore] = None,
        lock_store: Optional[LockStore] = None,
        action_endpoint: Optional[EndpointConfig] = None,
        fingerprint: Optional[Text] = None,
        model_directory: Optional[Text] = None,
        model_server: Optional[EndpointConfig] = None,
        remote_storage: Optional[Text] = None,
        path_to_model_archive: Optional[Text] = None,
    ):
        # Initializing variables with the passed parameters.
        self.domain = self._create_domain(domain)
        self.policy_ensemble = self._create_ensemble(policies)

        if self.domain is not None:
            self.domain.add_requested_slot()
            self.domain.add_form_id()
            self.domain.add_reset_slot()
            self.domain.add_knowledge_base_slots()
            self.domain.add_categorical_slot_default_value()

        PolicyEnsemble.check_domain_ensemble_compatibility(
            self.policy_ensemble, self.domain
        )

        self.interpreter = NaturalLanguageInterpreter.create(interpreter)

        self.nlg = NaturalLanguageGenerator.create(generator, self.domain)
        self.tracker_store = self.create_tracker_store(tracker_store, self.domain)
        self.lock_store = self._create_lock_store(lock_store)
        self.action_endpoint = action_endpoint

        self._set_fingerprint(fingerprint)
        self.model_directory = model_directory
        self.model_server = model_server
        self.remote_storage = remote_storage
        self.path_to_model_archive = path_to_model_archive
Exemplo n.º 24
0
def test_core(
    model: Optional[Text] = None,
    stories: Optional[Text] = None,
    endpoints: Optional[Text] = None,
    output: Text = DEFAULT_RESULTS_PATH,
    kwargs: Optional[Dict] = None,
):
    import rasa.core.test
    import rasa.core.utils as core_utils
    from rasa.nlu import utils as nlu_utils
    from rasa.model import get_model
    from rasa.core.interpreter import NaturalLanguageInterpreter
    from rasa.core.agent import Agent

    _endpoints = core_utils.AvailableEndpoints.read_endpoints(endpoints)

    if kwargs is None:
        kwargs = {}

    if output:
        nlu_utils.create_dir(output)

    loop = asyncio.get_event_loop()
    model_path = get_model(model)
    core_path, nlu_path = get_model_subdirectories(model_path)

    if os.path.exists(core_path) and os.path.exists(nlu_path):
        _interpreter = NaturalLanguageInterpreter.create(
            nlu_path, _endpoints.nlu)

        _agent = Agent.load(model_path, interpreter=_interpreter)

        kwargs = minimal_kwargs(kwargs, rasa.core.test, ["stories", "agent"])

        loop.run_until_complete(
            rasa.core.test(stories, _agent, out_directory=output, **kwargs))
    else:
        print_error(
            "Not able to test. Make sure both models - core and nlu - are available."
        )
Exemplo n.º 25
0
Arquivo: agent.py Projeto: delldu/Rasa
    def __init__(
        self,
        domain: Union[Text, Domain, None] = None,
        policies: Union[PolicyEnsemble, List[Policy], None] = None,
        interpreter: Optional[NaturalLanguageInterpreter] = None,
        generator: Union[EndpointConfig, NaturalLanguageGenerator,
                         None] = None,
        tracker_store: Optional[TrackerStore] = None,
        action_endpoint: Optional[EndpointConfig] = None,
        fingerprint: Optional[Text] = None,
        model_directory: Optional[Text] = None,
        model_server: Optional[EndpointConfig] = None,
        remote_storage: Optional[Text] = None,
    ):
        # Initializing variables with the passed parameters.
        self.domain = self._create_domain(domain)
        self.policy_ensemble = self._create_ensemble(policies)

        if self.domain is not None:
            self.domain.add_requested_slot()

        PolicyEnsemble.check_domain_ensemble_compatibility(
            self.policy_ensemble, self.domain)

        self.interpreter = NaturalLanguageInterpreter.create(interpreter)

        self.nlg = NaturalLanguageGenerator.create(generator, self.domain)
        self.tracker_store = self.create_tracker_store(tracker_store,
                                                       self.domain)
        self.action_endpoint = action_endpoint
        self.conversations_in_processing = {}

        self._set_fingerprint(fingerprint)
        self.model_directory = model_directory
        self.model_server = model_server
        self.remote_storage = remote_storage
Exemplo n.º 26
0
from rasa.core.channels.socketio import SocketIOInput
from rasa.core.agent import Agent
from rasa.core.interpreter import NaturalLanguageInterpreter

# load your trained agent
interpreter = NaturalLanguageInterpreter.create(
    'C:/Users/NJJainnew_ex/models/20190805-123059/nlu')
agent = Agent.load('C:/Users/NJJain/new_ex/models/20190805-123059',
                   interpreter=interpreter)

input_channel = SocketIOInput(
    # event name for messages sent from the user
    user_message_evt="user_uttered",
    # event name for messages sent from the bot
    bot_message_evt="bot_uttered",
    # socket.io namespace to use for the messages
    namespace=None)

# set serve_forever=False if you want to keep the server running
s = agent.handle_channels([input_channel], 5005)
Exemplo n.º 27
0
    def load(
        cls,
        model_path: Text,
        interpreter: Optional[Dict[Text, NaturalLanguageInterpreter]] = None,
        generator: Union[EndpointConfig, NaturalLanguageGenerator] = None,
        tracker_store: Optional[TrackerStore] = None,
        lock_store: Optional[LockStore] = None,
        action_endpoint: Optional[EndpointConfig] = None,
        model_server: Optional[EndpointConfig] = None,
        remote_storage: Optional[Text] = None,
        path_to_model_archive: Optional[Text] = None,
    ) -> "Agent":
        """Load a persisted model from the passed path."""
        try:
            if not model_path:
                raise ModelNotFound("No path specified.")
            elif not os.path.exists(model_path):
                raise ModelNotFound(f"No file or directory at '{model_path}'.")
            elif os.path.isfile(model_path):
                model_path = get_model(model_path)
        except ModelNotFound:
            raise ValueError(
                "You are trying to load a MODEL from '{}', which is not possible. \n"
                "The model path should be a 'tar.gz' file or a directory "
                "containing the various model files in the sub-directories 'core' "
                "and 'nlu'. \n\nIf you want to load training data instead of "
                "a model, use `agent.load_data(...)` instead.".format(model_path)
            )

        core_model, nlu_models = get_model_subdirectories(model_path)
        if nlu_models:
            if not interpreter:
                interpreter = {}
                for lang, model_path in nlu_models.items():
                    interpreter[lang] = NaturalLanguageInterpreter.create(os.path.join(model_path, model_path))
        else:
            from rasa.model import fingerprint_from_path, FINGERPRINT_CONFIG_NLU_KEY
            fingerprint = fingerprint_from_path(model_path)
            if len(fingerprint.get(FINGERPRINT_CONFIG_NLU_KEY).keys()):
                interpreter = {list(fingerprint.get(FINGERPRINT_CONFIG_NLU_KEY).keys())[0]: RegexInterpreter()}

        domain = None
        ensemble = None

        if core_model:
            domain = Domain.load(os.path.join(core_model, DEFAULT_DOMAIN_PATH))
            ensemble = PolicyEnsemble.load(core_model) if core_model else None

            # ensures the domain hasn't changed between test and train
            try:
                domain.compare_with_specification(core_model)
            except rasa.core.domain.InvalidDomain as e:
                logger.warning(e.message)
                domain = None

        return cls(
            domain=domain,
            policies=ensemble,
            interpreter=interpreter,
            generator=generator,
            tracker_store=tracker_store,
            lock_store=lock_store,
            action_endpoint=action_endpoint,
            model_directory=model_path,
            model_server=model_server,
            remote_storage=remote_storage,
            path_to_model_archive=path_to_model_archive,
        )
Exemplo n.º 28
0
# ---------------------

# In[37]:

from rasa.core.agent import Agent
from rasa.core.interpreter import NaturalLanguageInterpreter
from rasa.core.utils import EndpointConfig

# In[38]:

model_directory = os.path.join(model_dir, "current")

# In[39]:

interpreter = NaturalLanguageInterpreter.create(model_directory)

# In[40]:

action_endpoint = EndpointConfig(url="http://localhost:5055/webhook")

# In[41]:

agent_model_path = os.path.join(model_dir, policy_fixed_model_name)
agent = Agent.load(agent_model_path,
                   interpreter=interpreter,
                   action_endpoint=action_endpoint)

# In[44]:

loop = asyncio.get_event_loop()