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

add --port parameter for interactive learning #5837

Merged
merged 14 commits into from
May 23, 2020
Merged
2 changes: 2 additions & 0 deletions changelog/5837.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Added ``--port`` commandline argument to the interactive learning mode to allow
changing the port for the Rasa server running in the background.
6 changes: 5 additions & 1 deletion rasa/cli/arguments/interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@
add_augmentation_param,
add_persist_nlu_data_param,
)
from rasa.cli.arguments.run import add_port_argument


def set_interactive_arguments(parser: argparse.ArgumentParser) -> None:
parser.add_argument(
"--e2e",
action="store_true",
help="Save story files in e2e format. In this format user messages will be included in the stories.",
help="Save story files in e2e format. In this format user messages "
"will be included in the stories.",
)
add_port_argument(parser)

add_model_param(parser, default=None)
add_data_param(parser)
Expand All @@ -41,6 +44,7 @@ def set_interactive_core_arguments(parser: argparse.ArgumentParser) -> None:

_add_common_params(parser)
_add_training_arguments(parser)
add_port_argument(parser)


def _add_common_params(parser: argparse.ArgumentParser) -> None:
Expand Down
23 changes: 16 additions & 7 deletions rasa/cli/arguments/run.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import argparse
from typing import Union

from rasa.cli.arguments.default_arguments import add_model_param, add_endpoint_param
from rasa.core import constants
Expand All @@ -17,6 +18,18 @@ def set_run_action_arguments(parser: argparse.ArgumentParser):
sdk.add_endpoint_arguments(parser)


# noinspection PyProtectedMember
def add_port_argument(parser: Union[argparse.ArgumentParser, argparse._ArgumentGroup]):
"""Add an argument for port."""
parser.add_argument(
"-p",
"--port",
default=constants.DEFAULT_SERVER_PORT,
type=int,
help="Port to run the server at.",
)


def add_server_arguments(parser: argparse.ArgumentParser):
"""Add arguments for running API endpoint."""
parser.add_argument(
Expand All @@ -34,13 +47,9 @@ def add_server_arguments(parser: argparse.ArgumentParser):
)

server_arguments = parser.add_argument_group("Server Settings")
server_arguments.add_argument(
"-p",
"--port",
default=constants.DEFAULT_SERVER_PORT,
type=int,
help="Port to run the server at.",
)

add_port_argument(server_arguments)

server_arguments.add_argument(
"-t",
"--auth-token",
Expand Down
21 changes: 14 additions & 7 deletions rasa/core/training/interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -1492,10 +1492,11 @@ def _serve_application(
file_importer: TrainingDataImporter,
skip_visualization: bool,
conversation_id: Text,
port: int,
) -> Sanic:
"""Start a core server and attach the interactive learning IO."""

endpoint = EndpointConfig(url=DEFAULT_SERVER_URL)
endpoint = EndpointConfig(url=DEFAULT_SERVER_FORMAT.format("http", port))

async def run_interactive_io(running_app: Sanic) -> None:
"""Small wrapper to shut down the server once cmd io is done."""
Expand All @@ -1515,12 +1516,12 @@ async def run_interactive_io(running_app: Sanic) -> None:

update_sanic_log_level()

app.run(host="0.0.0.0", port=DEFAULT_SERVER_PORT)
app.run(host="0.0.0.0", port=port)

return app


def start_visualization(image_path: Text = None) -> None:
def start_visualization(image_path: Text, port: int) -> None:
"""Add routes to serve the conversation visualization files."""

app = Sanic(__name__)
Expand All @@ -1546,7 +1547,7 @@ def visualisation_png(request):

update_sanic_log_level()

app.run(host="0.0.0.0", port=DEFAULT_SERVER_PORT + 1, access_log=False)
app.run(host="0.0.0.0", port=port, access_log=False)


# noinspection PyUnusedLocal
Expand Down Expand Up @@ -1615,16 +1616,22 @@ def run_interactive_learning(
if server_args.get("domain"):
PATHS["domain"] = server_args["domain"]

port = server_args.get("port", DEFAULT_SERVER_PORT)

SAVE_IN_E2E = server_args["e2e"]

if not skip_visualization:
p = Process(target=start_visualization, args=(DEFAULT_STORY_GRAPH_FILE,))
visualisation_port = port + 1
p = Process(
target=start_visualization,
args=(DEFAULT_STORY_GRAPH_FILE, visualisation_port),
)
p.daemon = True
p.start()
else:
p = None

app = run.configure_app(enable_api=True, conversation_id="default")
app = run.configure_app(port=port, conversation_id="default", enable_api=True)
endpoints = AvailableEndpoints.read_endpoints(server_args.get("endpoints"))

# before_server_start handlers make sure the agent is loaded before the
Expand All @@ -1634,7 +1641,7 @@ def run_interactive_learning(
"before_server_start",
)

_serve_application(app, file_importer, skip_visualization, conversation_id)
_serve_application(app, file_importer, skip_visualization, conversation_id, port)

if not skip_visualization and p is not None:
p.terminate() # pytype: disable=attribute-error
Expand Down
8 changes: 5 additions & 3 deletions tests/cli/test_rasa_interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
def test_interactive_help(run: Callable[..., RunResult]):
output = run("interactive", "--help")

help_text = """usage: rasa interactive [-h] [-v] [-vv] [--quiet] [--e2e] [-m MODEL]
help_text = """usage: rasa interactive [-h] [-v] [-vv] [--quiet] [--e2e] [-p PORT] [-m MODEL]
[--data DATA [DATA ...]] [--skip-visualization]
[--conversation-id CONVERSATION_ID]
[--endpoints ENDPOINTS] [-c CONFIG] [-d DOMAIN]
Expand All @@ -35,7 +35,7 @@ def test_interactive_core_help(run: Callable[..., RunResult]):
[--conversation-id CONVERSATION_ID]
[--endpoints ENDPOINTS] [-c CONFIG] [-d DOMAIN]
[--out OUT] [--augmentation AUGMENTATION]
[--debug-plots]
[--debug-plots] [-p PORT]
[model-as-positional-argument]"""

lines = help_text.split("\n")
Expand Down Expand Up @@ -182,7 +182,9 @@ def test_pass_conversation_id_to_interactive_learning(monkeypatch: MonkeyPatch):

do_interactive_learning(args, Mock())

_serve_application.assert_called_once_with(ANY, ANY, True, expected_conversation_id)
_serve_application.assert_called_once_with(
ANY, ANY, True, expected_conversation_id, 5005
)


def test_generate_conversation_id_for_interactive_learning(monkeypatch: MonkeyPatch):
Expand Down