Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API improvements #6276

Merged
merged 17 commits into from
Sep 2, 2020
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/6276.improvement.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow Rasa to boot when model loading exception occurs. Forward HTTP Error responses to standard log output.
4 changes: 1 addition & 3 deletions rasa/core/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ def _load_interpreter(
The NLU interpreter.
"""
if nlu_path:
from rasa.core.interpreter import RasaNLUInterpreter

return RasaNLUInterpreter(model_directory=nlu_path)
return NaturalLanguageInterpreter.create(nlu_path)

return agent.interpreter or RegexInterpreter()

Expand Down
29 changes: 17 additions & 12 deletions rasa/core/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,11 @@ async def load_agent_on_start(
endpoints: AvailableEndpoints,
remote_storage: Optional[Text],
app: Sanic,
loop: Text,
pheel marked this conversation as resolved.
Show resolved Hide resolved
):
"""Load an agent.

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

# noinspection PyBroadException
try:
Expand All @@ -246,16 +245,22 @@ async def load_agent_on_start(

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,
)
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:
raise_warning(
f"The model at '{model_path}' could not be loaded. " f"Error: {e}"
)
app.agent = None

if not app.agent:
raise_warning(
Expand Down
1 change: 1 addition & 0 deletions rasa/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def __init__(
"code": status,
}
self.status = status
logger.error(message)


def _docs(sub_url: Text) -> Text:
Expand Down
40 changes: 39 additions & 1 deletion tests/core/test_run.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
from rasa.core import run
import pytest
from typing import Text
from sanic import Sanic
from pathlib import Path
from rasa.core import run, interpreter, policies, domain
from rasa.core.utils import AvailableEndpoints

CREDENTIALS_FILE = "examples/moodbot/credentials.yml"

Expand Down Expand Up @@ -40,3 +45,36 @@ def test_create_single_input_channels_by_class_wo_credentials():

assert len(channels) == 1
assert channels[0].name() == "rest"


async def test_load_agent_on_start_with_good_model_file(
trained_rasa_model: Text, rasa_server: Sanic,
):
agent = await run.load_agent_on_start(
trained_rasa_model, AvailableEndpoints(), None, rasa_server,
)

assert isinstance(agent.interpreter, interpreter.RasaNLUInterpreter)
assert isinstance(agent.policy_ensemble, policies.PolicyEnsemble)
assert isinstance(agent.domain, domain.Domain)


async def test_load_agent_on_start_with_bad_model_file(
tmp_path: Path, rasa_server: Sanic,
):
fake_model = tmp_path / "fake_model.tar.gz"
fake_model.touch()
fake_model_path = str(fake_model)

with pytest.warns(UserWarning) as warnings:
agent = await run.load_agent_on_start(
fake_model_path, AvailableEndpoints(), None, rasa_server,
)
assert any(
"fake_model.tar.gz' could not be loaded" in str(w.message) for w in warnings
)

# Fallback agent was loaded even if model was unusable
assert isinstance(agent.interpreter, interpreter.RegexInterpreter)
assert agent.policy_ensemble is None
assert isinstance(agent.domain, domain.Domain)