コード例 #1
0
def test_exception_tracker_store_from_endpoint_config(
        default_domain: Domain, monkeypatch: MonkeyPatch):
    """Check if tracker store properly handles exceptions.

    If we can not create a tracker store by instantiating the
    expected type (e.g. due to an exception) we should fallback to
    the default `InMemoryTrackerStore`."""

    store = read_endpoint_config(DEFAULT_ENDPOINTS_FILE, "tracker_store")
    mock = Mock(side_effect=Exception("test exception"))
    monkeypatch.setattr(rasa.core.tracker_store, "RedisTrackerStore", mock)

    with pytest.raises(Exception) as e:
        TrackerStore.create(store, default_domain)

    assert "test exception" in str(e.value)
コード例 #2
0
def create_agent(model: Text, endpoints: Text = None) -> "Agent":
    """Create an agent instance based on a stored model.

    Args:
        model: file path to the stored model
        endpoints: file path to the used endpoint configuration
    """
    from rasa.core.tracker_store import TrackerStore
    from rasa.core.utils import AvailableEndpoints
    from rasa.core.brokers.broker import EventBroker
    import rasa.utils.common

    _endpoints = AvailableEndpoints.read_endpoints(endpoints)

    _broker = rasa.utils.common.run_in_loop(
        EventBroker.create(_endpoints.event_broker))
    _tracker_store = TrackerStore.create(_endpoints.tracker_store,
                                         event_broker=_broker)
    _lock_store = LockStore.create(_endpoints.lock_store)

    return Agent.load(
        model,
        generator=_endpoints.nlg,
        tracker_store=_tracker_store,
        lock_store=_lock_store,
        action_endpoint=_endpoints.action,
    )
コード例 #3
0
ファイル: evaluate.py プロジェクト: spawn08/rasa
def _create_tracker_loader(
    endpoint_config: Text,
    strategy: Text,
    domain: Domain,
    count: Optional[int],
    seed: Optional[int],
) -> MarkerTrackerLoader:
    """Create a tracker loader against the configured tracker store.

    Args:
        endpoint_config: Path to the endpoint configuration defining the tracker
                         store to use.
        strategy: Strategy to use when selecting trackers to extract from.
        domain: The domain to use when connecting to the tracker store.
        count: (Optional) Number of trackers to extract from (for any strategy
               except 'all').
        seed: (Optional) The seed to initialise the random number generator for
              use with the 'sample_n' strategy.

    Returns:
        A MarkerTrackerLoader object configured with the specified strategy against
        the configured tracker store.
    """
    endpoints = AvailableEndpoints.read_endpoints(endpoint_config)
    tracker_store = TrackerStore.create(endpoints.tracker_store, domain=domain)
    return MarkerTrackerLoader(tracker_store, strategy, count, seed)
コード例 #4
0
ファイル: run.py プロジェクト: ravishankr/rasa
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
コード例 #5
0
def test_mongo_tracker_store_raise_exception(domain: Domain,
                                             monkeypatch: MonkeyPatch):
    monkeypatch.setattr(
        rasa.core.tracker_store,
        "MongoTrackerStore",
        Mock(side_effect=OperationFailure(
            "not authorized on logs to execute command.")),
    )
    with pytest.raises(ConnectionException) as error:
        TrackerStore.create(
            EndpointConfig(username="******",
                           password="******",
                           type="mongod"),
            domain,
        )

    assert "not authorized on logs to execute command." in str(error.value)
コード例 #6
0
def test_create_non_async_tracker_store(domain: Domain):
    endpoint_config = EndpointConfig(
        type="tests.core.test_tracker_stores.NonAsyncTrackerStore"
    )
    with pytest.warns(FutureWarning):
        tracker_store = TrackerStore.create(endpoint_config)
    assert isinstance(tracker_store, AwaitableTrackerStore)
    assert isinstance(tracker_store._tracker_store, NonAsyncTrackerStore)
コード例 #7
0
ファイル: run.py プロジェクト: meongithub/rasa-for-botfront
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)."""

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

    _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

    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,
    )

    if not app.agent:
        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,
        )

    return app.agent
コード例 #8
0
ファイル: test_tracker_stores.py プロジェクト: wavymazy/rasa
def test_tracker_store_from_invalid_module(domain: Domain):
    endpoints_path = "data/test_endpoints/custom_tracker_endpoints.yml"
    store_config = read_endpoint_config(endpoints_path, "tracker_store")
    store_config.type = "a.module.which.cannot.be.found"

    with pytest.warns(UserWarning):
        tracker_store = TrackerStore.create(store_config, domain)

    assert isinstance(tracker_store, InMemoryTrackerStore)
コード例 #9
0
ファイル: bots.py プロジェクト: wireless911/chatbot-saas
 def _load_endpoints(self, endpoints: Optional[Text] = None):
     """加载enpoints文件"""
     endpoints = AvailableEndpoints.read_endpoints(endpoints)
     broker = EventBroker.create(endpoints.event_broker)
     self.tracker_store = TrackerStore.create(endpoints.tracker_store,
                                              event_broker=broker)
     self.generator = endpoints.nlg
     self.action_endpoint = endpoints.action
     self.lock_store = LockStore.create(endpoints.lock_store)
コード例 #10
0
def test_tracker_store_deprecated_url_argument_from_string(default_domain: Domain):
    endpoints_path = "data/test_endpoints/custom_tracker_endpoints.yml"
    store_config = read_endpoint_config(endpoints_path, "tracker_store")
    store_config.type = "tests.core.test_tracker_stores.URLExampleTrackerStore"

    with pytest.warns(DeprecationWarning):
        tracker_store = TrackerStore.create(store_config, default_domain)

    assert isinstance(tracker_store, URLExampleTrackerStore)
コード例 #11
0
def test_tracker_store_from_invalid_string(default_domain: Domain):
    endpoints_path = "data/test_endpoints/custom_tracker_endpoints.yml"
    store_config = read_endpoint_config(endpoints_path, "tracker_store")
    store_config.type = "any string"

    with pytest.warns(UserWarning):
        tracker_store = TrackerStore.create(store_config, default_domain)

    assert isinstance(tracker_store, InMemoryTrackerStore)
コード例 #12
0
def test_tracker_store_with_host_argument_from_string(default_domain: Domain):
    endpoints_path = "data/test_endpoints/custom_tracker_endpoints.yml"
    store_config = read_endpoint_config(endpoints_path, "tracker_store")
    store_config.type = "tests.core.test_tracker_stores.HostExampleTrackerStore"

    with pytest.warns(None) as record:
        tracker_store = TrackerStore.create(store_config, default_domain)

    assert len(record) == 0

    assert isinstance(tracker_store, HostExampleTrackerStore)
コード例 #13
0
def test_create_tracker_store_from_endpoint_config(default_domain: Domain):
    store = read_endpoint_config(DEFAULT_ENDPOINTS_FILE, "tracker_store")
    tracker_store = RedisTrackerStore(
        domain=default_domain,
        host="localhost",
        port=6379,
        db=0,
        password="******",
        record_exp=3000,
    )

    assert isinstance(tracker_store, type(TrackerStore.create(store, default_domain)))
コード例 #14
0
ファイル: test_tracker_stores.py プロジェクト: wavymazy/rasa
def test_create_tracker_store_from_endpoint_config(domain: Domain,
                                                   endpoints_path: Text):
    store = read_endpoint_config(endpoints_path, "tracker_store")
    tracker_store = RedisTrackerStore(
        domain=domain,
        host="localhost",
        port=6379,
        db=0,
        password="******",
        record_exp=3000,
    )

    assert isinstance(tracker_store, type(TrackerStore.create(store, domain)))
コード例 #15
0
async def _load_agent(
    model_path: Optional[Text] = None,
    model_server: Optional[EndpointConfig] = None,
    remote_storage: Optional[Text] = None,
    endpoints: Optional[AvailableEndpoints] = None,
    lock_store: Optional[LockStore] = None,
) -> Agent:
    try:
        tracker_store = None
        generator = None
        action_endpoint = None

        if endpoints:
            broker = await EventBroker.create(endpoints.event_broker)
            tracker_store = TrackerStore.create(
                endpoints.tracker_store, event_broker=broker
            )
            generator = endpoints.nlg
            action_endpoint = endpoints.action
            if not lock_store:
                lock_store = LockStore.create(endpoints.lock_store)

        loaded_agent = await rasa.core.agent.load_agent(
            model_path,
            model_server,
            remote_storage,
            generator=generator,
            tracker_store=tracker_store,
            lock_store=lock_store,
            action_endpoint=action_endpoint,
        )
    except Exception as e:
        logger.debug(traceback.format_exc())
        raise ErrorResponse(
            HTTPStatus.INTERNAL_SERVER_ERROR,
            "LoadingError",
            f"An unexpected error occurred. Error: {e}",
        )

    if not loaded_agent:
        raise ErrorResponse(
            HTTPStatus.BAD_REQUEST,
            "BadRequest",
            f"Agent with name '{model_path}' could not be loaded.",
            {"parameter": "model", "in": "query"},
        )

    return loaded_agent
コード例 #16
0
def test_create_tracker_store_from_endpoint_config(domain: Domain,
                                                   endpoints_path: Text):
    store = read_endpoint_config(endpoints_path, "tracker_store")
    tracker_store = RedisTrackerStore(
        domain=domain,
        host="localhost",
        port=6379,
        db=0,
        password="******",
        record_exp=3000,
        use_ssl=True,
        ssl_keyfile="keyfile.key",
        ssl_certfile="certfile.crt",
        ssl_ca_certs="my-bundle.ca-bundle",
    )

    assert isinstance(tracker_store, type(TrackerStore.create(store, domain)))
コード例 #17
0
ファイル: run.py プロジェクト: pranavdurai10/rasa
def create_agent(model: Text, endpoints: Text = None) -> "Agent":
    from rasa.core.tracker_store import TrackerStore
    from rasa.core.utils import AvailableEndpoints
    from rasa.core.agent import Agent
    from rasa.core.brokers.broker import EventBroker

    _endpoints = AvailableEndpoints.read_endpoints(endpoints)

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

    return Agent.load(
        model,
        generator=_endpoints.nlg,
        tracker_store=_tracker_store,
        lock_store=_lock_store,
        action_endpoint=_endpoints.action,
    )
コード例 #18
0
ファイル: export.py プロジェクト: suryatmodulus/rasa
def _get_tracker_store(endpoints: "AvailableEndpoints") -> "TrackerStore":
    """Get `TrackerStore` from `endpoints`.

    Prints an error and exits if no tracker store could be loaded.

    Args:
        endpoints: `AvailableEndpoints` to initialize the tracker store from.

    Returns:
        Initialized tracker store.

    """
    if not endpoints.tracker_store:
        rasa.shared.utils.cli.print_error_and_exit(
            f"Could not find a `tracker_store` section in the supplied "
            f"endpoints file. Instructions on how to configure a tracker store "
            f"can be found here: {DOCS_URL_TRACKER_STORES}. "
            f"Exiting. ")

    from rasa.core.tracker_store import TrackerStore

    return TrackerStore.create(endpoints.tracker_store)
コード例 #19
0
ファイル: agent.py プロジェクト: spawn08/rasa
async def load_agent(
    model_path: Optional[Text] = None,
    model_server: Optional[EndpointConfig] = None,
    remote_storage: Optional[Text] = None,
    endpoints: Optional[AvailableEndpoints] = None,
    loop: Optional[AbstractEventLoop] = None,
) -> Agent:
    """Loads agent from server, remote storage or disk.

    Args:
        model_path: Path to the model if it's on disk.
        model_server: Configuration for a potential server which serves the model.
        remote_storage: URL of remote storage for model.
        endpoints: Endpoint configuration.
        loop: Optional async loop to pass to broker creation.

    Returns:
        The instantiated `Agent` or `None`.
    """
    from rasa.core.tracker_store import TrackerStore
    from rasa.core.brokers.broker import EventBroker

    tracker_store = None
    lock_store = None
    generator = None
    action_endpoint = None
    http_interpreter = None

    if endpoints:
        broker = await EventBroker.create(endpoints.event_broker, loop=loop)
        tracker_store = TrackerStore.create(endpoints.tracker_store,
                                            event_broker=broker)
        lock_store = LockStore.create(endpoints.lock_store)
        generator = endpoints.nlg
        action_endpoint = endpoints.action
        model_server = endpoints.model if endpoints.model else model_server
        if endpoints.nlu:
            http_interpreter = RasaNLUHttpInterpreter(endpoints.nlu)

    agent = Agent(
        generator=generator,
        tracker_store=tracker_store,
        lock_store=lock_store,
        action_endpoint=action_endpoint,
        model_server=model_server,
        remote_storage=remote_storage,
        http_interpreter=http_interpreter,
    )

    try:
        if model_server is not None:
            return await load_from_server(agent, model_server)

        elif remote_storage is not None:
            agent.load_model_from_remote_storage(model_path)

        elif model_path is not None and os.path.exists(model_path):
            try:
                agent.load_model(model_path)
            except ModelNotFound:
                rasa.shared.utils.io.raise_warning(
                    f"No valid model found at {model_path}!")
        else:
            rasa.shared.utils.io.raise_warning(
                "No valid configuration given to load agent. "
                "Agent loaded with no model!")
        return agent

    except Exception as e:
        logger.error(f"Could not load model due to {e}.")
        return agent
コード例 #20
0
def test_tracker_store_connection_error(config: Dict, default_domain: Domain):
    store = EndpointConfig.from_dict(config)

    with pytest.raises(ConnectionException):
        TrackerStore.create(store, default_domain)