LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows
Python ai-agentsdata-engineeringdocument-aigenerative-aiidpjson-extractionllmmcp-serverocrpdf-extractionprompt-engineeringstructured-output
Handled the logic to toggle between extract and summarize
Tahier Hussainfe3b2059916e
diff --git a/backend/prompt_studio/prompt_studio_core/constants.py b/backend/prompt_studio/prompt_studio_core/constants.pyindex df5be97e9..a848828af 100644--- a/backend/prompt_studio/prompt_studio_core/constants.py+++ b/backend/prompt_studio/prompt_studio_core/constants.py@@ -73,6 +73,8 @@ class ToolStudioPromptKeys:EVAL_SETTINGS_EVALUATE = "evaluate"EVAL_SETTINGS_MONITOR_LLM = "monitor_llm"EVAL_SETTINGS_EXCLUDE_FAILED = "exclude_failed"+ SUMMARIZE = "summarize"+ SUMMARIZED_RESULT = "summarized_result"class LogLevels:diff --git a/backend/prompt_studio/prompt_studio_core/models.py b/backend/prompt_studio/prompt_studio_core/models.pyindex 4b8128461..4a2327e7e 100644--- a/backend/prompt_studio/prompt_studio_core/models.py+++ b/backend/prompt_studio/prompt_studio_core/models.py@@ -45,6 +45,20 @@ class CustomTool(BaseModel):null=True,blank=True,)+ summarize_llm_profile = models.ForeignKey(+ ProfileManager,+ on_delete=models.SET_NULL,+ related_name="summarize_llm_profile",+ null=True,+ blank=True,+ )+ summarize_context = models.BooleanField(default=True)+ summarize_as_source = models.BooleanField(default=True)+ summarize_prompt = models.TextField(+ blank=True,+ db_comment="Field to store the summarize prompt",+ unique=False,+ )prompt_grammer = models.JSONField(null=True, blank=True, db_comment="Synonymous words used in prompt")diff --git a/backend/prompt_studio/prompt_studio_core/prompt_studio_helper.py b/backend/prompt_studio/prompt_studio_core/prompt_studio_helper.pyindex a2a4165a1..191618cb5 100644--- a/backend/prompt_studio/prompt_studio_core/prompt_studio_helper.py+++ b/backend/prompt_studio/prompt_studio_core/prompt_studio_helper.py@@ -2,7 +2,7 @@ import jsonimport loggingimport osfrom pathlib import Path-from typing import Any, Optional+from typing import Anyfrom django.conf import settingsfrom file_management.file_management_helper import FileManagerHelper@@ -16,7 +16,6 @@ from prompt_studio.prompt_studio_core.exceptions import (AnswerFetchError,DefaultProfileError,IndexingError,- OutputSaveError,PromptNotValid,ToolNotValid,)@@ -82,7 +81,7 @@ class PromptStudioHelper:@staticmethoddef index_document(- tool_id: str, file_name: str, org_id: str, user_id: str+ tool_id: str, file_name: str, org_id: str, user_id: str, is_summary: bool = False) -> Any:"""Method to index a document.@@ -95,16 +94,19 @@ class PromptStudioHelper:IndexingError"""tool: CustomTool = CustomTool.objects.get(pk=tool_id)- default_profile: Optional[ProfileManager] = tool.default_profile++ if is_summary:+ default_profile: ProfileManager = tool.summarize_llm_profile+ file_path = file_name+ else:+ default_profile: ProfileManager = tool.default_profile+ 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 default_profile:raise DefaultProfileError()- file_path = FileManagerHelper.handle_sub_directory_for_tenants(- org_id=org_id,- user_id=user_id,- tool_id=tool_id,- is_create=False,- )- file_path = str(Path(file_path) / file_name)stream_log.publish(tool_id,stream_log.log(@@ -134,6 +136,7 @@ class PromptStudioHelper:tool_id=tool_id,file_name=file_path,org_id=org_id,+ is_summary=is_summary,)logger.info(f"Indexing done sucessfully for {file_name}")stream_log.publish(@@ -182,6 +185,16 @@ class PromptStudioHelper:prompts: list[ToolStudioPrompt] = []prompts.append(prompt_instance)tool: CustomTool = prompt_instance.tool_id+++ if tool.summarize_as_source:+ directory, filename = os.path.split(file_path)+ file_path: str = os.path.join(+ directory,+ TSPKeys.SUMMARIZE,+ os.path.splitext(filename)[0] + ".txt",+ )+stream_log.publish(tool.tool_id,stream_log.log(@@ -277,6 +290,7 @@ class PromptStudioHelper:file_name=path,tool_id=str(tool.tool_id),org_id=org_id,+ is_summary=tool.summarize_as_source)output: dict[str, Any] = {}@@ -348,19 +362,6 @@ class PromptStudioHelper:if answer["status"] == "ERROR":raise AnswerFetchError()output_response = json.loads(answer["structure_output"])-- # persist output- try:- for key in output_response:- if TSPKeys.EVAL_RESULT_DELIM not in key:- persisted_prompt = ToolStudioPrompt.objects.get(- prompt_key=key- )- persisted_prompt.output = output_response[key] or ""- persisted_prompt.save()- except Exception as e:- logger.error(f"Exception while saving prompt output: {e}")- raise OutputSaveError()return output_response@staticmethod@@ -369,6 +370,7 @@ class PromptStudioHelper:tool_id: str,file_name: str,org_id: str,+ is_summary: bool = False,) -> str:try:util = PromptIdeBaseTool(log_level=LogLevel.INFO, org_id=org_id)@@ -391,5 +393,6 @@ class PromptStudioHelper:chunk_size=profile_manager.chunk_size,chunk_overlap=profile_manager.chunk_overlap,reindex=profile_manager.reindex,+ is_summary=is_summary,))