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

Make Rasa server response timeout configurable #5358

Merged
merged 27 commits into from
Mar 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
c2f6f19
Make Rasa server response timeout configurable
mmalhotra Mar 2, 2020
ac455b0
fix flak8 issues
mmalhotra Mar 2, 2020
e7ddf44
Merge remote-tracking branch 'upstream/master'
mmalhotra Mar 3, 2020
66069e6
Merge branch 'master' into master
ricwo Mar 16, 2020
ae4231d
Update changelog/4756.improvement.rst
mmalhotra Mar 16, 2020
4c9b3c6
fix deepsource issues
mmalhotra Mar 16, 2020
0c6ac73
fix tests
mmalhotra Mar 16, 2020
fc65882
fix changelog
mmalhotra Mar 16, 2020
270cfb0
fix changelog
mmalhotra Mar 16, 2020
1a37b91
Fix changelog
mmalhotra Mar 16, 2020
1eaae90
Merge branch 'master' of github.com:mmalhotra/rasa
mmalhotra Mar 16, 2020
fc3dadd
Update rasa/server.py
mmalhotra Mar 17, 2020
dd69268
Update rasa/server.py
mmalhotra Mar 17, 2020
de14033
Update rasa/server.py
mmalhotra Mar 17, 2020
642ccc3
Update rasa/cli/arguments/run.py
mmalhotra Mar 17, 2020
5b3c94f
Update rasa/cli/arguments/run.py
mmalhotra Mar 17, 2020
6ad8ddf
Update rasa/server.py
mmalhotra Mar 17, 2020
08a781b
Update rasa/server.py
mmalhotra Mar 17, 2020
1d8d994
Update rasa/server.py
mmalhotra Mar 17, 2020
79c5875
Update rasa/cli/arguments/run.py
mmalhotra Mar 17, 2020
51e2237
Update rasa/server.py
mmalhotra Mar 17, 2020
1618167
Update rasa/cli/arguments/run.py
mmalhotra Mar 17, 2020
66d9929
Update rasa/cli/arguments/run.py
mmalhotra Mar 17, 2020
463dde8
Update rasa/core/run.py
mmalhotra Mar 17, 2020
b45ed0f
Update rasa/server.py
mmalhotra Mar 17, 2020
08ea313
Update rasa/server.py
mmalhotra Mar 17, 2020
180a6f1
fix black issue
mmalhotra Mar 17, 2020
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
3 changes: 3 additions & 0 deletions changelog/4756.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Make response timeout configurable.
``rasa run``, ``rasa shell`` and ``rasa x`` can now be started with
``--response-timeout <int>`` to configure a response timeout of ``<int>`` seconds.
8 changes: 4 additions & 4 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,13 @@

latex_elements = {
# The paper size ('letterpaper' or 'a4paper').
#'papersize': 'letterpaper',
# 'papersize': 'letterpaper',
# The font size ('10pt', '11pt' or '12pt').
#'pointsize': '10pt',
# 'pointsize': '10pt',
# Additional stuff for the LaTeX preamble.
#'preamble': '',
# 'preamble': '',
# Latex figure (float) alignment
#'figure_align': 'htbp',
# 'figure_align': 'htbp',
}

# Grouping the document tree into LaTeX files. List of tuples
Expand Down
9 changes: 9 additions & 0 deletions rasa/cli/arguments/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@


def set_run_arguments(parser: argparse.ArgumentParser):
"""Arguments for running Rasa directly using `rasa run`."""
add_model_param(parser)
add_server_arguments(parser)


def set_run_action_arguments(parser: argparse.ArgumentParser):
"""Set arguments for running Rasa SDK."""
import rasa_sdk.cli.arguments as sdk

sdk.add_endpoint_arguments(parser)


def add_server_arguments(parser: argparse.ArgumentParser):
"""Add arguments for running API endpoint."""
parser.add_argument(
"--log-file",
type=str,
Expand Down Expand Up @@ -56,6 +59,12 @@ def add_server_arguments(parser: argparse.ArgumentParser):
action="store_true",
help="Start the web server API in addition to the input channel.",
)
server_arguments.add_argument(
"--response-timeout",
default=constants.DEFAULT_RESPONSE_TIMEOUT,
type=int,
help="Maximum time a response can take to process (sec).",
)
server_arguments.add_argument(
"--remote-storage",
help="Set the remote location where your Rasa model is stored, e.g. on AWS.",
Expand Down
1 change: 1 addition & 0 deletions rasa/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
DEFAULT_NLU_RESULTS_PATH = "nlu_comparison_results"
DEFAULT_CORE_SUBDIRECTORY_NAME = "core"
DEFAULT_REQUEST_TIMEOUT = 60 * 5 # 5 minutes
DEFAULT_RESPONSE_TIMEOUT = 60 * 60 # 1 hour

TEST_DATA_FILE = "test.md"
TRAIN_DATA_FILE = "train.md"
Expand Down
5 changes: 4 additions & 1 deletion rasa/core/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

DEFAULT_REQUEST_TIMEOUT = 60 * 5 # 5 minutes

DEFAULT_RESPONSE_TIMEOUT = 60 * 60 # 1 hour

DEFAULT_LOCK_LIFETIME = 60 # in seconds

REQUESTED_SLOT = "requested_slot"
Expand All @@ -38,7 +40,8 @@

BEARER_TOKEN_PREFIX = "Bearer "

# Key to access data in the event metadata which specifies if an event was caused by an external entity (e.g. a sensor).
# Key to access data in the event metadata
# It specifies if an event was caused by an external entity (e.g. a sensor).
IS_EXTERNAL = "is_external"

# the lowest priority intended to be used by machine learning policies
Expand Down
5 changes: 5 additions & 0 deletions rasa/core/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def configure_app(
cors: Optional[Union[Text, List[Text], None]] = None,
auth_token: Optional[Text] = None,
enable_api: bool = True,
response_timeout: int = constants.DEFAULT_RESPONSE_TIMEOUT,
jwt_secret: Optional[Text] = None,
jwt_method: Optional[Text] = None,
route: Optional[Text] = "/webhooks/",
Expand All @@ -99,6 +100,7 @@ def configure_app(
app = server.create_app(
cors_origins=cors,
auth_token=auth_token,
response_timeout=response_timeout,
jwt_secret=jwt_secret,
jwt_method=jwt_method,
endpoints=endpoints,
Expand Down Expand Up @@ -148,6 +150,7 @@ def serve_application(
cors: Optional[Union[Text, List[Text]]] = None,
auth_token: Optional[Text] = None,
enable_api: bool = True,
response_timeout: int = constants.DEFAULT_RESPONSE_TIMEOUT,
jwt_secret: Optional[Text] = None,
jwt_method: Optional[Text] = None,
endpoints: Optional[AvailableEndpoints] = None,
Expand All @@ -159,6 +162,7 @@ def serve_application(
ssl_password: Optional[Text] = None,
conversation_id: Optional[Text] = uuid.uuid4().hex,
):
"""Run the API entrypoint."""
from rasa import server

if not channel and not credentials:
Expand All @@ -171,6 +175,7 @@ def serve_application(
cors,
auth_token,
enable_api,
response_timeout,
jwt_secret,
jwt_method,
port=port,
Expand Down
18 changes: 13 additions & 5 deletions rasa/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from rasa.constants import (
DEFAULT_DOMAIN_PATH,
DEFAULT_MODELS_PATH,
DEFAULT_RESPONSE_TIMEOUT,
DOCS_BASE_URL,
MINIMUM_COMPATIBLE_VERSION,
)
Expand All @@ -39,11 +40,11 @@
from rasa.nlu.emulators.no_emulator import NoEmulator
from rasa.nlu.test import run_evaluation
from rasa.utils.endpoints import EndpointConfig
from sanic import Sanic, Sanic, response, response
from sanic.request import Request, Request
from sanic import Sanic, response
from sanic.request import Request
from sanic.response import HTTPResponse
from sanic_cors import CORS, CORS
from sanic_jwt import Initialize, Initialize, exceptions, exceptions
from sanic_cors import CORS
from sanic_jwt import Initialize, exceptions

if typing.TYPE_CHECKING:
from ssl import SSLContext
Expand Down Expand Up @@ -194,6 +195,7 @@ async def decorated(request: Request, *args: Any, **kwargs: Any) -> Any:
def event_verbosity_parameter(
request: Request, default_verbosity: EventVerbosity
) -> EventVerbosity:
"""Create `EventVerbosity` object using request params if present."""
event_verbosity_str = request.args.get(
"include_events", default_verbosity.name
).upper()
Expand All @@ -213,6 +215,7 @@ def event_verbosity_parameter(
async def get_tracker(
processor: "MessageProcessor", conversation_id: Text
) -> Optional[DialogueStateTracker]:
"""Get tracker object from `MessageProcessor`."""
tracker = await processor.get_tracker_with_session_start(conversation_id)
if not tracker:
raise ErrorResponse(
Expand All @@ -225,11 +228,13 @@ async def get_tracker(


def validate_request_body(request: Request, error_message: Text):
"""Check if `request` has a body."""
if not request.body:
raise ErrorResponse(400, "BadRequest", error_message)


async def authenticate(request: Request):
"""Callback for authentication failed."""
raise exceptions.AuthenticationFailed(
"Direct JWT authentication not supported. You should already have "
"a valid JWT from an authentication provider, Rasa will just make "
Expand Down Expand Up @@ -362,6 +367,8 @@ def configure_cors(


def add_root_route(app: Sanic):
"""Add '/' route to return hello."""

@app.get("/")
async def hello(request: Request):
"""Check if the server is running and responds with the version."""
Expand All @@ -372,14 +379,15 @@ def create_app(
agent: Optional["Agent"] = None,
cors_origins: Union[Text, List[Text], None] = "*",
auth_token: Optional[Text] = None,
response_timeout: int = DEFAULT_RESPONSE_TIMEOUT,
jwt_secret: Optional[Text] = None,
jwt_method: Text = "HS256",
endpoints: Optional[AvailableEndpoints] = None,
):
"""Class representing a Rasa HTTP server."""

app = Sanic(__name__)
app.config.RESPONSE_TIMEOUT = 60 * 60
app.config.RESPONSE_TIMEOUT = response_timeout
configure_cors(app, cors_origins)

# Setup the Sanic-JWT extension
Expand Down
1 change: 1 addition & 0 deletions tests/cli/test_rasa_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def test_run_help(run: Callable[..., RunResult]):
help_text = """usage: rasa run [-h] [-v] [-vv] [--quiet] [-m MODEL] [--log-file LOG_FILE]
[--endpoints ENDPOINTS] [-p PORT] [-t AUTH_TOKEN]
[--cors [CORS [CORS ...]]] [--enable-api]
[--response-timeout RESPONSE_TIMEOUT]
[--remote-storage REMOTE_STORAGE]
[--ssl-certificate SSL_CERTIFICATE]
[--ssl-keyfile SSL_KEYFILE] [--ssl-ca-file SSL_CA_FILE]
Expand Down
1 change: 1 addition & 0 deletions tests/cli/test_rasa_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def test_shell_help(run: Callable[..., RunResult]):
[--conversation-id CONVERSATION_ID] [-m MODEL]
[--log-file LOG_FILE] [--endpoints ENDPOINTS] [-p PORT]
[-t AUTH_TOKEN] [--cors [CORS [CORS ...]]] [--enable-api]
[--response-timeout RESPONSE_TIMEOUT]
[--remote-storage REMOTE_STORAGE]
[--ssl-certificate SSL_CERTIFICATE]
[--ssl-keyfile SSL_KEYFILE] [--ssl-ca-file SSL_CA_FILE]
Expand Down
1 change: 1 addition & 0 deletions tests/cli/test_rasa_x.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def test_x_help(run: Callable[..., RunResult]):
[--config-endpoint CONFIG_ENDPOINT] [--log-file LOG_FILE]
[--endpoints ENDPOINTS] [-p PORT] [-t AUTH_TOKEN]
[--cors [CORS [CORS ...]]] [--enable-api]
[--response-timeout RESPONSE_TIMEOUT]
[--remote-storage REMOTE_STORAGE]
[--ssl-certificate SSL_CERTIFICATE] [--ssl-keyfile SSL_KEYFILE]
[--ssl-ca-file SSL_CA_FILE] [--ssl-password SSL_PASSWORD]
Expand Down