From 39c80d62566f59f82fb84ecc7c9eeb5de5a565b6 Mon Sep 17 00:00:00 2001 From: zhen Date: Thu, 25 Apr 2024 21:06:17 +0800 Subject: [PATCH] [prompty] load_flow(source=path/to/prompty) is callable (#3015) # Description Please add an informative description that covers that changes made by the pull request and link all relevant issues. # All Promptflow Contribution checklist: - [ ] **The pull request does not introduce [breaking changes].** - [ ] **CHANGELOG is updated for new features, bug fixes or other significant changes.** - [ ] **I have read the [contribution guidelines](../CONTRIBUTING.md).** - [ ] **Create an issue and link to the pull request to get dedicated review from promptflow team. Learn more: [suggested workflow](../CONTRIBUTING.md#suggested-workflow).** ## 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. --- .../promptflow/_sdk/entities/_flows/prompty.py | 4 ++++ .../tests/sdk_cli_test/e2etests/test_prompty.py | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/promptflow-devkit/promptflow/_sdk/entities/_flows/prompty.py b/src/promptflow-devkit/promptflow/_sdk/entities/_flows/prompty.py index cf4e211dad7..29bac147694 100644 --- a/src/promptflow-devkit/promptflow/_sdk/entities/_flows/prompty.py +++ b/src/promptflow-devkit/promptflow/_sdk/entities/_flows/prompty.py @@ -27,6 +27,7 @@ def __init__( path = Path(path) # prompty folder path code = Path(code) + self._core_prompty = CorePrompty(path=path, **kwargs) super().__init__(code=code, path=path, data=data, content_hash=None, **kwargs) @property @@ -52,6 +53,9 @@ def _load(cls, path: Path, raise_error=True, **kwargs): ) return cls(path=path, code=path.parent, data=data, **kwargs) + def __call__(self, *args, **kwargs): + return self._core_prompty(*args, **kwargs) + # endregion overrides # region SchemaValidatableMixin diff --git a/src/promptflow-devkit/tests/sdk_cli_test/e2etests/test_prompty.py b/src/promptflow-devkit/tests/sdk_cli_test/e2etests/test_prompty.py index 72664337a5a..23bdb3d171d 100644 --- a/src/promptflow-devkit/tests/sdk_cli_test/e2etests/test_prompty.py +++ b/src/promptflow-devkit/tests/sdk_cli_test/e2etests/test_prompty.py @@ -10,6 +10,7 @@ from openai.types.chat import ChatCompletion from promptflow._sdk._pf_client import PFClient +from promptflow.client import load_flow from promptflow.core import AsyncPrompty, Flow, Prompty from promptflow.core._errors import ( InvalidConnectionError, @@ -19,6 +20,7 @@ ) from promptflow.core._model_configuration import AzureOpenAIModelConfiguration from promptflow.core._prompty_utils import convert_model_configuration_to_connection +from promptflow.exceptions import UserErrorException TEST_ROOT = PROMPTFLOW_ROOT / "tests" DATA_DIR = TEST_ROOT / "test_configs/datas" @@ -171,6 +173,14 @@ def test_prompty_callable(self, pf: PFClient): Prompty.load(source=f"{PROMPTY_DIR}/prompty_example.prompty", model=model_dict) assert "Cannot configure model config and connection" in ex.value.message + prompty = load_flow(source=f"{PROMPTY_DIR}/prompty_example.prompty") + result = prompty(question="what is the result of 1+1?") + assert "2" in result + + with pytest.raises(UserErrorException) as ex: + prompty("what is the result of 1+1?") + assert "Prompty can only be called with keyword arguments." in ex.value.message + def test_prompty_async_call(self): async_prompty = AsyncPrompty.load(source=f"{PROMPTY_DIR}/prompty_example.prompty") with pytest.raises(MissingRequiredInputError) as e: