Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] Tool Export Validation #299

Merged
merged 7 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions backend/prompt_studio/prompt_studio_registry/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,17 @@ 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 Prompt Studio project without prompts cannot be exported. "
"Try adding a prompt and executing it."
)


class InValidCustomToolError(APIException):
status_code = 500
default_detail = "This prompt studio project cannot be exported. It probably \
has some empty or unexecuted prompts."
harini-venkataraman marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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]
Expand All @@ -194,6 +206,21 @@ 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)
continue

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)
continue

if prompt.prompt_type == JsonSchemaKey.NOTES:
continue
if not prompt.profile_manager:
Expand Down Expand Up @@ -241,6 +268,17 @@ def frame_export_json(
llm = ""
embedding_model = ""

if invalidated_prompts:
raise InValidCustomToolError(
f"Cannot export tool. Prompt(s) : {invalidated_prompts} "
harini-venkataraman marked this conversation as resolved.
Show resolved Hide resolved
"do not have a valid prompt."
)
if invalidated_outputs:
raise InValidCustomToolError(
f"Cannot export tool. Prompt(s) : {invalidated_outputs} "
"are not executed."
harini-venkataraman marked this conversation as resolved.
Show resolved Hide resolved
)

export_metadata[JsonSchemaKey.OUTPUTS] = outputs
return export_metadata

Expand Down
Loading