Skip to content

Commit 92a5b94

Browse files
authored
Merge pull request #10 from Zipstack/fix/support-for-highlight-api
Added support Highlight API
2 parents 6f4412c + 16b4645 commit 92a5b94

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed

index.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,10 @@ class LLMWhispererClientV2 {
435435
* @param {string} [options.useWebhook=''] - Whether to use a webhook.
436436
* @param {boolean} [options.waitForCompletion=false] - Whether to wait for completion.
437437
* @param {number} [options.waitTimeout=180] - The timeout for waiting.
438+
* @param {boolean} [options.addLineNos=false] - If true, adds line numbers to the extracted text
439+
* and saves line metadata, which can be queried later
440+
* using the highlights API.
441+
438442
* @returns {Promise<Object>} The response from the whisper API.
439443
* @throws {LLMWhispererClientException} If there is an error in the request.
440444
*/
@@ -459,6 +463,7 @@ class LLMWhispererClientV2 {
459463
useWebhook = "",
460464
waitForCompletion = false,
461465
waitTimeout = 180,
466+
addLineNos = false,
462467
} = {}) {
463468
this.logger.debug("whisper called");
464469
const apiUrl = `${this.baseUrl}/whisper`;
@@ -482,6 +487,7 @@ class LLMWhispererClientV2 {
482487
use_webhook: useWebhook,
483488
wait_for_completion: waitForCompletion,
484489
wait_timeout: waitTimeout,
490+
add_line_nos: addLineNos,
485491
};
486492

487493
this.logger.debug(`api_url: ${apiUrl}`);
@@ -741,6 +747,55 @@ class LLMWhispererClientV2 {
741747
};
742748
}
743749
}
750+
751+
/**
752+
* Retrieves the highlight information of the LLMWhisperer API.
753+
*
754+
* This method sends a GET request to the '/highlights' endpoint of the LLMWhisperer API.
755+
* The response is a JSON object containing the usage information.
756+
* Refer to https://docs.unstract.com/llm_whisperer/apis/llm_whisperer_usage_api
757+
*
758+
* @param {string} whisperHash - The hash of the whisper operation.
759+
* @param {string} lines - Define which lines metadata to retrieve.
760+
* Example "1-5,7,21-" retrieves lines 1,2,3,4,5,7,21,22,23,...
761+
* @param {boolean} [extractAllLines=false] - If true, extract all lines.
762+
* @returns {Promise<Object>} A promise that resolves with the highlight information.
763+
* @throws {LLMWhispererClientException} If the API request fails.
764+
*/
765+
async getHighlightData(whisperHash, lines, extractAllLines = false) {
766+
this.logger.debug("highlight called");
767+
const url = `${this.baseUrl}/highlights`;
768+
769+
// Build query parameters
770+
const params = {
771+
whisper_hash: whisperHash,
772+
lines: lines,
773+
extract_all_lines: extractAllLines,
774+
};
775+
776+
try {
777+
const response = await axios(url, {
778+
method: "GET",
779+
headers: this.headers,
780+
params: params,
781+
});
782+
783+
if (response.status != 200) {
784+
// Parse error response and throw a custom exception
785+
const errorData = await response.data;
786+
errorData.status_code = response.status;
787+
throw new LLMWhispererClientException(errorData);
788+
}
789+
790+
return response.data;
791+
} catch (error) {
792+
const err = error.response
793+
? error.response.data
794+
: { message: error.message };
795+
err.statusCode = error.response ? error.response.status : -1;
796+
throw new LLMWhispererClientException(err.message, err.statusCode);
797+
}
798+
}
744799
}
745800

746801
module.exports = {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "llmwhisperer-client",
3-
"version": "2.1.0",
3+
"version": "2.2.0",
44
"description": "LLMWhisper JS Client",
55
"main": "index.js",
66
"scripts": {

test/test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,48 @@ describe("LLMWhispererClientV2", () => {
6363
},
6464
200000,
6565
);
66+
67+
test("highlight", async () => {
68+
const dataDir = path.join(__dirname, "data");
69+
const inputFile = "credit_card.pdf";
70+
const filePath = path.join(dataDir, inputFile);
71+
72+
// Call whisper API with line numbers enabled
73+
const whisperResult = await client.whisper({
74+
addLineNos: true,
75+
filePath: filePath,
76+
waitForCompletion: true,
77+
});
78+
79+
const whisperHash = whisperResult.whisper_hash;
80+
81+
// Fetch highlight data for lines 1-2
82+
const highlightData = await client.getHighlightData(whisperHash, "1-2");
83+
84+
// Validate the response structure
85+
expect(typeof highlightData).toBe("object");
86+
expect(Object.keys(highlightData).length).toBe(2);
87+
expect(highlightData).toHaveProperty("1");
88+
expect(highlightData).toHaveProperty("2");
89+
90+
// Validate line 1 data
91+
const line1 = highlightData["1"];
92+
expect(line1.base_y).toBe(0);
93+
expect(line1.base_y_percent).toBe(0);
94+
expect(line1.height).toBe(0);
95+
expect(line1.height_percent).toBe(0);
96+
expect(line1.page).toBe(0);
97+
expect(line1.page_height).toBe(0);
98+
expect(line1.raw).toEqual([0, 0, 0, 0]);
99+
100+
// Validate line 2 data
101+
const line2 = highlightData["2"];
102+
expect(line2.base_y).toBe(155);
103+
expect(line2.base_y_percent).toBeCloseTo(4.8927, 4); // Approximate float comparison
104+
expect(line2.height).toBe(51);
105+
expect(line2.height_percent).toBeCloseTo(1.6098, 4); // Approximate float comparison
106+
expect(line2.page).toBe(0);
107+
expect(line2.page_height).toBe(3168);
108+
}, 20000); // 20-second timeout
66109
});
67110

0 commit comments

Comments
 (0)