Skip to content

LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows

v2 changes of ProfileManager,core and studio (#471)

ali9b075c0db28a
diff --git a/backend/prompt_studio/prompt_profile_manager_v2/__init__.py b/backend/prompt_studio/prompt_profile_manager_v2/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/backend/prompt_studio/prompt_profile_manager_v2/admin.py b/backend/prompt_studio/prompt_profile_manager_v2/admin.py
new file mode 100644
index 000000000..6878f3f43
--- /dev/null
+++ b/backend/prompt_studio/prompt_profile_manager_v2/admin.py
@@ -0,0 +1,5 @@
+from django.contrib import admin
+
+from .models import ProfileManager
+
+admin.site.register(ProfileManager)
diff --git a/backend/prompt_studio/prompt_profile_manager_v2/apps.py b/backend/prompt_studio/prompt_profile_manager_v2/apps.py
new file mode 100644
index 000000000..02d4929c4
--- /dev/null
+++ b/backend/prompt_studio/prompt_profile_manager_v2/apps.py
@@ -0,0 +1,5 @@
+from django.apps import AppConfig
+
+
+class ProfileManager(AppConfig):
+ name = "prompt_studio.prompt_profile_manager_v2"
diff --git a/backend/prompt_studio/prompt_profile_manager_v2/constants.py b/backend/prompt_studio/prompt_profile_manager_v2/constants.py
new file mode 100644
index 000000000..6540b58ee
--- /dev/null
+++ b/backend/prompt_studio/prompt_profile_manager_v2/constants.py
@@ -0,0 +1,18 @@
+class ProfileManagerKeys:
+ CREATED_BY = "created_by"
+ TOOL_ID = "tool_id"
+ PROMPTS = "prompts"
+ ADAPTER_NAME = "adapter_name"
+ LLM = "llm"
+ VECTOR_STORE = "vector_store"
+ EMBEDDING_MODEL = "embedding_model"
+ X2TEXT = "x2text"
+ PROMPT_STUDIO_TOOL = "prompt_studio_tool"
+ MAX_PROFILE_COUNT = 4
+
+
+class ProfileManagerErrors:
+ SERIALIZATION_FAILED = "Data Serialization Failed."
+ PROFILE_NAME_EXISTS = "A profile with this name already exists."
+ DUPLICATE_API = "It appears that a duplicate call may have been made."
+ PLATFORM_ERROR = "Seems an error occured in Platform Service."
diff --git a/backend/prompt_studio/prompt_profile_manager_v2/exceptions.py b/backend/prompt_studio/prompt_profile_manager_v2/exceptions.py
new file mode 100644
index 000000000..023f6ad1c
--- /dev/null
+++ b/backend/prompt_studio/prompt_profile_manager_v2/exceptions.py
@@ -0,0 +1,6 @@
+from rest_framework.exceptions import APIException
+
+
+class PlatformServiceError(APIException):
+ status_code = 400
+ default_detail = "Seems an error occured in Platform Service."
diff --git a/backend/prompt_studio/prompt_profile_manager_v2/models.py b/backend/prompt_studio/prompt_profile_manager_v2/models.py
new file mode 100644
index 000000000..303e96f0f
--- /dev/null
+++ b/backend/prompt_studio/prompt_profile_manager_v2/models.py
@@ -0,0 +1,115 @@
+import uuid
+
+from account_v2.models import User
+from adapter_processor_v2.models import AdapterInstance
+from django.db import models
+from prompt_studio.prompt_studio_core_v2.exceptions import DefaultProfileError
+from prompt_studio.prompt_studio_core_v2.models import CustomTool
+from utils.models.base_model import BaseModel
+
+
+class ProfileManager(BaseModel):
+ """Model to store the LLM Triad management details for Prompt."""
+
+ class RetrievalStrategy(models.TextChoices):
+ SIMPLE = "simple", "Simple retrieval"
+ SUBQUESTION = "subquestion", "Subquestion retrieval"
+
+ profile_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
+ profile_name = models.TextField(blank=False)
+ vector_store = models.ForeignKey(
+ AdapterInstance,
+ db_comment="Field to store the chosen vector store.",
+ blank=False,
+ null=False,
+ on_delete=models.PROTECT,
+ related_name="profiles_vector_store",
+ )
+ embedding_model = models.ForeignKey(
+ AdapterInstance,
+ blank=False,
+ null=False,
+ on_delete=models.PROTECT,
+ related_name="profiles_embedding_model",
+ )
+ llm = models.ForeignKey(
+ AdapterInstance,
+ db_comment="Field to store the LLM chosen by the user",
+ blank=False,
+ null=False,
+ on_delete=models.PROTECT,
+ related_name="profiles_llm",
+ )
+ x2text = models.ForeignKey(
+ AdapterInstance,
+ db_comment="Field to store the X2Text Adapter chosen by the user",
+ blank=False,
+ null=False,
+ on_delete=models.PROTECT,
+ related_name="profiles_x2text",
+ )
+ chunk_size = models.IntegerField(null=True, blank=True)
+ chunk_overlap = models.IntegerField(null=True, blank=True)
+ reindex = models.BooleanField(default=False)
+ retrieval_strategy = models.TextField(
+ choices=RetrievalStrategy.choices,
+ blank=True,
+ db_comment="Field to store the retrieval strategy for prompts",
+ )
+ similarity_top_k = models.IntegerField(
+ blank=True,
+ null=True,
+ db_comment="Field to store number of top embeddings to take into context", # noqa: E501
+ )
+ section = models.TextField(
+ blank=True, null=True, db_comment="Field to store limit to section"
+ )
+ created_by = models.ForeignKey(
+ User,
+ on_delete=models.SET_NULL,
+ related_name="profile_managers_created",
+ null=True,
+ blank=True,
+ editable=False,
+ )
+ modified_by = models.ForeignKey(
+ User,
+ on_delete=models.SET_NULL,
+ related_name="profile_managers_modified",
+ null=True,
+ blank=True,
+ editable=False,
+ )
+
+ prompt_studio_tool = models.ForeignKey(
+ CustomTool, on_delete=models.CASCADE, null=True, related_name="profile_managers"
+ )
+ is_default = models.BooleanField(
+ default=False,
+ db_comment="Default LLM Profile used in prompt",
+ )
+
+ is_summarize_llm = models.BooleanField(
+ default=False,
+ db_comment="Default LLM Profile used for summarizing",
+ )
+
+ class Meta:
+ verbose_name = "Profile Manager"
+ verbose_name_plural = "Profile Managers"
+ db_table = "profile_manager_v2"
+ constraints = [
+ models.UniqueConstraint(
+ fields=["prompt_studio_tool", "profile_name"],
+ name="unique_prompt_studio_tool_profile_name_index",
+ ),
+ ]
+
+ @staticmethod
+ def get_default_llm_profile(tool: CustomTool) -> "ProfileManager":
+ try:
+ return ProfileManager.objects.get( # type: ignore
+ prompt_studio_tool=tool, is_default=True
+ )
+ except ProfileManager.DoesNotExist:
+ raise DefaultProfileError
diff --git a/backend/prompt_studio/prompt_profile_manager_v2/profile_manager_helper.py b/backend/prompt_studio/prompt_profile_manager_v2/profile_manager_helper.py
new file mode 100644
index 000000000..48c731b08
--- /dev/null
+++ b/backend/prompt_studio/prompt_profile_manager_v2/profile_manager_helper.py
@@ -0,0 +1,11 @@
+from prompt_studio.prompt_profile_manager_v2.models import ProfileManager
+
+
+class ProfileManagerHelper:
+
+ @classmethod
+ def get_profile_manager(cls, profile_manager_id: str) -> ProfileManager:
+ try:
+ return ProfileManager.objects.get(profile_id=profile_manager_id)
+ except ProfileManager.DoesNotExist:
+ raise ValueError("ProfileManager does not exist.")
diff --git a/backend/prompt_studio/prompt_profile_manager_v2/serializers.py b/backend/prompt_studio/prompt_profile_manager_v2/serializers.py
new file mode 100644
index 000000000..6810918e1
--- /dev/null
+++ b/backend/prompt_studio/prompt_profile_manager_v2/serializers.py
@@ -0,0 +1,53 @@
+import logging
+
+from adapter_processor_v2.adapter_processor import AdapterProcessor
+from prompt_studio.prompt_profile_manager_v2.constants import ProfileManagerKeys
+from prompt_studio.prompt_studio_core_v2.exceptions import MaxProfilesReachedError
+
+from backend.serializers import AuditSerializer
+
+from .models import ProfileManager
+
+logger = logging.getLogger(__name__)
+
+
+class ProfileManagerSerializer(AuditSerializer):
+ class Meta:
+ model = ProfileManager
+ fields = "__all__"
+
+ def to_representation(self, instance): # type: ignore
+ rep: dict[str, str] = super().to_representation(instance)
+ llm = rep[ProfileManagerKeys.LLM]
+ embedding = rep[ProfileManagerKeys.EMBEDDING_MODEL]
+ vector_db = rep[ProfileManagerKeys.VECTOR_STORE]
+ x2text = rep[ProfileManagerKeys.X2TEXT]
+ if llm:
+ rep[ProfileManagerKeys.LLM] = AdapterProcessor.get_adapter_instance_by_id(
+ llm
+ )
+ if embedding:
+ rep[ProfileManagerKeys.EMBEDDING_MODEL] = (
+ AdapterProcessor.get_adapter_instance_by_id(embedding)
+ )
+ if vector_db:
+ rep[ProfileManagerKeys.VECTOR_STORE] = (
+ AdapterProcessor.get_adapter_instance_by_id(vector_db)
+ )
+ if x2text:
+ rep[ProfileManagerKeys.X2TEXT] = (
+ AdapterProcessor.get_adapter_instance_by_id(x2text)
+ )
+ return rep
+
+ def validate(self, data):
+ prompt_studio_tool = data.get(ProfileManagerKeys.PROMPT_STUDIO_TOOL)
+
+ profile_count = ProfileManager.objects.filter(
+ prompt_studio_tool=prompt_studio_tool
+ ).count()
+
+ if profile_count >= ProfileManagerKeys.MAX_PROFILE_COUNT:
+ raise MaxProfilesReachedError()
+
+ return data
diff --git a/backend/prompt_studio/prompt_profile_manager_v2/urls.py b/backend/prompt_studio/prompt_profile_manager_v2/urls.py
new file mode 100644
index 000000000..ae95f1fb9
--- /dev/null
+++ b/backend/prompt_studio/prompt_profile_manager_v2/urls.py
@@ -0,0 +1,24 @@
+from django.urls import path
+from rest_framework.urlpatterns import format_suffix_patterns
+
+from .views import ProfileManagerView
+
+profile_manager_detail = ProfileManagerView.as_view(
+ {
+ "get": "retrieve",
+ "put": "update",
+ "patch": "partial_update",
+ "delete": "destroy",
+ }
+)
+
+
+urlpatterns = format_suffix_patterns(
+ [
+ path(
+ "profile-manager/<uuid:pk>/",
+ profile_manager_detail,
+ name="profile-manager-detail",
+ ),
+ ]
+)
diff --git a/backend/prompt_studio/prompt_profile_manager_v2/views.py b/backend/prompt_studio/prompt_profile_manager_v2/views.py
new file mode 100644
index 000000000..3bfd4a5fd
--- /dev/null
+++ b/backend/prompt_studio/prompt_profile_manager_v2/views.py
@@ -0,0 +1,50 @@
+from typing import Any, Optional
+
+from account_v2.custom_exceptions import DuplicateData
+from django.db import IntegrityError
+from django.db.models import QuerySet
+from django.http import HttpRequest
+from permissions.permission import IsOwner
+from prompt_studio.prompt_profile_manager_v2.constants import (
+ ProfileManagerErrors,
+ ProfileManagerKeys,
+)
+from prompt_studio.prompt_profile_manager_v2.serializers import ProfileManagerSerializer
+from rest_framework import status, viewsets
+from rest_framework.response import Response
+from rest_framework.versioning import URLPathVersioning
+from utils.filtering import FilterHelper
+
+from .models import ProfileManager
+
+
+class ProfileManagerView(viewsets.ModelViewSet):
+ """Viewset to handle all Custom tool related operations."""
+
+ versioning_class = URLPathVersioning
+ permission_classes = [IsOwner]
+ serializer_class = ProfileManagerSerializer
+
+ def get_queryset(self) -> Optional[QuerySet]:
+ filter_args = FilterHelper.build_filter_args(
+ self.request,
+ ProfileManagerKeys.CREATED_BY,
+ )
+ if filter_args:
+ queryset = ProfileManager.objects.filter(**filter_args)
+ else:
+ queryset = ProfileManager.objects.all()
+ return queryset
+
+ def create(
+ self, request: HttpRequest, *args: tuple[Any], **kwargs: dict[str, Any]
+ ) -> Response:
+ serializer: ProfileManagerSerializer = self.get_serializer(data=request.data)
+ # Overriding default exception behaviour
+ # TO DO : Handle model related exceptions.
+ serializer.is_valid(raise_exception=True)
+ try:
+ self.perform_create(serializer)
+ except IntegrityError:
+ raise DuplicateData(ProfileManagerErrors.PROFILE_NAME_EXISTS)
+ return Response(serializer.data, status=status.HTTP_201_CREATED)
diff --git a/backend/prompt_studio/prompt_studio_core_v2/__init__.py b/backend/prompt_studio/prompt_studio_core_v2/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/backend/prompt_studio/prompt_studio_core_v2/admin.py b/backend/prompt_studio/prompt_studio_core_v2/admin.py
new file mode 100644
index 000000000..e6e1457ea
--- /dev/null
+++ b/backend/prompt_studio/prompt_studio_core_v2/admin.py
@@ -0,0 +1,5 @@
+from django.contrib import admin
+
+from .models import CustomTool
+
+admin.site.register(CustomTool)
diff --git a/backend/prompt_studio/prompt_studio_core_v2/apps.py b/backend/prompt_studio/prompt_studio_core_v2/apps.py
new file mode 100644
index 000000000..085567889
--- /dev/null
+++ b/backend/prompt_studio/prompt_studio_core_v2/apps.py
@@ -0,0 +1,5 @@
+from django.apps import AppConfig
+
+
+class CustomTool(AppConfig):
+ name = "prompt_studio.prompt_studio_core_v2"
diff --git a/backend/prompt_studio/prompt_studio_core_v2/constants.py b/backend/prompt_studio/prompt_studio_core_v2/constants.py
new file mode 100644
index 000000000..55d61e32e
--- /dev/null
+++ b/backend/prompt_studio/prompt_studio_core_v2/constants.py
@@ -0,0 +1,134 @@
+from enum import Enum
+
+
+class ToolStudioKeys:
+ CREATED_BY = "created_by"
+ TOOL_ID = "tool_id"
+ PROMPTS = "prompts"
+ PLATFORM_SERVICE_API_KEY = "PLATFORM_SERVICE_API_KEY"
+ SUMMARIZE_LLM_PROFILE = "summarize_llm_profile"
+ DEFAULT_PROFILE = "default_profile"
+
+
+class ToolStudioErrors:
+ SERIALIZATION_FAILED = "Data Serialization Failed."
+ TOOL_NAME_EXISTS = "Tool with the name already exists"
+ DUPLICATE_API = "It appears that a duplicate call may have been made."
+ PLATFORM_ERROR = "Seems an error occured in Platform Service."
+ PROMPT_NAME_EXISTS = "Prompt with the name already exists"
+
+
+class ToolStudioPromptKeys:
+ CREATED_BY = "created_by"
+ TOOL_ID = "tool_id"
+ RUN_ID = "run_id"
+ NUMBER = "Number"
+ FLOAT = "Float"
+ PG_VECTOR = "Postgres pg_vector"
+ ANSWERS = "answers"
+ UNIQUE_FILE_ID = "unique_file_id"
+ ID = "id"
+ FILE_NAME = "file_name"
+ FILE_HASH = "file_hash"
+ TOOL_ID = "tool_id"
+ NAME = "name"
+ ACTIVE = "active"
+ PROMPT = "prompt"
+ CHUNK_SIZE = "chunk-size"
+ PROMPTX = "promptx"
+ VECTOR_DB = "vector-db"
+ EMBEDDING = "embedding"
+ X2TEXT_ADAPTER = "x2text_adapter"
+ CHUNK_OVERLAP = "chunk-overlap"
+ LLM = "llm"
+ IS_ASSERT = "is_assert"
+ ASSERTION_FAILURE_PROMPT = "assertion_failure_prompt"
+ RETRIEVAL_STRATEGY = "retrieval-strategy"
+ SIMPLE = "simple"
+ TYPE = "type"
+ NUMBER = "number"
+ EMAIL = "email"
+ DATE = "date"
+ BOOLEAN = "boolean"
+ JSON = "json"
+ PREAMBLE = "preamble"
+ SIMILARITY_TOP_K = "similarity-top-k"
+ PROMPT_TOKENS = "prompt_tokens"
+ COMPLETION_TOKENS = "completion_tokens"
+ TOTAL_TOKENS = "total_tokens"
+ RESPONSE = "response"
+ POSTAMBLE = "postamble"
+ GRAMMAR = "grammar"
+ WORD = "word"
+ SYNONYMS = "synonyms"
+ OUTPUTS = "outputs"
+ ASSERT_PROMPT = "assert_prompt"
+ SECTION = "section"
+ DEFAULT = "default"
+ REINDEX = "reindex"
+ EMBEDDING_SUFFIX = "embedding_suffix"
+ EVAL_METRIC_PREFIX = "eval_"
+ EVAL_RESULT_DELIM = "__"
+ EVAL_SETTINGS = "eval_settings"
+ EVAL_SETTINGS_EVALUATE = "evaluate"
+ EVAL_SETTINGS_MONITOR_LLM = "monitor_llm"
+ EVAL_SETTINGS_EXCLUDE_FAILED = "exclude_failed"
+ SUMMARIZE = "summarize"
+ SUMMARIZED_RESULT = "summarized_result"
+ DOCUMENT_ID = "document_id"
+ EXTRACT = "extract"
+ TOOL_SETTINGS = "tool_settings"
+ ENABLE_CHALLENGE = "enable_challenge"
+ CHALLENGE_LLM = "challenge_llm"
+ SINGLE_PASS_EXTRACTION_MODE = "single_pass_extraction_mode"
+ SINGLE_PASS_EXTRACTION = "single_pass_extraction"
+ NOTES = "NOTES"
+ OUTPUT = "output"
+ SEQUENCE_NUMBER = "sequence_number"
+ PROFILE_MANAGER_ID = "profile_manager"
+ CONTEXT = "context"
+ METADATA = "metadata"
+
+
+class FileViewTypes:
+ ORIGINAL = "ORIGINAL"
+ EXTRACT = "EXTRACT"
+ SUMMARIZE = "SUMMARIZE"
+
+
+class LogLevels:
+ INFO = "INFO"
+ ERROR = "ERROR"
+ DEBUG = "DEBUG"
+ RUN = "RUN"
+
+
+class LogLevel(Enum):
+ DEBUG = "DEBUG"
+ INFO = "INFO"
+ WARN = "WARN"
+ ERROR = "ERROR"
+ FATAL = "FATAL"
+
+
+class IndexingStatus(Enum):
+ PENDING_STATUS = "pending"
+ COMPLETED_STATUS = "completed"
+ STARTED_STATUS = "started"
+ DOCUMENT_BEING_INDEXED = "Document is being indexed"
+
+
+class DefaultPrompts:
+ PREAMBLE = (
+ "Your ability to extract and summarize this context accurately "
+ "is essential for effective analysis. "
+ "Pay close attention to the context's language, structure, and any "
+ "cross-references to ensure a comprehensive and precise extraction "
+ "of information. Do not use prior knowledge or information from "
+ "outside the context to answer the questions. Only use the "
+ "information provided in the context to answer the questions."
+ )
+ POSTAMBLE = (
+ "Do not include any explanation in the reply. "
+ "Only include the extracted information in the reply."
+ )
diff --git a/backend/prompt_studio/prompt_studio_core_v2/document_indexing_service.py b/backend/prompt_studio/prompt_studio_core_v2/document_indexing_service.py
new file mode 100644
index 000000000..a323b73fc
--- /dev/null
+++ b/backend/prompt_studio/prompt_studio_core_v2/document_indexing_service.py
@@ -0,0 +1,53 @@
+from typing import Optional
+
+from django.conf import settings
+from prompt_studio.prompt_studio_core_v2.constants import IndexingStatus
+from utils.cache_service import CacheService
+
+
+class DocumentIndexingService:
+ CACHE_PREFIX = "document_indexing:"
+
+ @classmethod
+ def set_document_indexing(cls, org_id: str, user_id: str, doc_id_key: str) -> None:
+ CacheService.set_key(
+ cls._cache_key(org_id, user_id, doc_id_key),
+ IndexingStatus.STARTED_STATUS.value,
+ expire=settings.INDEXING_FLAG_TTL,
+ )
+
+ @classmethod
+ def is_document_indexing(cls, org_id: str, user_id: str, doc_id_key: str) -> bool:
+ return (
+ CacheService.get_key(cls._cache_key(org_id, user_id, doc_id_key))
+ == IndexingStatus.STARTED_STATUS.value
+ )
+
+ @classmethod
+ def mark_document_indexed(
+ cls, org_id: str, user_id: str, doc_id_key: str, doc_id: str
+ ) -> None:
+ CacheService.set_key(
+ cls._cache_key(org_id, user_id, doc_id_key),
+ doc_id,
+ expire=settings.INDEXING_FLAG_TTL,
+ )
+
+ @classmethod
+ def get_indexed_document_id(
+ cls, org_id: str, user_id: str, doc_id_key: str
+ ) -> Optional[str]:
+ result = CacheService.get_key(cls._cache_key(org_id, user_id, doc_id_key))
+ if result and result != IndexingStatus.STARTED_STATUS.value:
+ return result
+ return None
+
+ @classmethod
+ def remove_document_indexing(
+ cls, org_id: str, user_id: str, doc_id_key: str
+ ) -> None:
+ CacheService.delete_a_key(cls._cache_key(org_id, user_id, doc_id_key))
+
+ @classmethod
+ def _cache_key(cls, org_id: str, user_id: str, doc_id_key: str) -> str:
+ return f"{cls.CACHE_PREFIX}{org_id}:{user_id}:{doc_id_key}"
diff --git a/backend/prompt_studio/prompt_studio_core_v2/exceptions.py b/backend/prompt_studio/prompt_studio_core_v2/exceptions.py
new file mode 100644
index 000000000..a58f672d2
--- /dev/null
+++ b/backend/prompt_studio/prompt_studio_core_v2/exceptions.py
@@ -0,0 +1,69 @@
+from prompt_studio.prompt_profile_manager_v2.constants import ProfileManagerKeys
+from prompt_studio.prompt_studio_core_v2.constants import ToolStudioErrors
+from rest_framework.exceptions import APIException
+
+
+class PlatformServiceError(APIException):
+ status_code = 400
+ default_detail = ToolStudioErrors.PLATFORM_ERROR
+
+
+class ToolNotValid(APIException):
+ status_code = 400
+ default_detail = "Custom tool is not valid."
+
+
+class IndexingAPIError(APIException):
+ status_code = 500
+ default_detail = "Error while indexing file"
+
+
+class AnswerFetchError(APIException):
+ status_code = 500
+ default_detail = "Error occured while fetching response for the prompt"
+
+
+class DefaultProfileError(APIException):
+ status_code = 500
+ default_detail = (
+ "Default LLM profile is not configured."
+ "Please set an LLM profile as default to continue."
+ )
+
+
+class EnvRequired(APIException):
+ status_code = 404
+ default_detail = "Environment variable not set"
+
+
+class OutputSaveError(APIException):
+ status_code = 500
+ default_detail = "Unable to store the output."
+
+
+class ToolDeleteError(APIException):
+ status_code = 500
+ default_detail = "Failed to delete the error"
+
+
+class NoPromptsFound(APIException):
+ status_code = 404
+ default_detail = "No prompts available to process"
+
+
+class PermissionError(APIException):
+ status_code = 403
+ default_detail = "You do not have permission to perform this action."
+
+
+class EmptyPromptError(APIException):
+ status_code = 422
+ default_detail = "Prompt(s) cannot be empty"
+
+
+class MaxProfilesReachedError(APIException):
+ status_code = 403
+ default_detail = (
+ f"Maximum number of profiles (max {ProfileManagerKeys.MAX_PROFILE_COUNT})"
+ " per prompt studio project has been reached."
+ )
diff --git a/backend/prompt_studio/prompt_studio_core_v2/models.py b/backend/prompt_studio/prompt_studio_core_v2/models.py
new file mode 100644
index 000000000..f4784e47d
--- /dev/null
+++ b/backend/prompt_studio/prompt_studio_core_v2/models.py
@@ -0,0 +1,158 @@
+import logging
+import shutil
+import uuid
+from typing import Any
+
+from account_v2.models import User
+from adapter_processor_v2.models import AdapterInstance
+from django.db import models
+from django.db.models import QuerySet
+from file_management.file_management_helper import FileManagerHelper
+from prompt_studio.prompt_studio_core_v2.constants import DefaultPrompts
+from utils.models.base_model import BaseModel
+from utils.models.organization_mixin import (
+ DefaultOrganizationManagerMixin,
+ DefaultOrganizationMixin,
+)
+
+logger = logging.getLogger(__name__)
+
+
+class CustomToolModelManager(DefaultOrganizationManagerMixin, models.Manager):
+
+ def for_user(self, user: User) -> QuerySet[Any]:
+ return (
+ self.get_queryset()
+ .filter(models.Q(created_by=user) | models.Q(shared_users=user))
+ .distinct("tool_id")
+ )
+
+
+class CustomTool(DefaultOrganizationMixin, BaseModel):
+ """Model to store the custom tools designed in the tool studio."""
+
+ tool_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
+ tool_name = models.TextField(blank=False, null=False)
+ description = models.TextField(blank=False, null=False)
+ author = models.TextField(
+ blank=False,
+ null=False,
+ db_comment="Specific to the user who created the tool.",
+ )
+ icon = models.TextField(
+ blank=True,
+ db_comment="Field to store \
+ icon url for the custom tool.",
+ )
+ output = models.TextField(
+ db_comment="Field to store the output format type.",
+ blank=True,
+ )
+ log_id = models.UUIDField(
+ default=uuid.uuid4,
+ db_comment="Field to store unique log_id for polling",
+ )
+
+ summarize_context = models.BooleanField(
+ default=False, db_comment="Flag to summarize content"
+ )
+ summarize_as_source = models.BooleanField(
+ default=False, db_comment="Flag to use summarized content as source"
+ )
+ summarize_prompt = models.TextField(
+ blank=True,
+ db_comment="Field to store the summarize prompt",
+ unique=False,
+ )
+ preamble = models.TextField(
+ blank=True,
+ db_comment="Preamble to the prompts",
+ default=DefaultPrompts.PREAMBLE,
+ )
+ postamble = models.TextField(
+ blank=True,
+ db_comment="Appended as postable to prompts.",
+ default=DefaultPrompts.POSTAMBLE,
+ )
+ prompt_grammer = models.JSONField(
+ null=True, blank=True, db_comment="Synonymous words used in prompt"
+ )
+ monitor_llm = models.ForeignKey(
+ AdapterInstance,
+ on_delete=models.PROTECT,
+ db_comment="Field to store monitor llm",
+ null=True,
+ blank=True,
+ related_name="custom_tools_monitor",
+ )
+ created_by = models.ForeignKey(
+ User,
+ on_delete=models.SET_NULL,
+ null=True,
+ blank=True,
+ editable=False,
+ related_name="custom_tools_created",
+ )
+ modified_by = models.ForeignKey(
+ User,
+ on_delete=models.SET_NULL,
+ null=True,
+ blank=True,
+ editable=False,
+ related_name="custom_tools_modified",
+ )
+
+ exclude_failed = models.BooleanField(
+ db_comment="Flag to make the answer null if it is incorrect",
+ default=True,
+ )
+ single_pass_extraction_mode = models.BooleanField(
+ db_comment="Flag to enable or disable single pass extraction mode",
+ default=False,
+ )
+ challenge_llm = models.ForeignKey(
+ AdapterInstance,
+ on_delete=models.PROTECT,
+ db_comment="Field to store challenge llm",
+ null=True,
+ blank=True,
+ related_name="custom_tools_challenge",
+ )
+ enable_challenge = models.BooleanField(
+ db_comment="Flag to enable or disable challenge", default=False
+ )
+
+ # Introduced field to establish M2M relation between users and custom_tool.
+ # This will introduce intermediary table which relates both the models.
+ shared_users = models.ManyToManyField(User, related_name="shared_custom_tools")
+
+ objects = CustomToolModelManager()
+
+ def delete(self, organization_id=None, *args, **kwargs):
+ # Delete the documents associated with the tool
+ file_path = FileManagerHelper.handle_sub_directory_for_tenants(
+ organization_id,
+ is_create=False,
+ user_id=self.created_by.user_id,
+ tool_id=str(self.tool_id),
+ )
+ if organization_id:
+ try:
+ shutil.rmtree(file_path)
+ except FileNotFoundError:
+ logger.error(f"The folder {file_path} does not exist.")
+ except OSError as e:
+ logger.error(f"Error: {file_path} : {e.strerror}")
+ # Continue with the deletion of the tool
+ super().delete(*args, **kwargs)
+
+ class Meta:
+ verbose_name = "Custom Tool"
+ verbose_name_plural = "Custom Tools"
+ db_table = "custom_tool_v2"
+ constraints = [
+ models.UniqueConstraint(
+ fields=["tool_name", "organization"],
+ name="unique_tool_name",
+ ),
+ ]
diff --git a/backend/prompt_studio/prompt_studio_core_v2/prompt_ide_base_tool.py b/backend/prompt_studio/prompt_studio_core_v2/prompt_ide_base_tool.py
new file mode 100644
index 000000000..d2b403974
--- /dev/null
+++ b/backend/prompt_studio/prompt_studio_core_v2/prompt_ide_base_tool.py
@@ -0,0 +1,42 @@
+import os
+
+from platform_settings_v2.platform_auth_service import PlatformAuthenticationService
+from prompt_studio.prompt_studio_core_v2.constants import LogLevel, ToolStudioKeys
+from unstract.sdk.tool.stream import StreamMixin
+
+
+class PromptIdeBaseTool(StreamMixin):
+ def __init__(self, log_level: LogLevel = LogLevel.INFO, org_id: str = "") -> None:
+ """
+ Args:
+ tool (UnstractAbstractTool): Instance of UnstractAbstractTool
+ Notes:
+ - PLATFORM_SERVICE_API_KEY environment variable is required.
+ """
+ self.log_level = log_level
+ self.org_id = org_id
+ super().__init__(log_level=log_level)
+
+ def get_env_or_die(self, env_key: str) -> str:
+ """Returns the value of an env variable.
+
+ If its empty or None, raises an error and exits
+
+ Args:
+ env_key (str): Key to retrieve
+
+ Returns:
+ str: Value of the env
+ """
+ # HACK: Adding platform key for multitenancy
+ if env_key == ToolStudioKeys.PLATFORM_SERVICE_API_KEY:
+ platform_key = PlatformAuthenticationService.get_active_platform_key(
+ self.org_id
+ )
+ key: str = str(platform_key.key)
+ return key
+ else:
+ env_value = os.environ.get(env_key)
+ if env_value is None or env_value == "":
+ self.stream_error_and_exit(f"Env variable {env_key} is required")
+ return env_value # type:ignore
diff --git a/backend/prompt_studio/prompt_studio_core_v2/prompt_studio_helper.py b/backend/prompt_studio/prompt_studio_core_v2/prompt_studio_helper.py
new file mode 100644
index 000000000..a597d1a4f
--- /dev/null
+++ b/backend/prompt_studio/prompt_studio_core_v2/prompt_studio_helper.py
@@ -0,0 +1,972 @@
+import json
+import logging
+import os
+import uuid
+from pathlib import Path
+from typing import Any, Optional
+
+from account_v2.constants import Common
+from account_v2.models import User
+from adapter_processor_v2.constants import AdapterKeys
+from adapter_processor_v2.models import AdapterInstance
+from django.conf import settings
+from django.db.models.manager import BaseManager
+from file_management.file_management_helper import FileManagerHelper
+from prompt_studio.prompt_profile_manager_v2.models import ProfileManager
+from prompt_studio.prompt_profile_manager_v2.profile_manager_helper import (
+ ProfileManagerHelper,
+)
+from prompt_studio.prompt_studio_core_v2.constants import IndexingStatus, LogLevels
+from prompt_studio.prompt_studio_core_v2.constants import (
+ ToolStudioPromptKeys as TSPKeys,
+)
+from prompt_studio.prompt_studio_core_v2.document_indexing_service import (
+ DocumentIndexingService,
+)
+from prompt_studio.prompt_studio_core_v2.exceptions import (
+ AnswerFetchError,
+ DefaultProfileError,
+ EmptyPromptError,
+ IndexingAPIError,
+ NoPromptsFound,
+ PermissionError,
+ ToolNotValid,
+)
+from prompt_studio.prompt_studio_core_v2.models import CustomTool
+from prompt_studio.prompt_studio_core_v2.prompt_ide_base_tool import PromptIdeBaseTool
+from prompt_studio.prompt_studio_document_manager_v2.models import DocumentManager
+from prompt_studio.prompt_studio_index_manager_v2.prompt_studio_index_helper import ( # noqa: E501
+ PromptStudioIndexHelper,
+)
+from prompt_studio.prompt_studio_output_manager_v2.output_manager_helper import (
+ OutputManagerHelper,
+)
+from prompt_studio.prompt_studio_v2.models import ToolStudioPrompt
+from unstract.sdk.constants import LogLevel
+from unstract.sdk.exceptions import IndexingError, SdkError
+from unstract.sdk.index import Index
+from unstract.sdk.prompt import PromptTool
+from unstract.sdk.utils.tool_utils import ToolUtils
+from utils.local_context import StateStore
+
+from unstract.core.pubsub_helper import LogPublisher
+
+CHOICES_JSON = "/static/select_choices.json"
+ERROR_MSG = "User %s doesn't have access to adapter %s"
+
+logger = logging.getLogger(__name__)
+
+
+class PromptStudioHelper:
+ """Helper class for Custom tool operations."""
+
+ @staticmethod
+ def create_default_profile_manager(user: User, tool_id: uuid) -> None:
+ """Create a default profile manager for a given user and tool.
+
+ Args:
+ user (User): The user for whom the default profile manager is
+ created.
+ tool_id (uuid): The ID of the tool for which the default profile
+ manager is created.
+
+ Raises:
+ AdapterInstance.DoesNotExist: If no suitable adapter instance is
+ found for creating the default profile manager.
+
+ Returns:
+ None
+ """
+ try:
+ AdapterInstance.objects.get(
+ is_friction_less=True,
+ is_usable=True,
+ adapter_type=AdapterKeys.LLM,
+ )
+
+ default_adapters: BaseManager[AdapterInstance] = (
+ AdapterInstance.objects.filter(is_friction_less=True)
+ )
+
+ profile_manager = ProfileManager(
+ prompt_studio_tool=CustomTool.objects.get(pk=tool_id),
+ is_default=True,
+ created_by=user,
+ modified_by=user,
+ chunk_size=0,
+ profile_name="sample profile",
+ chunk_overlap=0,
+ section="Default",
+ retrieval_strategy="simple",
+ similarity_top_k=3,
+ )
+
+ for adapter in default_adapters:
+ if adapter.adapter_type == AdapterKeys.LLM:
+ profile_manager.llm = adapter
+ elif adapter.adapter_type == AdapterKeys.VECTOR_DB:
+ profile_manager.vector_store = adapter
+ elif adapter.adapter_type == AdapterKeys.X2TEXT:
+ profile_manager.x2text = adapter
+ elif adapter.adapter_type == AdapterKeys.EMBEDDING:
+ profile_manager.embedding_model = adapter
+
+ profile_manager.save()
+
+ except AdapterInstance.DoesNotExist:
+ logger.info("skipping default profile creation")
+
+ @staticmethod
+ def validate_adapter_status(
+ profile_manager: ProfileManager,
+ ) -> None:
+ """Helper method to validate the status of adapters in profile manager.
+
+ Args:
+ profile_manager (ProfileManager): The profile manager instance to
+ validate.
+
+ Raises:
+ PermissionError: If the owner does not have permission to perform
+ the action.
+ """
+
+ error_msg = "Permission Error: Free usage for the configured trial adapter exhausted.Please connect your own service accounts to continue.Please see our documentation for more details:https://docs.unstract.com/unstract_platform/setup_accounts/whats_needed" # noqa: E501
+ adapters = [
+ profile_manager.llm,
+ profile_manager.vector_store,
+ profile_manager.embedding_model,
+ profile_manager.x2text,
+ ]
+
+ for adapter in adapters:
+ if not adapter.is_usable:
+ raise PermissionError(error_msg)
+
+ @staticmethod
+ def validate_profile_manager_owner_access(
+ profile_manager: ProfileManager,
+ ) -> None:
+ """Helper method to validate the owner's access to the profile manager.
+
+ Args:
+ profile_manager (ProfileManager): The profile manager instance to
+ validate.
+
+ Raises:
+ PermissionError: If the owner does not have permission to perform
+ the action.
+ """
+ profile_manager_owner = profile_manager.created_by
+
+ is_llm_owned = (
+ profile_manager.llm.shared_to_org
+ or profile_manager.llm.created_by == profile_manager_owner
+ or profile_manager.llm.shared_users.filter(
+ pk=profile_manager_owner.pk
+ ).exists()
+ )
+ is_vector_store_owned = (
+ profile_manager.vector_store.shared_to_org
+ or profile_manager.vector_store.created_by == profile_manager_owner
+ or profile_manager.vector_store.shared_users.filter(
+ pk=profile_manager_owner.pk
+ ).exists()
+ )
+ is_embedding_model_owned = (
+ profile_manager.embedding_model.shared_to_org
+ or profile_manager.embedding_model.created_by == profile_manager_owner
+ or profile_manager.embedding_model.shared_users.filter(
+ pk=profile_manager_owner.pk
+ ).exists()
+ )
+ is_x2text_owned = (
+ profile_manager.x2text.shared_to_org
+ or profile_manager.x2text.created_by == profile_manager_owner
+ or profile_manager.x2text.shared_users.filter(
+ pk=profile_manager_owner.pk
+ ).exists()
+ )
+
+ if not (
+ is_llm_owned
+ and is_vector_store_owned
+ and is_embedding_model_owned
+ and is_x2text_owned
+ ):
+ adapter_names = set()
+ if not is_llm_owned:
+ logger.error(
+ ERROR_MSG,
+ profile_manager_owner.user_id,
+ profile_manager.llm.id,
+ )
+ adapter_names.add(profile_manager.llm.adapter_name)
+ if not is_vector_store_owned:
+ logger.error(
+ ERROR_MSG,
+ profile_manager_owner.user_id,
+ profile_manager.vector_store.id,
+ )
+ adapter_names.add(profile_manager.vector_store.adapter_name)
+ if not is_embedding_model_owned:
+ logger.error(
+ ERROR_MSG,
+ profile_manager_owner.user_id,
+ profile_manager.embedding_model.id,
+ )
+ adapter_names.add(profile_manager.embedding_model.adapter_name)
+ if not is_x2text_owned:
+ logger.error(
+ ERROR_MSG,
+ profile_manager_owner.user_id,
+ profile_manager.x2text.id,
+ )
+ adapter_names.add(profile_manager.x2text.adapter_name)
+ if len(adapter_names) > 1:
+ error_msg = (
+ f"Multiple permission errors were encountered with {', '.join(adapter_names)}", # noqa: E501
+ )
+ else:
+ error_msg = (
+ f"Permission Error: You do not have access to {adapter_names.pop()}", # noqa: E501
+ )
+
+ raise PermissionError(error_msg)
+
+ @staticmethod
+ def _publish_log(
+ component: dict[str, str], level: str, state: str, message: str
+ ) -> None:
+ LogPublisher.publish(
+ StateStore.get(Common.LOG_EVENTS_ID),
+ LogPublisher.log_prompt(component, level, state, message),
+ )
+
+ @staticmethod
+ def get_select_fields() -> dict[str, Any]:
+ """Method to fetch dropdown field values for frontend.
+
+ Returns:
+ dict[str, Any]: Dict for dropdown data
+ """
+ f = open(f"{os.path.dirname(__file__)}{CHOICES_JSON}")
+ choices = f.read()
+ f.close()
+ response: dict[str, Any] = json.loads(choices)
+ return response
+
+ @staticmethod
+ def _fetch_prompt_from_id(id: str) -> ToolStudioPrompt:
+ """Internal function used to fetch prompt from ID.
+
+ Args:
+ id (_type_): UUID of the prompt
+
+ Returns:
+ ToolStudioPrompt: Instance of the model
+ """
+ prompt_instance: ToolStudioPrompt = ToolStudioPrompt.objects.get(pk=id)
+ return prompt_instance
+
+ @staticmethod
+ def fetch_prompt_from_tool(tool_id: str) -> list[ToolStudioPrompt]:
+ """Internal function used to fetch mapped prompts from ToolID.
+
+ Args:
+ tool_id (_type_): UUID of the tool
+
+ Returns:
+ List[ToolStudioPrompt]: List of instance of the model
+ """
+ prompt_instances: list[ToolStudioPrompt] = ToolStudioPrompt.objects.filter(
+ tool_id=tool_id
+ ).order_by(TSPKeys.SEQUENCE_NUMBER)
+ return prompt_instances
+
+ @staticmethod
+ def index_document(
+ tool_id: str,
+ file_name: str,
+ org_id: str,
+ user_id: str,
+ document_id: str,
+ is_summary: bool = False,
+ run_id: str = None,
+ ) -> Any:
+ """Method to index a document.
+
+ Args:
+ tool_id (str): Id of the tool
+ file_name (str): File to parse
+ org_id (str): The ID of the organization to which the user belongs.
+ user_id (str): The ID of the user who uploaded the document.
+ is_summary (bool, optional): Whether the document is a summary
+ or not. Defaults to False.
+
+ Raises:
+ ToolNotValid
+ IndexingError
+ """
+ tool: CustomTool = CustomTool.objects.get(pk=tool_id)
+ if is_summary:
+ profile_manager: ProfileManager = ProfileManager.objects.get(
+ prompt_studio_tool=tool, is_summarize_llm=True
+ )
+ default_profile = profile_manager
+ file_path = file_name
+ else:
+ default_profile = ProfileManager.get_default_llm_profile(tool)
+ file_path = FileManagerHelper.handle_sub_directory_for_tenants(
+ org_id,
+ is_create=False,
+ user_id=user_id,
+ tool_id=tool_id,
+ )
+ file_path = str(Path(file_path) / file_name)
+
+ if not tool:
+ logger.error(f"No tool instance found for the ID {tool_id}")
+ raise ToolNotValid()
+
+ logger.info(f"[{tool_id}] Indexing started for doc: {file_name}")
+ PromptStudioHelper._publish_log(
+ {"tool_id": tool_id, "run_id": run_id, "doc_name": file_name},
+ LogLevels.INFO,
+ LogLevels.RUN,
+ "Indexing started",
+ )
+
+ # Validate the status of adapter in profile manager
+ PromptStudioHelper.validate_adapter_status(default_profile)
+ # Need to check the user who created profile manager
+ # has access to adapters configured in profile manager
+ PromptStudioHelper.validate_profile_manager_owner_access(default_profile)
+
+ doc_id = PromptStudioHelper.dynamic_indexer(
+ profile_manager=default_profile,
+ tool_id=tool_id,
+ file_path=file_path,
+ org_id=org_id,
+ document_id=document_id,
+ is_summary=is_summary,
+ reindex=True,
+ run_id=run_id,
+ user_id=user_id,
+ )
+
+ logger.info(f"[{tool_id}] Indexing successful for doc: {file_name}")
+ PromptStudioHelper._publish_log(
+ {"tool_id": tool_id, "run_id": run_id, "doc_name": file_name},
+ LogLevels.INFO,
+ LogLevels.RUN,
+ "Indexing successful",
+ )
+
+ return doc_id.get("output")
+
+ @staticmethod
+ def prompt_responder(
+ tool_id: str,
+ org_id: str,
+ user_id: str,
+ document_id: str,
+ id: Optional[str] = None,
+ run_id: str = None,
+ profile_manager_id: Optional[str] = None,
+ ) -> Any:
+ """Execute chain/single run of the prompts. Makes a call to prompt
+ service and returns the dict of response.
+
+ Args:
+ tool_id (str): ID of tool created in prompt studio
+ org_id (str): Organization ID
+ user_id (str): User's ID
+ document_id (str): UUID of the document uploaded
+ id (Optional[str]): ID of the prompt
+ profile_manager_id (Optional[str]): UUID of the profile manager
+
+ Raises:
+ AnswerFetchError: Error from prompt-service
+
+ Returns:
+ Any: Dictionary containing the response from prompt-service
+ """
+ document: DocumentManager = DocumentManager.objects.get(pk=document_id)
+ doc_name: str = document.document_name
+ doc_path = PromptStudioHelper._get_document_path(
+ org_id, user_id, tool_id, doc_name
+ )
+
+ if id:
+ return PromptStudioHelper._execute_single_prompt(
+ id,
+ doc_path,
+ doc_name,
+ tool_id,
+ org_id,
+ user_id,
+ document_id,
+ run_id,
+ profile_manager_id,
+ )
+ else:
+ return PromptStudioHelper._execute_prompts_in_single_pass(
+ doc_path, tool_id, org_id, user_id, document_id, run_id
+ )
+
+ @staticmethod
+ def _execute_single_prompt(
+ id,
+ doc_path,
+ doc_name,
+ tool_id,
+ org_id,
+ user_id,
+ document_id,
+ run_id,
+ profile_manager_id,
+ ):
+ prompt_instance = PromptStudioHelper._fetch_prompt_from_id(id)
+ prompt_name = prompt_instance.prompt_key
+ PromptStudioHelper._publish_log(
+ {
+ "tool_id": tool_id,
+ "run_id": run_id,
+ "prompt_key": prompt_name,
+ "doc_name": doc_name,
+ },
+ LogLevels.INFO,
+ LogLevels.RUN,
+ "Executing single prompt",
+ )
+ prompts = [prompt_instance]
+ tool = prompt_instance.tool_id
+
+ if tool.summarize_as_source:
+ directory, filename = os.path.split(doc_path)
+ doc_path = os.path.join(
+ directory, TSPKeys.SUMMARIZE, os.path.splitext(filename)[0] + ".txt"
+ )
+
+ PromptStudioHelper._publish_log(
+ {
+ "tool_id": tool_id,
+ "run_id": run_id,
+ "prompt_key": prompt_name,
+ "doc_name": doc_name,
+ },
+ LogLevels.DEBUG,
+ LogLevels.RUN,
+ "Invoking prompt service",
+ )
+
+ try:
+ response = PromptStudioHelper._fetch_response(
+ doc_path=doc_path,
+ doc_name=doc_name,
+ tool=tool,
+ prompt=prompt_instance,
+ org_id=org_id,
+ document_id=document_id,
+ run_id=run_id,
+ profile_manager_id=profile_manager_id,
+ user_id=user_id,
+ )
+ return PromptStudioHelper._handle_response(
+ response, run_id, prompts, document_id, False, profile_manager_id
+ )
+ except Exception as e:
+ logger.error(
+ f"[{tool.tool_id}] Error while fetching response for "
+ f"prompt {id} and doc {document_id}: {e}"
+ )
+ msg = str(e)
+ PromptStudioHelper._publish_log(
+ {
+ "tool_id": tool_id,
+ "run_id": run_id,
+ "prompt_key": prompt_name,
+ "doc_name": doc_name,
+ },
+ LogLevels.ERROR,
+ LogLevels.RUN,
+ msg,
+ )
+ raise e
+
+ @staticmethod
+ def _execute_prompts_in_single_pass(
+ doc_path, tool_id, org_id, user_id, document_id, run_id
+ ):
+ prompts = PromptStudioHelper.fetch_prompt_from_tool(tool_id)
+ prompts = [prompt for prompt in prompts if prompt.prompt_type != TSPKeys.NOTES]
+ if not prompts:
+ logger.error(f"[{tool_id or 'NA'}] No prompts found for id: {id}")
+ raise NoPromptsFound()
+
+ PromptStudioHelper._publish_log(
+ {"tool_id": tool_id, "run_id": run_id, "prompt_id": str(id)},
+ LogLevels.INFO,
+ LogLevels.RUN,
+ "Executing prompts in single pass",
+ )
+
+ try:
+ tool = prompts[0].tool_id
+ response = PromptStudioHelper._fetch_single_pass_response(
+ file_path=doc_path,
+ tool=tool,
+ prompts=prompts,
+ org_id=org_id,
+ document_id=document_id,
+ run_id=run_id,
+ user_id=user_id,
+ )
+ return PromptStudioHelper._handle_response(
+ response, run_id, prompts, document_id, True
+ )
+ except Exception as e:
+ logger.error(
+ f"[{tool.tool_id}] Error while fetching single pass response: {e}"
+ )
+ PromptStudioHelper._publish_log(
+ {
+ "tool_id": tool_id,
+ "run_id": run_id,
+ "prompt_id": str(id),
+ },
+ LogLevels.ERROR,
+ LogLevels.RUN,
+ f"Failed to fetch single pass response. {e}",
+ )
+ raise e
+
+ @staticmethod
+ def _get_document_path(org_id, user_id, tool_id, doc_name):
+ doc_path = FileManagerHelper.handle_sub_directory_for_tenants(
+ org_id=org_id,
+ user_id=user_id,
+ tool_id=tool_id,
+ is_create=False,
+ )
+ return str(Path(doc_path) / doc_name)
+
+ @staticmethod
+ def _handle_response(
+ response, run_id, prompts, document_id, is_single_pass, profile_manager_id=None
+ ):
+ if response.get("status") == IndexingStatus.PENDING_STATUS.value:
+ return {
+ "status": IndexingStatus.PENDING_STATUS.value,
+ "message": IndexingStatus.DOCUMENT_BEING_INDEXED.value,
+ }
+
+ OutputManagerHelper.handle_prompt_output_update(
+ run_id=run_id,
+ prompts=prompts,
+ outputs=response["output"],
+ document_id=document_id,
+ is_single_pass_extract=is_single_pass,
+ profile_manager_id=profile_manager_id,
+ context=response["metadata"].get("context"),
+ )
+ return response
+
+ @staticmethod
+ def _fetch_response(
+ tool: CustomTool,
+ doc_path: str,
+ doc_name: str,
+ prompt: ToolStudioPrompt,
+ org_id: str,
+ document_id: str,
+ run_id: str,
+ user_id: str,
+ profile_manager_id: Optional[str] = None,
+ ) -> Any:
+ """Utility function to invoke prompt service. Used internally.
+
+ Args:
+ tool (CustomTool): CustomTool instance (prompt studio project)
+ doc_path (str): Path to the document
+ doc_name (str): Name of the document
+ prompt (ToolStudioPrompt): ToolStudioPrompt instance to fetch response
+ org_id (str): UUID of the organization
+ document_id (str): UUID of the document
+ profile_manager_id (Optional[str]): UUID of the profile manager
+ user_id (str): The ID of the user who uploaded the document
+
+
+ Raises:
+ DefaultProfileError: If no default profile is selected
+ AnswerFetchError: Due to failures in prompt service
+
+ Returns:
+ Any: Output from LLM
+ """
+
+ # Fetch the ProfileManager instance using the profile_manager_id if provided
+ profile_manager = prompt.profile_manager
+ if profile_manager_id:
+ profile_manager = ProfileManagerHelper.get_profile_manager(
+ profile_manager_id=profile_manager_id
+ )
+
+ monitor_llm_instance: Optional[AdapterInstance] = tool.monitor_llm
+ monitor_llm: Optional[str] = None
+ challenge_llm_instance: Optional[AdapterInstance] = tool.challenge_llm
+ challenge_llm: Optional[str] = None
+
+ if monitor_llm_instance:
+ monitor_llm = str(monitor_llm_instance.id)
+ else:
+ # Using default profile manager llm if monitor_llm is None
+ default_profile = ProfileManager.get_default_llm_profile(tool)
+ monitor_llm = str(default_profile.llm.id)
+
+ # Using default profile manager llm if challenge_llm is None
+ if challenge_llm_instance:
+ challenge_llm = str(challenge_llm_instance.id)
+ else:
+ default_profile = ProfileManager.get_default_llm_profile(tool)
+ challenge_llm = str(default_profile.llm.id)
+
+ # Need to check the user who created profile manager
+ PromptStudioHelper.validate_adapter_status(profile_manager)
+ # Need to check the user who created profile manager
+ # has access to adapters
+ PromptStudioHelper.validate_profile_manager_owner_access(profile_manager)
+ # Not checking reindex here as there might be
+ # change in Profile Manager
+ vector_db = str(profile_manager.vector_store.id)
+ embedding_model = str(profile_manager.embedding_model.id)
+ llm = str(profile_manager.llm.id)
+ x2text = str(profile_manager.x2text.id)
+ if not profile_manager:
+ raise DefaultProfileError()
+ index_result = PromptStudioHelper.dynamic_indexer(
+ profile_manager=profile_manager,
+ file_path=doc_path,
+ tool_id=str(tool.tool_id),
+ org_id=org_id,
+ document_id=document_id,
+ is_summary=tool.summarize_as_source,
+ run_id=run_id,
+ user_id=user_id,
+ )
+ if index_result.get("status") == IndexingStatus.PENDING_STATUS.value:
+ return {
+ "status": IndexingStatus.PENDING_STATUS.value,
+ "message": IndexingStatus.DOCUMENT_BEING_INDEXED.value,
+ }
+
+ output: dict[str, Any] = {}
+ outputs: list[dict[str, Any]] = []
+ grammer_dict = {}
+ grammar_list = []
+ # Adding validations
+ prompt_grammer = tool.prompt_grammer
+ if prompt_grammer:
+ for word, synonyms in prompt_grammer.items():
+ synonyms = prompt_grammer[word]
+ grammer_dict[TSPKeys.WORD] = word
+ grammer_dict[TSPKeys.SYNONYMS] = synonyms
+ grammar_list.append(grammer_dict)
+ grammer_dict = {}
+
+ output[TSPKeys.PROMPT] = prompt.prompt
+ output[TSPKeys.ACTIVE] = prompt.active
+ output[TSPKeys.CHUNK_SIZE] = profile_manager.chunk_size
+ output[TSPKeys.VECTOR_DB] = vector_db
+ output[TSPKeys.EMBEDDING] = embedding_model
+ output[TSPKeys.CHUNK_OVERLAP] = profile_manager.chunk_overlap
+ output[TSPKeys.LLM] = llm
+ output[TSPKeys.TYPE] = prompt.enforce_type
+ output[TSPKeys.NAME] = prompt.prompt_key
+ output[TSPKeys.RETRIEVAL_STRATEGY] = profile_manager.retrieval_strategy
+ output[TSPKeys.SIMILARITY_TOP_K] = profile_manager.similarity_top_k
+ output[TSPKeys.SECTION] = profile_manager.section
+ output[TSPKeys.X2TEXT_ADAPTER] = x2text
+ # Eval settings for the prompt
+ output[TSPKeys.EVAL_SETTINGS] = {}
+ output[TSPKeys.EVAL_SETTINGS][TSPKeys.EVAL_SETTINGS_EVALUATE] = prompt.evaluate
+ output[TSPKeys.EVAL_SETTINGS][TSPKeys.EVAL_SETTINGS_MONITOR_LLM] = [monitor_llm]
+ output[TSPKeys.EVAL_SETTINGS][
+ TSPKeys.EVAL_SETTINGS_EXCLUDE_FAILED
+ ] = tool.exclude_failed
+ for attr in dir(prompt):
+ if attr.startswith(TSPKeys.EVAL_METRIC_PREFIX):
+ attr_val = getattr(prompt, attr)
+ output[TSPKeys.EVAL_SETTINGS][attr] = attr_val
+
+ outputs.append(output)
+
+ tool_settings = {}
+ tool_settings[TSPKeys.ENABLE_CHALLENGE] = tool.enable_challenge
+ tool_settings[TSPKeys.CHALLENGE_LLM] = challenge_llm
+ tool_settings[TSPKeys.SINGLE_PASS_EXTRACTION_MODE] = (
+ tool.single_pass_extraction_mode
+ )
+ tool_settings[TSPKeys.PREAMBLE] = tool.preamble
+ tool_settings[TSPKeys.POSTAMBLE] = tool.postamble
+ tool_settings[TSPKeys.GRAMMAR] = grammar_list
+
+ tool_id = str(tool.tool_id)
+
+ file_hash = ToolUtils.get_hash_from_file(file_path=doc_path)
+
+ payload = {
+ TSPKeys.TOOL_SETTINGS: tool_settings,
+ TSPKeys.OUTPUTS: outputs,
+ TSPKeys.TOOL_ID: tool_id,
+ TSPKeys.RUN_ID: run_id,
+ TSPKeys.FILE_NAME: doc_name,
+ TSPKeys.FILE_HASH: file_hash,
+ Common.LOG_EVENTS_ID: StateStore.get(Common.LOG_EVENTS_ID),
+ }
+
+ util = PromptIdeBaseTool(log_level=LogLevel.INFO, org_id=org_id)
+
+ responder = PromptTool(
+ tool=util,
+ prompt_host=settings.PROMPT_HOST,
+ prompt_port=settings.PROMPT_PORT,
+ )
+
+ answer = responder.answer_prompt(payload)
+ # TODO: Make use of dataclasses
+ if answer["status"] == "ERROR":
+ # TODO: Publish to FE logs from here
+ error_message = answer.get("error", "")
+ raise AnswerFetchError(
+ "Error while fetching response for "
+ f"'{prompt.prompt_key}' with '{doc_name}'. {error_message}"
+ )
+ output_response = json.loads(answer["structure_output"])
+ return output_response
+
+ @staticmethod
+ def dynamic_indexer(
+ profile_manager: ProfileManager,
+ tool_id: str,
+ file_path: str,
+ org_id: str,
+ document_id: str,
+ user_id: str,
+ is_summary: bool = False,
+ reindex: bool = False,
+ run_id: str = None,
+ ) -> Any:
+ """Used to index a file based on the passed arguments.
+
+ This is useful when a file needs to be indexed dynamically as the
+ parameters meant for indexing changes. The file
+
+ Args:
+ profile_manager (ProfileManager): Profile manager instance that hold
+ values such as chunk size, chunk overlap and adapter IDs
+ tool_id (str): UUID of the prompt studio tool
+ file_path (str): Path to the file that needs to be indexed
+ org_id (str): ID of the organization
+ is_summary (bool, optional): Flag to ensure if extracted contents
+ need to be persisted. Defaults to False.
+ user_id (str): The ID of the user who uploaded the document
+
+ Returns:
+ str: Index key for the combination of arguments
+ """
+ embedding_model = str(profile_manager.embedding_model.id)
+ vector_db = str(profile_manager.vector_store.id)
+ x2text_adapter = str(profile_manager.x2text.id)
+ extract_file_path: Optional[str] = None
+
+ if not is_summary:
+ directory, filename = os.path.split(file_path)
+ extract_file_path = os.path.join(
+ directory, "extract", os.path.splitext(filename)[0] + ".txt"
+ )
+ else:
+ profile_manager.chunk_size = 0
+
+ try:
+
+ usage_kwargs = {"run_id": run_id}
+ util = PromptIdeBaseTool(log_level=LogLevel.INFO, org_id=org_id)
+ tool_index = Index(tool=util)
+ doc_id_key = tool_index.generate_file_id(
+ tool_id=tool_id,
+ vector_db=vector_db,
+ embedding=embedding_model,
+ x2text=x2text_adapter,
+ chunk_size=str(profile_manager.chunk_size),
+ chunk_overlap=str(profile_manager.chunk_overlap),
+ file_path=file_path,
+ file_hash=None,
+ )
+ if not reindex:
+ indexed_doc_id = DocumentIndexingService.get_indexed_document_id(
+ org_id=org_id, user_id=user_id, doc_id_key=doc_id_key
+ )
+ if indexed_doc_id:
+ return {
+ "status": IndexingStatus.COMPLETED_STATUS.value,
+ "output": indexed_doc_id,
+ }
+ # Polling if document is already being indexed
+ if DocumentIndexingService.is_document_indexing(
+ org_id=org_id, user_id=user_id, doc_id_key=doc_id_key
+ ):
+ return {
+ "status": IndexingStatus.PENDING_STATUS.value,
+ "output": IndexingStatus.DOCUMENT_BEING_INDEXED.value,
+ }
+
+ # Set the document as being indexed
+ DocumentIndexingService.set_document_indexing(
+ org_id=org_id, user_id=user_id, doc_id_key=doc_id_key
+ )
+ doc_id: str = tool_index.index(
+ tool_id=tool_id,
+ embedding_instance_id=embedding_model,
+ vector_db_instance_id=vector_db,
+ x2text_instance_id=x2text_adapter,
+ file_path=file_path,
+ chunk_size=profile_manager.chunk_size,
+ chunk_overlap=profile_manager.chunk_overlap,
+ reindex=reindex,
+ output_file_path=extract_file_path,
+ usage_kwargs=usage_kwargs.copy(),
+ )
+
+ PromptStudioIndexHelper.handle_index_manager(
+ document_id=document_id,
+ is_summary=is_summary,
+ profile_manager=profile_manager,
+ doc_id=doc_id,
+ )
+ DocumentIndexingService.mark_document_indexed(
+ org_id=org_id, user_id=user_id, doc_id_key=doc_id_key, doc_id=doc_id
+ )
+ return {"status": IndexingStatus.COMPLETED_STATUS.value, "output": doc_id}
+ except (IndexingError, IndexingAPIError, SdkError) as e:
+ doc_name = os.path.split(file_path)[1]
+ PromptStudioHelper._publish_log(
+ {"tool_id": tool_id, "run_id": run_id, "doc_name": doc_name},
+ LogLevels.ERROR,
+ LogLevels.RUN,
+ f"Indexing failed : {e}",
+ )
+ raise IndexingAPIError(
+ f"Error while indexing '{doc_name}'. {str(e)}"
+ ) from e
+
+ @staticmethod
+ def _fetch_single_pass_response(
+ tool: CustomTool,
+ file_path: str,
+ prompts: list[ToolStudioPrompt],
+ org_id: str,
+ user_id: str,
+ document_id: str,
+ run_id: str = None,
+ ) -> Any:
+ tool_id: str = str(tool.tool_id)
+ outputs: list[dict[str, Any]] = []
+ grammar: list[dict[str, Any]] = []
+ prompt_grammar = tool.prompt_grammer
+ default_profile = ProfileManager.get_default_llm_profile(tool)
+ challenge_llm_instance: Optional[AdapterInstance] = tool.challenge_llm
+ challenge_llm: Optional[str] = None
+ # Using default profile manager llm if challenge_llm is None
+ if challenge_llm_instance:
+ challenge_llm = str(challenge_llm_instance.id)
+ else:
+ challenge_llm = str(default_profile.llm.id)
+ # Need to check the user who created profile manager
+ PromptStudioHelper.validate_adapter_status(default_profile)
+ # has access to adapters configured in profile manager
+ PromptStudioHelper.validate_profile_manager_owner_access(default_profile)
+ default_profile.chunk_size = 0 # To retrive full context
+
+ if prompt_grammar:
+ for word, synonyms in prompt_grammar.items():
+ grammar.append({TSPKeys.WORD: word, TSPKeys.SYNONYMS: synonyms})
+
+ if not default_profile:
+ raise DefaultProfileError()
+
+ index_result = PromptStudioHelper.dynamic_indexer(
+ profile_manager=default_profile,
+ file_path=file_path,
+ tool_id=tool_id,
+ org_id=org_id,
+ is_summary=tool.summarize_as_source,
+ document_id=document_id,
+ run_id=run_id,
+ user_id=user_id,
+ )
+ if index_result.get("status") == IndexingStatus.PENDING_STATUS.value:
+ return {
+ "status": IndexingStatus.PENDING_STATUS.value,
+ "message": IndexingStatus.DOCUMENT_BEING_INDEXED.value,
+ }
+
+ vector_db = str(default_profile.vector_store.id)
+ embedding_model = str(default_profile.embedding_model.id)
+ llm = str(default_profile.llm.id)
+ x2text = str(default_profile.x2text.id)
+ tool_settings = {}
+ tool_settings[TSPKeys.PREAMBLE] = tool.preamble
+ tool_settings[TSPKeys.POSTAMBLE] = tool.postamble
+ tool_settings[TSPKeys.GRAMMAR] = grammar
+ tool_settings[TSPKeys.LLM] = llm
+ tool_settings[TSPKeys.X2TEXT_ADAPTER] = x2text
+ tool_settings[TSPKeys.VECTOR_DB] = vector_db
+ tool_settings[TSPKeys.EMBEDDING] = embedding_model
+ tool_settings[TSPKeys.CHUNK_SIZE] = default_profile.chunk_size
+ tool_settings[TSPKeys.CHUNK_OVERLAP] = default_profile.chunk_overlap
+ tool_settings[TSPKeys.ENABLE_CHALLENGE] = tool.enable_challenge
+ tool_settings[TSPKeys.CHALLENGE_LLM] = challenge_llm
+
+ for prompt in prompts:
+ if not prompt.prompt:
+ raise EmptyPromptError()
+ output: dict[str, Any] = {}
+ output[TSPKeys.PROMPT] = prompt.prompt
+ output[TSPKeys.ACTIVE] = prompt.active
+ output[TSPKeys.TYPE] = prompt.enforce_type
+ output[TSPKeys.NAME] = prompt.prompt_key
+ outputs.append(output)
+
+ if tool.summarize_as_source:
+ path = Path(file_path)
+ file_path = str(path.parent / TSPKeys.SUMMARIZE / (path.stem + ".txt"))
+ file_hash = ToolUtils.get_hash_from_file(file_path=file_path)
+
+ payload = {
+ TSPKeys.TOOL_SETTINGS: tool_settings,
+ TSPKeys.OUTPUTS: outputs,
+ TSPKeys.TOOL_ID: tool_id,
+ TSPKeys.RUN_ID: run_id,
+ TSPKeys.FILE_HASH: file_hash,
+ Common.LOG_EVENTS_ID: StateStore.get(Common.LOG_EVENTS_ID),
+ }
+
+ util = PromptIdeBaseTool(log_level=LogLevel.INFO, org_id=org_id)
+
+ responder = PromptTool(
+ tool=util,
+ prompt_host=settings.PROMPT_HOST,
+ prompt_port=settings.PROMPT_PORT,
+ )
+
+ answer = responder.single_pass_extraction(payload)
+ # TODO: Make use of dataclasses
+ if answer["status"] == "ERROR":
+ error_message = answer.get("error", None)
+ raise AnswerFetchError(
+ f"Error while fetching response for prompt. {error_message}"
+ )
+ output_response = json.loads(answer["structure_output"])
+ return output_response
diff --git a/backend/prompt_studio/prompt_studio_core_v2/serializers.py b/backend/prompt_studio/prompt_studio_core_v2/serializers.py
new file mode 100644
index 000000000..2e6b53ba1
--- /dev/null
+++ b/backend/prompt_studio/prompt_studio_core_v2/serializers.py
@@ -0,0 +1,116 @@
+import logging
+from typing import Any
+
+from account_v2.models import User
+from account_v2.serializer import UserSerializer
+from django.core.exceptions import ObjectDoesNotExist
+from file_management.constants import FileInformationKey
+from prompt_studio.prompt_profile_manager_v2.models import ProfileManager
+from prompt_studio.prompt_studio_core_v2.constants import ToolStudioKeys as TSKeys
+from prompt_studio.prompt_studio_core_v2.exceptions import DefaultProfileError
+from prompt_studio.prompt_studio_v2.models import ToolStudioPrompt
+from prompt_studio.prompt_studio_v2.serializers import ToolStudioPromptSerializer
+from rest_framework import serializers
+from utils.FileValidator import FileValidator
+
+from backend.serializers import AuditSerializer
+
+from .models import CustomTool
+
+logger = logging.getLogger(__name__)
+
+
+class CustomToolSerializer(AuditSerializer):
+ shared_users = serializers.PrimaryKeyRelatedField(
+ queryset=User.objects.all(), required=False, allow_null=True, many=True
+ )
+
+ class Meta:
+ model = CustomTool
+ fields = "__all__"
+
+ def to_representation(self, instance): # type: ignore
+ data = super().to_representation(instance)
+ try:
+ profile_manager = ProfileManager.objects.get(
+ prompt_studio_tool=instance, is_summarize_llm=True
+ )
+ data[TSKeys.SUMMARIZE_LLM_PROFILE] = profile_manager.profile_id
+ except ObjectDoesNotExist:
+ logger.info(
+ "Summarize LLM profile doesnt exist for prompt tool %s",
+ str(instance.tool_id),
+ )
+ try:
+ profile_manager = ProfileManager.get_default_llm_profile(instance)
+ data[TSKeys.DEFAULT_PROFILE] = profile_manager.profile_id
+ except DefaultProfileError:
+ logger.info(
+ "Default LLM profile doesnt exist for prompt tool %s",
+ str(instance.tool_id),
+ )
+ try:
+ prompt_instance: ToolStudioPrompt = ToolStudioPrompt.objects.filter(
+ tool_id=data.get(TSKeys.TOOL_ID)
+ ).order_by("sequence_number")
+ data[TSKeys.PROMPTS] = []
+ output: list[Any] = []
+ # Appending prompt instances of the tool for FE Processing
+ if prompt_instance.count() != 0:
+ for prompt in prompt_instance:
+ prompt_serializer = ToolStudioPromptSerializer(prompt)
+ output.append(prompt_serializer.data)
+ data[TSKeys.PROMPTS] = output
+ except Exception as e:
+ logger.error(f"Error occured while appending prompts {e}")
+ return data
+
+ data["created_by_email"] = instance.created_by.email
+
+ return data
+
+
+class PromptStudioIndexSerializer(serializers.Serializer):
+ document_id = serializers.CharField()
+
+
+class PromptStudioResponseSerializer(serializers.Serializer):
+ file_name = serializers.CharField()
+ tool_id = serializers.CharField()
+ id = serializers.CharField()
+
+
+class SharedUserListSerializer(serializers.ModelSerializer):
+ """Used for listing users of Custom tool."""
+
+ created_by = UserSerializer()
+ shared_users = UserSerializer(many=True)
+
+ class Meta:
+ model = CustomTool
+ fields = (
+ "tool_id",
+ "tool_name",
+ "created_by",
+ "shared_users",
+ )
+
+
+class FileInfoIdeSerializer(serializers.Serializer):
+ document_id = serializers.CharField()
+ view_type = serializers.CharField(required=False)
+
+
+class FileUploadIdeSerializer(serializers.Serializer):
+ file = serializers.ListField(
+ child=serializers.FileField(),
+ required=True,
+ validators=[
+ FileValidator(
+ allowed_extensions=FileInformationKey.FILE_UPLOAD_ALLOWED_EXT,
+ allowed_mimetypes=FileInformationKey.FILE_UPLOAD_ALLOWED_MIME,
+ min_size=0,
+ max_size=FileInformationKey.FILE_UPLOAD_MAX_SIZE,
+ )
+ ],
+ )
diff --git a/backend/prompt_studio/prompt_studio_core_v2/static/select_choices.json b/backend/prompt_studio/prompt_studio_core_v2/static/select_choices.json
new file mode 100644
index 000000000..1e9e22ef4
--- /dev/null
+++ b/backend/prompt_studio/prompt_studio_core_v2/static/select_choices.json
@@ -0,0 +1,33 @@
+{
+ "combined_output":
+ {
+ "JSON":"JSON",
+ "YAML":"YAML"
+ },
+ "choose_llm":{
+ "AZURE":"Azure OpenAI"
+ },
+ "output_type":{
+ "string":"Text",
+ "number":"number",
+ "email":"email",
+ "date":"date",
+ "boolean":"boolean",
+ "json":"json"
+ },
+ "output_processing":{
+ "DEFAULT":"Default"
+ },
+ "embedding":{
+ "azure_openai_embedding":"azure_openai_embedding",
+ "openai_embedding":"openai_embedding"
+ },
+ "retrieval_strategy":{
+ "simple":"simple",
+ "subquestion":"subquestion"
+ },
+ "vector_store":{
+ "Postgres pg_vector":"Postgres pg_vector",
+ "qdrant":"qdrant"
+ }
+}
diff --git a/backend/prompt_studio/prompt_studio_core_v2/urls.py b/backend/prompt_studio/prompt_studio_core_v2/urls.py
new file mode 100644
index 000000000..0abc1c436
--- /dev/null
+++ b/backend/prompt_studio/prompt_studio_core_v2/urls.py
@@ -0,0 +1,118 @@
+from django.db import transaction
+from django.urls import path
+from django.utils.decorators import method_decorator
+from rest_framework.urlpatterns import format_suffix_patterns
+
+from .views import PromptStudioCoreView
+
+prompt_studio_list = PromptStudioCoreView.as_view({"get": "list", "post": "create"})
+prompt_studio_detail = PromptStudioCoreView.as_view(
+ {
+ "get": "retrieve",
+ "put": "update",
+ "patch": "partial_update",
+ "delete": "destroy",
+ }
+)
+prompt_studio_choices = PromptStudioCoreView.as_view({"get": "get_select_choices"})
+prompt_studio_profiles = PromptStudioCoreView.as_view(
+ {"get": "list_profiles", "patch": "make_profile_default"}
+)
+
+prompt_studio_prompts = PromptStudioCoreView.as_view({"post": "create_prompt"})
+
+prompt_studio_profilemanager = PromptStudioCoreView.as_view(
+ {"post": "create_profile_manager"}
+)
+
+prompt_studio_prompt_index = PromptStudioCoreView.as_view({"post": "index_document"})
+prompt_studio_prompt_response = PromptStudioCoreView.as_view({"post": "fetch_response"})
+prompt_studio_adapter_choices = PromptStudioCoreView.as_view(
+ {"get": "get_adapter_choices"}
+)
+prompt_studio_single_pass_extraction = PromptStudioCoreView.as_view(
+ {"post": "single_pass_extraction"}
+)
+prompt_studio_users = PromptStudioCoreView.as_view({"get": "list_of_shared_users"})
+
+
+prompt_studio_file = PromptStudioCoreView.as_view(
+ {
+ "post": "upload_for_ide",
+ "get": "fetch_contents_ide",
+ "delete": "delete_for_ide",
+ }
+)
+
+prompt_studio_export = PromptStudioCoreView.as_view(
+ {"post": "export_tool", "get": "export_tool_info"}
+)
+
+
+urlpatterns = format_suffix_patterns(
+ [
+ path("prompt-studio/", prompt_studio_list, name="prompt-studio-list"),
+ path(
+ "prompt-studio/<uuid:pk>/",
+ prompt_studio_detail,
+ name="tool-studio-detail",
+ ),
+ path(
+ "prompt-studio/select_choices/",
+ prompt_studio_choices,
+ name="prompt-studio-choices",
+ ),
+ path(
+ "prompt-studio/prompt-studio-profile/<uuid:pk>/",
+ prompt_studio_profiles,
+ name="prompt-studio-profiles",
+ ),
+ path(
+ "prompt-studio/prompt-studio-prompt/<uuid:pk>/",
+ prompt_studio_prompts,
+ name="prompt-studio-prompts",
+ ),
+ path(
+ "prompt-studio/profilemanager/<uuid:pk>",
+ prompt_studio_profilemanager,
+ name="prompt-studio-profilemanager",
+ ),
+ path(
+ "prompt-studio/index-document/<uuid:pk>",
+ method_decorator(transaction.non_atomic_requests)(
+ prompt_studio_prompt_index
+ ),
+ name="prompt-studio-prompt-index",
+ ),
+ path(
+ "prompt-studio/fetch_response/<uuid:pk>",
+ prompt_studio_prompt_response,
+ name="prompt-studio-prompt-response",
+ ),
+ path(
+ "prompt-studio/adapter-choices/",
+ prompt_studio_adapter_choices,
+ name="prompt-studio-adapter-choices",
+ ),
+ path(
+ "prompt-studio/single-pass-extraction/<uuid:pk>",
+ prompt_studio_single_pass_extraction,
+ name="prompt-studio-single-pass-extraction",
+ ),
+ path(
+ "prompt-studio/users/<uuid:pk>",
+ prompt_studio_users,
+ name="prompt-studio-users",
+ ),
+ path(
+ "prompt-studio/file/<uuid:pk>",
+ prompt_studio_file,
+ name="prompt_studio_file",
+ ),
+ path(
+ "prompt-studio/export/<uuid:pk>",
+ prompt_studio_export,
+ name="prompt_studio_export",
+ ),
+ ]
+)
diff --git a/backend/prompt_studio/prompt_studio_core_v2/views.py b/backend/prompt_studio/prompt_studio_core_v2/views.py
new file mode 100644
index 000000000..e2b1e1d2a
--- /dev/null
+++ b/backend/prompt_studio/prompt_studio_core_v2/views.py
@@ -0,0 +1,525 @@
+import logging
+import uuid
+from typing import Any, Optional
+
+from account_v2.custom_exceptions import DuplicateData
+from django.db import IntegrityError
+from django.db.models import QuerySet
+from django.http import HttpRequest
+from file_management.exceptions import FileNotFound
+from file_management.file_management_helper import FileManagerHelper
+from permissions.permission import IsOwner, IsOwnerOrSharedUser
+from prompt_studio.processor_loader import ProcessorConfig, load_plugins
+from prompt_studio.prompt_profile_manager_v2.constants import ProfileManagerErrors
+from prompt_studio.prompt_profile_manager_v2.models import ProfileManager
+from prompt_studio.prompt_profile_manager_v2.serializers import ProfileManagerSerializer
+from prompt_studio.prompt_studio_core_v2.constants import (
+ FileViewTypes,
+ ToolStudioErrors,
+ ToolStudioKeys,
+ ToolStudioPromptKeys,
+)
+from prompt_studio.prompt_studio_core_v2.document_indexing_service import (
+ DocumentIndexingService,
+)
+from prompt_studio.prompt_studio_core_v2.exceptions import (
+ IndexingAPIError,
+ ToolDeleteError,
+)
+from prompt_studio.prompt_studio_core_v2.prompt_studio_helper import PromptStudioHelper
+from prompt_studio.prompt_studio_document_manager_v2.models import DocumentManager
+from prompt_studio.prompt_studio_document_manager_v2.prompt_studio_document_helper import ( # noqa: E501
+ PromptStudioDocumentHelper,
+)
+from prompt_studio.prompt_studio_index_manager_v2.models import IndexManager
+from prompt_studio.prompt_studio_registry_v2.prompt_studio_registry_helper import (
+ PromptStudioRegistryHelper,
+)
+from prompt_studio.prompt_studio_registry_v2.serializers import (
+ ExportToolRequestSerializer,
+ PromptStudioRegistryInfoSerializer,
+)
+from prompt_studio.prompt_studio_v2.constants import ToolStudioPromptErrors
+from prompt_studio.prompt_studio_v2.serializers import ToolStudioPromptSerializer
+from rest_framework import status, viewsets
+from rest_framework.decorators import action
+from rest_framework.request import Request
+from rest_framework.response import Response
+from rest_framework.versioning import URLPathVersioning
+from tool_instance.models import ToolInstance
+from unstract.sdk.utils.common_utils import CommonUtils
+from utils.user_session import UserSessionUtils
+
+from unstract.connectors.filesystems.local_storage.local_storage import LocalStorageFS
+
+from .models import CustomTool
+from .serializers import (
+ CustomToolSerializer,
+ FileInfoIdeSerializer,
+ FileUploadIdeSerializer,
+ PromptStudioIndexSerializer,
+ SharedUserListSerializer,
+)
+
+logger = logging.getLogger(__name__)
+
+
+class PromptStudioCoreView(viewsets.ModelViewSet):
+ """Viewset to handle all Custom tool related operations."""
+
+ versioning_class = URLPathVersioning
+
+ serializer_class = CustomToolSerializer
+
+ processor_plugins = load_plugins()
+
+ def get_permissions(self) -> list[Any]:
+ if self.action == "destroy":
+ return [IsOwner()]
+
+ return [IsOwnerOrSharedUser()]
+
+ def get_queryset(self) -> Optional[QuerySet]:
+ return CustomTool.objects.for_user(self.request.user)
+
+ def create(self, request: HttpRequest) -> Response:
+ serializer = self.get_serializer(data=request.data)
+ serializer.is_valid(raise_exception=True)
+ try:
+ self.perform_create(serializer)
+ except IntegrityError:
+ raise DuplicateData(
+ f"{ToolStudioErrors.TOOL_NAME_EXISTS}, \
+ {ToolStudioErrors.DUPLICATE_API}"
+ )
+ PromptStudioHelper.create_default_profile_manager(
+ request.user, serializer.data["tool_id"]
+ )
+ return Response(serializer.data, status=status.HTTP_201_CREATED)
+
+ def perform_destroy(self, instance: CustomTool) -> None:
+ organization_id = UserSessionUtils.get_organization_id(self.request)
+ instance.delete(organization_id)
+
+ def destroy(
+ self, request: Request, *args: tuple[Any], **kwargs: dict[str, Any]
+ ) -> Response:
+ instance: CustomTool = self.get_object()
+ # Checks if tool is exported
+ if hasattr(instance, "prompt_studio_registry"):
+ exported_tool_instances_in_use = ToolInstance.objects.filter(
+ tool_id__exact=instance.prompt_studio_registry.pk
+ )
+ dependent_wfs = set()
+ for tool_instance in exported_tool_instances_in_use:
+ dependent_wfs.add(tool_instance.workflow_id)
+ if len(dependent_wfs) > 0:
+ logger.info(
+ f"Cannot destroy custom tool {instance.tool_id},"
+ f" depended by workflows {dependent_wfs}"
+ )
+ raise ToolDeleteError(
+ "Failed to delete tool, its used in other workflows. "
+ "Delete its usages first"
+ )
+ return super().destroy(request, *args, **kwargs)
+
+ def partial_update(
+ self, request: Request, *args: tuple[Any], **kwargs: dict[str, Any]
+ ) -> Response:
+ summarize_llm_profile_id = request.data.get(
+ ToolStudioKeys.SUMMARIZE_LLM_PROFILE, None
+ )
+ if summarize_llm_profile_id:
+ prompt_tool = self.get_object()
+
+ ProfileManager.objects.filter(prompt_studio_tool=prompt_tool).update(
+ is_summarize_llm=False
+ )
+ profile_manager = ProfileManager.objects.get(pk=summarize_llm_profile_id)
+ profile_manager.is_summarize_llm = True
+ profile_manager.save()
+
+ return super().partial_update(request, *args, **kwargs)
+
+ @action(detail=True, methods=["get"])
+ def get_select_choices(self, request: HttpRequest) -> Response:
+ """Method to return all static dropdown field values.
+
+ The field values are retrieved from `./static/select_choices.json`.
+
+ Returns:
+ Response: Reponse of dropdown dict
+ """
+ try:
+ select_choices: dict[str, Any] = PromptStudioHelper.get_select_fields()
+ return Response(select_choices, status=status.HTTP_200_OK)
+ except Exception as e:
+ logger.error(f"Error occured while fetching select fields {e}")
+ return Response(select_choices, status=status.HTTP_204_NO_CONTENT)
+
+ @action(detail=True, methods=["get"])
+ def list_profiles(self, request: HttpRequest, pk: Any = None) -> Response:
+ prompt_tool = (
+ self.get_object()
+ ) # Assuming you have a get_object method in your viewset
+
+ profile_manager_instances = ProfileManager.objects.filter(
+ prompt_studio_tool=prompt_tool
+ )
+
+ serialized_instances = ProfileManagerSerializer(
+ profile_manager_instances, many=True
+ ).data
+
+ return Response(serialized_instances)
+
+ @action(detail=True, methods=["patch"])
+ def make_profile_default(self, request: HttpRequest, pk: Any = None) -> Response:
+ prompt_tool = (
+ self.get_object()
+ ) # Assuming you have a get_object method in your viewset
+
+ ProfileManager.objects.filter(prompt_studio_tool=prompt_tool).update(
+ is_default=False
+ )
+
+ profile_manager = ProfileManager.objects.get(pk=request.data["default_profile"])
+ profile_manager.is_default = True
+ profile_manager.save()
+
+ return Response(
+ status=status.HTTP_200_OK,
+ data={"default_profile": profile_manager.profile_id},
+ )
+
+ @action(detail=True, methods=["post"])
+ def index_document(self, request: HttpRequest, pk: Any = None) -> Response:
+ """API Entry point method to index input file.
+
+ Args:
+ request (HttpRequest)
+
+ Raises:
+ IndexingError
+ ValidationError
+
+ Returns:
+ Response
+ """
+ tool = self.get_object()
+ serializer = PromptStudioIndexSerializer(data=request.data)
+ serializer.is_valid(raise_exception=True)
+ document_id: str = serializer.validated_data.get(
+ ToolStudioPromptKeys.DOCUMENT_ID
+ )
+ document: DocumentManager = DocumentManager.objects.get(pk=document_id)
+ file_name: str = document.document_name
+ # Generate a run_id
+ run_id = CommonUtils.generate_uuid()
+ unique_id = PromptStudioHelper.index_document(
+ tool_id=str(tool.tool_id),
+ file_name=file_name,
+ org_id=UserSessionUtils.get_organization_id(request),
+ user_id=tool.created_by.user_id,
+ document_id=document_id,
+ run_id=run_id,
+ )
+
+ usage_kwargs: dict[Any, Any] = dict()
+ usage_kwargs[ToolStudioPromptKeys.RUN_ID] = run_id
+ for processor_plugin in self.processor_plugins:
+ cls = processor_plugin[ProcessorConfig.METADATA][
+ ProcessorConfig.METADATA_SERVICE_CLASS
+ ]
+ cls.process(
+ tool_id=str(tool.tool_id),
+ file_name=file_name,
+ org_id=UserSessionUtils.get_organization_id(request),
+ user_id=tool.created_by.user_id,
+ document_id=document_id,
+ usage_kwargs=usage_kwargs.copy(),
+ )
+
+ if unique_id:
+ return Response(
+ {"message": "Document indexed successfully."},
+ status=status.HTTP_200_OK,
+ )
+ else:
+ logger.error("Error occured while indexing. Unique ID is not valid.")
+ raise IndexingAPIError()
+
+ @action(detail=True, methods=["post"])
+ def fetch_response(self, request: HttpRequest, pk: Any = None) -> Response:
+ """API Entry point method to fetch response to prompt.
+
+ Args:
+ request (HttpRequest): _description_
+
+ Raises:
+ FilenameMissingError: _description_
+
+ Returns:
+ Response
+ """
+ custom_tool = self.get_object()
+ tool_id: str = str(custom_tool.tool_id)
+ document_id: str = request.data.get(ToolStudioPromptKeys.DOCUMENT_ID)
+ id: str = request.data.get(ToolStudioPromptKeys.ID)
+ run_id: str = request.data.get(ToolStudioPromptKeys.RUN_ID)
+ profile_manager: str = request.data.get(ToolStudioPromptKeys.PROFILE_MANAGER_ID)
+ if not run_id:
+ # Generate a run_id
+ run_id = CommonUtils.generate_uuid()
+
+ response: dict[str, Any] = PromptStudioHelper.prompt_responder(
+ id=id,
+ tool_id=tool_id,
+ org_id=UserSessionUtils.get_organization_id(request),
+ user_id=custom_tool.created_by.user_id,
+ document_id=document_id,
+ run_id=run_id,
+ profile_manager_id=profile_manager,
+ )
+ return Response(response, status=status.HTTP_200_OK)
+
+ @action(detail=True, methods=["post"])
+ def single_pass_extraction(self, request: HttpRequest, pk: uuid) -> Response:
+ """API Entry point method to fetch response to prompt.
+
+ Args:
+ request (HttpRequest): _description_
+ pk (Any): Primary key of the CustomTool
+
+ Returns:
+ Response
+ """
+ # TODO: Handle fetch_response and single_pass_
+ # extraction using common function
+ custom_tool = self.get_object()
+ tool_id: str = str(custom_tool.tool_id)
+ document_id: str = request.data.get(ToolStudioPromptKeys.DOCUMENT_ID)
+ run_id: str = request.data.get(ToolStudioPromptKeys.RUN_ID)
+ if not run_id:
+ # Generate a run_id
+ run_id = CommonUtils.generate_uuid()
+ response: dict[str, Any] = PromptStudioHelper.prompt_responder(
+ tool_id=tool_id,
+ org_id=UserSessionUtils.get_organization_id(request),
+ user_id=custom_tool.created_by.user_id,
+ document_id=document_id,
+ run_id=run_id,
+ )
+ return Response(response, status=status.HTTP_200_OK)
+
+ @action(detail=True, methods=["get"])
+ def list_of_shared_users(self, request: HttpRequest, pk: Any = None) -> Response:
+
+ custom_tool = (
+ self.get_object()
+ ) # Assuming you have a get_object method in your viewset
+
+ serialized_instances = SharedUserListSerializer(custom_tool).data
+
+ return Response(serialized_instances)
+
+ @action(detail=True, methods=["post"])
+ def create_prompt(self, request: HttpRequest, pk: Any = None) -> Response:
+ context = super().get_serializer_context()
+ serializer = ToolStudioPromptSerializer(data=request.data, context=context)
+ serializer.is_valid(raise_exception=True)
+ try:
+ # serializer.save()
+ self.perform_create(serializer)
+ except IntegrityError:
+ raise DuplicateData(
+ f"{ToolStudioPromptErrors.PROMPT_NAME_EXISTS}, \
+ {ToolStudioPromptErrors.DUPLICATE_API}"
+ )
+ return Response(serializer.data, status=status.HTTP_201_CREATED)
+
+ @action(detail=True, methods=["post"])
+ def create_profile_manager(self, request: HttpRequest, pk: Any = None) -> Response:
+ context = super().get_serializer_context()
+ serializer = ProfileManagerSerializer(data=request.data, context=context)
+
+ serializer.is_valid(raise_exception=True)
+ try:
+ self.perform_create(serializer)
+ except IntegrityError:
+ raise DuplicateData(
+ f"{ProfileManagerErrors.PROFILE_NAME_EXISTS}, \
+ {ProfileManagerErrors.DUPLICATE_API}"
+ )
+ return Response(serializer.data, status=status.HTTP_201_CREATED)
+
+ @action(detail=True, methods=["get"])
+ def fetch_contents_ide(self, request: HttpRequest, pk: Any = None) -> Response:
+ custom_tool = self.get_object()
+ serializer = FileInfoIdeSerializer(data=request.GET)
+ serializer.is_valid(raise_exception=True)
+ document_id: str = serializer.validated_data.get("document_id")
+ document: DocumentManager = DocumentManager.objects.get(pk=document_id)
+ file_name: str = document.document_name
+ view_type: str = serializer.validated_data.get("view_type")
+
+ filename_without_extension = file_name.rsplit(".", 1)[0]
+ if view_type == FileViewTypes.EXTRACT:
+ file_name = (
+ f"{FileViewTypes.EXTRACT.lower()}/" f"{filename_without_extension}.txt"
+ )
+ if view_type == FileViewTypes.SUMMARIZE:
+ file_name = (
+ f"{FileViewTypes.SUMMARIZE.lower()}/"
+ f"{filename_without_extension}.txt"
+ )
+
+ file_path = file_path = FileManagerHelper.handle_sub_directory_for_tenants(
+ UserSessionUtils.get_organization_id(request),
+ is_create=True,
+ user_id=custom_tool.created_by.user_id,
+ tool_id=str(custom_tool.tool_id),
+ )
+ file_system = LocalStorageFS(settings={"path": file_path})
+ if not file_path.endswith("/"):
+ file_path += "/"
+ file_path += file_name
+ # Temporary Hack for frictionless onboarding as the user id will be empty
+ try:
+ contents = FileManagerHelper.fetch_file_contents(file_system, file_path)
+ except FileNotFound:
+ file_path = file_path = FileManagerHelper.handle_sub_directory_for_tenants(
+ UserSessionUtils.get_organization_id(request),
+ is_create=True,
+ user_id="",
+ tool_id=str(custom_tool.tool_id),
+ )
+ if not file_path.endswith("/"):
+ file_path += "/"
+ file_path += file_name
+ contents = FileManagerHelper.fetch_file_contents(file_system, file_path)
+
+ return Response({"data": contents}, status=status.HTTP_200_OK)
+
+ @action(detail=True, methods=["post"])
+ def upload_for_ide(self, request: HttpRequest, pk: Any = None) -> Response:
+ custom_tool = self.get_object()
+ serializer = FileUploadIdeSerializer(data=request.data)
+ serializer.is_valid(raise_exception=True)
+ uploaded_files: Any = serializer.validated_data.get("file")
+
+ file_path = FileManagerHelper.handle_sub_directory_for_tenants(
+ UserSessionUtils.get_organization_id(request),
+ is_create=True,
+ user_id=custom_tool.created_by.user_id,
+ tool_id=str(custom_tool.tool_id),
+ )
+ file_system = LocalStorageFS(settings={"path": file_path})
+
+ documents = []
+ for uploaded_file in uploaded_files:
+ file_name = uploaded_file.name
+
+ # Create a record in the db for the file
+ document = PromptStudioDocumentHelper.create(
+ tool_id=str(custom_tool.tool_id), document_name=file_name
+ )
+ # Create a dictionary to store document data
+ doc = {
+ "document_id": document.document_id,
+ "document_name": document.document_name,
+ "tool": document.tool.tool_id,
+ }
+ # Store file
+ logger.info(
+ f"Uploading file: {file_name}" if file_name else "Uploading file"
+ )
+ FileManagerHelper.upload_file(
+ file_system,
+ file_path,
+ uploaded_file,
+ file_name,
+ )
+ documents.append(doc)
+ return Response({"data": documents})
+
+ @action(detail=True, methods=["delete"])
+ def delete_for_ide(self, request: HttpRequest, pk: uuid) -> Response:
+ custom_tool = self.get_object()
+ serializer = FileInfoIdeSerializer(data=request.data)
+ serializer.is_valid(raise_exception=True)
+ document_id: str = serializer.validated_data.get(
+ ToolStudioPromptKeys.DOCUMENT_ID
+ )
+ org_id = UserSessionUtils.get_organization_id(request)
+ user_id = custom_tool.created_by.user_id
+ document: DocumentManager = DocumentManager.objects.get(pk=document_id)
+ file_name: str = document.document_name
+ file_path = FileManagerHelper.handle_sub_directory_for_tenants(
+ org_id=org_id,
+ is_create=False,
+ user_id=user_id,
+ tool_id=str(custom_tool.tool_id),
+ )
+ path = file_path
+ file_system = LocalStorageFS(settings={"path": path})
+ try:
+ # Delete indexed flags in redis
+ index_managers = IndexManager.objects.filter(document_manager=document_id)
+ for index_manager in index_managers:
+ raw_index_id = index_manager.raw_index_id
+ DocumentIndexingService.remove_document_indexing(
+ org_id=org_id, user_id=user_id, doc_id_key=raw_index_id
+ )
+ # Delete the document record
+ document.delete()
+ # Delete the files
+ FileManagerHelper.delete_file(file_system, path, file_name)
+ # Directories to delete the text files
+ directories = ["extract/", "summarize/"]
+ FileManagerHelper.delete_related_files(
+ file_system, path, file_name, directories
+ )
+ return Response(
+ {"data": "File deleted succesfully."},
+ status=status.HTTP_200_OK,
+ )
+ except Exception as exc:
+ logger.error(f"Exception thrown from file deletion, error {exc}")
+ return Response(
+ {"data": "File deletion failed."},
+ status=status.HTTP_400_BAD_REQUEST,
+ )
+
+ @action(detail=True, methods=["post"])
+ def export_tool(self, request: Request, pk: Any = None) -> Response:
+ """API Endpoint for exporting required jsons for the custom tool."""
+ custom_tool = self.get_object()
+ serializer = ExportToolRequestSerializer(data=request.data)
+ serializer.is_valid(raise_exception=True)
+ is_shared_with_org: bool = serializer.validated_data.get("is_shared_with_org")
+ user_ids = set(serializer.validated_data.get("user_id"))
+
+ PromptStudioRegistryHelper.update_or_create_psr_tool(
+ custom_tool=custom_tool,
+ shared_with_org=is_shared_with_org,
+ user_ids=user_ids,
+ )
+ return Response(
+ {"message": "Custom tool exported sucessfully."},
+ status=status.HTTP_200_OK,
+ )
+
+ @action(detail=True, methods=["get"])
+ def export_tool_info(self, request: Request, pk: Any = None) -> Response:
+ custom_tool = self.get_object()
+ serialized_instances = None
+ if hasattr(custom_tool, "prompt_studio_registry"):
+ serialized_instances = PromptStudioRegistryInfoSerializer(
+ custom_tool.prompt_studio_registry
+ ).data
+
+ return Response(serialized_instances)
+ else:
+ return Response(status=status.HTTP_204_NO_CONTENT)
diff --git a/backend/prompt_studio/prompt_studio_v2/__init__.py b/backend/prompt_studio/prompt_studio_v2/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/backend/prompt_studio/prompt_studio_v2/admin.py b/backend/prompt_studio/prompt_studio_v2/admin.py
new file mode 100644
index 000000000..bbcd76c6e
--- /dev/null
+++ b/backend/prompt_studio/prompt_studio_v2/admin.py
@@ -0,0 +1,5 @@
+from django.contrib import admin
+
+from .models import ToolStudioPrompt
+
+admin.site.register(ToolStudioPrompt)
diff --git a/backend/prompt_studio/prompt_studio_v2/apps.py b/backend/prompt_studio/prompt_studio_v2/apps.py
new file mode 100644
index 000000000..0a6f1e517
--- /dev/null
+++ b/backend/prompt_studio/prompt_studio_v2/apps.py
@@ -0,0 +1,5 @@
+from django.apps import AppConfig
+
+
+class ToolStudioPrompt(AppConfig):
+ name = "prompt_studio.prompt_studio_v2"
diff --git a/backend/prompt_studio/prompt_studio_v2/constants.py b/backend/prompt_studio/prompt_studio_v2/constants.py
new file mode 100644
index 000000000..6554a9f8c
--- /dev/null
+++ b/backend/prompt_studio/prompt_studio_v2/constants.py
@@ -0,0 +1,32 @@
+class ToolStudioPromptKeys:
+ CREATED_BY = "created_by"
+ TOOL_ID = "tool_id"
+ NUMBER = "Number"
+ FLOAT = "Float"
+ PG_VECTOR = "Postgres pg_vector"
+ ANSWERS = "answers"
+ UNIQUE_FILE_ID = "unique_file_id"
+ ID = "id"
+ FILE_NAME = "file_name"
+ UNDEFINED = "undefined"
+ ACTIVE = "active"
+ PROMPT_KEY = "prompt_key"
+ EVAL_METRIC_PREFIX = "eval_"
+ EVAL_RESULT_DELIM = "__"
+ SEQUENCE_NUMBER = "sequence_number"
+ START_SEQUENCE_NUMBER = "start_sequence_number"
+ END_SEQUENCE_NUMBER = "end_sequence_number"
+ PROMPT_ID = "prompt_id"
+
+
+class ToolStudioPromptErrors:
+ SERIALIZATION_FAILED = "Data Serialization Failed."
+ DUPLICATE_API = "It appears that a duplicate call may have been made."
+ PROMPT_NAME_EXISTS = "Prompt with the name already exists"
+
+
+class LogLevels:
+ INFO = "INFO"
+ ERROR = "ERROR"
+ DEBUG = "DEBUG"
+ RUN = "RUN"
diff --git a/backend/prompt_studio/prompt_studio_v2/controller.py b/backend/prompt_studio/prompt_studio_v2/controller.py
new file mode 100644
index 000000000..30cacfe29
--- /dev/null
+++ b/backend/prompt_studio/prompt_studio_v2/controller.py
@@ -0,0 +1,58 @@
+import logging
+
+from prompt_studio.prompt_studio_v2.constants import ToolStudioPromptKeys
+from prompt_studio.prompt_studio_v2.helper import PromptStudioHelper
+from prompt_studio.prompt_studio_v2.models import ToolStudioPrompt
+from prompt_studio.prompt_studio_v2.serializers import ReorderPromptsSerializer
+from rest_framework import status
+from rest_framework.request import Request
+from rest_framework.response import Response
+
+logger = logging.getLogger(__name__)
+
+
+class PromptStudioController:
+ def reorder_prompts(self, request: Request) -> Response:
+ """Reorder the sequence of prompts based on the start and end sequence
+ numbers.
+
+ This action handles the reordering of prompts by updating their sequence
+ numbers. It increments or decrements the sequence numbers of the relevant
+ prompts to reflect the new order. If the start and end sequence numbers
+ are equal, it returns a bad request response.
+
+ Args:
+ request (Request): The HTTP request object containing the data to
+ reorder prompts.
+
+ Returns:
+ Response: A Response object with the status of the reordering operation.
+ """
+ try:
+ # Validate request data
+ serializer = ReorderPromptsSerializer(data=request.data)
+ serializer.is_valid(raise_exception=True)
+
+ # Extract validated data from the serializer
+ start_sequence_number = serializer.validated_data.get(
+ ToolStudioPromptKeys.START_SEQUENCE_NUMBER
+ )
+ end_sequence_number = serializer.validated_data.get(
+ ToolStudioPromptKeys.END_SEQUENCE_NUMBER
+ )
+ prompt_id = serializer.validated_data.get(ToolStudioPromptKeys.PROMPT_ID)
+
+ filtered_prompts_data = PromptStudioHelper.reorder_prompts_helper(
+ prompt_id=prompt_id,
+ start_sequence_number=start_sequence_number,
+ end_sequence_number=end_sequence_number,
+ )
+
+ logger.info("Re-ordering completed successfully.")
+ return Response(status=status.HTTP_200_OK, data=filtered_prompts_data)
+
+ except ToolStudioPrompt.DoesNotExist:
+ logger.error(f"Prompt with ID {prompt_id} not found.")
+ return Response(
+ status=status.HTTP_404_NOT_FOUND, data={"detail": "Prompt not found."}
+ )
diff --git a/backend/prompt_studio/prompt_studio_v2/exceptions.py b/backend/prompt_studio/prompt_studio_v2/exceptions.py
new file mode 100644
index 000000000..c78c3a740
--- /dev/null
+++ b/backend/prompt_studio/prompt_studio_v2/exceptions.py
@@ -0,0 +1,16 @@
+from rest_framework.exceptions import APIException
+
+
+class IndexingError(APIException):
+ status_code = 400
+ default_detail = "Error while indexing file"
+
+
+class AnswerFetchError(APIException):
+ status_code = 400
+ default_detail = "Error occured while fetching response for the prompt"
+
+
+class ToolNotValid(APIException):
+ status_code = 400
+ default_detail = "Custom tool is not valid."
diff --git a/backend/prompt_studio/prompt_studio_v2/helper.py b/backend/prompt_studio/prompt_studio_v2/helper.py
new file mode 100644
index 000000000..8491332a4
--- /dev/null
+++ b/backend/prompt_studio/prompt_studio_v2/helper.py
@@ -0,0 +1,111 @@
+import logging
+
+from prompt_studio.prompt_studio_v2.models import ToolStudioPrompt
+
+logger = logging.getLogger(__name__)
+
+
+class PromptStudioHelper:
+ @staticmethod
+ def reorder_prompts_helper(
+ prompt_id: str, start_sequence_number: int, end_sequence_number: int
+ ) -> list[dict[str, int]]:
+ """Helper method to reorder prompts based on sequence numbers.
+
+ Args:
+ prompt_id (str): The ID of the prompt to be reordered.
+ start_sequence_number (int): The initial sequence number of the prompt.
+ end_sequence_number (int): The new sequence number of the prompt.
+
+ Returns:
+ list[dict[str, int]]: A list of updated prompt data with their IDs
+ and new sequence numbers.
+ """
+ prompt_instance: ToolStudioPrompt = ToolStudioPrompt.objects.get(pk=prompt_id)
+ filtered_prompts_data = []
+ tool_id = prompt_instance.tool_id
+
+ # Determine the direction of sequence adjustment based on start
+ # and end sequence numbers
+ if start_sequence_number < end_sequence_number:
+ logger.info(
+ "Start sequence number is less than end sequence number. "
+ "Decrementing sequence numbers."
+ )
+ filters = {
+ "sequence_number__gt": start_sequence_number,
+ "sequence_number__lte": end_sequence_number,
+ "tool_id": tool_id,
+ }
+ increment = False
+
+ elif start_sequence_number > end_sequence_number:
+ logger.info(
+ "Start sequence number is greater than end sequence number. "
+ "Incrementing sequence numbers."
+ )
+ filters = {
+ "sequence_number__lt": start_sequence_number,
+ "sequence_number__gte": end_sequence_number,
+ "tool_id": tool_id,
+ }
+ increment = True
+
+ # Call helper method to update sequence numbers and get filtered prompt data
+ filtered_prompts_data = PromptStudioHelper.update_sequence_numbers(
+ filters, increment
+ )
+
+ # Update the sequence number of the moved prompt
+ prompt_instance.sequence_number = end_sequence_number
+ prompt_instance.save()
+
+ # Append the updated prompt instance data to the response
+ filtered_prompts_data.append(
+ {
+ "id": prompt_instance.prompt_id,
+ "sequence_number": prompt_instance.sequence_number,
+ }
+ )
+
+ return filtered_prompts_data
+
+ @staticmethod
+ def update_sequence_numbers(filters: dict, increment: bool) -> list[dict[str, int]]:
+ """Update the sequence numbers for prompts based on the provided
+ filters and increment flag.
+
+ Args:
+ filters (dict): The filter criteria for selecting prompts.
+ increment (bool): Whether to increment (True) or decrement (False)
+ the sequence numbers.
+
+ Returns:
+ list[dict[str, int]]: A list of updated prompt data with their IDs
+ and new sequence numbers.
+ """
+ # Filter prompts based on the provided filters
+ filtered_prompts = ToolStudioPrompt.objects.filter(**filters)
+
+ # List to hold updated prompt data
+ filtered_prompts_data = []
+
+ # Prepare updates and collect data
+ for prompt in filtered_prompts:
+ if increment:
+ prompt.sequence_number += 1
+ else:
+ prompt.sequence_number -= 1
+
+ # Append prompt data to the list
+ filtered_prompts_data.append(
+ {
+ "id": prompt.prompt_id,
+ "sequence_number": prompt.sequence_number,
+ }
+ )
+
+ # Bulk update the sequence numbers
+ ToolStudioPrompt.objects.bulk_update(filtered_prompts, ["sequence_number"])
+
+ return filtered_prompts_data
diff --git a/backend/prompt_studio/prompt_studio_v2/models.py b/backend/prompt_studio/prompt_studio_v2/models.py
new file mode 100644
index 000000000..ba928f7c5
--- /dev/null
+++ b/backend/prompt_studio/prompt_studio_v2/models.py
@@ -0,0 +1,127 @@
+import uuid
+
+from account_v2.models import User
+from django.db import models
+from prompt_studio.prompt_profile_manager_v2.models import ProfileManager
+from prompt_studio.prompt_studio_core_v2.models import CustomTool
+from utils.models.base_model import BaseModel
+
+
+class ToolStudioPrompt(BaseModel):
+ """Model class while store Prompt data for Custom Tool Studio.
+
+ It has Many to one relation with CustomTool for ToolStudio.
+ """
+
+ class EnforceType(models.TextChoices):
+ TEXT = "Text", "Response sent as Text"
+ NUMBER = "number", "Response sent as number"
+ EMAIL = "email", "Response sent as email"
+ DATE = "date", "Response sent as date"
+ BOOLEAN = "boolean", "Response sent as boolean"
+ JSON = "json", "Response sent as json"
+
+ class PromptType(models.TextChoices):
+ PROMPT = "PROMPT", "Response sent as Text"
+ NOTES = "NOTES", "Response sent as float"
+
+ class Mode(models.TextChoices):
+ DEFAULT = "Default", "Default choice for output"
+
+ prompt_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
+ prompt_key = models.TextField(
+ blank=False,
+ db_comment="Field to store the prompt key",
+ )
+ enforce_type = models.TextField(
+ blank=True,
+ db_comment="Field to store the type in \
+ which the response to be returned.",
+ choices=EnforceType.choices,
+ default=EnforceType.TEXT,
+ )
+ prompt = models.TextField(
+ blank=True, db_comment="Field to store the prompt", unique=False
+ )
+ tool_id = models.ForeignKey(
+ CustomTool,
+ on_delete=models.CASCADE,
+ related_name="tool_studio_prompts",
+ null=True,
+ blank=True,
+ )
+ sequence_number = models.IntegerField(null=True, blank=True)
+ prompt_type = models.TextField(
+ blank=True,
+ db_comment="Field to store the type of the input prompt",
+ choices=PromptType.choices,
+ )
+ profile_manager = models.ForeignKey(
+ ProfileManager,
+ on_delete=models.SET_NULL,
+ related_name="tool_studio_prompts",
+ null=True,
+ blank=True,
+ )
+ output = models.TextField(blank=True)
+ # TODO: Remove below 3 fields related to assertion
+ assert_prompt = models.TextField(
+ blank=True,
+ null=True,
+ db_comment="Field to store the asserted prompt",
+ unique=False,
+ )
+ assertion_failure_prompt = models.TextField(
+ blank=True,
+ null=True,
+ db_comment="Field to store the prompt key",
+ unique=False,
+ )
+ is_assert = models.BooleanField(default=False)
+ active = models.BooleanField(default=True, null=False, blank=False)
+ output_metadata = models.JSONField(
+ db_column="output_metadata",
+ null=False,
+ blank=False,
+ default=dict,
+ db_comment="JSON adapter metadata for the FE to load the pagination",
+ )
+ created_by = models.ForeignKey(
+ User,
+ on_delete=models.SET_NULL,
+ related_name="tool_studio_prompts_created",
+ null=True,
+ blank=True,
+ editable=False,
+ )
+ modified_by = models.ForeignKey(
+ User,
+ on_delete=models.SET_NULL,
+ related_name="tool_studio_prompts_modified",
+ null=True,
+ blank=True,
+ editable=False,
+ )
+ # Eval settings for the prompt
+ # NOTE:
+ # - Field name format is eval_<metric_type>_<metric_name>
+ # - Metric name alone should be UNIQUE across all eval metrics
+ evaluate = models.BooleanField(default=True)
+ eval_quality_faithfulness = models.BooleanField(default=True)
+ eval_quality_correctness = models.BooleanField(default=True)
+ eval_quality_relevance = models.BooleanField(default=True)
+ eval_security_pii = models.BooleanField(default=True)
+ eval_guidance_toxicity = models.BooleanField(default=True)
+ eval_guidance_completeness = models.BooleanField(default=True)
+ #
+
+ class Meta:
+ verbose_name = "Tool Studio Prompt"
+ verbose_name_plural = "Tool Studio Prompts"
+ db_table = "tool_studio_prompt_v2"
+ constraints = [
+ models.UniqueConstraint(
+ fields=["prompt_key", "tool_id"],
+ name="unique_prompt_key_tool_id_index",
+ ),
+ ]
diff --git a/backend/prompt_studio/prompt_studio_v2/serializers.py b/backend/prompt_studio/prompt_studio_v2/serializers.py
new file mode 100644
index 000000000..e1adddc33
--- /dev/null
+++ b/backend/prompt_studio/prompt_studio_v2/serializers.py
@@ -0,0 +1,33 @@
+from rest_framework import serializers
+
+from backend.serializers import AuditSerializer
+
+from .models import ToolStudioPrompt
+
+
+class ToolStudioPromptSerializer(AuditSerializer):
+ class Meta:
+ model = ToolStudioPrompt
+ fields = "__all__"
+
+
+class ToolStudioIndexSerializer(serializers.Serializer):
+ file_name = serializers.CharField()
+ tool_id = serializers.CharField()
+
+
+class ReorderPromptsSerializer(serializers.Serializer):
+ start_sequence_number = serializers.IntegerField(required=True)
+ end_sequence_number = serializers.IntegerField(required=True)
+ prompt_id = serializers.CharField(required=True)
+
+ def validate(self, data):
+ start_sequence_number = data.get("start_sequence_number")
+ end_sequence_number = data.get("end_sequence_number")
+
+ if start_sequence_number == end_sequence_number:
+ raise serializers.ValidationError(
+ "Start and end sequence numbers cannot be the same."
+ )
+
+ return data
diff --git a/backend/prompt_studio/prompt_studio_v2/urls.py b/backend/prompt_studio/prompt_studio_v2/urls.py
new file mode 100644
index 000000000..3d8c3d74c
--- /dev/null
+++ b/backend/prompt_studio/prompt_studio_v2/urls.py
@@ -0,0 +1,30 @@
+from django.urls import path
+from rest_framework.urlpatterns import format_suffix_patterns
+
+from .views import ToolStudioPromptView
+
+prompt_studio_prompt_detail = ToolStudioPromptView.as_view(
+ {
+ "get": "retrieve",
+ "put": "update",
+ "patch": "partial_update",
+ "delete": "destroy",
+ }
+)
+
+reorder_prompts = ToolStudioPromptView.as_view({"post": "reorder_prompts"})
+
+urlpatterns = format_suffix_patterns(
+ [
+ path(
+ "prompt/<uuid:pk>/",
+ prompt_studio_prompt_detail,
+ name="tool-studio-prompt-detail",
+ ),
+ path(
+ "prompt/reorder",
+ reorder_prompts,
+ name="reorder_prompts",
+ ),
+ ]
+)
diff --git a/backend/prompt_studio/prompt_studio_v2/views.py b/backend/prompt_studio/prompt_studio_v2/views.py
new file mode 100644
index 000000000..ccf63aa0d
--- /dev/null
+++ b/backend/prompt_studio/prompt_studio_v2/views.py
@@ -0,0 +1,56 @@
+from typing import Optional
+
+from django.db.models import QuerySet
+from prompt_studio.permission import PromptAcesssToUser
+from prompt_studio.prompt_studio_v2.constants import ToolStudioPromptKeys
+from prompt_studio.prompt_studio_v2.controller import PromptStudioController
+from prompt_studio.prompt_studio_v2.models import ToolStudioPrompt
+from prompt_studio.prompt_studio_v2.serializers import ToolStudioPromptSerializer
+from rest_framework import viewsets
+from rest_framework.decorators import action
+from rest_framework.request import Request
+from rest_framework.response import Response
+from rest_framework.versioning import URLPathVersioning
+from utils.filtering import FilterHelper
+
+
+class ToolStudioPromptView(viewsets.ModelViewSet):
+ """Viewset to handle all Tool Studio prompt related API logics.
+
+ Args:
+ viewsets (_type_)
+
+ Raises:
+ DuplicateData
+ FilenameMissingError
+ IndexingError
+ ValidationError
+ """
+
+ versioning_class = URLPathVersioning
+ serializer_class = ToolStudioPromptSerializer
+ permission_classes: list[type[PromptAcesssToUser]] = [PromptAcesssToUser]
+
+ def get_queryset(self) -> Optional[QuerySet]:
+ filter_args = FilterHelper.build_filter_args(
+ self.request,
+ ToolStudioPromptKeys.TOOL_ID,
+ )
+ if filter_args:
+ queryset = ToolStudioPrompt.objects.filter(**filter_args)
+ else:
+ queryset = ToolStudioPrompt.objects.all()
+ return queryset
+
+ @action(detail=True, methods=["post"])
+ def reorder_prompts(self, request: Request) -> Response:
+ """Reorder the sequence of prompts based on the provided data.
+
+ Args:
+ request (Request): The HTTP request containing the reorder data.
+
+ Returns:
+ Response: The HTTP response indicating the status of the reorder operation.
+ """
+ prompt_studio_controller = PromptStudioController()
+ return prompt_studio_controller.reorder_prompts(request)