Skip to content

Commit

Permalink
merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen1993 committed Apr 9, 2024
2 parents 6d4beba + 0631a9e commit 1782f8a
Show file tree
Hide file tree
Showing 18 changed files with 132 additions and 94 deletions.
32 changes: 4 additions & 28 deletions src/promptflow-azure/promptflow/azure/_cli/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# ---------------------------------------------------------
# pylint: disable=wrong-import-position
import json
import time

from promptflow._cli._pf.help import show_privacy_statement, show_welcome_message
Expand All @@ -17,14 +16,7 @@
import logging # noqa: E402
import sys # noqa: E402

from promptflow._sdk._utils import ( # noqa: E402
get_promptflow_azure_version,
get_promptflow_core_version,
get_promptflow_devkit_version,
get_promptflow_sdk_version,
get_promptflow_tracing_version,
print_pf_version,
)
from promptflow._sdk._utils import print_pf_version, print_promptflow_version_dict_string # noqa: E402
from promptflow._utils.logger_utils import get_cli_sdk_logger # noqa: E402
from promptflow._utils.user_agent_utils import setup_user_agent_to_operation_context # noqa: E402
from promptflow.azure._cli._flow import add_parser_flow, dispatch_flow_commands # noqa: E402
Expand All @@ -47,7 +39,7 @@ def run_command(args):
for handler in logger.handlers:
handler.setLevel(logging.DEBUG)
if args.version:
print_pf_version(with_azure=True)
print_pf_version(with_azure=True, ignore_none=True)
elif args.action == "run":
dispatch_run_commands(args)
elif args.action == "flow":
Expand Down Expand Up @@ -123,24 +115,8 @@ def main():
"""Entrance of pf CLI."""
command_args = sys.argv[1:]
if len(command_args) == 1 and command_args[0] == "version":
version_dict = {"promptflow": get_promptflow_sdk_version()}
# check tracing version
version_tracing = get_promptflow_tracing_version()
if version_tracing:
version_dict["promptflow-tracing"] = version_tracing
# check azure version
version_azure = get_promptflow_azure_version()
if version_azure:
version_dict["promptflow-azure"] = version_azure
# check core version
version_core = get_promptflow_core_version()
if version_core:
version_dict["promptflow-core"] = version_core
# check devkit version
version_devkit = get_promptflow_devkit_version()
if version_devkit:
version_dict["promptflow-devkit"] = version_devkit
return json.dumps(version_dict, ensure_ascii=False, indent=2, sort_keys=True, separators=(",", ": ")) + "\n"
print_promptflow_version_dict_string(with_azure=True, ignore_none=True)
return
if len(command_args) == 0:
# print privacy statement & welcome message like azure-cli
show_privacy_statement()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
from azure.core.exceptions import HttpResponseError

from promptflow._constants import FlowType as FlowYamlType
from promptflow._proxy import ProxyFactory
from promptflow._sdk._constants import (
CLIENT_FLOW_TYPE_2_SERVICE_FLOW_TYPE,
MAX_LIST_CLI_RESULTS,
Expand Down Expand Up @@ -476,6 +475,8 @@ def _resolve_arm_id_or_upload_dependencies(self, flow: Flow, ignore_tools_json=F

@classmethod
def _try_resolve_code_for_flow(cls, flow: Flow, ops: OperationOrchestrator, ignore_tools_json=False) -> None:
from promptflow._proxy import ProxyFactory

if flow.path:
# remote path
if flow.path.startswith("azureml://datastores"):
Expand Down
4 changes: 3 additions & 1 deletion src/promptflow-core/promptflow/_utils/version_hint_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ def hint_for_update():
if LATEST_VERSION in cached_versions:
from packaging.version import parse

if parse(cached_versions[CURRENT_VERSION]) < parse(cached_versions[LATEST_VERSION]):
if cached_versions[CURRENT_VERSION] is None or parse(cached_versions[CURRENT_VERSION]) < parse(
cached_versions[LATEST_VERSION]
):
cached_versions[LAST_HINT_TIME] = str(datetime.datetime.now())
message = (
f"New prompt flow version available: promptflow-{cached_versions[LATEST_VERSION]}. Running "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ def initialize(request: InitializationRequest):
set_environment_variables(request)
# init batch coordinator to validate flow and create process pool
batch_coordinator = BatchCoordinator(
request.working_dir,
request.flow_file,
request.output_dir,
request.connections,
working_dir=request.working_dir,
flow_file=request.flow_file,
output_dir=request.output_dir,
flow_name=request.flow_name,
connections=request.connections,
worker_count=request.worker_count,
line_timeout_sec=request.line_timeout_sec,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def flow_test(request: FlowExecutionRequest):
request.inputs,
run_id=request.run_id,
storage=storage,
name=request.flow_name,
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class BaseExecutionRequest(BaseRequest):
log_path: Optional[str] = None
connections: Optional[Mapping[str, Any]] = None
environment_variables: Optional[Mapping[str, Any]] = None
flow_name: str = None

def get_run_mode(self):
raise NotImplementedError(f"Request type {self.__class__.__name__} is not implemented.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def __init__(
working_dir: Path,
flow_file: Path,
output_dir: Path,
flow_name: str = None,
connections: Optional[Mapping[str, Any]] = None,
worker_count: Optional[int] = None,
line_timeout_sec: Optional[int] = None,
Expand All @@ -46,7 +47,7 @@ def __init__(
# So we pass DummyRunStorage to FlowExecutor because we don't need to
# persist the run infos during execution in server mode.
self._flow_executor = FlowExecutor.create(
flow_file, connections, working_dir, storage=DummyRunStorage(), raise_ex=False
flow_file, connections, working_dir, storage=DummyRunStorage(), raise_ex=False, name=flow_name
)

# Init line execution process pool and set serialize_multimedia_during_execution to True
Expand Down
12 changes: 6 additions & 6 deletions src/promptflow-devkit/promptflow/_cli/_pf/_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
check_pfs_service_status,
dump_port_to_config,
get_current_env_pfs_file,
get_pfs_version,
get_port_from_config,
get_started_service_info,
is_port_in_use,
is_run_from_built_binary,
kill_exist_service,
)
from promptflow._sdk._utils import get_promptflow_sdk_version
from promptflow._utils.logger_utils import get_cli_sdk_logger # noqa: E402
from promptflow.exceptions import UserErrorException

Expand Down Expand Up @@ -193,7 +193,7 @@ def validate_port(port, force_start):
app.logger.setLevel(logging.DEBUG)
else:
app.logger.setLevel(logging.INFO)
message = f"Start Prompt Flow Service on {port}, version: {get_promptflow_sdk_version()}."
message = f"Start Prompt Flow Service on {port}, version: {get_pfs_version()}."
app.logger.info(message)
print(message)
sys.stdout.flush()
Expand Down Expand Up @@ -259,20 +259,20 @@ def validate_port(port, force_start):
subprocess.Popen(cmd, stdout=subprocess.DEVNULL, start_new_session=True)
is_healthy = check_pfs_service_status(port)
if is_healthy:
message = f"Start Prompt Flow Service on port {port}, version: {get_promptflow_sdk_version()}."
message = f"Start Promptflow Service on port {port}, version: {get_pfs_version()}."
print(message)
logger.info(message)
else:
logger.warning(f"Pfs service start failed in {port}.")
logger.warning(f"Promptflow service start failed in {port}.")


def stop_service():
port = get_port_from_config()
if port is not None and is_port_in_use(port):
kill_exist_service(port)
message = f"Pfs service stop in {port}."
message = f"Promptflow service stop in {port}."
else:
message = "Pfs service is not started."
message = "Promptflow service is not started."
logger.debug(message)
print(message)

Expand Down
7 changes: 5 additions & 2 deletions src/promptflow-devkit/promptflow/_cli/_pf/_upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,18 @@ def upgrade_version(args):
from packaging.version import parse

from promptflow._constants import _ENV_PF_INSTALLER, CLI_PACKAGE_NAME
from promptflow._sdk._utils import get_promptflow_sdk_version
from promptflow._utils.version_hint_utils import get_latest_version
from promptflow._version import VERSION as local_version

installer = os.getenv(_ENV_PF_INSTALLER) or ""
installer = installer.upper()
print(f"installer: {installer}")
latest_version = get_latest_version(CLI_PACKAGE_NAME, installer=installer)
local_version = get_promptflow_sdk_version()
if not latest_version:
logger.warning("Failed to get the latest prompt flow version.")
return
elif parse(latest_version) <= parse(local_version):
elif local_version and parse(latest_version) <= parse(local_version):
logger.warning("You already have the latest prompt flow version: %s", local_version)
return

Expand Down Expand Up @@ -109,6 +110,8 @@ def upgrade_version(args):
importlib.reload(json)

version_result = subprocess.check_output(["pf", "version"], shell=platform.system() == "Windows")
# Remove ANSI codes which control color and format of text in the console output.
version_result = version_result.decode().replace("\x1b[0m", "").strip()
version_json = json.loads(version_result)
new_version = version_json["promptflow"]

Expand Down
28 changes: 2 additions & 26 deletions src/promptflow-devkit/promptflow/_cli/_pf/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# ---------------------------------------------------------
# pylint: disable=wrong-import-position
import json
import time

from promptflow._cli._pf._experiment import add_experiment_parser, dispatch_experiment_commands
Expand All @@ -28,13 +27,7 @@
from promptflow._cli._pf._upgrade import add_upgrade_parser, upgrade_version # noqa: E402
from promptflow._cli._pf.help import show_privacy_statement, show_welcome_message # noqa: E402
from promptflow._cli._user_agent import USER_AGENT # noqa: E402
from promptflow._sdk._utils import ( # noqa: E402
get_promptflow_core_version,
get_promptflow_devkit_version,
get_promptflow_sdk_version,
get_promptflow_tracing_version,
print_pf_version,
)
from promptflow._sdk._utils import print_pf_version, print_promptflow_version_dict_string # noqa: E402
from promptflow._utils.logger_utils import get_cli_sdk_logger # noqa: E402
from promptflow._utils.user_agent_utils import setup_user_agent_to_operation_context # noqa: E402

Expand Down Expand Up @@ -138,24 +131,7 @@ def main():
"""Entrance of pf CLI."""
command_args = sys.argv[1:]
if len(command_args) == 1 and command_args[0] == "version":
version_dict = {"promptflow": get_promptflow_sdk_version()}
# check tracing version
version_tracing = get_promptflow_tracing_version()
if version_tracing:
version_dict["promptflow-tracing"] = version_tracing
# check core version
version_core = get_promptflow_core_version()
if version_core:
version_dict["promptflow-core"] = version_core
# check devkit version
version_devkit = get_promptflow_devkit_version()
if version_devkit:
version_dict["promptflow-devkit"] = version_devkit

version_dict_string = (
json.dumps(version_dict, ensure_ascii=False, indent=2, sort_keys=True, separators=(",", ": ")) + "\n"
)
print(version_dict_string)
print_promptflow_version_dict_string()
return
if len(command_args) == 0:
# print privacy statement & welcome message like azure-cli
Expand Down
13 changes: 10 additions & 3 deletions src/promptflow-devkit/promptflow/_proxy/_python_executor_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from promptflow._core._errors import UnexpectedError
from promptflow._core.run_tracker import RunTracker
from promptflow._sdk._constants import FLOW_META_JSON_GEN_TIMEOUT, FLOW_TOOLS_JSON_GEN_TIMEOUT
from promptflow._sdk._utils import can_accept_kwargs
from promptflow._utils.flow_utils import resolve_entry_file
from promptflow._utils.logger_utils import bulk_logger
from promptflow._utils.yaml_utils import load_yaml
Expand Down Expand Up @@ -60,9 +61,15 @@ async def create(
init_kwargs: Optional[Dict[str, Any]] = None,
**kwargs,
) -> "PythonExecutorProxy":
flow_executor = FlowExecutor.create(
flow_file, connections, working_dir, storage=storage, raise_ex=False, init_kwargs=init_kwargs, **kwargs
)
# Check if the method accepts kwargs in case of customer using an outdated version of core package.
if can_accept_kwargs(FlowExecutor.create):
flow_executor = FlowExecutor.create(
flow_file, connections, working_dir, storage=storage, raise_ex=False, init_kwargs=init_kwargs, **kwargs
)
else:
flow_executor = FlowExecutor.create(
flow_file, connections, working_dir, storage=storage, raise_ex=False, init_kwargs=init_kwargs
)
return cls(flow_executor)

async def exec_aggregation_async(
Expand Down
5 changes: 3 additions & 2 deletions src/promptflow-devkit/promptflow/_sdk/_service/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,19 @@
from promptflow._sdk._service.utils.utils import (
FormattedException,
get_current_env_pfs_file,
get_pfs_version,
get_port_from_config,
is_run_from_built_binary,
kill_exist_service,
)
from promptflow._sdk._utils import get_promptflow_sdk_version, overwrite_null_std_logger, read_write_by_user
from promptflow._sdk._utils import overwrite_null_std_logger, read_write_by_user
from promptflow._utils.thread_utils import ThreadWithContextVars

overwrite_null_std_logger()


def heartbeat():
response = {"promptflow": get_promptflow_sdk_version()}
response = {"promptflow": get_pfs_version()}
return jsonify(response)


Expand Down
19 changes: 15 additions & 4 deletions src/promptflow-devkit/promptflow/_sdk/_service/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
PF_SERVICE_PORT_FILE,
)
from promptflow._sdk._errors import ConnectionNotFoundError, RunNotFoundError
from promptflow._sdk._utils import get_promptflow_sdk_version, read_write_by_user
from promptflow._sdk._utils import get_promptflow_devkit_version, get_promptflow_sdk_version, read_write_by_user
from promptflow._sdk._version import VERSION
from promptflow._utils.logger_utils import get_cli_sdk_logger
from promptflow._utils.yaml_utils import dump_yaml, load_yaml
Expand Down Expand Up @@ -155,6 +155,16 @@ def make_response_no_content():
return make_response("", 204)


def get_pfs_version():
"""Promptflow service show promptflow version if installed from root, else devkit version"""
version_promptflow = get_promptflow_sdk_version()
if version_promptflow:
return version_promptflow
else:
version_devkit = get_promptflow_devkit_version()
return version_devkit


def is_pfs_service_healthy(pfs_port) -> bool:
"""Check if pfs service is running and pfs version matches pf version."""
try:
Expand All @@ -164,15 +174,16 @@ def is_pfs_service_healthy(pfs_port) -> bool:
match = re.search(r'"promptflow":"(.*?)"', response.text)
if match:
version = match.group(1)
is_healthy = version == get_promptflow_sdk_version()
local_version = get_pfs_version()
is_healthy = version == local_version
if not is_healthy:
logger.warning(
f"Promptflow service is running on port {pfs_port}, but the version is not the same as "
f"promptflow sdk version {get_promptflow_sdk_version()}. The service version is {version}."
f"local sdk version {local_version}. The service version is {version}."
)
else:
is_healthy = False
logger.warning("/heartbeat response doesn't contain current pfs version.")
logger.warning("/heartbeat response doesn't contain current promptflow service version.")
return is_healthy
except Exception: # pylint: disable=broad-except
pass
Expand Down
Loading

0 comments on commit 1782f8a

Please sign in to comment.