Skip to content

Commit

Permalink
[SDK/CLI] Change flow op class methods to instance methods (microsoft…
Browse files Browse the repository at this point in the history
…#515)

# Description

Please add an informative description that covers that changes made by
the pull request and link all relevant issues.

# All Promptflow Contribution checklist:
- [x] **The pull request does not introduce [breaking changes]**
- [ ] **CHANGELOG is updated for new features, bug fixes or other
significant changes.**
- [x] **I have read the [contribution guidelines](../CONTRIBUTING.md).**

## General Guidelines and Best Practices
- [ ] Title of the pull request is clear and informative.
- [ ] There are a small number of commits, each of which have an
informative message. This means that previously merged commits do not
appear in the history of the PR. For more information on cleaning up the
commits in your PR, [see this
page](https://github.com/Azure/azure-powershell/blob/master/documentation/development-docs/cleaning-up-commits.md).

### Testing Guidelines
- [ ] Pull request includes test coverage for the included changes.
  • Loading branch information
0mza987 committed Sep 15, 2023
1 parent 881f3c9 commit f7205a0
Showing 1 changed file with 13 additions and 21 deletions.
34 changes: 13 additions & 21 deletions src/promptflow/promptflow/_sdk/operations/_flow_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,7 @@ def _chat(
show_step_output=kwargs.get("show_step_output", False),
)

@classmethod
def _build_environment_config(cls, flow_dag_path: Path):
def _build_environment_config(self, flow_dag_path: Path):
flow_info = yaml.safe_load(flow_dag_path.read_text())
# standard env object:
# environment:
Expand Down Expand Up @@ -223,8 +222,7 @@ def _build_environment_config(cls, flow_dag_path: Path):

return env_obj

@classmethod
def _dump_connection(cls, connection, output_path: Path):
def _dump_connection(self, connection, output_path: Path):
# connection yaml should be a dict instead of ordered dict
connection_dict = connection._to_dict()
connection_yaml = {
Expand Down Expand Up @@ -259,8 +257,7 @@ def _dump_connection(cls, connection, output_path: Path):
f.write(dump_yaml(sorted_connection_dict, sort_keys=False))
return env_var_names

@classmethod
def _migrate_connections(cls, connection_names: List[str], output_dir: Path):
def _migrate_connections(self, connection_names: List[str], output_dir: Path):
from promptflow._sdk._pf_client import PFClient

output_dir.mkdir(parents=True, exist_ok=True)
Expand All @@ -270,7 +267,7 @@ def _migrate_connections(cls, connection_names: List[str], output_dir: Path):
for connection_name in connection_names:
connection = local_client.connections.get(name=connection_name, with_secrets=True)
connection_paths.append(output_dir / f"{connection_name}.yaml")
for env_var_name in cls._dump_connection(
for env_var_name in self._dump_connection(
connection,
connection_paths[-1],
):
Expand All @@ -283,9 +280,8 @@ def _migrate_connections(cls, connection_names: List[str], output_dir: Path):

return connection_paths, list(env_var_names.keys())

@classmethod
def _export_flow_connections(
cls,
self,
flow_dag_path: Path,
*,
output_dir: Path,
Expand All @@ -294,14 +290,13 @@ def _export_flow_connections(

executable = ExecutableFlow.from_yaml(flow_file=Path(flow_dag_path.name), working_dir=flow_dag_path.parent)

return cls._migrate_connections(
return self._migrate_connections(
connection_names=executable.get_connection_names(),
output_dir=output_dir,
)

@classmethod
def _build_flow(
cls,
self,
flow_dag_path: Path,
*,
output: Union[str, PathLike],
Expand All @@ -327,9 +322,8 @@ def _build_flow(
generate_flow_tools_json(flow_copy_target)
return flow_copy_target / flow_dag_path.name

@classmethod
def _export_to_docker(
cls,
self,
flow_dag_path: Path,
output_dir: Path,
*,
Expand All @@ -342,7 +336,7 @@ def _export_to_docker(
encoding="utf-8",
)

environment_config = cls._build_environment_config(flow_dag_path)
environment_config = self._build_environment_config(flow_dag_path)

# TODO: make below strings constants
copy_tree_respect_template_and_ignore_file(
Expand All @@ -356,9 +350,8 @@ def _export_to_docker(
},
)

@classmethod
def build(
cls,
self,
flow: Union[str, PathLike],
*,
output: Union[str, PathLike],
Expand Down Expand Up @@ -407,7 +400,7 @@ def build(
else:
output_flow_dir = output_dir / "flow"

new_flow_dag_path = cls._build_flow(
new_flow_dag_path = self._build_flow(
flow_dag_path=flow_dag_path,
output=output_flow_dir,
tuning_node=tuning_node,
Expand All @@ -418,21 +411,20 @@ def build(
return

# use new flow dag path below as origin one may miss additional includes
connection_paths, env_var_names = cls._export_flow_connections(
connection_paths, env_var_names = self._export_flow_connections(
flow_dag_path=new_flow_dag_path,
output_dir=output_dir / "connections",
)

if format == "docker":
cls._export_to_docker(
self._export_to_docker(
flow_dag_path=new_flow_dag_path,
output_dir=output_dir,
connection_paths=connection_paths,
flow_name=flow_dag_path.parent.stem,
env_var_names=env_var_names,
)

@classmethod
@contextlib.contextmanager
def _resolve_additional_includes(cls, flow_dag_path: Path) -> Iterable[Path]:
if _get_additional_includes(flow_dag_path):
Expand Down

0 comments on commit f7205a0

Please sign in to comment.