Skip to content

Commit

Permalink
Merge branch 'master' into multiple-queues
Browse files Browse the repository at this point in the history
  • Loading branch information
ricwo committed Mar 17, 2020
2 parents 873d344 + 6b8090b commit 1e52625
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 10 deletions.
3 changes: 3 additions & 0 deletions changelog/4756.improvement.rst
@@ -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
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
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
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
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
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
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
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
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
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

0 comments on commit 1e52625

Please sign in to comment.