LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows
Python ai-agentsdata-engineeringdocument-aigenerative-aiidpjson-extractionllmmcp-serverocrpdf-extractionprompt-engineeringstructured-output
feat: Replace LLM profile selection with direct LLM adapter selection in summarization settings (#1456)
jagadeeswaran-zipstackce0ab669d83c
diff --git a/backend/prompt_studio/prompt_profile_manager_v2/migrations/0002_alter_profilemanager_is_summarize_llm.py b/backend/prompt_studio/prompt_profile_manager_v2/migrations/0002_alter_profilemanager_is_summarize_llm.pynew file mode 100644index 000000000..dc635f08f--- /dev/null+++ b/backend/prompt_studio/prompt_profile_manager_v2/migrations/0002_alter_profilemanager_is_summarize_llm.py@@ -0,0 +1,20 @@+# Generated by Django 4.2.1 on 2025-07-28 12:19++from django.db import migrations, models+++class Migration(migrations.Migration):+ dependencies = [+ ("prompt_profile_manager_v2", "0001_initial"),+ ]++ operations = [+ migrations.AlterField(+ model_name="profilemanager",+ name="is_summarize_llm",+ field=models.BooleanField(+ db_comment="DEPRECATED: Default LLM Profile used for summarizing. Use CustomTool.summarize_llm_adapter instead.",+ default=False,+ ),+ ),+ ]diff --git a/backend/prompt_studio/prompt_profile_manager_v2/models.py b/backend/prompt_studio/prompt_profile_manager_v2/models.pyindex 8a814618c..dac8bad82 100644--- a/backend/prompt_studio/prompt_profile_manager_v2/models.py+++ b/backend/prompt_studio/prompt_profile_manager_v2/models.py@@ -98,7 +98,7 @@ class ProfileManager(BaseModel):is_summarize_llm = models.BooleanField(default=False,- db_comment="Default LLM Profile used for summarizing",+ db_comment="DEPRECATED: Default LLM Profile used for summarizing. Use CustomTool.summarize_llm_adapter instead.",)class Meta:diff --git a/backend/prompt_studio/prompt_studio_core_v2/constants.py b/backend/prompt_studio/prompt_studio_core_v2/constants.pyindex dd9980bc5..eab0cd839 100644--- a/backend/prompt_studio/prompt_studio_core_v2/constants.py+++ b/backend/prompt_studio/prompt_studio_core_v2/constants.py@@ -7,6 +7,7 @@ class ToolStudioKeys:PROMPTS = "prompts"PLATFORM_SERVICE_API_KEY = "PLATFORM_SERVICE_API_KEY"SUMMARIZE_LLM_PROFILE = "summarize_llm_profile"+ SUMMARIZE_LLM_ADAPTER = "summarize_llm_adapter"DEFAULT_PROFILE = "default_profile"diff --git a/backend/prompt_studio/prompt_studio_core_v2/migration_utils.py b/backend/prompt_studio/prompt_studio_core_v2/migration_utils.pynew file mode 100644index 000000000..b9236d04f--- /dev/null+++ b/backend/prompt_studio/prompt_studio_core_v2/migration_utils.py@@ -0,0 +1,99 @@+"""Migration utilities for safely handling lazy migration from profile-based+to adapter-based summarization configuration.+"""++import logging++from django.core.exceptions import ObjectDoesNotExist+from django.db import transaction++logger = logging.getLogger(__name__)+++class SummarizeMigrationUtils:+ """Utility class for handling summarize LLM adapter migrations safely."""++ @staticmethod+ def migrate_tool_to_adapter_based(+ tool_instance, skip_if_migrated: bool = True+ ) -> bool:+ """Safely migrate a tool from profile-based to adapter-based summarization.++ Args:+ tool_instance: The CustomTool instance to migrate+ skip_if_migrated: Skip if already migrated (default: True)++ Returns:+ bool: True if migration was performed, False if skipped or failed+ """+ # Import here to avoid circular import with ProfileManager -> CustomTool -> migration_utils+ from prompt_studio.prompt_profile_manager_v2.models import ProfileManager++ # Skip if already migrated+ if skip_if_migrated and tool_instance.summarize_llm_adapter:+ logger.debug(+ f"Tool {tool_instance.tool_id} already migrated to adapter-based summarization, "+ f"skipping migration (created_by: {tool_instance.created_by.email if tool_instance.created_by else 'unknown'}, "+ f"org: {tool_instance.organization})"+ )+ return False++ # Check if there's a summarize profile before entering transaction+ try:+ summarize_profile = ProfileManager.objects.get(+ prompt_studio_tool=tool_instance, is_summarize_llm=True+ )+ except ObjectDoesNotExist:+ logger.info(+ f"No summarize profile found for tool {tool_instance.tool_id}, skipping migration"+ )+ return False++ try:+ with transaction.atomic():+ # Re-fetch the instance within transaction to ensure fresh data+ tool_instance.refresh_from_db()++ # Double-check migration status within transaction+ if skip_if_migrated and tool_instance.summarize_llm_adapter:+ return False++ # Re-fetch the summarize profile with lock within transaction+ try:+ summarize_profile = ProfileManager.objects.select_for_update().get(+ prompt_studio_tool=tool_instance, is_summarize_llm=True+ )+ except ObjectDoesNotExist:+ logger.info(+ f"No summarize profile found for tool {tool_instance.tool_id}, skipping migration"+ )+ return False++ # Check if profile has an LLM adapter+ if not summarize_profile.llm:+ logger.warning(+ f"Summarize profile for tool {tool_instance.tool_id} has no LLM adapter, skipping migration"+ )+ return False++ # Perform the migration+ tool_instance.summarize_llm_adapter = summarize_profile.llm+ tool_instance.save(update_fields=["summarize_llm_adapter"])++ # Clear any existing profile-based summarize setting after successful migration+ ProfileManager.objects.filter(prompt_studio_tool=tool_instance).update(+ is_summarize_llm=False+ )++ logger.info(+ f"Successfully migrated tool {tool_instance.tool_id} from profile-based to adapter-based summarization"+ )+ return True++ except Exception as e:+ logger.warning(+ f"Failed to migrate tool {tool_instance.tool_id}: {str(e)}\n"+ f"Continuing with the deprecated approach for now",+ exc_info=True,+ )+ return Falsediff --git a/backend/prompt_studio/prompt_studio_core_v2/migrations/0002_add_summarize_llm_adapter.py b/backend/prompt_studio/prompt_studio_core_v2/migrations/0002_add_summarize_llm_adapter.pynew file mode 100644index 000000000..d025592e6--- /dev/null+++ b/backend/prompt_studio/prompt_studio_core_v2/migrations/0002_add_summarize_llm_adapter.py@@ -0,0 +1,26 @@+# Generated manually for UN-2239: Add summarize_llm_adapter field++import django.db.models.deletion+from django.db import migrations, models+++class Migration(migrations.Migration):+ dependencies = [+ ("adapter_processor_v2", "0001_initial"),+ ("prompt_studio_core_v2", "0001_initial"),+ ]++ operations = [+ migrations.AddField(+ model_name="customtool",+ name="summarize_llm_adapter",+ field=models.ForeignKey(+ blank=True,+ db_comment="Field to store the LLM adapter for summarization",+ null=True,+ on_delete=django.db.models.deletion.PROTECT,+ related_name="custom_tools_summarize_llm",+ to="adapter_processor_v2.adapterinstance",+ ),+ ),+ ]diff --git a/backend/prompt_studio/prompt_studio_core_v2/migrations/0003_alter_customtool_summarize_llm_adapter.py b/backend/prompt_studio/prompt_studio_core_v2/migrations/0003_alter_customtool_summarize_llm_adapter.pynew file mode 100644index 000000000..83f9beceb--- /dev/null+++ b/backend/prompt_studio/prompt_studio_core_v2/migrations/0003_alter_customtool_summarize_llm_adapter.py@@ -0,0 +1,26 @@+# Generated by Django 4.2.1 on 2025-07-28 12:19++import django.db.models.deletion+from django.db import migrations, models+++class Migration(migrations.Migration):+ dependencies = [+ ("adapter_processor_v2", "0001_initial"),+ ("prompt_studio_core_v2", "0002_add_summarize_llm_adapter"),+ ]++ operations = [+ migrations.AlterField(+ model_name="customtool",+ name="summarize_llm_adapter",+ field=models.ForeignKey(+ blank=True,+ db_comment="Field to store the LLM adapter for summarization",+ null=True,+ on_delete=django.db.models.deletion.PROTECT,+ related_name="summarize_enabled_custom_tools",+ to="adapter_processor_v2.adapterinstance",+ ),+ ),+ ]diff --git a/backend/prompt_studio/prompt_studio_core_v2/models.py b/backend/prompt_studio/prompt_studio_core_v2/models.pyindex de34f6410..41cf9a607 100644--- a/backend/prompt_studio/prompt_studio_core_v2/models.py+++ b/backend/prompt_studio/prompt_studio_core_v2/models.py@@ -66,6 +66,14 @@ class CustomTool(DefaultOrganizationMixin, BaseModel):db_comment="Field to store the summarize prompt",unique=False,)+ summarize_llm_adapter = models.ForeignKey(+ AdapterInstance,+ on_delete=models.PROTECT,+ db_comment="Field to store the LLM adapter for summarization",+ null=True,+ blank=True,+ related_name="summarize_enabled_custom_tools",+ )preamble = models.TextField(blank=True,db_comment="Preamble to the prompts",diff --git a/backend/prompt_studio/prompt_studio_core_v2/serializers.py b/backend/prompt_studio/prompt_studio_core_v2/serializers.pyindex 328719996..88b3f34ce 100644--- a/backend/prompt_studio/prompt_studio_core_v2/serializers.py+++ b/backend/prompt_studio/prompt_studio_core_v2/serializers.py@@ -3,8 +3,10 @@ from typing import Anyfrom account_v2.models import Userfrom account_v2.serializer import UserSerializer+from adapter_processor_v2.models import AdapterInstancefrom django.core.exceptions import ObjectDoesNotExistfrom rest_framework import serializers+from rest_framework.exceptions import ValidationErrorfrom utils.FileValidator import FileValidatorfrom utils.serializer.integrity_error_mixin import IntegrityErrorMixin@@ -17,6 +19,7 @@ from prompt_studio.prompt_studio_output_manager_v2.output_manager_util import ()from prompt_studio.prompt_studio_v2.models import ToolStudioPromptfrom prompt_studio.prompt_studio_v2.serializers import ToolStudioPromptSerializer+from unstract.sdk.adapters.enums import AdapterTypesfrom .models import CustomTool@@ -48,21 +51,55 @@ class CustomToolSerializer(IntegrityErrorMixin, AuditSerializer):}}+ def validate_summarize_llm_adapter(self, value):+ """Validate that the adapter type is LLM and is accessible to the user."""+ if value is None:+ return value++ # Check if user has access to this adapter+ request = self.context.get("request")+ if request and hasattr(request, "user"):+ try:+ adapter = AdapterInstance.objects.for_user(request.user).get(id=value.id)+ # Validate that the adapter type is LLM+ if adapter.adapter_type != AdapterTypes.LLM.value:+ raise ValidationError(+ "Only LLM adapters are allowed for summarization"+ )+ except AdapterInstance.DoesNotExist:+ raise ValidationError("Selected LLM adapter not found or not accessible")++ return value++ def update(self, instance, validated_data):+ """Custom update method to handle profile clearing when adapter is set."""+ instance = super().update(instance, validated_data)+ if validated_data.get("summarize_llm_adapter"):+ # Clear any existing deprecated profile-based summarize setting+ ProfileManager.objects.filter(prompt_studio_tool=instance).update(+ is_summarize_llm=False+ )+ return instance++ return instance+def to_representation(self, instance): # type: ignoredata = super().to_representation(instance)default_profile = None- # Fetch summarize LLM profile+ # Check new adapter-based approach first+ if instance.summarize_llm_adapter:+ data[TSKeys.SUMMARIZE_LLM_ADAPTER] = instance.summarize_llm_adapter.id++ # Check legacy profile-based approachtry:summarize_profile = ProfileManager.objects.get(prompt_studio_tool=instance, is_summarize_llm=True)- data[TSKeys.SUMMARIZE_LLM_PROFILE] = summarize_profile.profile_id+ if summarize_profile.llm:+ data[TSKeys.SUMMARIZE_LLM_PROFILE] = summarize_profile.profile_idexcept ObjectDoesNotExist:- logger.info(- "Summarize LLM profile doesn't exist for prompt tool %s",- str(instance.tool_id),- )+ pass# Fetch default LLM profiletry:diff --git a/backend/prompt_studio/prompt_studio_core_v2/views.py b/backend/prompt_studio/prompt_studio_core_v2/views.pyindex 07e993622..83a8e4866 100644--- a/backend/prompt_studio/prompt_studio_core_v2/views.py+++ b/backend/prompt_studio/prompt_studio_core_v2/views.py@@ -32,7 +32,6 @@ from prompt_studio.prompt_profile_manager_v2.serializers import ProfileManagerSefrom prompt_studio.prompt_studio_core_v2.constants import (FileViewTypes,ToolStudioErrors,- ToolStudioKeys,ToolStudioPromptKeys,)from prompt_studio.prompt_studio_core_v2.document_indexing_service import (@@ -43,6 +42,7 @@ from prompt_studio.prompt_studio_core_v2.exceptions import (MaxProfilesReachedError,ToolDeleteError,)+from prompt_studio.prompt_studio_core_v2.migration_utils import SummarizeMigrationUtilsfrom prompt_studio.prompt_studio_core_v2.prompt_studio_helper import PromptStudioHelperfrom prompt_studio.prompt_studio_core_v2.retrieval_strategies import (get_retrieval_strategy_metadata,@@ -94,6 +94,15 @@ class PromptStudioCoreView(viewsets.ModelViewSet):def get_queryset(self) -> QuerySet | None:return CustomTool.objects.for_user(self.request.user)+ def get_object(self):+ """Override get_object to trigger lazy migration when accessing tools."""+ instance = super().get_object()++ # Trigger lazy migration if needed (safe, with locking)+ SummarizeMigrationUtils.migrate_tool_to_adapter_based(instance)++ return instance+def create(self, request: HttpRequest) -> Response:serializer = self.get_serializer(data=request.data)serializer.is_valid(raise_exception=True)@@ -139,19 +148,6 @@ class PromptStudioCoreView(viewsets.ModelViewSet):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"])diff --git a/frontend/src/components/custom-tools/settings-modal/SettingsModal.jsx b/frontend/src/components/custom-tools/settings-modal/SettingsModal.jsxindex ea73f8606..a3f41300f 100644--- a/frontend/src/components/custom-tools/settings-modal/SettingsModal.jsx+++ b/frontend/src/components/custom-tools/settings-modal/SettingsModal.jsx@@ -11,7 +11,6 @@ import {import SpaceWrapper from "../../widgets/space-wrapper/SpaceWrapper";import { getMenuItem } from "../../../helpers/GetStaticData";import { ManageLlmProfiles } from "../manage-llm-profiles/ManageLlmProfiles";-import { useCustomToolStore } from "../../../store/custom-tool-store";import { CustomSynonyms } from "../custom-synonyms/CustomSynonyms";import { PreAndPostAmbleModal } from "../pre-and-post-amble-modal/PreAndPostAmbleModal";@@ -33,8 +32,6 @@ try {}function SettingsModal({ open, setOpen, handleUpdateTool }) {const [selectedId, setSelectedId] = useState(1);- const [llmItems, setLlmItems] = useState([]);- const { llmProfiles } = useCustomToolStore();const [menuItems, setMenuItems] = useState([]);const [components, setComponents] = useState([]);@@ -71,10 +68,7 @@ function SettingsModal({ open, setOpen, handleUpdateTool }) {getMenuItem("SummarizedExtraction", 2, <FileTextOutlined />));listOfComponents[2] = (- <SummarizeManager- llmItems={llmItems}- handleUpdateTool={handleUpdateTool}- />+ <SummarizeManager handleUpdateTool={handleUpdateTool} />);position++;}@@ -116,27 +110,13 @@ function SettingsModal({ open, setOpen, handleUpdateTool }) {}setMenuItems(items);setComponents(listOfComponents);- }, [llmItems]);-- useEffect(() => {- getLlmProfilesDropdown();- }, [llmProfiles]);+ }, []);const handleSelectItem = (e) => {const id = e.key;setSelectedId(id?.toString());};- const getLlmProfilesDropdown = () => {- const items = [...llmProfiles].map((item) => {- return {- value: item?.profile_id,- label: item?.profile_name,- };- });- setLlmItems(items);- };-return (<Modalopen={open}