From 54455bbae5bd36786d241744a21c7118cafb58f9 Mon Sep 17 00:00:00 2001 From: harini-venkataraman Date: Mon, 29 Apr 2024 17:38:13 +0530 Subject: [PATCH 1/5] Tool Export Validation --- .../prompt_studio_registry/exceptions.py | 12 ++++++ .../prompt_studio_registry_helper.py | 38 ++++++++++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/backend/prompt_studio/prompt_studio_registry/exceptions.py b/backend/prompt_studio/prompt_studio_registry/exceptions.py index 038878cb2..112cb4631 100644 --- a/backend/prompt_studio/prompt_studio_registry/exceptions.py +++ b/backend/prompt_studio/prompt_studio_registry/exceptions.py @@ -14,3 +14,15 @@ class ToolDoesNotExist(APIException): class ToolSaveError(APIException): status_code = 500 default_detail = "Error while saving the tool." + + +class EmptyToolExportError(APIException): + status_code = 500 + default_detail = "Empty tool without prompts cannot be exported. \ + Try adding a prompt and executing it." + + +class InValidCustomToolError(APIException): + status_code = 500 + default_detail = "This tool cannot be exported. It probably \ + has some empty or unexecuted prompts." diff --git a/backend/prompt_studio/prompt_studio_registry/prompt_studio_registry_helper.py b/backend/prompt_studio/prompt_studio_registry/prompt_studio_registry_helper.py index 00d4addb9..9916f7bd0 100644 --- a/backend/prompt_studio/prompt_studio_registry/prompt_studio_registry_helper.py +++ b/backend/prompt_studio/prompt_studio_registry/prompt_studio_registry_helper.py @@ -8,10 +8,16 @@ from prompt_studio.prompt_studio.models import ToolStudioPrompt from prompt_studio.prompt_studio_core.models import CustomTool from prompt_studio.prompt_studio_core.prompt_studio_helper import PromptStudioHelper +from prompt_studio.prompt_studio_output_manager.models import PromptStudioOutputManager from unstract.tool_registry.dto import Properties, Spec, Tool from .constants import JsonSchemaKey -from .exceptions import InternalError, ToolSaveError +from .exceptions import ( + EmptyToolExportError, + InternalError, + InValidCustomToolError, + ToolSaveError, +) from .models import PromptStudioRegistry from .serializers import PromptStudioRegistrySerializer @@ -173,6 +179,12 @@ def frame_export_json( grammer_dict = {} outputs: list[dict[str, Any]] = [] output: dict[str, Any] = {} + invalidated_prompts: list[str] = [] + invalidated_outputs: list[str] = [] + + if not prompts: + raise EmptyToolExportError() + if prompt_grammer: for word, synonyms in prompt_grammer.items(): synonyms = prompt_grammer[word] @@ -194,6 +206,19 @@ def frame_export_json( default_llm_profile = ProfileManager.get_default_llm_profile(tool) for prompt in prompts: + + if not prompt.prompt: + invalidated_prompts.append(prompt.prompt_key) + + prompt_output = PromptStudioOutputManager.objects.filter( + tool_id=tool.tool_id, + prompt_id=prompt.prompt_id, + profile_manager=prompt.profile_manager, + ).all() + + if not prompt_output: + invalidated_outputs.append(prompt.prompt_key) + if prompt.prompt_type == JsonSchemaKey.NOTES: continue if not prompt.profile_manager: @@ -241,6 +266,17 @@ def frame_export_json( llm = "" embedding_model = "" + if invalidated_prompts: + raise InValidCustomToolError( + f"Cannot export tool. Prompts : {invalidated_prompts} " + "does not have a valid prompt." + ) + if invalidated_outputs: + raise InValidCustomToolError( + f"Cannot export tool. Prompts : {invalidated_outputs} " + "is not executed." + ) + export_metadata[JsonSchemaKey.OUTPUTS] = outputs return export_metadata From 1b10852586b456d6649b19642b5f6ce6c4f876e5 Mon Sep 17 00:00:00 2001 From: harini-venkataraman <115449948+harini-venkataraman@users.noreply.github.com> Date: Thu, 2 May 2024 14:22:52 +0530 Subject: [PATCH 2/5] Error message refactor Co-authored-by: Chandrasekharan M <117059509+chandrasekharan-zipstack@users.noreply.github.com> Signed-off-by: harini-venkataraman <115449948+harini-venkataraman@users.noreply.github.com> --- backend/prompt_studio/prompt_studio_registry/exceptions.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/backend/prompt_studio/prompt_studio_registry/exceptions.py b/backend/prompt_studio/prompt_studio_registry/exceptions.py index 112cb4631..52b1bece2 100644 --- a/backend/prompt_studio/prompt_studio_registry/exceptions.py +++ b/backend/prompt_studio/prompt_studio_registry/exceptions.py @@ -18,8 +18,10 @@ class ToolSaveError(APIException): class EmptyToolExportError(APIException): status_code = 500 - default_detail = "Empty tool without prompts cannot be exported. \ - Try adding a prompt and executing it." + default_detail = ( + "Empty Prompt Studio project without prompts cannot be exported. " + "Try adding a prompt and executing it." + ) class InValidCustomToolError(APIException): From b93122266695a069919bf044d157dab3fcd20249 Mon Sep 17 00:00:00 2001 From: harini-venkataraman Date: Thu, 2 May 2024 15:03:24 +0530 Subject: [PATCH 3/5] Replacing fail fast with continue --- .../prompt_studio/prompt_studio_registry/exceptions.py | 2 +- .../prompt_studio_registry_helper.py | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/backend/prompt_studio/prompt_studio_registry/exceptions.py b/backend/prompt_studio/prompt_studio_registry/exceptions.py index 52b1bece2..f1e1743ef 100644 --- a/backend/prompt_studio/prompt_studio_registry/exceptions.py +++ b/backend/prompt_studio/prompt_studio_registry/exceptions.py @@ -26,5 +26,5 @@ class EmptyToolExportError(APIException): class InValidCustomToolError(APIException): status_code = 500 - default_detail = "This tool cannot be exported. It probably \ + default_detail = "This prompt studio project cannot be exported. It probably \ has some empty or unexecuted prompts." diff --git a/backend/prompt_studio/prompt_studio_registry/prompt_studio_registry_helper.py b/backend/prompt_studio/prompt_studio_registry/prompt_studio_registry_helper.py index 9916f7bd0..36920a0e1 100644 --- a/backend/prompt_studio/prompt_studio_registry/prompt_studio_registry_helper.py +++ b/backend/prompt_studio/prompt_studio_registry/prompt_studio_registry_helper.py @@ -209,6 +209,7 @@ def frame_export_json( if not prompt.prompt: invalidated_prompts.append(prompt.prompt_key) + continue prompt_output = PromptStudioOutputManager.objects.filter( tool_id=tool.tool_id, @@ -218,6 +219,7 @@ def frame_export_json( if not prompt_output: invalidated_outputs.append(prompt.prompt_key) + continue if prompt.prompt_type == JsonSchemaKey.NOTES: continue @@ -268,13 +270,13 @@ def frame_export_json( if invalidated_prompts: raise InValidCustomToolError( - f"Cannot export tool. Prompts : {invalidated_prompts} " - "does not have a valid prompt." + f"Cannot export tool. Prompt(s) : {invalidated_prompts} " + "do not have a valid prompt." ) if invalidated_outputs: raise InValidCustomToolError( - f"Cannot export tool. Prompts : {invalidated_outputs} " - "is not executed." + f"Cannot export tool. Prompt(s) : {invalidated_outputs} " + "are not executed." ) export_metadata[JsonSchemaKey.OUTPUTS] = outputs From e33f33363d2c8e5a0c085a092534416e43ecee7b Mon Sep 17 00:00:00 2001 From: harini-venkataraman <115449948+harini-venkataraman@users.noreply.github.com> Date: Thu, 2 May 2024 17:09:28 +0530 Subject: [PATCH 4/5] Error message refactor Co-authored-by: Chandrasekharan M <117059509+chandrasekharan-zipstack@users.noreply.github.com> Signed-off-by: harini-venkataraman <115449948+harini-venkataraman@users.noreply.github.com> --- backend/prompt_studio/prompt_studio_registry/exceptions.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/backend/prompt_studio/prompt_studio_registry/exceptions.py b/backend/prompt_studio/prompt_studio_registry/exceptions.py index f1e1743ef..12a182991 100644 --- a/backend/prompt_studio/prompt_studio_registry/exceptions.py +++ b/backend/prompt_studio/prompt_studio_registry/exceptions.py @@ -26,5 +26,7 @@ class EmptyToolExportError(APIException): class InValidCustomToolError(APIException): status_code = 500 - default_detail = "This prompt studio project cannot be exported. It probably \ - has some empty or unexecuted prompts." + default_detail = ( + "This prompt studio project cannot be exported. It probably " + "has some empty or unexecuted prompts." + ) From 8ef45f659589bbf42bee4afa6be43d9ec86572f0 Mon Sep 17 00:00:00 2001 From: harini-venkataraman Date: Thu, 2 May 2024 17:33:11 +0530 Subject: [PATCH 5/5] Changing commit messages --- .../prompt_studio_registry/prompt_studio_registry_helper.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/prompt_studio/prompt_studio_registry/prompt_studio_registry_helper.py b/backend/prompt_studio/prompt_studio_registry/prompt_studio_registry_helper.py index 36920a0e1..4b8ecfc2e 100644 --- a/backend/prompt_studio/prompt_studio_registry/prompt_studio_registry_helper.py +++ b/backend/prompt_studio/prompt_studio_registry/prompt_studio_registry_helper.py @@ -271,12 +271,12 @@ def frame_export_json( if invalidated_prompts: raise InValidCustomToolError( f"Cannot export tool. Prompt(s) : {invalidated_prompts} " - "do not have a valid prompt." + "are not valid. Please enter a valid prompt." ) if invalidated_outputs: raise InValidCustomToolError( f"Cannot export tool. Prompt(s) : {invalidated_outputs} " - "are not executed." + "were not run. Please run them before exporting." ) export_metadata[JsonSchemaKey.OUTPUTS] = outputs