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] Support for table extraction in Prompt studio (#564)
harini-venkataramanae45e2a01134
diff --git a/backend/backend/urls.py b/backend/backend/urls.pyindex de1efb150..0667f2ff9 100644--- a/backend/backend/urls.py+++ b/backend/backend/urls.py@@ -118,10 +118,17 @@ except ImportError:# Clone urlstry:- import pluggable_apps.clone.urls # noqa # pylint: disable=unused-import-urlpatterns += [path("", include("pluggable_apps.clone.urls")),]except ImportError:pass++try:+ import pluggable_apps.apps.table_settings # noqa # pylint: disable=unused-import++ urlpatterns += [+ path("", include("pluggable_apps.apps.table_settings.urls")),+ ]+except ImportError:+ passdiff --git a/backend/prompt_studio/modifier_loader.py b/backend/prompt_studio/modifier_loader.pynew file mode 100644index 000000000..ca1cd1eb9--- /dev/null+++ b/backend/prompt_studio/modifier_loader.py@@ -0,0 +1,72 @@+import logging+import os+from importlib import import_module+from typing import Any++from django.apps import apps++logger = logging.getLogger(__name__)+++class ModifierConfig:+ """Loader config for extraction plugins."""++ PLUGINS_APP = "plugins"+ PLUGIN_DIR = "modifier"+ MODULE = "module"+ METADATA = "metadata"+ METADATA_NAME = "name"+ METADATA_SERVICE_CLASS = "service_class"+ METADATA_IS_ACTIVE = "is_active"+++def load_plugins() -> list[Any]:+ """Iterate through the extraction plugins and register them."""+ plugins_app = apps.get_app_config(ModifierConfig.PLUGINS_APP)+ package_path = plugins_app.module.__package__+ modifier_dir = os.path.join(plugins_app.path, ModifierConfig.PLUGIN_DIR)+ modifier_package_path = f"{package_path}.{ModifierConfig.PLUGIN_DIR}"+ modifier_plugins: list[Any] = []++ if not os.path.exists(modifier_dir):+ return modifier_plugins++ for item in os.listdir(modifier_dir):+ # Loads a plugin if it is in a directory.+ if os.path.isdir(os.path.join(modifier_dir, item)):+ modifier_module_name = item+ # Loads a plugin if it is a shared library.+ # Module name is extracted from shared library name.+ elif item.endswith(".so"):+ modifier_module_name = item.split(".")[0]+ else:+ continue+ try:+ full_module_path = f"{modifier_package_path}.{modifier_module_name}"+ module = import_module(full_module_path)+ metadata = getattr(module, ModifierConfig.METADATA, {})++ if metadata.get(ModifierConfig.METADATA_IS_ACTIVE, False):+ modifier_plugins.append(+ {+ ModifierConfig.MODULE: module,+ ModifierConfig.METADATA: module.metadata,+ }+ )+ logger.info(+ "Loaded modifier plugin: %s, is_active: %s",+ module.metadata[ModifierConfig.METADATA_NAME],+ module.metadata[ModifierConfig.METADATA_IS_ACTIVE],+ )+ else:+ logger.info(+ "modifier plugin %s is not active.",+ modifier_module_name,+ )+ except ModuleNotFoundError:+ logger.warning("No prompt modifier plugins loaded")++ if len(modifier_plugins) == 0:+ logger.info("No modifier plugins found.")++ return modifier_pluginsdiff --git a/backend/prompt_studio/prompt_studio/migrations/0007_alter_toolstudioprompt_enforce_type.py b/backend/prompt_studio/prompt_studio/migrations/0007_alter_toolstudioprompt_enforce_type.pynew file mode 100644index 000000000..3a83c4919--- /dev/null+++ b/backend/prompt_studio/prompt_studio/migrations/0007_alter_toolstudioprompt_enforce_type.py@@ -0,0 +1,31 @@+# Generated by Django 4.2.1 on 2024-08-07 14:20++from django.db import migrations, models+++class Migration(migrations.Migration):++ dependencies = [+ ("prompt_studio", "0006_alter_toolstudioprompt_prompt_key_and_more"),+ ]++ 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"),+ ],+ 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.pyindex 642051979..df2e9dc3f 100644--- a/backend/prompt_studio/prompt_studio/models.py+++ b/backend/prompt_studio/prompt_studio/models.py@@ -20,6 +20,7 @@ class ToolStudioPrompt(BaseModel):DATE = "date", "Response sent as date"BOOLEAN = "boolean", "Response sent as boolean"JSON = "json", "Response sent as json"+ TABLE = "table", "Response sent as table"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.pyindex 8fea5f4fd..1de398c5f 100644--- a/backend/prompt_studio/prompt_studio_core/constants.py+++ b/backend/prompt_studio/prompt_studio_core/constants.py@@ -88,6 +88,9 @@ class ToolStudioPromptKeys:CONTEXT = "context"METADATA = "metadata"INCLUDE_METADATA = "include_metadata"+ TXT_EXTENTION = ".txt"+ TABLE = "table"+ EXTRACT = "extract"PLATFORM_POSTAMBLE = "platform_postamble"SUMMARIZE_AS_SOURCE = "summarize_as_source"diff --git a/backend/prompt_studio/prompt_studio_core/exceptions.py b/backend/prompt_studio/prompt_studio_core/exceptions.pyindex 241418060..a5604e51d 100644--- a/backend/prompt_studio/prompt_studio_core/exceptions.py+++ b/backend/prompt_studio/prompt_studio_core/exceptions.py@@ -67,3 +67,13 @@ class MaxProfilesReachedError(APIException):f"Maximum number of profiles (max {ProfileManagerKeys.MAX_PROFILE_COUNT})"" per prompt studio project has been reached.")+++class OperationNotSupported(APIException):+ status_code = 403+ default_detail = (+ "This feature is not supported "+ "in the open-source version. "+ "Please check our cloud or enterprise on-premise offering "+ "for access to this functionality."+ )diff --git a/backend/prompt_studio/prompt_studio_core/prompt_studio_helper.py b/backend/prompt_studio/prompt_studio_core/prompt_studio_helper.pyindex c215409e2..4ed32ca04 100644--- a/backend/prompt_studio/prompt_studio_core/prompt_studio_helper.py+++ b/backend/prompt_studio/prompt_studio_core/prompt_studio_helper.py@@ -12,6 +12,8 @@ from adapter_processor.models import AdapterInstancefrom django.conf import settingsfrom django.db.models.manager import BaseManagerfrom file_management.file_management_helper import FileManagerHelper+from prompt_studio.modifier_loader import ModifierConfig+from prompt_studio.modifier_loader import load_plugins as load_modifier_pluginsfrom prompt_studio.prompt_profile_manager.models import ProfileManagerfrom prompt_studio.prompt_profile_manager.profile_manager_helper import (ProfileManagerHelper,@@ -28,6 +30,7 @@ from prompt_studio.prompt_studio_core.exceptions import (EmptyPromptError,IndexingAPIError,NoPromptsFound,+ OperationNotSupported,PermissionError,)from prompt_studio.prompt_studio_core.models import CustomTool@@ -53,6 +56,8 @@ ERROR_MSG = "User %s doesn't have access to adapter %s"logger = logging.getLogger(__name__)+modifier_loader = load_modifier_plugins()+class PromptStudioHelper:"""Helper class for Custom tool operations."""@@ -434,6 +439,10 @@ class PromptStudioHelper:text_processor: Optional[type[Any]] = None,):prompt_instance = PromptStudioHelper._fetch_prompt_from_id(id)++ if prompt_instance.enforce_type == TSPKeys.TABLE and not modifier_loader:+ raise OperationNotSupported()+prompt_name = prompt_instance.prompt_keyPromptStudioHelper._publish_log({@@ -523,7 +532,9 @@ class PromptStudioHelper:prompts = [promptfor prompt in prompts- if prompt.prompt_type != TSPKeys.NOTES and prompt.active+ if prompt.prompt_type != TSPKeys.NOTES+ and prompt.active+ and prompt.enforce_type != TSPKeys.TABLE]if not prompts:logger.error(f"[{tool_id or 'NA'}] No prompts found for id: {id}")@@ -583,6 +594,19 @@ class PromptStudioHelper:)return str(Path(doc_path) / doc_name)+ @staticmethod+ def _get_extract_or_summary_document_path(+ org_id, user_id, tool_id, doc_name, doc_type+ ) -> str:+ doc_path = FileManagerHelper.handle_sub_directory_for_tenants(+ org_id=org_id,+ user_id=user_id,+ tool_id=tool_id,+ is_create=False,+ )+ extracted_doc_name = Path(doc_name).stem + TSPKeys.TXT_EXTENTION+ return str(Path(doc_path) / doc_type / extracted_doc_name)+@staticmethoddef _handle_response(response,@@ -698,7 +722,7 @@ class PromptStudioHelper:"status": IndexingStatus.PENDING_STATUS.value,"message": IndexingStatus.DOCUMENT_BEING_INDEXED.value,}-+ tool_id = str(tool.tool_id)output: dict[str, Any] = {}outputs: list[dict[str, Any]] = []grammer_dict = {}@@ -738,6 +762,10 @@ class PromptStudioHelper:attr_val = getattr(prompt, attr)output[TSPKeys.EVAL_SETTINGS][attr] = attr_val+ output = PromptStudioHelper.fetch_table_settings_if_enabled(+ doc_name, prompt, org_id, user_id, tool_id, output+ )+outputs.append(output)tool_settings = {}@@ -754,8 +782,6 @@ class PromptStudioHelper:settings, TSPKeys.PLATFORM_POSTAMBLE.upper(), "")- tool_id = str(tool.tool_id)-file_hash = ToolUtils.get_hash_from_file(file_path=doc_path)payload = {@@ -789,6 +815,36 @@ class PromptStudioHelper:output_response = json.loads(answer["structure_output"])return output_response+ @staticmethod+ def fetch_table_settings_if_enabled(+ doc_name: str,+ prompt: ToolStudioPrompt,+ org_id: str,+ user_id: str,+ tool_id: str,+ output: dict[str, Any],+ ) -> dict[str, Any]:++ if prompt.enforce_type == TSPKeys.TABLE:+ 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:+ cls = modifier_plugin[ModifierConfig.METADATA][+ ModifierConfig.METADATA_SERVICE_CLASS+ ]+ output = cls.update(+ output=output,+ tool_id=tool_id,+ prompt_id=str(prompt.prompt_id),+ prompt=prompt.prompt,+ input_file=extract_doc_path,+ )++ return output+@staticmethoddef dynamic_indexer(profile_manager: ProfileManager,diff --git a/backend/prompt_studio/prompt_studio_core/static/select_choices.json b/backend/prompt_studio/prompt_studio_core/static/select_choices.jsonindex 1e9e22ef4..4fa10995f 100644--- a/backend/prompt_studio/prompt_studio_core/static/select_choices.json+++ b/backend/prompt_studio/prompt_studio_core/static/select_choices.json@@ -13,7 +13,8 @@"email":"email","date":"date","boolean":"boolean",- "json":"json"+ "json":"json",+ "table":"table"},"output_processing":{"DEFAULT":"Default"diff --git a/backend/prompt_studio/prompt_studio_registry/constants.py b/backend/prompt_studio/prompt_studio_registry/constants.pyindex 6cd809ae9..0c2e3d250 100644--- a/backend/prompt_studio/prompt_studio_registry/constants.py+++ b/backend/prompt_studio/prompt_studio_registry/constants.py@@ -9,6 +9,7 @@ class PromptStudioRegistryKeys:PROMPT_REGISTRY_ID = "prompt_registry_id"FILE_NAME = "file_name"UNDEFINED = "undefined"+ TABLE = "table"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.pyindex adb062984..837dbc2bc 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@@ -5,6 +5,8 @@ from account.models import Userfrom adapter_processor.models import AdapterInstancefrom django.conf import settingsfrom django.db import IntegrityError+from prompt_studio.modifier_loader import ModifierConfig+from prompt_studio.modifier_loader import load_plugins as load_modifier_pluginsfrom prompt_studio.prompt_profile_manager.models import ProfileManagerfrom prompt_studio.prompt_studio.models import ToolStudioPromptfrom prompt_studio.prompt_studio_core.models import CustomTool@@ -12,7 +14,7 @@ from prompt_studio.prompt_studio_core.prompt_studio_helper import PromptStudioHefrom prompt_studio.prompt_studio_output_manager.models import PromptStudioOutputManagerfrom unstract.tool_registry.dto import Properties, Spec, Tool-from .constants import JsonSchemaKey+from .constants import JsonSchemaKey, PromptStudioRegistryKeysfrom .exceptions import (EmptyToolExportError,InternalError,@@ -23,6 +25,7 @@ from .models import PromptStudioRegistryfrom .serializers import PromptStudioRegistrySerializerlogger = logging.getLogger(__name__)+modifier_loader = load_modifier_plugins()class PromptStudioRegistryHelper:@@ -320,6 +323,19 @@ class PromptStudioRegistryHelper:output[JsonSchemaKey.SECTION] = prompt.profile_manager.sectionoutput[JsonSchemaKey.REINDEX] = prompt.profile_manager.reindexoutput[JsonSchemaKey.EMBEDDING_SUFFIX] = embedding_suffix++ if prompt.enforce_type == PromptStudioRegistryKeys.TABLE:+ for modifier_plugin in modifier_loader:+ cls = modifier_plugin[ModifierConfig.METADATA][+ ModifierConfig.METADATA_SERVICE_CLASS+ ]+ output = cls.update(+ output=output,+ tool_id=tool.tool_id,+ prompt_id=prompt.prompt_id,+ prompt=prompt.prompt,+ )+outputs.append(output)output = {}vector_db = ""diff --git a/frontend/src/components/custom-tools/prompt-card/PromptCardItems.jsx b/frontend/src/components/custom-tools/prompt-card/PromptCardItems.jsxindex 4f64f8484..ff1ded335 100644--- a/frontend/src/components/custom-tools/prompt-card/PromptCardItems.jsx+++ b/frontend/src/components/custom-tools/prompt-card/PromptCardItems.jsx@@ -1,49 +1,36 @@import PropTypes from "prop-types";-import {- ArrowsAltOutlined,- CheckCircleOutlined,- DatabaseOutlined,- ExclamationCircleFilled,- InfoCircleFilled,- InfoCircleOutlined,- PlayCircleFilled,- PlayCircleOutlined,- SearchOutlined,-} from "@ant-design/icons";+import { SearchOutlined } from "@ant-design/icons";import {Button,Card,- Col,Collapse,Divider,- Image,- Radio,Row,Select,Space,- Spin,- Tooltip,Typography,} from "antd";import { useEffect, useRef, useState } from "react";-import { motion, AnimatePresence } from "framer-motion";-import CheckableTag from "antd/es/tag/CheckableTag";-import {- displayPromptResult,- getFormattedTotalCost,-} from "../../../helpers/GetStaticData";import { SpinnerLoader } from "../../widgets/spinner-loader/SpinnerLoader";import { EditableText } from "../editable-text/EditableText";-import { TokenUsage } from "../token-usage/TokenUsage";import { useCustomToolStore } from "../../../store/custom-tool-store";import { Header } from "./Header";import { OutputForIndex } from "./OutputForIndex";-import { useWindowDimensions } from "../../../hooks/useWindowDimensions";+import { PromptOutput } from "./PromptOutput";+import { TABLE_ENFORCE_TYPE } from "./constants";const EvalBtn = null;const EvalMetrics = null;+let TableExtractionSettingsBtn;+try {+ TableExtractionSettingsBtn =+ require("../../../plugins/prompt-card/TableExtractionSettingsBtn").TableExtractionSettingsBtn;+} catch {+ // The component will remain null of it is not available+}+function PromptCardItems({promptDetails,enforceTypeList,@@ -95,10 +82,16 @@ function PromptCardItems({);const [expandedProfiles, setExpandedProfiles] = useState([]); // New state for expanded profilesconst [isIndexOpen, setIsIndexOpen] = useState(false);- const { width: windowWidth } = useWindowDimensions();- const componentWidth = windowWidth * 0.4;const isNotSingleLlmProfile = llmProfiles.length > 1;const divRef = useRef(null);+ const [enforceType, setEnforceType] = useState("");++ useEffect(() => {+ if (enforceType !== promptDetails?.enforce_type) {+ setEnforceType(promptDetails?.enforce_type);+ }+ }, [promptDetails]);+const getModelOrAdapterId = (profile, adapters) => {const result = { conf: {} };const keys = [@@ -150,90 +143,6 @@ function PromptCardItems({);};- const tooltipContent = (adapterConf) => (- <div>- {Object.entries(adapterConf)?.map(([key, value]) => (- <div key={key}>- <strong>{key}:</strong> {value}- </div>- ))}- </div>- );-- const handleExpandClick = (profile) => {- const profileId = profile?.profile_id;- setExpandedProfiles((prevState) =>- prevState.includes(profileId)- ? prevState.filter((id) => id !== profileId)- : [...prevState, profileId]- );- };-- const handleTagChange = (checked, profileId) => {- setEnabledProfiles((prevState) =>- checked- ? [...prevState, profileId]- : prevState.filter((id) => id !== profileId)- );- };-- const getColSpan = () => (componentWidth < 1200 ? 24 : 6);-- const renderSinglePassResult = () => {- const [firstResult] = result || [];- if (- (promptDetails.active || isSimplePromptStudio) &&- (firstResult?.output ||- firstResult?.output === 0 ||- spsLoading[selectedDoc?.document_id])- ) {- return (- <>- <Divider className="prompt-card-divider" />- <div- className={`prompt-card-result prompt-card-div ${- expandedProfiles.includes(firstResult?.profileManager) &&- "prompt-profile-run-expanded"- }`}- >- {isSinglePassExtractLoading ||- spsLoading[selectedDoc?.document_id] ? (- <Spin indicator={<SpinnerLoader size="small" />} />- ) : (- <Typography.Paragraph className="prompt-card-res font-size-12">- <div- className={- expandedProfiles.includes(firstResult.profileManager)- ? "expanded-output"- : "collapsed-output"- }- >- {displayPromptResult(firstResult.output, true)}- </div>- </Typography.Paragraph>- )}- <div className="prompt-profile-run">- <Tooltip title="Expand">- <Button- size="small"- type="text"- className="prompt-card-action-button"- onClick={() =>- handleExpandClick({- profile_id: firstResult.profileManager,- })- }- >- <ArrowsAltOutlined className="prompt-card-actions-head" />- </Button>- </Tooltip>- </div>- </div>- </>- );- }- return <></>;- };const getCoverageData = () => {const profileId = singlePassExtractMode? defaultLlmProfile@@ -343,6 +252,12 @@ function PromptCardItems({</Button></Space><Space>+ {enforceType === TABLE_ENFORCE_TYPE &&+ TableExtractionSettingsBtn && (+ <TableExtractionSettingsBtn+ promptId={promptDetails?.prompt_id}+ />+ )}<SelectclassName="prompt-card-select-type"size="small"@@ -368,234 +283,25 @@ function PromptCardItems({)}</><Row>- <AnimatePresence>- {!singlePassExtractMode &&- !isSimplePromptStudio &&- llmProfileDetails.map((profile, index) => {- const profileId = profile?.profile_id;- const isChecked = enabledProfiles.includes(profileId);- const tokenUsageId =- promptDetails?.prompt_id +- "__" +- selectedDoc?.document_id +- "__" +- profileId;- return (- <motion.div- key={profileId}- initial={{ x: 0 }}- animate={{- x:- profileId === selectedLlmProfileId && index !== 0- ? -10- : 0,- }}- transition={{ duration: 0.5, ease: "linear" }}- className="prompt-card-llm"- >- <Col- key={profileId}- className="prompt-card-llm-container"- xs={{ span: getColSpan() }}- >- <Divider className="prompt-card-divider" />- <Space- direction="vertical"- className="prompt-card-llm-layout"- >- <div className="llm-info">- <Image- src={profile?.icon}- width={15}- height={15}- preview={false}- className="prompt-card-llm-icon"- />- <Typography.Title- className="prompt-card-llm-title"- level={5}- >- {profile?.conf?.LLM}- </Typography.Title>- </div>- <div className="prompt-cost">- <Typography.Text className="prompt-cost-item">- Tokens:{" "}- {!singlePassExtractMode && (- <TokenUsage tokenUsageId={tokenUsageId} />- )}- </Typography.Text>- <Typography.Text className="prompt-cost-item">- Time: {timers[tokenUsageId] || 0}s- </Typography.Text>- <Typography.Text className="prompt-cost-item">- Cost: ${getFormattedTotalCost(result, profile)}- </Typography.Text>- </div>- <div className="prompt-info">- <CheckableTag- checked={isChecked}- onChange={(checked) =>- handleTagChange(checked, profileId)- }- disabled={isPublicSource}- className={isChecked ? "checked" : "unchecked"}- >- {isChecked ? (- <span>- Enabled- <CheckCircleOutlined- style={{- color: "#52c41a",- marginLeft: "5px",- }}- />- </span>- ) : (- <span>- Disabled- <ExclamationCircleFilled- style={{- color: "#BABBBC",- marginLeft: "5px",- }}- />- </span>- )}- </CheckableTag>- <div className="llm-info-container">- <Tooltip title={tooltipContent(profile?.conf)}>- <InfoCircleOutlined />- </Tooltip>- <Tooltip title="Chunk used">- <DatabaseOutlined- onClick={() => {- setIsIndexOpen(true);- setOpenIndexProfile(- result.find(- (r) => r?.profileManager === profileId- )?.context- );- }}- className="prompt-card-actions-head"- />- </Tooltip>- {isNotSingleLlmProfile && (- <Radio- checked={profileId === selectedLlmProfileId}- onChange={() =>- handleSelectDefaultLLM(profileId)- }- disabled={isPublicSource}- >- Default- </Radio>- )}- </div>- </div>- </Space>- <>- <Divider className="prompt-card-divider" />- <div- className={`prompt-card-result prompt-card-div ${- expandedProfiles.includes(profileId) &&- "prompt-profile-run-expanded"- }`}- >- {isRunLoading[- `${selectedDoc?.document_id}_${profileId}`- ] ? (- <Spin- indicator={<SpinnerLoader size="small" />}- />- ) : (- <Typography.Paragraph className="prompt-card-res font-size-12">- <div- className={- expandedProfiles.includes(profileId)- ? "expanded-output"- : "collapsed-output"- }- >- {!result.find(- (r) => r?.profileManager === profileId- )?.output ? (- <Typography.Text className="prompt-not-ran">- <span>- <InfoCircleFilled- style={{ color: "#F0AD4E" }}- />- </span>{" "}- Yet to run- </Typography.Text>- ) : (- displayPromptResult(- result.find(- (r) => r?.profileManager === profileId- )?.output,- true- )- )}- </div>- </Typography.Paragraph>- )}- <div className="prompt-profile-run">- {isNotSingleLlmProfile && (- <>- <Tooltip title="Run">- <Button- size="small"- type="text"- className="prompt-card-action-button"- onClick={() =>- handleRun(profileId, false)- }- disabled={- isRunLoading[- `${selectedDoc?.document_id}_${profileId}`- ] || isPublicSource- }- >- <PlayCircleOutlined className="prompt-card-actions-head" />- </Button>- </Tooltip>- <Tooltip title="Run All">- <Button- size="small"- type="text"- className="prompt-card-action-button"- onClick={() => handleRun(profileId, true)}- disabled={- isRunLoading[- `${selectedDoc?.document_id}_${profileId}`- ] || isPublicSource- }- >- <PlayCircleFilled className="prompt-card-actions-head" />- </Button>- </Tooltip>- </>- )}- <Tooltip title="Expand">- <Button- size="small"- type="text"- className="prompt-card-action-button"- onClick={() => handleExpandClick(profile)}- >- <ArrowsAltOutlined className="prompt-card-actions-head" />- </Button>- </Tooltip>- </div>- </div>- </>- </Col>- </motion.div>- );- })}- </AnimatePresence>- {(singlePassExtractMode || isSimplePromptStudio) &&- renderSinglePassResult()}+ <PromptOutput+ promptDetails={promptDetails}+ isRunLoading={isRunLoading}+ result={result}+ handleRun={handleRun}+ selectedLlmProfileId={selectedLlmProfileId}+ handleSelectDefaultLLM={handleSelectDefaultLLM}+ timers={timers}+ spsLoading={spsLoading}+ llmProfileDetails={llmProfileDetails}+ setOpenIndexProfile={setOpenIndexProfile}+ enabledProfiles={enabledProfiles}+ setEnabledProfiles={setEnabledProfiles}+ expandedProfiles={expandedProfiles}+ setExpandedProfiles={setExpandedProfiles}+ isNotSingleLlmProfile={isNotSingleLlmProfile}+ setIsIndexOpen={setIsIndexOpen}+ enforceType={enforceType}+ /></Row></Collapse.Panel></Collapse>diff --git a/frontend/src/components/custom-tools/prompt-card/PromptOutput.jsx b/frontend/src/components/custom-tools/prompt-card/PromptOutput.jsxnew file mode 100644index 000000000..189f43631--- /dev/null+++ b/frontend/src/components/custom-tools/prompt-card/PromptOutput.jsx@@ -0,0 +1,403 @@+import {+ ArrowsAltOutlined,+ CheckCircleOutlined,+ DatabaseOutlined,+ ExclamationCircleFilled,+ InfoCircleFilled,+ InfoCircleOutlined,+ PlayCircleFilled,+ PlayCircleOutlined,+} from "@ant-design/icons";+import PropTypes from "prop-types";+import {+ Button,+ Col,+ Divider,+ Image,+ Radio,+ Space,+ Spin,+ Tooltip,+ Typography,+} from "antd";+import { motion, AnimatePresence } from "framer-motion";+import CheckableTag from "antd/es/tag/CheckableTag";++import {+ displayPromptResult,+ getFormattedTotalCost,+} from "../../../helpers/GetStaticData";+import { SpinnerLoader } from "../../widgets/spinner-loader/SpinnerLoader";+import { TokenUsage } from "../token-usage/TokenUsage";+import { useWindowDimensions } from "../../../hooks/useWindowDimensions";+import { useCustomToolStore } from "../../../store/custom-tool-store";+import { TABLE_ENFORCE_TYPE } from "./constants";++let TableOutput;+try {+ TableOutput = require("../../../plugins/prompt-card/TableOutput").TableOutput;+} catch {+ // The component will remain null of it is not available+}++function PromptOutput({+ promptDetails,+ isRunLoading,+ result,+ handleRun,+ selectedLlmProfileId,+ handleSelectDefaultLLM,+ timers,+ spsLoading,+ llmProfileDetails,+ setOpenIndexProfile,+ enabledProfiles,+ setEnabledProfiles,+ expandedProfiles,+ setExpandedProfiles,+ isNotSingleLlmProfile,+ setIsIndexOpen,+ enforceType,+}) {+ const [firstResult] = result || [];+ const { width: windowWidth } = useWindowDimensions();+ const componentWidth = windowWidth * 0.4;+ const {+ selectedDoc,+ singlePassExtractMode,+ isSinglePassExtractLoading,+ isSimplePromptStudio,+ isPublicSource,+ } = useCustomToolStore();++ const tooltipContent = (adapterConf) => (+ <div>+ {Object.entries(adapterConf)?.map(([key, value]) => (+ <div key={key}>+ <strong>{key}:</strong> {value}+ </div>+ ))}+ </div>+ );++ const handleExpandClick = (profile) => {+ const profileId = profile?.profile_id;+ setExpandedProfiles((prevState) =>+ prevState.includes(profileId)+ ? prevState.filter((id) => id !== profileId)+ : [...prevState, profileId]+ );+ };++ const handleTagChange = (checked, profileId) => {+ setEnabledProfiles((prevState) =>+ checked+ ? [...prevState, profileId]+ : prevState.filter((id) => id !== profileId)+ );+ };++ const getColSpan = () => (componentWidth < 1200 ? 24 : 6);++ if (+ (singlePassExtractMode || isSimplePromptStudio) &&+ (promptDetails?.active || isSimplePromptStudio) &&+ (firstResult?.output ||+ firstResult?.output === 0 ||+ spsLoading[selectedDoc?.document_id])+ ) {+ return (+ <>+ <Divider className="prompt-card-divider" />+ <div+ className={`prompt-card-result prompt-card-div ${+ expandedProfiles.includes(firstResult?.profileManager) &&+ "prompt-profile-run-expanded"+ }`}+ >+ {isSinglePassExtractLoading ||+ spsLoading[selectedDoc?.document_id] ? (+ <Spin indicator={<SpinnerLoader size="small" />} />+ ) : (+ <Typography.Paragraph className="prompt-card-res font-size-12">+ <div+ className={+ expandedProfiles.includes(firstResult?.profileManager)+ ? "expanded-output"+ : "collapsed-output"+ }+ >+ {displayPromptResult(firstResult?.output, true)}+ </div>+ </Typography.Paragraph>+ )}+ <div className="prompt-profile-run">+ <Tooltip title="Expand">+ <Button+ size="small"+ type="text"+ className="prompt-card-action-button"+ onClick={() =>+ handleExpandClick({+ profile_id: firstResult?.profileManager,+ })+ }+ >+ <ArrowsAltOutlined className="prompt-card-actions-head" />+ </Button>+ </Tooltip>+ </div>+ </div>+ </>+ );+ }++ return (+ <AnimatePresence>+ {!singlePassExtractMode &&+ !isSimplePromptStudio &&+ llmProfileDetails.map((profile, index) => {+ const profileId = profile?.profile_id;+ const isChecked = enabledProfiles.includes(profileId);+ const tokenUsageId =+ promptDetails?.prompt_id ++ "__" ++ selectedDoc?.document_id ++ "__" ++ profileId;+ return (+ <motion.div+ key={profileId}+ initial={{ x: 0 }}+ animate={{+ x: profileId === selectedLlmProfileId && index !== 0 ? -10 : 0,+ }}+ transition={{ duration: 0.5, ease: "linear" }}+ className="prompt-card-llm"+ >+ <Col+ key={profileId}+ className="prompt-card-llm-container"+ xs={{ span: getColSpan() }}+ >+ <Divider className="prompt-card-divider" />+ <Space direction="vertical" className="prompt-card-llm-layout">+ <div className="llm-info">+ <Image+ src={profile?.icon}+ width={15}+ height={15}+ preview={false}+ className="prompt-card-llm-icon"+ />+ <Typography.Title+ className="prompt-card-llm-title"+ level={5}+ >+ {profile?.conf?.LLM}+ </Typography.Title>+ </div>+ <div className="prompt-cost">+ <Typography.Text className="prompt-cost-item">+ Tokens:{" "}+ {!singlePassExtractMode && (+ <TokenUsage tokenUsageId={tokenUsageId} />+ )}+ </Typography.Text>+ <Typography.Text className="prompt-cost-item">+ Time: {timers[tokenUsageId] || 0}s+ </Typography.Text>+ <Typography.Text className="prompt-cost-item">+ Cost: ${getFormattedTotalCost(result, profile)}+ </Typography.Text>+ </div>+ <div className="prompt-info">+ <CheckableTag+ checked={isChecked}+ onChange={(checked) =>+ handleTagChange(checked, profileId)+ }+ disabled={isPublicSource}+ className={isChecked ? "checked" : "unchecked"}+ >+ {isChecked ? (+ <span>+ Enabled+ <CheckCircleOutlined+ style={{+ color: "#52c41a",+ marginLeft: "5px",+ }}+ />+ </span>+ ) : (+ <span>+ Disabled+ <ExclamationCircleFilled+ style={{+ color: "#BABBBC",+ marginLeft: "5px",+ }}+ />+ </span>+ )}+ </CheckableTag>+ <div className="llm-info-container">+ <Tooltip title={tooltipContent(profile?.conf)}>+ <InfoCircleOutlined />+ </Tooltip>+ <Tooltip title="Chunk used">+ <DatabaseOutlined+ onClick={() => {+ setIsIndexOpen(true);+ setOpenIndexProfile(+ result.find(+ (r) => r?.profileManager === profileId+ )?.context+ );+ }}+ className="prompt-card-actions-head"+ />+ </Tooltip>+ {isNotSingleLlmProfile && (+ <Radio+ checked={profileId === selectedLlmProfileId}+ onChange={() => handleSelectDefaultLLM(profileId)}+ disabled={isPublicSource}+ >+ Default+ </Radio>+ )}+ </div>+ </div>+ </Space>+ <>+ <Divider className="prompt-card-divider" />+ <div+ className={`prompt-card-result prompt-card-div ${+ expandedProfiles.includes(profileId) &&+ "prompt-profile-run-expanded"+ }`}+ >+ {enforceType === TABLE_ENFORCE_TYPE ? (+ <div />+ ) : (+ <>+ {isRunLoading[+ `${selectedDoc?.document_id}_${profileId}`+ ] ? (+ <Spin indicator={<SpinnerLoader size="small" />} />+ ) : (+ <Typography.Paragraph className="prompt-card-res font-size-12">+ <div+ className={+ expandedProfiles.includes(profileId)+ ? "expanded-output"+ : "collapsed-output"+ }+ >+ {!result.find(+ (r) => r?.profileManager === profileId+ )?.output ? (+ <Typography.Text className="prompt-not-ran">+ <span>+ <InfoCircleFilled+ style={{ color: "#F0AD4E" }}+ />+ </span>{" "}+ Yet to run+ </Typography.Text>+ ) : (+ displayPromptResult(+ result.find(+ (r) => r?.profileManager === profileId+ )?.output,+ true+ )+ )}+ </div>+ </Typography.Paragraph>+ )}+ </>+ )}+ <div className="prompt-profile-run">+ <Tooltip title="Run">+ <Button+ size="small"+ type="text"+ className="prompt-card-action-button"+ onClick={() => handleRun(profileId, false)}+ disabled={+ isRunLoading[+ `${selectedDoc?.document_id}_${profileId}`+ ] || isPublicSource+ }+ >+ <PlayCircleOutlined className="prompt-card-actions-head" />+ </Button>+ </Tooltip>+ <Tooltip title="Run All">+ <Button+ size="small"+ type="text"+ className="prompt-card-action-button"+ onClick={() => handleRun(profileId, true)}+ disabled={+ isRunLoading[+ `${selectedDoc?.document_id}_${profileId}`+ ] || isPublicSource+ }+ >+ <PlayCircleFilled className="prompt-card-actions-head" />+ </Button>+ </Tooltip>+ <Tooltip title="Expand">+ <Button+ size="small"+ type="text"+ className="prompt-card-action-button"+ onClick={() => handleExpandClick(profile)}+ >+ <ArrowsAltOutlined className="prompt-card-actions-head" />+ </Button>+ </Tooltip>+ </div>+ </div>+ {enforceType === TABLE_ENFORCE_TYPE && TableOutput && (+ <TableOutput+ output={+ result.find((r) => r?.profileManager === profileId)+ ?.output+ }+ />+ )}+ </>+ </Col>+ </motion.div>+ );+ })}+ </AnimatePresence>+ );+}++PromptOutput.propTypes = {+ promptDetails: PropTypes.object.isRequired,+ isRunLoading: PropTypes.object,+ result: PropTypes.object.isRequired,+ handleRun: PropTypes.func.isRequired,+ handleSelectDefaultLLM: PropTypes.func.isRequired,+ selectedLlmProfileId: PropTypes.string,+ timers: PropTypes.object.isRequired,+ spsLoading: PropTypes.object,+ llmProfileDetails: PropTypes.array.isRequired,+ setOpenIndexProfile: PropTypes.func.isRequired,+ enabledProfiles: PropTypes.array.isRequired,+ setEnabledProfiles: PropTypes.func.isRequired,+ expandedProfiles: PropTypes.array.isRequired,+ setExpandedProfiles: PropTypes.func.isRequired,+ isNotSingleLlmProfile: PropTypes.bool.isRequired,+ setIsIndexOpen: PropTypes.func.isRequired,+ enforceType: PropTypes.string.isRequired,+};++export { PromptOutput };diff --git a/frontend/src/components/custom-tools/prompt-card/PromptOutputActions.jsx b/frontend/src/components/custom-tools/prompt-card/PromptOutputActions.jsxnew file mode 100644index 000000000..03a087a5f--- /dev/null+++ b/frontend/src/components/custom-tools/prompt-card/PromptOutputActions.jsx@@ -0,0 +1,60 @@+import { PlayCircleFilled, PlayCircleOutlined } from "@ant-design/icons";+import PropTypes from "prop-types";+import { Button, Tooltip } from "antd";++import { useCustomToolStore } from "../../../store/custom-tool-store";++function PromptOutputActions({+ isNotSingleLlmProfile,+ handleRun,+ profileId,+ isRunLoading,+}) {+ const { selectedDoc, isPublicSource } = useCustomToolStore();++ if (isNotSingleLlmProfile) {+ return <></>;+ }++ return (+ <>+ <Tooltip title="Run">+ <Button+ size="small"+ type="text"+ className="prompt-card-action-button"+ onClick={() => handleRun(profileId, false)}+ disabled={+ isRunLoading[`${selectedDoc?.document_id}_${profileId}`] ||+ isPublicSource+ }+ >+ <PlayCircleOutlined className="prompt-card-actions-head" />+ </Button>+ </Tooltip>+ <Tooltip title="Run All">+ <Button+ size="small"+ type="text"+ className="prompt-card-action-button"+ onClick={() => handleRun(profileId, true)}+ disabled={+ isRunLoading[`${selectedDoc?.document_id}_${profileId}`] ||+ isPublicSource+ }+ >+ <PlayCircleFilled className="prompt-card-actions-head" />+ </Button>+ </Tooltip>+ </>+ );+}++PromptOutputActions.propTypes = {+ isNotSingleLlmProfile: PropTypes.bool.isRequired,+ handleRun: PropTypes.func.isRequired,+ profileId: PropTypes.string.isRequired,+ isRunLoading: PropTypes.bool.isRequired,+};++export { PromptOutputActions };diff --git a/frontend/src/components/custom-tools/prompt-card/constants.js b/frontend/src/components/custom-tools/prompt-card/constants.jsnew file mode 100644index 000000000..e88a6f482--- /dev/null+++ b/frontend/src/components/custom-tools/prompt-card/constants.js@@ -0,0 +1,3 @@+const TABLE_ENFORCE_TYPE = "table";++export { TABLE_ENFORCE_TYPE };diff --git a/prompt-service/src/unstract/prompt_service/constants.py b/prompt-service/src/unstract/prompt_service/constants.pyindex 3d3b46c77..781b437ef 100644--- a/prompt-service/src/unstract/prompt_service/constants.py+++ b/prompt-service/src/unstract/prompt_service/constants.py@@ -58,6 +58,8 @@ class PromptServiceContants:OUTPUT = "output"CONTEXT = "context"INCLUDE_METADATA = "include_metadata"+ TABLE = "table"+ TABLE_SETTINGS = "table_settings"EPILOGUE = "epilogue"PLATFORM_POSTAMBLE = "platform_postamble"EXTRACT_EPILOGUE = "extract-epilogue"@@ -82,6 +84,7 @@ class RunLevel(Enum):RUN = "RUN"EVAL = "EVAL"CHALLENGE = "CHALLENGE"+ TABLE_EXTRACTION = "TABLE_EXTRACTION"class FeatureFlag:diff --git a/prompt-service/src/unstract/prompt_service/helper.py b/prompt-service/src/unstract/prompt_service/helper.pyindex f75578cef..764502e29 100644--- a/prompt-service/src/unstract/prompt_service/helper.py+++ b/prompt-service/src/unstract/prompt_service/helper.py@@ -296,3 +296,29 @@ def run_completion(logger.error(f"Error fetching response for prompt: {e}.")# TODO: Publish this error as a FE updateraise APIError(str(e)) from e+++def extract_table(+ output: dict[str, Any],+ plugins: dict[str, dict[str, Any]],+ structured_output: dict[str, Any],+ llm: LLM,+) -> dict[str, Any]:+ table_settings = output[PSKeys.TABLE_SETTINGS]+ table_extractor: dict[str, Any] = plugins.get("table-extractor", {})+ if not table_extractor:+ raise APIError(+ "Unable to extract table details. "+ "Please contact admin to resolve this issue."+ )+ try:+ answer = table_extractor["entrypoint_cls"].extract_large_table(+ llm=llm, table_settings=table_settings+ )+ structured_output[output[PSKeys.NAME]] = answer+ # We do not support summary and eval for table.+ # Hence returning the result+ return structured_output+ except table_extractor["exception_cls"] as e:+ msg = f"Couldn't extract table. {e}"+ raise APIError(message=msg)diff --git a/prompt-service/src/unstract/prompt_service/main.py b/prompt-service/src/unstract/prompt_service/main.pyindex 40c85c2f5..eebecbfbc 100644--- a/prompt-service/src/unstract/prompt_service/main.py+++ b/prompt-service/src/unstract/prompt_service/main.py@@ -13,6 +13,7 @@ from unstract.prompt_service.constants import RunLevelfrom unstract.prompt_service.exceptions import APIError, ErrorResponse, NoPayloadErrorfrom unstract.prompt_service.helper import (construct_and_run_prompt,+ extract_table,extract_variable,get_cleaned_context,plugin_loader,@@ -31,6 +32,9 @@ from werkzeug.exceptions import HTTPExceptionfrom unstract.core.pubsub_helper import LogPublisher+USE_UNSTRACT_PROMPT = True+MAX_RETRIES = 3+NO_CONTEXT_ERROR = ("Couldn't fetch context from vector DB. ""This happens usually due to a delay by the Vector DB "@@ -188,6 +192,39 @@ def prompt_processor() -> Any:"Unable to obtain LLM / embedding / vectorDB",)return APIError(message=msg)++ if output[PSKeys.TYPE] == PSKeys.TABLE:+ try:+ structured_output = extract_table(+ output=output,+ plugins=plugins,+ structured_output=structured_output,+ llm=llm,+ )+ metadata = query_usage_metadata(token=platform_key, metadata=metadata)+ response = {+ PSKeys.METADATA: metadata,+ PSKeys.OUTPUT: structured_output,+ }+ return response+ except APIError as api_error:+ app.logger.error(+ "Failed to extract table for the prompt %s: %s",+ output[PSKeys.NAME],+ str(api_error),+ )+ _publish_log(+ log_events_id,+ {+ "tool_id": tool_id,+ "prompt_key": prompt_name,+ "doc_name": doc_name,+ },+ LogLevel.ERROR,+ RunLevel.TABLE_EXTRACTION,+ "Error while extracting table for the prompt",+ )+try:vector_index = vector_db.get_vector_store_index()diff --git a/tools/structure/src/config/properties.json b/tools/structure/src/config/properties.jsonindex b60516be7..7883e92eb 100644--- a/tools/structure/src/config/properties.json+++ b/tools/structure/src/config/properties.json@@ -2,7 +2,7 @@"schemaVersion": "0.0.1","displayName": "Structure Tool","functionName": "structure_tool",- "toolVersion": "0.0.37",+ "toolVersion": "0.0.38","description": "This is a template tool which can answer set of input prompts designed in the Prompt Studio","input": {"description": "File that needs to be indexed and parsed for answers"diff --git a/tools/structure/src/constants.py b/tools/structure/src/constants.pyindex 1f79c109e..cc341e447 100644--- a/tools/structure/src/constants.py+++ b/tools/structure/src/constants.py@@ -67,6 +67,9 @@ class SettingsKeys:FILE_HASH = "file_hash"ENABLE_HIGHLIGHT = "enable_highlight"NAME = "name"+ INCLUDE_METADATA = "include_metadata"+ TABLE_SETTINGS = "table_settings"+ INPUT_FILE = "input_file"METADATA = "metadata"EPILOGUE = "epilogue"HIGHLIGHT_DATA = "highlight_data"diff --git a/tools/structure/src/main.py b/tools/structure/src/main.pyindex c42d547a8..aa5551e50 100644--- a/tools/structure/src/main.py+++ b/tools/structure/src/main.py@@ -64,7 +64,6 @@ class StructureTool(BaseTool):single_pass_extraction_mode)tool_settings[SettingsKeys.SUMMARIZE_AS_SOURCE] = summarize_as_source-prompt_service_resp = None_, file_name = os.path.split(input_file)if summarize_as_source:@@ -166,6 +165,19 @@ class StructureTool(BaseTool):reindex = Falseexcept Exception as e:self.stream_error_and_exit(f"Error fetching data and indexing: {e}")++ # TODO : Make this snippet pluggable and introduce pluggablity for tools.+ for output in outputs:+ try:+ table_settings = output[SettingsKeys.TABLE_SETTINGS]+ extracted_input_file = tool_data_dir / SettingsKeys.EXTRACT+ table_settings[SettingsKeys.INPUT_FILE] = extracted_input_file+ output.update({SettingsKeys.TABLE_SETTINGS: table_settings})++ except KeyError:+ # To check if the prompt has table enforce type selected.+ pass+self.stream_log("Fetching responses for prompts...")prompt_service_resp = responder.answer_prompt(payload=payload,