LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows
Python ai-agentsdata-engineeringdocument-aigenerative-aiidpjson-extractionllmmcp-serverocrpdf-extractionprompt-engineeringstructured-output
Changes for coupling profile manager with prompt studio (#71)
Rahul Johnyeda1170470a6
diff --git a/backend/prompt_studio/prompt_profile_manager/migrations/0007_profilemanager_is_default_and_more.py b/backend/prompt_studio/prompt_profile_manager/migrations/0007_profilemanager_is_default_and_more.pynew file mode 100644index 000000000..e66aeb27b--- /dev/null+++ b/backend/prompt_studio/prompt_profile_manager/migrations/0007_profilemanager_is_default_and_more.py@@ -0,0 +1,53 @@+# Generated by Django 4.2.1 on 2024-03-08 11:44++import django.db.models.deletion+from django.db import migrations, models+++class Migration(migrations.Migration):+ dependencies = [+ (+ "prompt_studio_core",+ "0007_remove_customtool_default_profile_and_more",+ ),+ ("prompt_profile_manager", "0006_alter_profilemanager_x2text"),+ ]++ operations = [+ migrations.AddField(+ model_name="profilemanager",+ name="is_default",+ field=models.BooleanField(+ db_comment="Default LLM Profile used in prompt", default=False+ ),+ ),+ migrations.AddField(+ model_name="profilemanager",+ name="is_summarize_llm",+ field=models.BooleanField(+ db_comment="Default LLM Profile used for summarizing",+ default=False,+ ),+ ),+ migrations.AddField(+ model_name="profilemanager",+ name="prompt_studio_tool",+ field=models.ForeignKey(+ null=True,+ on_delete=django.db.models.deletion.PROTECT,+ to="prompt_studio_core.customtool",+ ),+ ),+ migrations.AlterField(+ model_name="profilemanager",+ name="profile_name",+ field=models.TextField(),+ ),+ migrations.AddConstraint(+ model_name="profilemanager",+ constraint=models.UniqueConstraint(+ fields=("prompt_studio_tool", "profile_name"),+ name="unique_prompt_studio_tool_profile_name",+ ),+ ),+ ]diff --git a/backend/prompt_studio/prompt_profile_manager/migrations/0008_profilemanager_migration.py b/backend/prompt_studio/prompt_profile_manager/migrations/0008_profilemanager_migration.pynew file mode 100644index 000000000..c703139af--- /dev/null+++ b/backend/prompt_studio/prompt_profile_manager/migrations/0008_profilemanager_migration.py@@ -0,0 +1,64 @@+# Generated by Django 4.2.1 on 2024-03-08 06:30++from typing import Any++from django.db import migrations+++class Migration(migrations.Migration):+ dependencies = [+ (+ "prompt_profile_manager",+ "0007_profilemanager_is_default_and_more",+ ),+ ]++ def MigrateProfileManager(apps: Any, schema_editor: Any) -> None:+ CustomTool = apps.get_model("prompt_studio_core", "CustomTool")+ ProfileManager = apps.get_model(+ "prompt_profile_manager", "ProfileManager"+ )+ ToolStudioPrompt = apps.get_model("prompt_studio", "ToolStudioPrompt")++ # Iterate over prompt studio tools+ custom_tools = CustomTool.objects.all()+ for custom_tool in custom_tools:+ custom_prompts = ToolStudioPrompt.objects.filter(+ tool_id=custom_tool+ ).distinct("profile_manager")+ """Iterate over prompts inside specific tool and return the once+ which has distinct profile manager."""++ for index, custom_prompt in enumerate(custom_prompts):+ profile_manager = ProfileManager.objects.get(+ pk=custom_prompt.profile_manager.profile_id+ )+ """Check this profile manager to see if the profile manager is+ tagged to any prompt tool If its none, meaning profile manager+ created was global and before migration."""++ if not profile_manager.prompt_studio_tool:+ # Copy Profile manager Id before replicating+ profile_id = profile_manager.profile_id+ # Replicate those profile manager+ replicated_profile_manager = profile_manager+ replicated_profile_manager.profile_id = None+ replicated_profile_manager.profile_name = (+ f"Profile_{index+1}"+ )+ """Attach the replicated profile manager to the+ corresponding prompt tool."""+ replicated_profile_manager.prompt_studio_tool = custom_tool+ replicated_profile_manager.save()+ """Custom prompts where the profile manager and tool+ associated replace it with replicated profile manager."""++ ToolStudioPrompt.objects.filter(+ profile_manager=profile_id, tool_id=custom_tool+ ).update(profile_manager=replicated_profile_manager)++ operations = [+ migrations.RunPython(+ MigrateProfileManager, reverse_code=migrations.RunPython.noop+ ),+ ]diff --git a/backend/prompt_studio/prompt_profile_manager/models.py b/backend/prompt_studio/prompt_profile_manager/models.pyindex 99f65433b..b84263e6f 100644--- a/backend/prompt_studio/prompt_profile_manager/models.py+++ b/backend/prompt_studio/prompt_profile_manager/models.py@@ -3,6 +3,7 @@ import uuidfrom account.models import Userfrom adapter_processor.models import AdapterInstancefrom django.db import models+from prompt_studio.prompt_studio_core.models import CustomToolfrom utils.models.base_model import BaseModel@@ -17,7 +18,7 @@ class ProfileManager(BaseModel):profile_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)- profile_name = models.TextField(unique=True, blank=False)+ profile_name = models.TextField(blank=False)vector_store = models.ForeignKey(AdapterInstance,db_comment="Field to store the chosen vector store.",@@ -79,3 +80,24 @@ class ProfileManager(BaseModel):blank=True,editable=False,)++ prompt_studio_tool = models.ForeignKey(+ CustomTool, on_delete=models.PROTECT, null=True+ )+ 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:+ constraints = [+ models.UniqueConstraint(+ fields=["prompt_studio_tool", "profile_name"],+ name="unique_prompt_studio_tool_profile_name",+ ),+ ]diff --git a/backend/prompt_studio/prompt_profile_manager/urls.py b/backend/prompt_studio/prompt_profile_manager/urls.pyindex 7de978a83..c846b259d 100644--- a/backend/prompt_studio/prompt_profile_manager/urls.py+++ b/backend/prompt_studio/prompt_profile_manager/urls.py@@ -3,9 +3,7 @@ from rest_framework.urlpatterns import format_suffix_patternsfrom .views import ProfileManagerView-profile_manager_list = ProfileManagerView.as_view(- {"get": "list", "post": "create"}-)+profile_manager_list = ProfileManagerView.as_view({"post": "create"})profile_manager_detail = ProfileManagerView.as_view({"get": "retrieve",diff --git a/backend/prompt_studio/prompt_studio_core/constants.py b/backend/prompt_studio/prompt_studio_core/constants.pyindex a848828af..6c5b4ad01 100644--- a/backend/prompt_studio/prompt_studio_core/constants.py+++ b/backend/prompt_studio/prompt_studio_core/constants.py@@ -6,6 +6,8 @@ class ToolStudioKeys: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:diff --git a/backend/prompt_studio/prompt_studio_core/migrations/0007_remove_customtool_default_profile_and_more.py b/backend/prompt_studio/prompt_studio_core/migrations/0007_remove_customtool_default_profile_and_more.pynew file mode 100644index 000000000..93157a8a1--- /dev/null+++ b/backend/prompt_studio/prompt_studio_core/migrations/0007_remove_customtool_default_profile_and_more.py@@ -0,0 +1,23 @@+# Generated by Django 4.2.1 on 2024-03-07 06:25++from django.db import migrations+++class Migration(migrations.Migration):+ dependencies = [+ (+ "prompt_studio_core",+ "0006_alter_customtool_summarize_as_source_and_more",+ ),+ ]++ operations = [+ migrations.RemoveField(+ model_name="customtool",+ name="default_profile",+ ),+ migrations.RemoveField(+ model_name="customtool",+ name="summarize_llm_profile",+ ),+ ]diff --git a/backend/prompt_studio/prompt_studio_core/models.py b/backend/prompt_studio/prompt_studio_core/models.pyindex 9ef5127ab..4dbf94358 100644--- a/backend/prompt_studio/prompt_studio_core/models.py+++ b/backend/prompt_studio/prompt_studio_core/models.py@@ -2,7 +2,6 @@ import uuidfrom account.models import Userfrom django.db import models-from prompt_studio.prompt_profile_manager.models import ProfileManagerfrom utils.models.base_model import BaseModel@@ -38,22 +37,7 @@ class CustomTool(BaseModel):postamble = models.TextField(blank=True, db_comment="Appended as postable to prompts.")- default_profile = models.ForeignKey(- ProfileManager,- on_delete=models.SET_NULL,- related_name="default_profile",- null=True,- blank=True,- db_comment="Default LLM Profile used in prompt",- )- summarize_llm_profile = models.ForeignKey(- ProfileManager,- on_delete=models.SET_NULL,- related_name="summarize_llm_profile",- null=True,- blank=True,- db_comment="LLM Profile used for summarize",- )+summarize_context = models.BooleanField(default=False, db_comment="Flag to summarize content")diff --git a/backend/prompt_studio/prompt_studio_core/prompt_studio_helper.py b/backend/prompt_studio/prompt_studio_core/prompt_studio_helper.pyindex 249fb53db..bd5e1aab7 100644--- a/backend/prompt_studio/prompt_studio_core/prompt_studio_helper.py+++ b/backend/prompt_studio/prompt_studio_core/prompt_studio_helper.py@@ -103,10 +103,16 @@ class PromptStudioHelper:"""tool: CustomTool = CustomTool.objects.get(pk=tool_id)if is_summary:- default_profile = tool.summarize_llm_profile+ profile_manager = ProfileManager.objects.get(+ prompt_studio_tool=tool, is_summarize_llm=True+ )+ default_profile = profile_managerfile_path = file_nameelse:- default_profile = tool.default_profile+ profile_manager = ProfileManager.objects.get(+ prompt_studio_tool=tool, is_default=True+ )+ default_profile = profile_managerfile_path = FileManagerHelper.handle_sub_directory_for_tenants(org_id, is_create=False, user_id=user_id, tool_id=tool_id)diff --git a/backend/prompt_studio/prompt_studio_core/serializers.py b/backend/prompt_studio/prompt_studio_core/serializers.pyindex af5fada93..f711e1230 100644--- a/backend/prompt_studio/prompt_studio_core/serializers.py+++ b/backend/prompt_studio/prompt_studio_core/serializers.py@@ -1,12 +1,15 @@import loggingfrom typing import Any-from backend.serializers import AuditSerializer+from django.core.exceptions import ObjectDoesNotExist+from prompt_studio.prompt_profile_manager.models import ProfileManagerfrom prompt_studio.prompt_studio.models import ToolStudioPromptfrom prompt_studio.prompt_studio.serializers import ToolStudioPromptSerializerfrom prompt_studio.prompt_studio_core.constants import ToolStudioKeys as TSKeysfrom rest_framework import serializers+from backend.serializers import AuditSerializer+from .models import CustomToollogger = logging.getLogger(__name__)@@ -19,6 +22,26 @@ class CustomToolSerializer(AuditSerializer):def to_representation(self, instance): # type: ignoredata = 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.objects.get(+ prompt_studio_tool=instance, is_default=True+ )+ data[TSKeys.DEFAULT_PROFILE] = profile_manager.profile_id+ except ObjectDoesNotExist:+ 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)@@ -34,6 +57,7 @@ class CustomToolSerializer(AuditSerializer):except Exception as e:logger.error(f"Error occured while appending prompts {e}")return data+return datadiff --git a/backend/prompt_studio/prompt_studio_core/urls.py b/backend/prompt_studio/prompt_studio_core/urls.pyindex 6469f6236..b90476b63 100644--- a/backend/prompt_studio/prompt_studio_core/urls.py+++ b/backend/prompt_studio/prompt_studio_core/urls.py@@ -17,6 +17,10 @@ prompt_studio_detail = PromptStudioCoreView.as_view(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_prompt_index = PromptStudioCoreView.as_view({"post": "index_document"})@@ -40,6 +44,11 @@ urlpatterns = format_suffix_patterns(prompt_studio_choices,name="prompt-studio-choices",),+ path(+ "prompt-studio/profiles/<uuid:pk>/",+ prompt_studio_profiles,+ name="prompt-studio-profiles",+ ),path("prompt-studio/index-document/",prompt_studio_prompt_index,diff --git a/backend/prompt_studio/prompt_studio_core/views.py b/backend/prompt_studio/prompt_studio_core/views.pyindex 5109782d3..a81203344 100644--- a/backend/prompt_studio/prompt_studio_core/views.py+++ b/backend/prompt_studio/prompt_studio_core/views.py@@ -7,6 +7,10 @@ from django.db.models import QuerySetfrom django.http import HttpRequestfrom permissions.permission import IsOwnerfrom prompt_studio.processor_loader import ProcessorConfig, load_plugins+from prompt_studio.prompt_profile_manager.models import ProfileManager+from prompt_studio.prompt_profile_manager.serializers import (+ ProfileManagerSerializer,+)from prompt_studio.prompt_studio.exceptions import FilenameMissingErrorfrom prompt_studio.prompt_studio_core.constants import (ToolStudioErrors,@@ -27,7 +31,6 @@ from rest_framework.response import Responsefrom rest_framework.versioning import URLPathVersioningfrom tool_instance.models import ToolInstancefrom utils.filtering import FilterHelper-from prompt_studio.prompt_studio_registry.models import PromptStudioRegistryfrom .models import CustomToolfrom .serializers import CustomToolSerializer, PromptStudioIndexSerializer@@ -97,6 +100,26 @@ class PromptStudioCoreView(viewsets.ModelViewSet):)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.@@ -115,6 +138,45 @@ class PromptStudioCoreView(viewsets.ModelViewSet):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=["get"])def index_document(self, request: HttpRequest) -> Response:"""API Entry point method to index input file.diff --git a/frontend/src/components/custom-tools/add-llm-profile-modal/AddLlmProfileModal.jsx b/frontend/src/components/custom-tools/add-llm-profile-modal/AddLlmProfileModal.jsxindex a3afdec67..8a4609ac6 100644--- a/frontend/src/components/custom-tools/add-llm-profile-modal/AddLlmProfileModal.jsx+++ b/frontend/src/components/custom-tools/add-llm-profile-modal/AddLlmProfileModal.jsx@@ -47,7 +47,7 @@ function AddLlmProfileModal({const [activeKey, setActiveKey] = useState(false);const [loading, setLoading] = useState(false);const { sessionDetails } = useSessionStore();- const { getDropdownItems, llmProfiles, updateCustomTool } =+ const { details, getDropdownItems, llmProfiles, updateCustomTool } =useCustomToolStore();const { setAlertDetails } = useAlertStore();const axiosPrivate = useAxiosPrivate();@@ -88,6 +88,7 @@ function AddLlmProfileModal({similarity_top_k: 1,section: "Default",reindex: false,+ prompt_studio_tool: details?.tool_id,});setEditLlmProfileId(null);@@ -133,6 +134,7 @@ function AddLlmProfileModal({similarity_top_k: llmProfileDetails?.similarity_top_k,section: llmProfileDetails?.section,reindex: llmProfileDetails?.reindex,+ prompt_studio_tool: details?.tool_id,});setActiveKey(true);}, [editLlmProfileId]);diff --git a/frontend/src/components/custom-tools/manage-llm-profiles/ManageLlmProfiles.jsx b/frontend/src/components/custom-tools/manage-llm-profiles/ManageLlmProfiles.jsxindex 47076df0a..08bed93b5 100644--- a/frontend/src/components/custom-tools/manage-llm-profiles/ManageLlmProfiles.jsx+++ b/frontend/src/components/custom-tools/manage-llm-profiles/ManageLlmProfiles.jsx@@ -78,7 +78,7 @@ function ManageLlmProfiles({const requestOptions = {method: "PATCH",- url: `/api/v1/unstract/${sessionDetails?.orgId}/prompt-studio/${details?.tool_id}/`,+ url: `/api/v1/unstract/${sessionDetails?.orgId}/prompt-studio/profiles/${details?.tool_id}/`,headers: {"X-CSRFToken": sessionDetails?.csrfToken,"Content-Type": "application/json",diff --git a/frontend/src/components/helpers/custom-tools/CustomToolsHelper.js b/frontend/src/components/helpers/custom-tools/CustomToolsHelper.jsindex ec3c3d5ac..bcd2e3ec4 100644--- a/frontend/src/components/helpers/custom-tools/CustomToolsHelper.js+++ b/frontend/src/components/helpers/custom-tools/CustomToolsHelper.js@@ -68,7 +68,7 @@ function CustomToolsHelper() {const reqOpsLlmProfiles = {method: "GET",- url: `/api/v1/unstract/${sessionDetails?.orgId}/prompt-studio/profile-manager/`,+ url: `/api/v1/unstract/${sessionDetails?.orgId}/prompt-studio/profiles/${id}`,};return handleApiRequest(reqOpsLlmProfiles);