Skip to content

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

[FEAT] Table extractor Enhancements (#631)

harini-venkataraman9754247b8c71
diff --git a/backend/prompt_studio/prompt_studio/migrations/0008_alter_toolstudioprompt_enforce_type.py b/backend/prompt_studio/prompt_studio/migrations/0008_alter_toolstudioprompt_enforce_type.py
new file mode 100644
index 000000000..5e6012a54
--- /dev/null
+++ b/backend/prompt_studio/prompt_studio/migrations/0008_alter_toolstudioprompt_enforce_type.py
@@ -0,0 +1,35 @@
+# Generated by Django 4.2.1 on 2024-09-26 08:38
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("prompt_studio", "0007_alter_toolstudioprompt_enforce_type"),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name="toolstudioprompt",
+ name="enforce_type",
+ field=models.TextField(
+ blank=True,
+ choices=[
+ ("Text", "Response sent as Text"),
+ ("number", "Response sent as number"),
+ ("email", "Response sent as email"),
+ ("date", "Response sent as date"),
+ ("boolean", "Response sent as boolean"),
+ ("json", "Response sent as json"),
+ ("table", "Response sent as table"),
+ (
+ "record",
+ "Response sent for records. Entries of records are list of logical and organized individual entities with distint values",
+ ),
+ ],
+ db_comment="Field to store the type in which the response to be returned.",
+ default="Text",
+ ),
+ ),
+ ]
diff --git a/backend/prompt_studio/prompt_studio/models.py b/backend/prompt_studio/prompt_studio/models.py
index df2e9dc3f..5cc054efb 100644
--- a/backend/prompt_studio/prompt_studio/models.py
+++ b/backend/prompt_studio/prompt_studio/models.py
@@ -21,6 +21,12 @@ class ToolStudioPrompt(BaseModel):
BOOLEAN = "boolean", "Response sent as boolean"
JSON = "json", "Response sent as json"
TABLE = "table", "Response sent as table"
+ RECORD = "record", (
+ "Response sent for records. "
+ "Entries of records are list of "
+ "logical and organized individual "
+ "entities with distint values"
+ )
class PromptType(models.TextChoices):
PROMPT = "PROMPT", "Response sent as Text"
diff --git a/backend/prompt_studio/prompt_studio_core/constants.py b/backend/prompt_studio/prompt_studio_core/constants.py
index afba6153d..659270975 100644
--- a/backend/prompt_studio/prompt_studio_core/constants.py
+++ b/backend/prompt_studio/prompt_studio_core/constants.py
@@ -94,6 +94,7 @@ class ToolStudioPromptKeys:
PLATFORM_POSTAMBLE = "platform_postamble"
SUMMARIZE_AS_SOURCE = "summarize_as_source"
VARIABLE_MAP = "variable_map"
+ RECORD = "record"
class FileViewTypes:
diff --git a/backend/prompt_studio/prompt_studio_core/prompt_studio_helper.py b/backend/prompt_studio/prompt_studio_core/prompt_studio_helper.py
index 9a37b038a..f3fb59ce6 100644
--- a/backend/prompt_studio/prompt_studio_core/prompt_studio_helper.py
+++ b/backend/prompt_studio/prompt_studio_core/prompt_studio_helper.py
@@ -59,7 +59,7 @@ ERROR_MSG = "User %s doesn't have access to adapter %s"
logger = logging.getLogger(__name__)
-modifier_loader = load_modifier_plugins()
+modifier_plugins = load_modifier_plugins()
class PromptStudioHelper:
@@ -259,6 +259,13 @@ class PromptStudioHelper:
choices = f.read()
f.close()
response: dict[str, Any] = json.loads(choices)
+
+ for modifier_plugin in modifier_plugins:
+ cls = modifier_plugin[ModifierConfig.METADATA][
+ ModifierConfig.METADATA_SERVICE_CLASS
+ ]
+ response = cls.update_select_choices(default_choices=response)
+
return response
@staticmethod
@@ -444,7 +451,10 @@ class PromptStudioHelper:
):
prompt_instance = PromptStudioHelper._fetch_prompt_from_id(id)
- if prompt_instance.enforce_type == TSPKeys.TABLE and not modifier_loader:
+ if (
+ prompt_instance.enforce_type == TSPKeys.TABLE
+ or prompt_instance.enforce_type == TSPKeys.RECORD
+ ) and not modifier_plugins:
raise OperationNotSupported()
prompt_name = prompt_instance.prompt_key
@@ -540,6 +550,7 @@ class PromptStudioHelper:
if prompt.prompt_type != TSPKeys.NOTES
and prompt.active
and prompt.enforce_type != TSPKeys.TABLE
+ and prompt.enforce_type != TSPKeys.RECORD
]
if not prompts:
logger.error(f"[{tool_id or 'NA'}] No prompts found for id: {id}")
@@ -834,13 +845,16 @@ class PromptStudioHelper:
output: dict[str, Any],
) -> dict[str, Any]:
- if prompt.enforce_type == TSPKeys.TABLE:
+ if (
+ prompt.enforce_type == TSPKeys.TABLE
+ or prompt.enforce_type == TSPKeys.RECORD
+ ):
extract_doc_path: str = (
PromptStudioHelper._get_extract_or_summary_document_path(
org_id, user_id, tool_id, doc_name, TSPKeys.EXTRACT
)
)
- for modifier_plugin in modifier_loader:
+ for modifier_plugin in modifier_plugins:
cls = modifier_plugin[ModifierConfig.METADATA][
ModifierConfig.METADATA_SERVICE_CLASS
]
@@ -850,6 +864,7 @@ class PromptStudioHelper:
prompt_id=str(prompt.prompt_id),
prompt=prompt.prompt,
input_file=extract_doc_path,
+ clean_pages=True,
)
return output
diff --git a/backend/prompt_studio/prompt_studio_core/static/select_choices.json b/backend/prompt_studio/prompt_studio_core/static/select_choices.json
index 4fa10995f..f9e002f7d 100644
--- a/backend/prompt_studio/prompt_studio_core/static/select_choices.json
+++ b/backend/prompt_studio/prompt_studio_core/static/select_choices.json
@@ -14,7 +14,8 @@
"date":"date",
"boolean":"boolean",
"json":"json",
- "table":"table"
+ "table":"table",
+ "record":"record"
},
"output_processing":{
"DEFAULT":"Default"
diff --git a/backend/prompt_studio/prompt_studio_registry/constants.py b/backend/prompt_studio/prompt_studio_registry/constants.py
index 0c2e3d250..966f30826 100644
--- a/backend/prompt_studio/prompt_studio_registry/constants.py
+++ b/backend/prompt_studio/prompt_studio_registry/constants.py
@@ -10,6 +10,7 @@ class PromptStudioRegistryKeys:
FILE_NAME = "file_name"
UNDEFINED = "undefined"
TABLE = "table"
+ RECORD = "record"
class PromptStudioRegistryErrors:
diff --git a/backend/prompt_studio/prompt_studio_registry/prompt_studio_registry_helper.py b/backend/prompt_studio/prompt_studio_registry/prompt_studio_registry_helper.py
index b2f4a3dc2..af00cffa6 100644
--- a/backend/prompt_studio/prompt_studio_registry/prompt_studio_registry_helper.py
+++ b/backend/prompt_studio/prompt_studio_registry/prompt_studio_registry_helper.py
@@ -25,7 +25,7 @@ from .models import PromptStudioRegistry
from .serializers import PromptStudioRegistrySerializer
logger = logging.getLogger(__name__)
-modifier_loader = load_modifier_plugins()
+modifier_plugins = load_modifier_plugins()
class PromptStudioRegistryHelper:
@@ -335,8 +335,11 @@ class PromptStudioRegistryHelper:
output[JsonSchemaKey.REINDEX] = prompt.profile_manager.reindex
output[JsonSchemaKey.EMBEDDING_SUFFIX] = embedding_suffix
- if prompt.enforce_type == PromptStudioRegistryKeys.TABLE:
- for modifier_plugin in modifier_loader:
+ if (
+ prompt.enforce_type == PromptStudioRegistryKeys.TABLE
+ or prompt.enforce_type == PromptStudioRegistryKeys.RECORD
+ ):
+ for modifier_plugin in modifier_plugins:
cls = modifier_plugin[ModifierConfig.METADATA][
ModifierConfig.METADATA_SERVICE_CLASS
]
diff --git a/backend/utils/common_utils.py b/backend/utils/common_utils.py
index 8db14369e..8e2c2973c 100644
--- a/backend/utils/common_utils.py
+++ b/backend/utils/common_utils.py
@@ -1,3 +1,4 @@
+import json
from enum import Enum
@@ -14,6 +15,22 @@ class CommonUtils:
"""
return string.lower() == "true"
+ @staticmethod
+ def is_json(string: str) -> bool:
+ """Utility method to check if input is a JSON String.
+
+ Args:
+ string (str): String to be checked.
+
+ Returns:
+ bool : true if valid JSON String
+ """
+ try:
+ json.loads(string)
+ except json.JSONDecodeError:
+ return False
+ return True
+
class ModelEnum(Enum):
@classmethod
diff --git a/prompt-service/src/unstract/prompt_service/constants.py b/prompt-service/src/unstract/prompt_service/constants.py
index f5ee6f2e0..7ff4ca965 100644
--- a/prompt-service/src/unstract/prompt_service/constants.py
+++ b/prompt-service/src/unstract/prompt_service/constants.py
@@ -66,6 +66,7 @@ class PromptServiceContants:
CLEAN_CONTEXT = "clean-context"
SUMMARIZE_AS_SOURCE = "summarize_as_source"
VARIABLE_MAP = "variable_map"
+ RECORD = "record"
TEXT = "text"
diff --git a/prompt-service/src/unstract/prompt_service/helper.py b/prompt-service/src/unstract/prompt_service/helper.py
index 43a29245a..fc0628a3f 100644
--- a/prompt-service/src/unstract/prompt_service/helper.py
+++ b/prompt-service/src/unstract/prompt_service/helper.py
@@ -353,6 +353,7 @@ def extract_table(
plugins: dict[str, dict[str, Any]],
structured_output: dict[str, Any],
llm: LLM,
+ enforce_type: str,
) -> dict[str, Any]:
table_settings = output[PSKeys.TABLE_SETTINGS]
table_extractor: dict[str, Any] = plugins.get("table-extractor", {})
@@ -363,7 +364,7 @@ def extract_table(
)
try:
answer = table_extractor["entrypoint_cls"].extract_large_table(
- llm=llm, table_settings=table_settings
+ llm=llm, table_settings=table_settings, enforce_type=enforce_type
)
structured_output[output[PSKeys.NAME]] = answer
# We do not support summary and eval for table.
diff --git a/prompt-service/src/unstract/prompt_service/main.py b/prompt-service/src/unstract/prompt_service/main.py
index 473c7c4d3..369bda80f 100644
--- a/prompt-service/src/unstract/prompt_service/main.py
+++ b/prompt-service/src/unstract/prompt_service/main.py
@@ -268,13 +268,14 @@ def prompt_processor() -> Any:
)
return APIError(message=msg)
- if output[PSKeys.TYPE] == PSKeys.TABLE:
+ if output[PSKeys.TYPE] == PSKeys.TABLE or output[PSKeys.TYPE] == PSKeys.RECORD:
try:
structured_output = extract_table(
output=output,
plugins=plugins,
structured_output=structured_output,
llm=llm,
+ enforce_type=output[PSKeys.TYPE],
)
metadata = query_usage_metadata(token=platform_key, metadata=metadata)
response = {
@@ -299,6 +300,7 @@ def prompt_processor() -> Any:
RunLevel.TABLE_EXTRACTION,
"Error while extracting table for the prompt",
)
+ raise api_error
try:
context = ""