LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows
Python ai-agentsdata-engineeringdocument-aigenerative-aiidpjson-extractionllmmcp-serverocrpdf-extractionprompt-engineeringstructured-output
Added search and highlight for chunk used (#660)
jagadeeswaran-zipstackfd94b31608c0
diff --git a/frontend/src/components/custom-tools/prompt-card/OutputForIndex.jsx b/frontend/src/components/custom-tools/prompt-card/OutputForIndex.jsxindex ecc9fc88b..506414174 100644--- a/frontend/src/components/custom-tools/prompt-card/OutputForIndex.jsx+++ b/frontend/src/components/custom-tools/prompt-card/OutputForIndex.jsx@@ -1,33 +1,151 @@import PropTypes from "prop-types";-import { Modal } from "antd";+import { useState, useEffect, useRef, useCallback } from "react";+import { Modal, Input, Button, Typography } from "antd";import "./PromptCard.css";import { uniqueId } from "lodash";+import debounce from "lodash/debounce";+import { ArrowDownOutlined, ArrowUpOutlined } from "@ant-design/icons";import { TextViewerPre } from "../text-viewer-pre/TextViewerPre";function OutputForIndex({ chunkData, setIsIndexOpen, isIndexOpen }) {+ const [searchTerm, setSearchTerm] = useState("");+ const [highlightedChunks, setHighlightedChunks] = useState([]);+ const [currentIndex, setCurrentIndex] = useState(0);+ const [chunks, setChunks] = useState([]);+ const activeRef = useRef(null);++ useEffect(() => {+ if (!chunkData) {+ setChunks([]);+ }+ // Split chunkData into chunks using \f\n delimiter+ const tempChunks = chunkData?.split("\f\n");+ // To remove " at the end+ if (tempChunks?.length > 0) {+ const lastChunk = tempChunks[tempChunks?.length - 1].trim();+ if (lastChunk === '\\n"' || lastChunk === "") {+ tempChunks.pop();+ }+ }+ setChunks(tempChunks);+ }, [chunkData]);++ // Debounced search handler+ const handleSearch = useCallback(+ debounce((term) => {+ if (!term) {+ setHighlightedChunks([]);+ return;+ }+ const allResults = [];+ chunks?.forEach((chunk, chunkIndex) => {+ const lines = chunk?.split("\\n");+ lines.forEach((line, lineIndex) => {+ const regex = new RegExp(`(${term})`, "gi");+ let match;+ while ((match = regex.exec(line)) !== null) {+ allResults.push({+ chunkIndex,+ lineIndex,+ startIndex: match?.index,+ matchLength: match[0]?.length,+ });+ }+ });+ });+ setHighlightedChunks(allResults);+ setCurrentIndex(0);+ }, 300), // Debounce delay in milliseconds+ [chunks]+ );++ useEffect(() => {+ handleSearch(searchTerm);+ }, [searchTerm]);++ useEffect(() => {+ if (activeRef.current) {+ activeRef.current.scrollIntoView({+ behavior: "smooth",+ block: "center",+ });+ }+ }, [currentIndex]);+const handleClose = () => {setIsIndexOpen(false);+ setSearchTerm("");+ setHighlightedChunks([]);+ setCurrentIndex(0);};- const lines = chunkData?.split("\\n"); // Split text into lines and remove any empty lines+ const handleSearchChange = (e) => {+ setSearchTerm(e.target.value);+ };- const renderContent = (chunk) => {- if (!chunk) {- return <p>No chunks founds</p>;- }- return (- <>- {chunk?.map((line) => (- <div key={uniqueId()}>- {line}- <br />- </div>- ))}- </>+ const handleNext = () => {+ setCurrentIndex((prev) => (prev + 1) % highlightedChunks?.length);+ };++ const handlePrev = () => {+ setCurrentIndex((prev) =>+ prev === 0 ? highlightedChunks?.length - 1 : prev - 1);};+ const renderHighlightedLine = (line, lineIndex, chunkIndex) => {+ if (!searchTerm) return line;++ const matchesInLine = highlightedChunks.filter(+ (chunk) =>+ chunk.lineIndex === lineIndex && chunk.chunkIndex === chunkIndex+ );++ if (!matchesInLine?.length) return line;++ const parts = [];+ let lastIndex = 0;++ matchesInLine.forEach((match, idx) => {+ if (lastIndex < match.startIndex) {+ parts.push(line.substring(lastIndex, match.startIndex));+ }++ const isActive =+ currentIndex ===+ highlightedChunks.findIndex(+ (h) =>+ h.chunkIndex === chunkIndex &&+ h.lineIndex === lineIndex &&+ h.startIndex === match.startIndex+ );++ parts.push(+ <span+ key={uniqueId()}+ className={`chunk-highlight ${+ isActive ? "active-chunk-highlight" : ""+ }`}+ ref={isActive ? activeRef : null}+ >+ {line.substring(+ match.startIndex,+ match.startIndex + match.matchLength+ )}+ </span>+ );++ lastIndex = match.startIndex + match.matchLength;+ });++ if (lastIndex < line.length) {+ parts.push(line.substring(lastIndex));+ }++ return parts;+ };+return (<Modaltitle="Index Data"@@ -38,8 +156,52 @@ function OutputForIndex({ chunkData, setIsIndexOpen, isIndexOpen }) {footer={null}width={1000}>+ <div className="chunk-search-container">+ <Input+ placeholder="Search..."+ value={searchTerm}+ onChange={handleSearchChange}+ className="chunk-search-input"+ />+ <div className="search-control-container">+ <Button+ size="small"+ onClick={handlePrev}+ disabled={highlightedChunks.length === 0}+ >+ <ArrowUpOutlined />+ </Button>+ <span className="page-count-container">+ {highlightedChunks.length > 0 ? currentIndex + 1 : 0}/{" "}+ {highlightedChunks.length}+ </span>+ <Button+ size="small"+ onClick={handleNext}+ disabled={highlightedChunks.length === 0}+ >+ <ArrowDownOutlined />+ </Button>+ </div>+ </div><div className="index-output-tab">- <TextViewerPre text={renderContent(lines)} />+ {chunks?.map((chunk, chunkIndex) => (+ <div key={uniqueId()} className="chunk-container">+ <Typography.Text strong>Chunk {chunkIndex + 1}</Typography.Text>+ <TextViewerPre+ text={+ <>+ {chunk?.split("\\n")?.map((line, lineIndex) => (+ <div key={uniqueId()}>+ {renderHighlightedLine(line, lineIndex, chunkIndex)}+ <br />+ </div>+ ))}+ </>+ }+ />+ </div>+ ))}</div></Modal>);diff --git a/frontend/src/components/custom-tools/prompt-card/PromptCard.css b/frontend/src/components/custom-tools/prompt-card/PromptCard.cssindex 2203e2200..a97e1b7e2 100644--- a/frontend/src/components/custom-tools/prompt-card/PromptCard.css+++ b/frontend/src/components/custom-tools/prompt-card/PromptCard.css@@ -218,10 +218,45 @@}.prompt-output-llm-bg {- background-color: #DDDDDD;+ background-color: #dddddd;padding: 10px;}.prompt-output-title {font-size: 18px;}++/* PromptCard.css */+.chunk-highlight {+ background-color: yellow;+}++.active-chunk-highlight {+ background-color: orange;+ border: 1px solid red;+}++.chunk-container {+ margin-bottom: 20px;+ padding: 10px;+ border: 1px solid #ddd;+ border-radius: 4px;+ background-color: #f9f9f9;+}++.chunk-search-container {+ display: flex;+ gap: 10px;+ margin-bottom: 10px;+}++.search-control-container {+ min-width: 120px;+ align-self: center;+}++.page-count-container {+ background-color: #f9f9f9;+ border-radius: 10px;+ padding: 5px 10px;+}diff --git a/frontend/src/components/custom-tools/prompt-card/PromptOutputExpandBtn.jsx b/frontend/src/components/custom-tools/prompt-card/PromptOutputExpandBtn.jsxindex f7c219c41..a62d01267 100644--- a/frontend/src/components/custom-tools/prompt-card/PromptOutputExpandBtn.jsx+++ b/frontend/src/components/custom-tools/prompt-card/PromptOutputExpandBtn.jsx@@ -1,9 +1,10 @@import { ArrowsAltOutlined } from "@ant-design/icons";import { Button, Tooltip } from "antd";import PropTypes from "prop-types";-import { PromptOutputsModal } from "./PromptOutputsModal";import { useState } from "react";+import { PromptOutputsModal } from "./PromptOutputsModal";+function PromptOutputExpandBtn({llmProfiles,result,