|
11 | 11 | * |
12 | 12 |
|
13 | 13 | */ |
14 | | - |
| 14 | +require("dotenv").config(); |
15 | 15 | const axios = require("axios"); |
16 | 16 | const winston = require("winston"); |
17 | 17 | const fs = require("fs"); |
18 | 18 | const { register } = require("module"); |
19 | | - |
20 | 19 | const BASE_URL = "https://llmwhisperer-api.unstract.com/v1"; |
21 | 20 | const BASE_URL_V2 = "https://llmwhisperer-api.us-central.unstract.com/api/v2"; |
22 | 21 |
|
@@ -374,8 +373,12 @@ class LLMWhispererClientV2 { |
374 | 373 |
|
375 | 374 | this.headers = { |
376 | 375 | "unstract-key": this.apiKey, |
377 | | - "Subscription-Id": "test", //TODO: Remove this line. For testing only |
378 | | - "Start-Date": "9-07-2024", //TODO: Remove this line. For testing only |
| 376 | + // "Subscription-Id": "jsclient-client", |
| 377 | + // "Subscription-Name": "jsclient-client", |
| 378 | + // "User-Id": "jsclient-client-user", |
| 379 | + // "Product-Id": "jsclient-client-product", |
| 380 | + // "Product-Name": "jsclient-client-product", |
| 381 | + // "Start-Date": "2024-07-09", |
379 | 382 | }; |
380 | 383 | } |
381 | 384 |
|
@@ -532,34 +535,30 @@ class LLMWhispererClientV2 { |
532 | 535 | message["extraction"] = {}; |
533 | 536 | message["status_code"] = -1; |
534 | 537 | message["message"] = "Whisper client operation timed out"; |
535 | | - break; |
| 538 | + return message; |
536 | 539 | } |
537 | 540 | const whisperStatus = await this.whisperStatus(whisperHash); |
| 541 | + this.logger.debug(`whisperStatus: ${JSON.stringify(whisperStatus)}`); |
| 542 | + |
538 | 543 | if (whisperStatus.statusCode !== 200) { |
539 | 544 | message["extraction"] = {}; |
540 | 545 | message["status_code"] = whisperStatus.statusCode; |
541 | 546 | message["message"] = "Whisper client operation failed"; |
542 | | - break; |
| 547 | + return message; |
543 | 548 | } |
544 | | - if (whisperStatus.status === "processing") { |
545 | | - this.logger.debug("Status: processing"); |
546 | | - } else if (whisperStatus.status === "delivered") { |
547 | | - this.logger.debug("Status: delivered"); |
548 | | - throw new LLMWhispererClientException( |
549 | | - "Whisper operation already delivered", |
550 | | - -1, |
| 549 | + if (whisperStatus.status === "accepted") { |
| 550 | + this.logger.debug("Status: accepted..."); |
| 551 | + } else if (whisperStatus.status === "processing") { |
| 552 | + this.logger.debug("Status: processing..."); |
| 553 | + } else if (whisperStatus.status === "error") { |
| 554 | + this.logger.debug("Status: error"); |
| 555 | + this.logger.error( |
| 556 | + "Whisper-hash: ${whisperHash} | STATUS: failed with ${whisperStatus.message}", |
551 | 557 | ); |
552 | | - } else if (whisperStatus.status === "unknown") { |
553 | | - this.logger.debug("Status: unknown"); |
554 | | - throw new LLMWhispererClientException( |
555 | | - "Whisper operation status unknown", |
556 | | - -1, |
557 | | - ); |
558 | | - } else if (whisperStatus.status === "failed") { |
559 | | - this.logger.debug("Status: failed"); |
560 | 558 | message["extraction"] = {}; |
561 | 559 | message["status_code"] = -1; |
562 | | - message["message"] = "Whisper client operation failed"; |
| 560 | + message["status"] = "error"; |
| 561 | + message["message"] = whisperStatus.message; |
563 | 562 | break; |
564 | 563 | } else if (whisperStatus.status === "processed") { |
565 | 564 | this.logger.debug("Status: processed"); |
@@ -602,25 +601,30 @@ class LLMWhispererClientV2 { |
602 | 601 | * @throws {LLMWhispererClientException} Throws an LLMWhispererClientException if an error occurs during the operation. |
603 | 602 | */ |
604 | 603 | async whisperStatus(whisperHash) { |
605 | | - this.logger.debug("whisper_status called"); |
| 604 | + this.logger.debug(`whisper_status called for ${whisperHash}`); |
606 | 605 | const url = `${this.baseUrl}/whisper-status`; |
607 | 606 | const params = { whisper_hash: whisperHash }; |
608 | 607 | this.logger.debug(`url: ${url}`); |
| 608 | + this.logger.debug(`params: ${JSON.stringify(params)}`); |
| 609 | + delete this.headers["Content-Length"]; |
| 610 | + this.logger.debug(`headers: ${JSON.stringify(this.headers)}`); |
609 | 611 |
|
610 | 612 | try { |
611 | 613 | const response = await axios.get(url, { |
612 | 614 | headers: this.headers, |
613 | 615 | params, |
614 | 616 | timeout: this.apiTimeout * 1000, |
615 | 617 | }); |
616 | | - |
617 | 618 | const message = response.data; |
618 | 619 | message.statusCode = response.status; |
619 | 620 | return message; |
620 | 621 | } catch (error) { |
| 622 | + this.logger.debug("Hel00000000002"); |
| 623 | + this.logger.debug(`error: ${JSON.stringify(error)}`); |
621 | 624 | const err = error.response |
622 | 625 | ? error.response.data |
623 | 626 | : { message: error.message }; |
| 627 | + this.logger.debug(`error: ${JSON.stringify(err)}`); |
624 | 628 | err.statusCode = error.response ? error.response.status : -1; |
625 | 629 | throw new LLMWhispererClientException(err.message, err.statusCode); |
626 | 630 | } |
|
0 commit comments