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

support serving engine switch in pf serve cmd #3161

Merged
merged 10 commits into from
May 11, 2024
5 changes: 5 additions & 0 deletions src/promptflow-devkit/promptflow/_cli/_pf/_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ def add_parser_serve_flow(subparsers):
add_param_skip_browser = lambda parser: parser.add_argument( # noqa: E731
"--skip-open-browser", action="store_true", default=False, help="Skip open browser for flow serving."
)
add_param_engine = lambda parser: parser.add_argument( # noqa: E731
wxpjimmy marked this conversation as resolved.
Show resolved Hide resolved
"--engine", type=str, default="flask", help="The engine to serve the flow, can be flask or fastapi."
)
activate_action(
name="serve",
description="Serving a flow as an endpoint.",
Expand All @@ -207,6 +210,7 @@ def add_parser_serve_flow(subparsers):
add_param_source,
add_param_port,
add_param_host,
add_param_engine,
add_param_static_folder,
add_param_environment_variables,
add_param_config,
Expand Down Expand Up @@ -595,6 +599,7 @@ def serve_flow(args):
host=args.host,
port=args.port,
skip_open_browser=args.skip_open_browser,
engine=args.engine,
wxpjimmy marked this conversation as resolved.
Show resolved Hide resolved
)
logger.info("Promptflow app ended")

Expand Down
23 changes: 21 additions & 2 deletions src/promptflow-devkit/promptflow/_sdk/_utilities/serve_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def start_flow_service(
environment_variables: Dict[str, str] = None,
init: Dict[str, Any] = None,
skip_open_browser: bool = True,
engine: str = "flask",
):
logger.info(
"Start promptflow server with port %s",
Expand All @@ -72,6 +73,11 @@ def start_flow_service(
message_format="Support directory `source` for Python flow only for now, but got {source}.",
source=source,
)
if engine not in ["flask", "fastapi"]:
raise UserErrorException(
message_format="Unsupported engine {engine} for Python flow, only support 'flask' and 'fastapi'.",
engine=engine,
)
serve_python_flow(
flow_file_name=flow_file_name,
flow_dir=flow_dir,
Expand All @@ -82,6 +88,7 @@ def start_flow_service(
config=config or {},
environment_variables=environment_variables or {},
skip_open_browser=skip_open_browser,
engine=engine,
)
else:
serve_csharp_flow(
Expand All @@ -103,6 +110,7 @@ def serve_python_flow(
environment_variables,
init,
skip_open_browser: bool,
engine,
):
from promptflow._sdk._configuration import Configuration
from promptflow.core._serving.app import create_app
Expand All @@ -121,13 +129,24 @@ def serve_python_flow(
environment_variables=environment_variables,
connection_provider=connection_provider,
init=init,
engine=engine,
)
if not skip_open_browser:
target = f"http://{host}:{port}"
logger.info(f"Opening browser {target}...")
webbrowser.open(target)
# Debug is not supported for now as debug will rerun command, and we changed working directory.
app.run(port=port, host=host)
if engine == "flask":
# Debug is not supported for now as debug will rerun command, and we changed working directory.
app.run(port=port, host=host)
else:
try:
import uvicorn

uvicorn.run(app, host=host, port=port)
except ImportError:
raise UserErrorException(
message_format="FastAPI engine requires uvicorn, please install uvicorn by `pip install uvicorn`."
)


@contextlib.contextmanager
Expand Down