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] Cell type merge - Table and record. (#1239)
harini-venkataramanbe56583684ef
diff --git a/backend/prompt_studio/prompt_studio_core_v2/constants.py b/backend/prompt_studio/prompt_studio_core_v2/constants.pyindex ad3451175..1c13ca1e7 100644--- a/backend/prompt_studio/prompt_studio_core_v2/constants.py+++ b/backend/prompt_studio/prompt_studio_core_v2/constants.py@@ -99,6 +99,7 @@ class ToolStudioPromptKeys:ENABLE_HIGHLIGHT = "enable_highlight"REQUIRED = "required"EXECUTION_SOURCE = "execution_source"+ LINE_ITEM = "line-item"class FileViewTypes: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.pyindex c90325100..056f3a785 100644--- a/backend/prompt_studio/prompt_studio_core_v2/prompt_studio_helper.py+++ b/backend/prompt_studio/prompt_studio_core_v2/prompt_studio_helper.py@@ -981,7 +981,7 @@ class PromptStudioHelper:tool_id: str,output: dict[str, Any],) -> dict[str, Any]:- if prompt.enforce_type == TSPKeys.TABLE or prompt.enforce_type == TSPKeys.RECORD:+ if prompt.enforce_type == TSPKeys.LINE_ITEM:extract_doc_path: str = (PromptStudioHelper._get_extract_or_summary_document_path(org_id, user_id, tool_id, doc_name, TSPKeys.EXTRACTdiff --git a/backend/prompt_studio/prompt_studio_core_v2/static/select_choices.json b/backend/prompt_studio/prompt_studio_core_v2/static/select_choices.jsonindex 2e260a452..39265bece 100644--- a/backend/prompt_studio/prompt_studio_core_v2/static/select_choices.json+++ b/backend/prompt_studio/prompt_studio_core_v2/static/select_choices.json@@ -8,14 +8,12 @@"AZURE":"Azure OpenAI"},"output_type":{- "string":"Text",+ "string":"text","number":"number","email":"email","date":"date","boolean":"boolean","json":"json",- "table":"table",- "record":"record","line_item":"line-item"},"output_processing":{diff --git a/backend/prompt_studio/prompt_studio_v2/migrations/0007_merge_table_record_cell_types.py b/backend/prompt_studio/prompt_studio_v2/migrations/0007_merge_table_record_cell_types.pynew file mode 100644index 000000000..5f5ccd449--- /dev/null+++ b/backend/prompt_studio/prompt_studio_v2/migrations/0007_merge_table_record_cell_types.py@@ -0,0 +1,46 @@+# Generated by Django 4.2.1 on 2025-04-24 11:31++from typing import Any++from django.db import migrations, models+++def forward_enforce_type_updates(apps: Any, schema_editor: Any) -> None:+ prompt = apps.get_model("prompt_studio_v2", "ToolStudioPrompt")++ # line-item -> json+ prompt.objects.filter(enforce_type="line-item").update(+ enforce_type="json", is_line_item=True+ )++ # table or record -> line-item+ prompt.objects.filter(enforce_type__in=["table", "record"]).update(+ enforce_type="line-item"+ )+++def revert_enforce_type_updates(apps, schema_editor: Any) -> None:+ prompt = apps.get_model("prompt_studio_v2", "ToolStudioPrompt")++ # Revert json -> line-item for records where is_line_item is True+ prompt.objects.filter(enforce_type="json", is_line_item=True).update(+ enforce_type="line-item"+ )++ # Revert line-item -> table or record for records+ prompt.objects.filter(enforce_type="line-item").update(enforce_type="table")+++class Migration(migrations.Migration):+ dependencies = [+ ("prompt_studio_v2", "0006_alter_toolstudioprompt_enforce_type"),+ ]++ operations = [+ migrations.AddField(+ model_name="toolstudioprompt",+ name="has_line_item_history",+ field=models.BooleanField(default=False),+ ),+ migrations.RunPython(forward_enforce_type_updates, revert_enforce_type_updates),+ ]diff --git a/backend/prompt_studio/prompt_studio_v2/migrations/0008_alter_toolstudioprompt_enforce_type.py b/backend/prompt_studio/prompt_studio_v2/migrations/0008_alter_toolstudioprompt_enforce_type.pynew file mode 100644index 000000000..4221de0f2--- /dev/null+++ b/backend/prompt_studio/prompt_studio_v2/migrations/0008_alter_toolstudioprompt_enforce_type.py@@ -0,0 +1,33 @@+# Generated by Django 4.2.1 on 2025-04-24 11:40++from django.db import migrations, models+++class Migration(migrations.Migration):+ dependencies = [+ ("prompt_studio_v2", "0007_merge_table_record_cell_types"),+ ]++ 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"),+ (+ "line-item",+ "Response sent as line-item which is large a JSON output. If extraction stopped due to token limitation, we try to continue extraction from where it stopped",+ ),+ ],+ db_comment="Field to store the type in which the response to be returned.",+ default="Text",+ ),+ ),+ ]diff --git a/backend/prompt_studio/prompt_studio_v2/migrations/0009_alter_toolstudioprompt_enforce_type.py b/backend/prompt_studio/prompt_studio_v2/migrations/0009_alter_toolstudioprompt_enforce_type.pynew file mode 100644index 000000000..37fdcd6c8--- /dev/null+++ b/backend/prompt_studio/prompt_studio_v2/migrations/0009_alter_toolstudioprompt_enforce_type.py@@ -0,0 +1,33 @@+# Generated by Django 4.2.1 on 2025-04-28 05:49++from django.db import migrations, models+++class Migration(migrations.Migration):+ dependencies = [+ ("prompt_studio_v2", "0008_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"),+ (+ "line-item",+ "Response sent as line-item which is large a JSON output. If extraction stopped due to token limitation, we try to continue extraction from where it stopped",+ ),+ ],+ db_comment="Field to store the type in which the response to be returned.",+ default="text",+ ),+ ),+ ]diff --git a/backend/prompt_studio/prompt_studio_v2/migrations/0010_update_enforce_type_text.py b/backend/prompt_studio/prompt_studio_v2/migrations/0010_update_enforce_type_text.pynew file mode 100644index 000000000..3cb09d2ab--- /dev/null+++ b/backend/prompt_studio/prompt_studio_v2/migrations/0010_update_enforce_type_text.py@@ -0,0 +1,27 @@+# Generated by Django 4.2.1 on 2025-04-28 06:00++from typing import Any++from django.db import migrations+++def update_text_enforce_type(apps, schema_editor: Any) -> None:+ prompt = apps.get_model("prompt_studio_v2", "ToolStudioPrompt")+ prompt.objects.filter(enforce_type="Text").update(enforce_type="text")+++def revert_update_text_enforce_type(apps, schema_editor: Any) -> None:+ prompt = apps.get_model("prompt_studio_v2", "ToolStudioPrompt")+ prompt.objects.filter(enforce_type="text").update(enforce_type="Text")+++class Migration(migrations.Migration):+ dependencies = [+ ("prompt_studio_v2", "0009_alter_toolstudioprompt_enforce_type"),+ ]++ operations = [+ migrations.RunPython(+ update_text_enforce_type, reverse_code=revert_update_text_enforce_type+ ),+ ]diff --git a/backend/prompt_studio/prompt_studio_v2/models.py b/backend/prompt_studio/prompt_studio_v2/models.pyindex 6ec75f97e..402e79722 100644--- a/backend/prompt_studio/prompt_studio_v2/models.py+++ b/backend/prompt_studio/prompt_studio_v2/models.py@@ -15,22 +15,12 @@ class ToolStudioPrompt(BaseModel):"""class EnforceType(models.TextChoices):- TEXT = "Text", "Response sent as Text"+ 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"- 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"- ),- )LINE_ITEM = ("line-item",(@@ -68,6 +58,8 @@ class ToolStudioPrompt(BaseModel):choices=EnforceType.choices,default=EnforceType.TEXT,)+ # New field to track if it was line-item+ has_line_item_history = models.BooleanField(default=False)prompt = models.TextField(blank=True, db_comment="Field to store the prompt", unique=False)diff --git a/frontend/src/components/custom-tools/prompt-card/PromptCardItems.jsx b/frontend/src/components/custom-tools/prompt-card/PromptCardItems.jsxindex 3f3a25ba9..bb05367a4 100644--- a/frontend/src/components/custom-tools/prompt-card/PromptCardItems.jsx+++ b/frontend/src/components/custom-tools/prompt-card/PromptCardItems.jsx@@ -19,7 +19,7 @@ import { useCustomToolStore } from "../../../store/custom-tool-store";import { Header } from "./Header";import { OutputForIndex } from "./OutputForIndex";import { PromptOutput } from "./PromptOutput";-import { TABLE_ENFORCE_TYPE, RECORD_ENFORCE_TYPE } from "./constants";+import { LINE_ITEM_ENFORCE_TYPE } from "./constants";let TableExtractionSettingsBtn;try {@@ -269,8 +269,7 @@ function PromptCardItems({)}</Space><Space>- {(enforceType === TABLE_ENFORCE_TYPE ||- enforceType === RECORD_ENFORCE_TYPE) &&+ {enforceType === LINE_ITEM_ENFORCE_TYPE &&TableExtractionSettingsBtn && (<TableExtractionSettingsBtnpromptId={promptDetails?.prompt_id}diff --git a/frontend/src/components/custom-tools/prompt-card/PromptOutput.jsx b/frontend/src/components/custom-tools/prompt-card/PromptOutput.jsxindex 29bda5fd5..71431e036 100644--- a/frontend/src/components/custom-tools/prompt-card/PromptOutput.jsx+++ b/frontend/src/components/custom-tools/prompt-card/PromptOutput.jsx@@ -29,7 +29,7 @@ import {import { TokenUsage } from "../token-usage/TokenUsage";import { useWindowDimensions } from "../../../hooks/useWindowDimensions";import { useCustomToolStore } from "../../../store/custom-tool-store";-import { TABLE_ENFORCE_TYPE, RECORD_ENFORCE_TYPE } from "./constants";+import { LINE_ITEM_ENFORCE_TYPE } from "./constants";import { CopyPromptOutputBtn } from "./CopyPromptOutputBtn";import { useAlertStore } from "../../../store/alert-store";import { PromptOutputExpandBtn } from "./PromptOutputExpandBtn";@@ -82,8 +82,7 @@ function PromptOutput({} = useCustomToolStore();const { setAlertDetails } = useAlertStore();const { generatePromptOutputKey } = usePromptOutput();- const isTableExtraction =- enforceType === TABLE_ENFORCE_TYPE || enforceType === RECORD_ENFORCE_TYPE;+ const isTableExtraction = enforceType === LINE_ITEM_ENFORCE_TYPE;const noHighlightEnforceType = !["table", "record"].includes(enforceType);const tooltipContent = (adapterConf) => (<div>diff --git a/frontend/src/components/custom-tools/prompt-card/PromptOutputsModal.jsx b/frontend/src/components/custom-tools/prompt-card/PromptOutputsModal.jsxindex 4fdce45b7..2f264283f 100644--- a/frontend/src/components/custom-tools/prompt-card/PromptOutputsModal.jsx+++ b/frontend/src/components/custom-tools/prompt-card/PromptOutputsModal.jsx@@ -2,7 +2,7 @@ import { Col, Image, Modal, Row, Typography } from "antd";import PropTypes from "prop-types";import { DisplayPromptResult } from "./DisplayPromptResult";-import { TABLE_ENFORCE_TYPE, RECORD_ENFORCE_TYPE } from "./constants";+import { LINE_ITEM_ENFORCE_TYPE } from "./constants";import SpaceWrapper from "../../widgets/space-wrapper/SpaceWrapper";import { useCustomToolStore } from "../../../store/custom-tool-store";import usePromptOutput from "../../../hooks/usePromptOutput";@@ -81,9 +81,7 @@ function PromptOutputsModal({)}</div><div className="flex-1 overflow-y-auto pad-top-10">- {(enforceType === TABLE_ENFORCE_TYPE ||- enforceType === RECORD_ENFORCE_TYPE) &&- TableOutput ? (+ {enforceType === LINE_ITEM_ENFORCE_TYPE && TableOutput ? (<TableOutputoutput={promptOutputData?.output}pagination={10}diff --git a/frontend/src/components/custom-tools/prompt-card/constants.js b/frontend/src/components/custom-tools/prompt-card/constants.jsindex 9e35aa5b0..cc6c7253d 100644--- a/frontend/src/components/custom-tools/prompt-card/constants.js+++ b/frontend/src/components/custom-tools/prompt-card/constants.js@@ -1,5 +1,4 @@-const TABLE_ENFORCE_TYPE = "table";-const RECORD_ENFORCE_TYPE = "record";+const LINE_ITEM_ENFORCE_TYPE = "line-item";const handleUpdateStatus = (isUpdate, promptId, value, setUpdateStatus) => {if (!isUpdate) {@@ -11,4 +10,4 @@ const handleUpdateStatus = (isUpdate, promptId, value, setUpdateStatus) => {});};-export { TABLE_ENFORCE_TYPE, handleUpdateStatus, RECORD_ENFORCE_TYPE };+export { handleUpdateStatus, LINE_ITEM_ENFORCE_TYPE };diff --git a/prompt-service/pyproject.toml b/prompt-service/pyproject.tomlindex b3894b1ba..cc2757859 100644--- a/prompt-service/pyproject.toml+++ b/prompt-service/pyproject.toml@@ -13,6 +13,7 @@ dependencies = ["flask~=3.0","llama-index==0.12.8","python-dotenv==1.0.0",+ "json-repair~=0.42.0",# TODO: Temporarily removing the extra dependencies of aws and gcs from unstract-sdk# to resolve lock file. Will have to be re-looked into"unstract-sdk~=0.70.0",diff --git a/prompt-service/src/unstract/prompt_service/constants.py b/prompt-service/src/unstract/prompt_service/constants.pyindex 2ad922183..b49aec238 100644--- a/prompt-service/src/unstract/prompt_service/constants.py+++ b/prompt-service/src/unstract/prompt_service/constants.py@@ -92,6 +92,7 @@ class PromptServiceConstants:"provider to confirm writes to DB. ""Please try again after some time")+ COMBINED_PROMPT = "combined_prompt"class RunLevel(Enum):diff --git a/prompt-service/src/unstract/prompt_service/controllers/answer_prompt.py b/prompt-service/src/unstract/prompt_service/controllers/answer_prompt.pyindex db950ac11..1fad52806 100644--- a/prompt-service/src/unstract/prompt_service/controllers/answer_prompt.py+++ b/prompt-service/src/unstract/prompt_service/controllers/answer_prompt.py@@ -1,7 +1,5 @@"""Published API Controller"""-import json-from json import JSONDecodeErrorfrom typing import Anyfrom flask import Blueprint, request@@ -173,13 +171,12 @@ def prompt_processor() -> Any:)return APIError(message=msg)- if output[PSKeys.TYPE] == PSKeys.TABLE or output[PSKeys.TYPE] == PSKeys.RECORD:+ if output[PSKeys.TYPE] == PSKeys.LINE_ITEM:try:- structured_output = AnswerPromptService.extract_table(+ structured_output = AnswerPromptService.extract_line_item(output=output,structured_output=structured_output,llm=llm,- enforce_type=output[PSKeys.TYPE],execution_source=execution_source,)metadata = UsageHelper.query_usage_metadata(@@ -209,36 +206,6 @@ def prompt_processor() -> Any:"Error while extracting table for the prompt",)raise api_error- elif output[PSKeys.TYPE] == PSKeys.LINE_ITEM:- try:- structured_output = AnswerPromptService.extract_line_item(- tool_settings=tool_settings,- output=output,- structured_output=structured_output,- llm=llm,- file_path=file_path,- metadata=metadata,- execution_source=execution_source,- )- continue- except APIError as e:- app.logger.error(- "Failed to extract line-item for the prompt %s: %s",- output[PSKeys.NAME],- str(e),- )- publish_log(- log_events_id,- {- "tool_id": tool_id,- "prompt_key": prompt_name,- "doc_name": doc_name,- },- LogLevel.ERROR,- RunLevel.RUN,- "Error while extracting line-item for the prompt",- )- raise etry:answer = "NA"@@ -383,46 +350,19 @@ def prompt_processor() -> Any:else:structured_output[output[PSKeys.NAME]] = Falseelif output[PSKeys.TYPE] == PSKeys.JSON:- if answer.lower() == "[]" or answer.lower() == "na":- structured_output[output[PSKeys.NAME]] = None- else:- try:- structured_output[output[PSKeys.NAME]] = json.loads(answer)- except JSONDecodeError:- prompt = f"Convert the following text into valid JSON string: \- \n{answer}\n\n The JSON string should be able to be parsed \- into a Python dictionary. \- Output just the JSON string. No explanation is required. \- If you cannot extract the JSON string, output {{}}"- try:- answer = AnswerPromptService.run_completion(- llm=llm,- prompt=prompt,- prompt_type=PSKeys.JSON,- )- structured_output[output[PSKeys.NAME]] = json.loads(answer)- except JSONDecodeError as e:- err_msg = (- f"Error parsing response (to json): {e}\n"- f"Candidate JSON: {answer}"- )- app.logger.info(err_msg, LogLevel.ERROR)- # Format log message after unifying these types- publish_log(- log_events_id,- {- "tool_id": tool_id,- "prompt_key": prompt_name,- "doc_name": doc_name,- },- LogLevel.INFO,- RunLevel.RUN,- "Unable to parse JSON response from LLM, try using our"- " cloud / enterprise feature of 'line-item', "- "'record' or 'table' type",- )- structured_output[output[PSKeys.NAME]] = {}-+ AnswerPromptService.handle_json(+ answer=answer,+ structured_output=structured_output,+ output=output,+ log_events_id=log_events_id,+ tool_id=tool_id,+ doc_name=doc_name,+ llm=llm,+ enable_highlight=tool_settings.get(PSKeys.ENABLE_HIGHLIGHT, False),+ execution_source=execution_source,+ metadata=metadata,+ file_path=file_path,+ )else:structured_output[output[PSKeys.NAME]] = answerdiff --git a/prompt-service/src/unstract/prompt_service/services/answer_prompt.py b/prompt-service/src/unstract/prompt_service/services/answer_prompt.pyindex 028099687..9770816cf 100644--- a/prompt-service/src/unstract/prompt_service/services/answer_prompt.py+++ b/prompt-service/src/unstract/prompt_service/services/answer_prompt.py@@ -2,12 +2,16 @@ from logging import Loggerfrom typing import Anyfrom flask import current_app as app+from json_repair import repair_jsonfrom unstract.core.flask.exceptions import APIError-from unstract.prompt_service.constants import ExecutionSource, FileStorageKeys+from unstract.prompt_service.constants import ExecutionSource, FileStorageKeys, RunLevelfrom unstract.prompt_service.constants import PromptServiceConstants as PSKeysfrom unstract.prompt_service.exceptions import RateLimitErrorfrom unstract.prompt_service.helpers.plugin import PluginManager+from unstract.prompt_service.utils.file_utils import FileUtils+from unstract.prompt_service.utils.log import publish_log+from unstract.sdk.constants import LogLevelfrom unstract.sdk.exceptions import RateLimitError as SdkRateLimitErrorfrom unstract.sdk.exceptions import SdkErrorfrom unstract.sdk.file_storage import FileStorage, FileStorageProvider@@ -57,6 +61,9 @@ class AnswerPromptService:enable_highlight = tool_settings.get(PSKeys.ENABLE_HIGHLIGHT, False)if not enable_highlight or summarize_as_source:platform_postamble = ""+ plugin = PluginManager().get_plugin("json-extraction")+ if plugin and hasattr(plugin["entrypoint_cls"], "update_settings"):+ plugin["entrypoint_cls"].update_settings(tool_settings, output)prompt = AnswerPromptService.construct_prompt(preamble=tool_settings.get(PSKeys.PREAMBLE, ""),prompt=output[prompt],@@ -65,6 +72,7 @@ class AnswerPromptService:context=context,platform_postamble=platform_postamble,)+ output[PSKeys.COMBINED_PROMPT] = promptreturn AnswerPromptService.run_completion(llm=llm,prompt=prompt,@@ -123,7 +131,11 @@ class AnswerPromptService:PSKeys.HIGHLIGHT_DATA_PLUGIN)highlight_data = None- if highlight_data_plugin and enable_highlight:+ if (+ highlight_data_plugin+ and enable_highlight+ and prompt_type.lower() != PSKeys.JSON+ ):fs_instance: FileStorage = FileStorage(FileStorageProvider.LOCAL)if execution_source == ExecutionSource.IDE.value:fs_instance = EnvHelper.get_storage(@@ -142,7 +154,7 @@ class AnswerPromptService:completion = llm.complete(prompt=prompt,process_text=highlight_data,- extract_json=prompt_type.lower() != PSKeys.TEXT,+ extract_json=prompt_type.lower() != PSKeys.JSON,)answer: str = completion[PSKeys.RESPONSE].texthighlight_data = completion.get(PSKeys.HIGHLIGHT_DATA, [])@@ -169,18 +181,17 @@ class AnswerPromptService:raise APIError(str(e)) from e@staticmethod- def extract_table(+ def extract_line_item(output: dict[str, Any],structured_output: dict[str, Any],llm: LLM,- enforce_type: str,execution_source: str,) -> dict[str, Any]:table_settings = output[PSKeys.TABLE_SETTINGS]table_extractor: dict[str, Any] = PluginManager().get_plugin("table-extractor")if not table_extractor:raise APIError(- "Unable to extract table details. "+ "Unable to extract line-item details. ""Please contact admin to resolve this issue.")fs_instance: FileStorage = FileStorage(FileStorageProvider.LOCAL)@@ -198,7 +209,6 @@ class AnswerPromptService:answer = table_extractor["entrypoint_cls"].extract_large_table(llm=llm,table_settings=table_settings,- enforce_type=enforce_type,fs_instance=fs_instance,)structured_output[output[PSKeys.NAME]] = answer@@ -210,63 +220,91 @@ class AnswerPromptService:raise APIError(message=msg)@staticmethod- def extract_line_item(- tool_settings: dict[str, Any],- output: dict[str, Any],+ def handle_json(+ answer: str,structured_output: dict[str, Any],+ output: dict[str, Any],+ log_events_id: str,+ tool_id: str,+ doc_name: str,llm: LLM,- file_path: str,- metadata: dict[str, str] | None,- execution_source: str,- ) -> dict[str, Any]:- line_item_extraction_plugin: dict[str, Any] = PluginManager().get_plugin(- "line-item-extraction"- )- if not line_item_extraction_plugin:- raise APIError(PSKeys.PAID_FEATURE_MSG)-- extract_file_path = file_path-- # Read file content into context- fs_instance: FileStorage = FileStorage(FileStorageProvider.LOCAL)- if execution_source == ExecutionSource.IDE.value:- fs_instance = EnvHelper.get_storage(- storage_type=StorageType.PERMANENT,- env_name=FileStorageKeys.PERMANENT_REMOTE_STORAGE,- )- if execution_source == ExecutionSource.TOOL.value:- fs_instance = EnvHelper.get_storage(- storage_type=StorageType.SHARED_TEMPORARY,- env_name=FileStorageKeys.TEMPORARY_REMOTE_STORAGE,- )-- if not fs_instance.exists(extract_file_path):- raise FileNotFoundError(- f"The file at path '{extract_file_path}' does not exist."+ enable_highlight: bool = False,+ execution_source: str = ExecutionSource.IDE.value,+ metadata: dict[str, Any] | None = None,+ file_path: str = "",+ ) -> None:+ """Handle JSON responses from the LLM."""+ prompt_key = output[PSKeys.NAME]+ if answer.lower() == "na":+ structured_output[prompt_key] = None+ else:+ json_extraction_plugin = PluginManager().get_plugin("json-extraction")+ if json_extraction_plugin:+ answer = json_extraction_plugin["entrypoint_cls"](+ llm=llm,+ output=output,+ prompt=output[PSKeys.COMBINED_PROMPT],+ structured_output=structured_output,+ answer=answer,+ ).run()+ if enable_highlight:+ AnswerPromptService.handle_highlight(+ execution_source=execution_source,+ output=output,+ answer=answer,+ metadata=metadata,+ file_path=file_path,+ )+ parsed_data = repair_json(+ json_str=answer,+ return_objects=True,)- context = fs_instance.read(path=extract_file_path, encoding="utf-8", mode="r")-- prompt = AnswerPromptService.construct_prompt(- preamble=tool_settings.get(PSKeys.PREAMBLE, ""),- prompt=output["promptx"],- postamble=tool_settings.get(PSKeys.POSTAMBLE, ""),- grammar_list=tool_settings.get(PSKeys.GRAMMAR, []),- context=context,- platform_postamble="",- )+ if isinstance(parsed_data, str):+ err_msg = "Error parsing response (to json)\n" f"Candidate JSON: {answer}"+ app.logger.info(err_msg, LogLevel.ERROR)+ publish_log(+ log_events_id,+ {+ "tool_id": tool_id,+ "prompt_key": prompt_key,+ "doc_name": doc_name,+ },+ LogLevel.INFO,+ RunLevel.RUN,+ "Unable to parse JSON response from LLM, try using our"+ " cloud / enterprise feature 'record' or 'table' type",+ )+ structured_output[prompt_key] = {}+ else:+ structured_output[prompt_key] = parsed_data- try:- line_item_extraction = line_item_extraction_plugin["entrypoint_cls"](- llm=llm,- tool_settings=tool_settings,- output=output,- prompt=prompt,- structured_output=structured_output,- )- answer = line_item_extraction.run()- structured_output[output[PSKeys.NAME]] = answer- metadata[PSKeys.CONTEXT][output[PSKeys.NAME]] = [context]- return structured_output- except line_item_extraction_plugin["exception_cls"] as e:- msg = f"Couldn't extract table. {e}"- raise APIError(message=msg)+ @staticmethod+ def handle_highlight(+ execution_source: str,+ output: dict[str, Any],+ answer: str,+ metadata: dict[str, Any] | None = None,+ file_path: str = "",+ ) -> None:+ """Handle highlight data from the LLM."""+ highlight_data_plugin = PluginManager().get_plugin("highlight-data")+ if highlight_data_plugin:+ highlight_response = highlight_data_plugin["entrypoint_cls"](+ file_path=file_path,+ fs_instance=FileUtils.get_fs_instance(execution_source=execution_source),+ ).run(None, True, answer)+ highlight_data = highlight_response.get(PSKeys.HIGHLIGHT_DATA, [])+ confidence_data = highlight_response.get(PSKeys.CONFIDENCE_DATA)+ line_numbers = highlight_response.get(PSKeys.LINE_NUMBERS, [])+ whisper_hash = highlight_response.get(PSKeys.WHISPER_HASH, "")+ prompt_key = output[PSKeys.NAME]+ if metadata is not None and prompt_key:+ metadata.setdefault(PSKeys.HIGHLIGHT_DATA, {})[prompt_key] = (+ highlight_data+ )+ metadata.setdefault(PSKeys.LINE_NUMBERS, {})[prompt_key] = line_numbers+ metadata[PSKeys.WHISPER_HASH] = whisper_hash+ if confidence_data:+ metadata.setdefault(PSKeys.CONFIDENCE_DATA, {})[prompt_key] = (+ confidence_data+ )diff --git a/prompt-service/uv.lock b/prompt-service/uv.lockindex 3c4751da5..5b134d083 100644--- a/prompt-service/uv.lock+++ b/prompt-service/uv.lock@@ -1169,6 +1169,15 @@ wheels = [{ url = "https://files.pythonhosted.org/packages/91/29/df4b9b42f2be0b623cbd5e2140cafcaa2bef0759a00b7b70104dcfe2fb51/joblib-1.4.2-py3-none-any.whl", hash = "sha256:06d478d5674cbc267e7496a410ee875abd68e4340feff4490bcb7afb88060ae6", size = 301817 },]+[[package]]+name = "json-repair"+version = "0.42.0"+source = { registry = "https://pypi.org/simple" }+sdist = { url = "https://files.pythonhosted.org/packages/f4/1e/aea70e484e271e3d841773d5d185bedf488cf8f881088d47faae31f19116/json_repair-0.42.0.tar.gz", hash = "sha256:1a901f706c5b6b4325f0f79b53b0d998c5b327070e98b530da71cc5a3eda8616", size = 31349 }+wheels = [+ { url = "https://files.pythonhosted.org/packages/6a/c7/dd23f764de95771300a8a7ae17293c47ba0e48f826154229d18ecfe147cd/json_repair-0.42.0-py3-none-any.whl", hash = "sha256:7b6805162053dfe65722e961bc51b5eecec0582ec8a8e0fd218d33e8de757daf", size = 21612 },+]+[[package]]name = "jsonpath-python"version = "1.0.6"@@ -3002,6 +3011,7 @@ source = { editable = "." }dependencies = [{ name = "flask" },{ name = "gcsfs" },+ { name = "json-repair" },{ name = "llama-index" },{ name = "nltk" },{ name = "peewee" },@@ -3034,6 +3044,7 @@ test = [requires-dist = [{ name = "flask", specifier = "~=3.0" },{ name = "gcsfs", specifier = "==2024.10.0" },+ { name = "json-repair", specifier = "~=0.42.0" },{ name = "llama-index", specifier = "==0.12.8" },{ name = "nltk", specifier = "~=3.8" },{ name = "peewee", specifier = "~=3.16" },