Skip to content

Commit

Permalink
feat: support class init
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotzh committed Apr 25, 2024
1 parent d107bb4 commit 385ccc1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/promptflow-devkit/promptflow/_sdk/_utils/serve_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import abc
import contextlib
import json
import logging
import os
import shutil
Expand Down Expand Up @@ -160,8 +162,9 @@ def __init__(self, *, flow_file_name, flow_dir, init, port):
self.flow_dir, self.flow_file_name = flow_dir, flow_file_name
super().__init__()

@contextlib.contextmanager
def _construct_command(self):
return [
cmd = [
"dotnet",
EXECUTOR_SERVICE_DLL,
"--port",
Expand All @@ -176,10 +179,21 @@ def _construct_command(self):
"",
"--serving",
]
if self._init:
init_json_path = Path(tempfile.mktemp()).with_suffix(".json")
with open(init_json_path, "w") as f:
json.dump(self._init, f)
cmd.extend(["--init", init_json_path.as_posix()])
try:
yield cmd
finally:
os.remove(init_json_path)
else:
yield cmd

def run(self):
try:
command = self._construct_command()
subprocess.run(command, cwd=self.flow_dir, stdout=sys.stdout, stderr=sys.stderr)
with self._construct_command() as command:
subprocess.run(command, cwd=self.flow_dir, stdout=sys.stdout, stderr=sys.stderr)
except KeyboardInterrupt:
pass
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pytest

from promptflow._cli._pf.entry import main
from promptflow._sdk._utils.serve_utils import find_available_port


# TODO: move this to a shared utility module
Expand Down Expand Up @@ -104,6 +105,35 @@ def test_pf_flow_test(self, request, target_fixture_name: str):
cmd.extend(["--init", test_case["init"]])
run_pf_command(*cmd)

@pytest.mark.skip(reason="need to figure out how to check serve status in subprocess")
def test_flow_serve(self, csharp_test_project_class_init_flex_flow: CSharpProject):
port = find_available_port()
run_pf_command(
"flow",
"serve",
"--source",
csharp_test_project_class_init_flex_flow["flow_dir"],
"--port",
str(port),
"--init",
"connection=azure_open_ai_connection",
"name=Promptflow",
)

@pytest.mark.skip(reason="need to figure out how to check serve status in subprocess")
def test_flow_serve_init_json(self, csharp_test_project_class_init_flex_flow: CSharpProject):
port = find_available_port()
run_pf_command(
"flow",
"serve",
"--source",
csharp_test_project_class_init_flex_flow["flow_dir"],
"--port",
str(port),
"--init",
csharp_test_project_class_init_flex_flow["init"],
)

def test_flow_test_include_log(self, csharp_test_project_basic: CSharpProject, capfd):
run_pf_command(
"flow",
Expand Down

0 comments on commit 385ccc1

Please sign in to comment.