Skip to content

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

FIX: Optimize Timer Handling for Prompt Run API Calls (#698)

Tahier Hussain19d82495ba0b
diff --git a/frontend/src/components/custom-tools/prompt-card/PromptCard.jsx b/frontend/src/components/custom-tools/prompt-card/PromptCard.jsx
index 9f5ea896e..a8063e7e8 100644
--- a/frontend/src/components/custom-tools/prompt-card/PromptCard.jsx
+++ b/frontend/src/components/custom-tools/prompt-card/PromptCard.jsx
@@ -281,10 +281,6 @@ function PromptCard({
}));
};
- const getTokenUsageId = (promptId, docId, profileManagerId) => {
- return promptId + "__" + docId + "__" + profileManagerId;
- };
-
// Generate the result for the currently selected document
const handleRun = (
profileManagerId,
@@ -380,6 +376,7 @@ function PromptCard({
selectedLlmProfiles.includes(profile?.profile_id)
);
}
+
for (const profile of selectedProfiles) {
handleDocOutputs(
docId,
@@ -391,6 +388,8 @@ function PromptCard({
setIsCoverageLoading(true);
handleIsRunLoading(selectedDoc?.document_id, profile?.profile_id, true);
+
+ const startTime = Date.now();
handleRunApiRequest(docId, profile?.profile_id)
.then((res) => {
const data = res?.data || [];
@@ -426,6 +425,8 @@ function PromptCard({
.finally(() => {
handleIsRunLoading(docId, profile?.profile_id, false);
setIsCoverageLoading(false);
+ const endTime = Date.now();
+ updateTimers(startTime, endTime, profile?.profile_id);
});
runCoverageForAllDoc(coverAllDoc, profile.profile_id);
}
@@ -438,6 +439,7 @@ function PromptCard({
true,
null
);
+ const startTime = Date.now();
handleRunApiRequest(docId, profileManagerId)
.then((res) => {
const data = res?.data || [];
@@ -478,11 +480,23 @@ function PromptCard({
.finally(() => {
setIsCoverageLoading(false);
handleIsRunLoading(selectedDoc?.document_id, profileManagerId, false);
+ const endTime = Date.now();
+ updateTimers(startTime, endTime, profileManagerId);
});
runCoverageForAllDoc(coverAllDoc, profileManagerId);
}
};
+ const updateTimers = (startTime, endTime, profileManagerId) => {
+ setTimers((prev) => {
+ prev[profileManagerId] = {
+ startTime,
+ endTime,
+ };
+ return prev;
+ });
+ };
+
const runCoverageForAllDoc = (coverAllDoc, profileManagerId) => {
if (coverAllDoc) {
handleCoverage(profileManagerId);
@@ -613,22 +627,13 @@ function PromptCard({
if (profileManagerId) {
body.profile_manager = profileManagerId;
- let tokenUsageId;
let url = `/api/v1/unstract/${sessionDetails?.orgId}/prompt-studio/fetch_response/${details?.tool_id}`;
if (!isSimplePromptStudio) {
body["run_id"] = runId;
- tokenUsageId = getTokenUsageId(promptId, docId, profileManagerId);
-
- // Set prompt run timer
- setTimers((prev) => ({
- ...prev,
- [tokenUsageId]: 0,
- }));
} else {
body["sps_id"] = details?.tool_id;
url = promptRunApiSps;
}
- const timerIntervalId = startTimer(tokenUsageId);
const requestOptions = {
method: "POST",
@@ -656,36 +661,10 @@ function PromptCard({
})
.catch((err) => {
throw err;
- })
- .finally(() => {
- if (!isSimplePromptStudio) {
- stopTimer(tokenUsageId, timerIntervalId);
- }
});
}
};
- const startTimer = (profileId) => {
- setTimers((prev) => ({
- ...prev,
- [profileId]: (prev[profileId] || 0) + 1,
- }));
- return setInterval(() => {
- setTimers((prev) => ({
- ...prev,
- [profileId]: (prev[profileId] || 0) + 1,
- }));
- }, 1000);
- };
-
- const stopTimer = (profileId, intervalId) => {
- clearInterval(intervalId);
- setTimers((prev) => ({
- ...prev,
- [profileId]: prev[profileId] || 0,
- }));
- };
-
return (
<>
<PromptCardItems
diff --git a/frontend/src/components/custom-tools/prompt-card/PromptOutput.jsx b/frontend/src/components/custom-tools/prompt-card/PromptOutput.jsx
index c26702f99..a77a749a2 100644
--- a/frontend/src/components/custom-tools/prompt-card/PromptOutput.jsx
+++ b/frontend/src/components/custom-tools/prompt-card/PromptOutput.jsx
@@ -21,11 +21,7 @@ import {
import { motion, AnimatePresence } from "framer-motion";
import CheckableTag from "antd/es/tag/CheckableTag";
-import {
- displayPromptResult,
- formatNumberWithCommas,
- getFormattedTotalCost,
-} from "../../../helpers/GetStaticData";
+import { displayPromptResult } from "../../../helpers/GetStaticData";
import { SpinnerLoader } from "../../widgets/spinner-loader/SpinnerLoader";
import { TokenUsage } from "../token-usage/TokenUsage";
import { useWindowDimensions } from "../../../hooks/useWindowDimensions";
@@ -36,6 +32,8 @@ import { useAlertStore } from "../../../store/alert-store";
import { PromptOutputExpandBtn } from "./PromptOutputExpandBtn";
import { DisplayPromptResult } from "./DisplayPromptResult";
import usePromptOutput from "../../../hooks/usePromptOutput";
+import { PromptRunTimer } from "./PromptRunTimer";
+import { PromptRunCost } from "./PromptRunCost";
let TableOutput;
try {
@@ -239,14 +237,24 @@ function PromptOutput({
)}
</Typography.Text>
<Typography.Text className="prompt-cost-item">
- Time:
- {timers[tokenUsageId] || 0}s
+ <PromptRunTimer
+ timer={timers[profileId]}
+ isLoading={
+ isRunLoading[
+ `${selectedDoc?.document_id}_${profileId}`
+ ]
+ }
+ />
</Typography.Text>
<Typography.Text className="prompt-cost-item">
- Cost: $
- {formatNumberWithCommas(
- getFormattedTotalCost(promptOutputData?.tokenUsage)
- )}
+ <PromptRunCost
+ tokenUsage={promptOutputData?.tokenUsage}
+ isLoading={
+ isRunLoading[
+ `${selectedDoc?.document_id}_${profileId}`
+ ]
+ }
+ />
</Typography.Text>
</div>
<div className="prompt-info">
diff --git a/frontend/src/components/custom-tools/prompt-card/PromptRunCost.jsx b/frontend/src/components/custom-tools/prompt-card/PromptRunCost.jsx
new file mode 100644
index 000000000..01438442f
--- /dev/null
+++ b/frontend/src/components/custom-tools/prompt-card/PromptRunCost.jsx
@@ -0,0 +1,23 @@
+import { memo } from "react";
+import PropTypes from "prop-types";
+import {
+ formatNumberWithCommas,
+ getFormattedTotalCost,
+} from "../../../helpers/GetStaticData";
+
+const PromptRunCost = memo(({ tokenUsage, isLoading }) => {
+ if (!tokenUsage || isLoading) {
+ return "Cost: NA";
+ }
+
+ return `Cost: $${formatNumberWithCommas(getFormattedTotalCost(tokenUsage))}`;
+});
+
+PromptRunCost.displayName = "PromptRunCost";
+
+PromptRunCost.propTypes = {
+ tokenUsage: PropTypes.number,
+ isLoading: PropTypes.bool.isRequired,
+};
+
+export { PromptRunCost };
diff --git a/frontend/src/components/custom-tools/prompt-card/PromptRunTimer.jsx b/frontend/src/components/custom-tools/prompt-card/PromptRunTimer.jsx
new file mode 100644
index 000000000..0e2e2ef63
--- /dev/null
+++ b/frontend/src/components/custom-tools/prompt-card/PromptRunTimer.jsx
@@ -0,0 +1,19 @@
+import { memo } from "react";
+import PropTypes from "prop-types";
+
+const PromptRunTimer = memo(({ timer, isLoading }) => {
+ if (!timer?.startTime || !timer?.endTime || isLoading) {
+ return "Time: 0s";
+ }
+
+ return `Time: ${Math.floor((timer?.endTime - timer?.startTime) / 1000)}s`;
+});
+
+PromptRunTimer.displayName = "PromptRunTimer";
+
+PromptRunTimer.propTypes = {
+ timer: PropTypes.object,
+ isLoading: PropTypes.bool.isRequired,
+};
+
+export { PromptRunTimer };