Skip to content

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

FIX - Prompt Studio UI Improvements and Bug Fixes (#134)

Tahier Hussain0cb56dc49d0d
diff --git a/backend/prompt_studio/prompt_profile_manager/migrations/0009_alter_profilemanager_prompt_studio_tool.py b/backend/prompt_studio/prompt_profile_manager/migrations/0009_alter_profilemanager_prompt_studio_tool.py
new file mode 100644
index 000000000..e35da9b9e
--- /dev/null
+++ b/backend/prompt_studio/prompt_profile_manager/migrations/0009_alter_profilemanager_prompt_studio_tool.py
@@ -0,0 +1,23 @@
+# Generated by Django 4.2.1 on 2024-03-18 07:58
+
+import django.db.models.deletion
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("prompt_studio_core", "0008_customtool_exclude_failed_customtool_monitor_llm"),
+ ("prompt_profile_manager", "0008_profilemanager_migration"),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name="profilemanager",
+ name="prompt_studio_tool",
+ field=models.ForeignKey(
+ null=True,
+ on_delete=django.db.models.deletion.CASCADE,
+ to="prompt_studio_core.customtool",
+ ),
+ ),
+ ]
diff --git a/backend/prompt_studio/prompt_profile_manager/models.py b/backend/prompt_studio/prompt_profile_manager/models.py
index b84263e6f..529f50a76 100644
--- a/backend/prompt_studio/prompt_profile_manager/models.py
+++ b/backend/prompt_studio/prompt_profile_manager/models.py
@@ -82,7 +82,7 @@ class ProfileManager(BaseModel):
)
prompt_studio_tool = models.ForeignKey(
- CustomTool, on_delete=models.PROTECT, null=True
+ CustomTool, on_delete=models.CASCADE, null=True
)
is_default = models.BooleanField(
default=False,
diff --git a/frontend/package-lock.json b/frontend/package-lock.json
index dbf4f3dcf..ae28e110c 100644
--- a/frontend/package-lock.json
+++ b/frontend/package-lock.json
@@ -24,6 +24,7 @@
"cron-validator": "^1.3.1",
"emoji-picker-react": "^4.8.0",
"emoji-regex": "^10.3.0",
+ "escape-html": "^1.0.3",
"handlebars": "^4.7.8",
"http-proxy-middleware": "^2.0.6",
"js-yaml": "^4.1.0",
diff --git a/frontend/package.json b/frontend/package.json
index fdc22fe68..4a1771e61 100644
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -19,6 +19,7 @@
"cron-validator": "^1.3.1",
"emoji-picker-react": "^4.8.0",
"emoji-regex": "^10.3.0",
+ "escape-html": "^1.0.3",
"handlebars": "^4.7.8",
"http-proxy-middleware": "^2.0.6",
"js-yaml": "^4.1.0",
diff --git a/frontend/src/components/custom-tools/combined-output/CombinedOutput.jsx b/frontend/src/components/custom-tools/combined-output/CombinedOutput.jsx
index 1baa101e3..9711eaf9f 100644
--- a/frontend/src/components/custom-tools/combined-output/CombinedOutput.jsx
+++ b/frontend/src/components/custom-tools/combined-output/CombinedOutput.jsx
@@ -15,7 +15,12 @@ import { SpinnerLoader } from "../../widgets/spinner-loader/SpinnerLoader";
import "./CombinedOutput.css";
import { useExceptionHandler } from "../../../hooks/useExceptionHandler";
-function CombinedOutput({ docId, setFilledFields, triggerRunSinglePass }) {
+function CombinedOutput({
+ docId,
+ setFilledFields,
+ triggerRunSinglePass,
+ setTriggerRunSinglePass,
+}) {
const [combinedOutput, setCombinedOutput] = useState({});
const [isOutputLoading, setIsOutputLoading] = useState(false);
const { details, isSinglePassExtract, updateCustomTool } =
@@ -91,6 +96,10 @@ function CombinedOutput({ docId, setFilledFields, triggerRunSinglePass }) {
}, [combinedOutput]);
useEffect(() => {
+ if (!triggerRunSinglePass) {
+ return;
+ }
+ setTriggerRunSinglePass(false);
runSinglePassExtraction();
}, [triggerRunSinglePass]);
@@ -169,6 +178,7 @@ CombinedOutput.propTypes = {
docId: PropTypes.string,
setFilledFields: PropTypes.func,
triggerRunSinglePass: PropTypes.bool.isRequired,
+ setTriggerRunSinglePass: PropTypes.func.isRequired,
};
export { CombinedOutput };
diff --git a/frontend/src/components/custom-tools/output-for-doc-modal/OutputForDocModal.jsx b/frontend/src/components/custom-tools/output-for-doc-modal/OutputForDocModal.jsx
index a7af08f1e..3602cccf7 100644
--- a/frontend/src/components/custom-tools/output-for-doc-modal/OutputForDocModal.jsx
+++ b/frontend/src/components/custom-tools/output-for-doc-modal/OutputForDocModal.jsx
@@ -9,6 +9,9 @@ import { useSessionStore } from "../../../store/session-store";
import { useAxiosPrivate } from "../../../hooks/useAxiosPrivate";
import "./OutputForDocModal.css";
import { displayPromptResult } from "../../../helpers/GetStaticData";
+import { SpinnerLoader } from "../../widgets/spinner-loader/SpinnerLoader";
+import { useAlertStore } from "../../../store/alert-store";
+import { useExceptionHandler } from "../../../hooks/useExceptionHandler";
const columns = [
{
@@ -20,6 +23,7 @@ const columns = [
title: "Value",
dataIndex: "value",
key: "value",
+ width: 700,
},
];
@@ -29,17 +33,88 @@ function OutputForDocModal({
promptId,
promptKey,
profileManagerId,
+ docOutputs,
}) {
+ const [promptOutputs, setPromptOutputs] = useState([]);
const [rows, setRows] = useState([]);
- const { details, listOfDocs } = useCustomToolStore();
+ const [isLoading, setIsLoading] = useState(false);
+ const { details, listOfDocs, selectedDoc } = useCustomToolStore();
const { sessionDetails } = useSessionStore();
const axiosPrivate = useAxiosPrivate();
const navigate = useNavigate();
+ const { setAlertDetails } = useAlertStore();
+ const { handleException } = useExceptionHandler();
useEffect(() => {
+ if (!open) {
+ return;
+ }
handleGetOutputForDocs();
}, [open]);
+ useEffect(() => {
+ updatePromptOutput();
+ }, [docOutputs]);
+
+ useEffect(() => {
+ handleRowsGeneration(promptOutputs);
+ }, [promptOutputs]);
+
+ const moveSelectedDocToTop = () => {
+ // Create a copy of the list of documents
+ const docs = [...listOfDocs];
+
+ // Find the index of the selected document within the list
+ const index = docs.findIndex(
+ (item) => item?.document_id === selectedDoc?.document_id
+ );
+
+ // If the selected document exists in the list and is not already at the top (index 0)
+ if (index !== -1 && index !== 0) {
+ // Remove the selected document from its current position
+ const doc = docs.splice(index, 1)[0];
+ // Insert the selected document at the beginning of the list
+ docs.unshift(doc);
+ }
+
+ // Return the updated list of documents
+ return docs;
+ };
+
+ const updatePromptOutput = (data) => {
+ setPromptOutputs((prev) => {
+ // If data is provided, use it; otherwise, create a copy of the previous state
+ const updatedPromptOutput = data || [...prev];
+
+ // Get the keys of docOutputs
+ const keys = Object.keys(docOutputs);
+
+ keys.forEach((key) => {
+ // Find the index of the prompt output corresponding to the document manager key
+ const index = updatedPromptOutput.findIndex(
+ (promptOutput) => promptOutput?.document_manager === key
+ );
+
+ // If the prompt output for the current key doesn't exist, skip it
+ if (index === -1) {
+ return;
+ }
+
+ // Retrieve the prompt output instance
+ const promptOutputInstance = updatedPromptOutput[index];
+
+ // Update output and isLoading properties based on docOutputs
+ promptOutputInstance["output"] = docOutputs[key]?.output || null;
+ promptOutputInstance["isLoading"] = docOutputs[key]?.isLoading || false;
+
+ // Update the prompt output instance in the array
+ updatedPromptOutput[index] = promptOutputInstance;
+ });
+
+ return updatedPromptOutput;
+ });
+ };
+
const handleGetOutputForDocs = () => {
if (!profileManagerId) {
setRows([]);
@@ -53,22 +128,29 @@ function OutputForDocModal({
},
};
+ setIsLoading(true);
axiosPrivate(requestOptions)
.then((res) => {
const data = res?.data || [];
data.sort((a, b) => {
return new Date(b.modified_at) - new Date(a.modified_at);
});
- handleRowsGeneration(data);
+ updatePromptOutput(data);
})
.catch((err) => {
- throw err;
+ setAlertDetails(
+ handleException(err, "Failed to loaded the prompt results")
+ );
+ })
+ .finally(() => {
+ setIsLoading(false);
});
};
const handleRowsGeneration = (data) => {
const rowsData = [];
- [...listOfDocs].forEach((item) => {
+ const docs = moveSelectedDocToTop();
+ docs.forEach((item) => {
const output = data.find(
(outputValue) => outputValue?.document_manager === item?.document_id
);
@@ -78,16 +160,22 @@ function OutputForDocModal({
key: item,
document: item?.document_name,
value: (
- <Typography.Text>
- <span style={{ marginRight: "8px" }}>
- {isSuccess ? (
- <CheckCircleFilled style={{ color: "#52C41A" }} />
- ) : (
- <CloseCircleFilled style={{ color: "#FF4D4F" }} />
- )}
- </span>{" "}
- {isSuccess ? displayPromptResult(output?.output) : "Failed"}
- </Typography.Text>
+ <>
+ {output?.isLoading ? (
+ <SpinnerLoader align="default" />
+ ) : (
+ <Typography.Text>
+ <span style={{ marginRight: "8px" }}>
+ {isSuccess ? (
+ <CheckCircleFilled style={{ color: "#52C41A" }} />
+ ) : (
+ <CloseCircleFilled style={{ color: "#FF4D4F" }} />
+ )}
+ </span>{" "}
+ {isSuccess ? displayPromptResult(output?.output) : "Failed"}
+ </Typography.Text>
+ )}
+ </>
),
};
rowsData.push(result);
@@ -123,6 +211,7 @@ function OutputForDocModal({
columns={columns}
dataSource={rows}
pagination={{ pageSize: 5 }}
+ loading={isLoading}
/>
</div>
</div>
@@ -136,6 +225,7 @@ OutputForDocModal.propTypes = {
promptId: PropTypes.string.isRequired,
promptKey: PropTypes.string.isRequired,
profileManagerId: PropTypes.string,
+ docOutputs: PropTypes.object,
};
export { OutputForDocModal };
diff --git a/frontend/src/components/custom-tools/prompt-card/PromptCard.jsx b/frontend/src/components/custom-tools/prompt-card/PromptCard.jsx
index ab17d6076..c1df0774d 100644
--- a/frontend/src/components/custom-tools/prompt-card/PromptCard.jsx
+++ b/frontend/src/components/custom-tools/prompt-card/PromptCard.jsx
@@ -96,6 +96,7 @@ function PromptCard({
const [isCoverageLoading, setIsCoverageLoading] = useState(false);
const [openOutputForDoc, setOpenOutputForDoc] = useState(false);
const [progressMsg, setProgressMsg] = useState({});
+ const [docOutputs, setDocOutputs] = useState({});
const divRef = useRef(null);
const {
getDropdownItems,
@@ -265,6 +266,18 @@ function PromptCard({
);
};
+ const handleDocOutputs = (docId, isLoading, output) => {
+ setDocOutputs((prev) => {
+ const updatedDocOutputs = { ...prev };
+ // Update the entry for the provided docId with isLoading and output
+ updatedDocOutputs[docId] = {
+ isLoading,
+ output,
+ };
+ return updatedDocOutputs;
+ });
+ };
+
// Generate the result for the currently selected document
const handleRun = () => {
if (!promptDetails?.profile_manager?.length) {
@@ -304,6 +317,7 @@ function PromptCard({
setCoverage(0);
setCoverageTotal(0);
+ const docId = selectedDoc?.document_id;
let method = "POST";
let url = `/api/v1/unstract/${sessionDetails?.orgId}/prompt-studio/prompt-output/`;
if (result?.promptOutputId) {
@@ -312,8 +326,7 @@ function PromptCard({
}
const isSummaryIndexed = [...summarizeIndexStatus].find(
- (item) =>
- item?.docId === selectedDoc?.document_id && item?.isIndexed === true
+ (item) => item?.docId === docId && item?.isIndexed === true
);
if (
@@ -322,7 +335,7 @@ function PromptCard({
details?.summarize_llm_profile
) {
// Summary needs to be indexed before running the prompt
- handleUpdateOutput(null, selectedDoc?.document_id, [], method, url);
+ handleUpdateOutput(null, docId, [], method, url);
handleStepsAfterRunCompletion();
setAlertDetails({
type: "error",
@@ -330,7 +343,9 @@ function PromptCard({
});
return;
}
- handleRunApiRequest(selectedDoc?.document_id)
+
+ handleDocOutputs(docId, true, null);
+ handleRunApiRequest(docId)
.then((res) => {
const data = res?.data;
const value = data[promptDetails?.prompt_key];
@@ -344,21 +359,15 @@ function PromptCard({
promptDetails?.prompt_key,
data
);
- handleUpdateOutput(
- value,
- selectedDoc?.document_id,
- evalMetrics,
- method,
- url
- );
+
+ handleDocOutputs(docId, false, value);
+ handleUpdateOutput(value, docId, evalMetrics, method, url);
})
.catch((err) => {
- handleUpdateOutput(null, selectedDoc?.document_id, [], method, url);
+ handleDocOutputs(docId, false, null);
+ handleUpdateOutput(null, docId, [], method, url);
setAlertDetails(
- handleException(
- err,
- `Failed to generate output for ${selectedDoc?.document_id}`
- )
+ handleException(err, `Failed to generate output for ${docId}`)
);
})
.finally(() => {
@@ -385,11 +394,10 @@ function PromptCard({
let totalCoverageValue = 1;
listOfDocsToProcess.forEach((item) => {
+ const docId = item?.document_id;
let method = "POST";
let url = `/api/v1/unstract/${sessionDetails?.orgId}/prompt-studio/prompt-output/`;
- const outputId = outputIds.find(
- (output) => output?.docId === item?.document_id
- );
+ const outputId = outputIds.find((output) => output?.docId === docId);
if (outputId?.promptOutputId?.length) {
method = "PATCH";
url += `${outputId?.promptOutputId}/`;
@@ -397,8 +405,7 @@ function PromptCard({
const isSummaryIndexed = [...summarizeIndexStatus].find(
(indexStatus) =>
- indexStatus?.docId === item?.document_id &&
- indexStatus?.isIndexed === true
+ indexStatus?.docId === docId && indexStatus?.isIndexed === true
);
if (
@@ -407,7 +414,7 @@ function PromptCard({
details?.summarize_llm_profile
) {
// Summary needs to be indexed before running the prompt
- handleUpdateOutput(null, item?.document_id, [], method, url);
+ handleUpdateOutput(null, docId, [], method, url);
totalCoverageValue++;
setCoverageTotal(totalCoverageValue);
setAlertDetails({
@@ -416,7 +423,9 @@ function PromptCard({
});
return;
}
- handleRunApiRequest(item?.document_id)
+
+ handleDocOutputs(docId, true, null);
+ handleRunApiRequest(docId)
.then((res) => {
const data = res?.data;
const outputValue = data[promptDetails?.prompt_key];
@@ -430,21 +439,15 @@ function PromptCard({
promptDetails?.prompt_key,
data
);
- handleUpdateOutput(
- outputValue,
- item?.document_id,
- evalMetrics,
- method,
- url
- );
+
+ handleDocOutputs(docId, false, outputValue);
+ handleUpdateOutput(outputValue, docId, evalMetrics, method, url);
})
.catch((err) => {
- handleUpdateOutput(null, item?.document_id, [], method, url);
+ handleDocOutputs(docId, false, null);
+ handleUpdateOutput(null, docId, [], method, url);
setAlertDetails(
- handleException(
- err,
- `Failed to generate output for ${item?.document_id}`
- )
+ handleException(err, `Failed to generate output for ${docId}`)
);
})
.finally(() => {
@@ -871,14 +874,19 @@ function PromptCard({
size="small"
type="link"
className="display-flex-align-center"
- icon={<SearchOutlined className="font-size-12" />}
- loading={isCoverageLoading}
onClick={() => setOpenOutputForDoc(true)}
disabled={outputIds?.length === 0}
>
- <Typography.Link className="font-size-12">
- Coverage: {coverage} of {listOfDocs?.length || 0} docs
- </Typography.Link>
+ <Space>
+ {isCoverageLoading ? (
+ <SpinnerLoader size="small" />
+ ) : (
+ <SearchOutlined className="font-size-12" />
+ )}
+ <Typography.Link className="font-size-12">
+ Coverage: {coverage} of {listOfDocs?.length || 0} docs
+ </Typography.Link>
+ </Space>
</Button>
</Space>
<div>
@@ -983,6 +991,7 @@ function PromptCard({
promptId={promptDetails?.prompt_id}
promptKey={promptDetails?.prompt_key}
profileManagerId={promptDetails?.profile_manager}
+ docOutputs={docOutputs}
/>
</>
);
diff --git a/frontend/src/components/custom-tools/text-viewer-pre/TextViewerPre.jsx b/frontend/src/components/custom-tools/text-viewer-pre/TextViewerPre.jsx
index 739b2aae4..8686c4272 100644
--- a/frontend/src/components/custom-tools/text-viewer-pre/TextViewerPre.jsx
+++ b/frontend/src/components/custom-tools/text-viewer-pre/TextViewerPre.jsx
@@ -1,9 +1,10 @@
import PropTypes from "prop-types";
+import escapeHTML from "escape-html";
function TextViewerPre({ text }) {
return (
<div className="text-viewer-layout">
- <pre style={{ fontSize: "12px" }}>{text}</pre>
+ <pre style={{ fontSize: "12px" }}>{escapeHTML(text)}</pre>
</div>
);
}
diff --git a/frontend/src/components/custom-tools/tools-main/ToolsMain.jsx b/frontend/src/components/custom-tools/tools-main/ToolsMain.jsx
index 424ad7026..53fb59bcc 100644
--- a/frontend/src/components/custom-tools/tools-main/ToolsMain.jsx
+++ b/frontend/src/components/custom-tools/tools-main/ToolsMain.jsx
@@ -1,6 +1,6 @@
-import { Button, Tabs, Tooltip } from "antd";
+import { Button, Space, Tabs, Tooltip } from "antd";
import { useEffect, useState } from "react";
-import { PlayCircleOutlined } from "@ant-design/icons";
+import { BarChartOutlined, PlayCircleOutlined } from "@ant-design/icons";
import { promptType } from "../../../helpers/GetStaticData";
import { useAxiosPrivate } from "../../../hooks/useAxiosPrivate";
@@ -12,6 +12,7 @@ import { DocumentParser } from "../document-parser/DocumentParser";
import { Footer } from "../footer/Footer";
import "./ToolsMain.css";
import { useExceptionHandler } from "../../../hooks/useExceptionHandler";
+import { useNavigate } from "react-router-dom";
function ToolsMain() {
const [activeKey, setActiveKey] = useState("1");
@@ -30,6 +31,7 @@ function ToolsMain() {
const { setAlertDetails } = useAlertStore();
const axiosPrivate = useAxiosPrivate();
const handleException = useExceptionHandler();
+ const navigate = useNavigate();
const items = [
{
@@ -96,7 +98,7 @@ function ToolsMain() {
return;
}
setActiveKey("2");
- setTriggerRunSinglePass((prev) => !prev);
+ setTriggerRunSinglePass(true);
}, [isSinglePassExtract]);
const onChange = (key) => {
@@ -146,14 +148,22 @@ function ToolsMain() {
<Tabs activeKey={activeKey} items={items} onChange={onChange} />
</div>
<div className="display-flex-align-center">
- <Tooltip title="Single Pass Extraction">
- <Button
- onClick={handleSinglePassExtraction}
- loading={isSinglePassExtract}
- disabled={disableLlmOrDocChange?.length > 0}
- icon={<PlayCircleOutlined />}
- />
- </Tooltip>
+ <Space>
+ <Tooltip title="Output Analyzer">
+ <Button
+ icon={<BarChartOutlined />}
+ onClick={() => navigate("outputAnalyzer")}
+ />
+ </Tooltip>
+ <Tooltip title="Single Pass Extraction">
+ <Button
+ onClick={handleSinglePassExtraction}
+ loading={isSinglePassExtract}
+ disabled={disableLlmOrDocChange?.length > 0}
+ icon={<PlayCircleOutlined />}
+ />
+ </Tooltip>
+ </Space>
</div>
</div>
<div className="tools-main-body">
@@ -168,6 +178,7 @@ function ToolsMain() {
<CombinedOutput
docId={selectedDoc?.document_id}
triggerRunSinglePass={triggerRunSinglePass}
+ setTriggerRunSinglePass={setTriggerRunSinglePass}
/>
)}
</div>
diff --git a/frontend/src/components/helpers/custom-tools/CustomToolsHelper.js b/frontend/src/components/helpers/custom-tools/CustomToolsHelper.js
index 7ec18b6bd..483d5fd0f 100644
--- a/frontend/src/components/helpers/custom-tools/CustomToolsHelper.js
+++ b/frontend/src/components/helpers/custom-tools/CustomToolsHelper.js
@@ -8,6 +8,7 @@ import { useCustomToolStore } from "../../../store/custom-tool-store";
import { useSessionStore } from "../../../store/session-store";
import { SpinnerLoader } from "../../widgets/spinner-loader/SpinnerLoader";
import { SocketMessages } from "../socket-messages/SocketMessages";
+import { useSocketCustomToolStore } from "../../../store/socket-custom-tool";
function CustomToolsHelper() {
const [isLoading, setIsLoading] = useState(true);
@@ -15,6 +16,7 @@ function CustomToolsHelper() {
const { id } = useParams();
const { sessionDetails } = useSessionStore();
const { updateCustomTool, setDefaultCustomTool } = useCustomToolStore();
+ const { emptyCusToolMessages } = useSocketCustomToolStore();
const { setAlertDetails } = useAlertStore();
const navigate = useNavigate();
const axiosPrivate = useAxiosPrivate();
@@ -91,6 +93,7 @@ function CustomToolsHelper() {
useEffect(() => {
return () => {
setDefaultCustomTool();
+ emptyCusToolMessages();
};
}, []);