From 62b874e14fb7c5609639b22bc172bc95dea036dc Mon Sep 17 00:00:00 2001
From: Alex Yang <himself65@outlook.com>
Date: Thu, 1 Aug 2024 14:05:19 -0700
Subject: [PATCH] fix: enforce `no-base-to-string` (#1097)

---
 .eslintrc.cjs                                          |  7 ++++++-
 .../experimental/src/engines/query/JSONQueryEngine.ts  | 10 +++++-----
 packages/llamaindex/src/embeddings/JinaAIEmbedding.ts  |  2 +-
 .../llamaindex/src/engines/query/RouterQueryEngine.ts  |  2 +-
 .../src/engines/query/SubQuestionQueryEngine.ts        |  6 +++---
 packages/llamaindex/src/evaluation/Relevancy.ts        |  2 +-
 packages/llamaindex/src/readers/JSONReader.ts          |  2 +-
 7 files changed, 18 insertions(+), 13 deletions(-)

diff --git a/.eslintrc.cjs b/.eslintrc.cjs
index 6d5bdf4cf..e30126da7 100644
--- a/.eslintrc.cjs
+++ b/.eslintrc.cjs
@@ -31,7 +31,12 @@ module.exports = {
     "@typescript-eslint/ban-types": "off",
     "no-array-constructor": "off",
     "@typescript-eslint/no-array-constructor": "off",
-    "@typescript-eslint/no-base-to-string": "off",
+    "@typescript-eslint/no-base-to-string": [
+      "error",
+      {
+        ignoredTypeNames: ["Error", "RegExp", "URL", "URLSearchParams"],
+      },
+    ],
     "@typescript-eslint/no-duplicate-enum-values": "off",
     "@typescript-eslint/no-duplicate-type-constituents": "off",
     "@typescript-eslint/no-explicit-any": "off",
diff --git a/packages/experimental/src/engines/query/JSONQueryEngine.ts b/packages/experimental/src/engines/query/JSONQueryEngine.ts
index 4f5e469e5..f6a73cd8e 100644
--- a/packages/experimental/src/engines/query/JSONQueryEngine.ts
+++ b/packages/experimental/src/engines/query/JSONQueryEngine.ts
@@ -162,18 +162,18 @@ export class JSONQueryEngine implements QueryEngine {
 
     const schema = this.getSchemaContext();
 
-    const jsonPathResponseStr = await this.serviceContext.llm.complete({
+    const { text: jsonPathResponse } = await this.serviceContext.llm.complete({
       prompt: this.jsonPathPrompt({ query, schema }),
     });
 
     if (this.verbose) {
       console.log(
-        `> JSONPath Instructions:\n\`\`\`\n${jsonPathResponseStr}\n\`\`\`\n`,
+        `> JSONPath Instructions:\n\`\`\`\n${jsonPathResponse}\n\`\`\`\n`,
       );
     }
 
     const jsonPathOutput = await this.outputProcessor({
-      llmOutput: jsonPathResponseStr.text,
+      llmOutput: jsonPathResponse,
       jsonValue: this.jsonValue,
     });
 
@@ -188,7 +188,7 @@ export class JSONQueryEngine implements QueryEngine {
         prompt: this.responseSynthesisPrompt({
           query,
           jsonSchema: schema,
-          jsonPath: jsonPathResponseStr.text,
+          jsonPath: jsonPathResponse,
           jsonPathValue: JSON.stringify(jsonPathOutput),
         }),
       });
@@ -199,7 +199,7 @@ export class JSONQueryEngine implements QueryEngine {
     }
 
     const responseMetadata = {
-      jsonPathResponseStr,
+      jsonPathResponse,
     };
 
     const response = EngineResponse.fromResponse(responseStr, false);
diff --git a/packages/llamaindex/src/embeddings/JinaAIEmbedding.ts b/packages/llamaindex/src/embeddings/JinaAIEmbedding.ts
index e1bc39972..c15447787 100644
--- a/packages/llamaindex/src/embeddings/JinaAIEmbedding.ts
+++ b/packages/llamaindex/src/embeddings/JinaAIEmbedding.ts
@@ -79,7 +79,7 @@ export class JinaAIEmbedding extends MultiModalEmbedding {
   private async getImageInput(
     image: ImageType,
   ): Promise<{ bytes: string } | { url: string }> {
-    if (isLocal(image)) {
+    if (isLocal(image) || image instanceof Blob) {
       const base64 = await imageToDataUrl(image);
       const bytes = base64.split(",")[1];
       return { bytes };
diff --git a/packages/llamaindex/src/engines/query/RouterQueryEngine.ts b/packages/llamaindex/src/engines/query/RouterQueryEngine.ts
index 6378734f6..8c709b4f3 100644
--- a/packages/llamaindex/src/engines/query/RouterQueryEngine.ts
+++ b/packages/llamaindex/src/engines/query/RouterQueryEngine.ts
@@ -133,7 +133,7 @@ export class RouterQueryEngine extends PromptMixin implements QueryEngine {
       const responses: EngineResponse[] = [];
       for (let i = 0; i < result.selections.length; i++) {
         const engineInd = result.selections[i];
-        const logStr = `Selecting query engine ${engineInd}: ${result.selections[i]}.`;
+        const logStr = `Selecting query engine ${engineInd.index}: ${result.selections[i].index}.`;
 
         if (this.verbose) {
           console.log(logStr + "\n");
diff --git a/packages/llamaindex/src/engines/query/SubQuestionQueryEngine.ts b/packages/llamaindex/src/engines/query/SubQuestionQueryEngine.ts
index 82685eaaa..79ad860b4 100644
--- a/packages/llamaindex/src/engines/query/SubQuestionQueryEngine.ts
+++ b/packages/llamaindex/src/engines/query/SubQuestionQueryEngine.ts
@@ -119,15 +119,15 @@ export class SubQuestionQueryEngine
         return null;
       }
 
-      const responseText = await queryEngine?.call?.({
+      const responseValue = await queryEngine?.call?.({
         query: question,
       });
 
-      if (!responseText) {
+      if (responseValue == null) {
         return null;
       }
 
-      const nodeText = `Sub question: ${question}\nResponse: ${responseText}`;
+      const nodeText = `Sub question: ${question}\nResponse: ${typeof responseValue === "string" ? responseValue : JSON.stringify(responseValue)}`;
       const node = new TextNode({ text: nodeText });
       return { node, score: 0 };
     } catch (error) {
diff --git a/packages/llamaindex/src/evaluation/Relevancy.ts b/packages/llamaindex/src/evaluation/Relevancy.ts
index f8b161306..e738b328e 100644
--- a/packages/llamaindex/src/evaluation/Relevancy.ts
+++ b/packages/llamaindex/src/evaluation/Relevancy.ts
@@ -78,7 +78,7 @@ export class RelevancyEvaluator extends PromptMixin implements BaseEvaluator {
       serviceContext: this.serviceContext,
     });
 
-    const queryResponse = `Question: ${query}\nResponse: ${response}`;
+    const queryResponse = `Question: ${extractText(query)}\nResponse: ${response}`;
 
     const queryEngine = index.asQueryEngine();
 
diff --git a/packages/llamaindex/src/readers/JSONReader.ts b/packages/llamaindex/src/readers/JSONReader.ts
index fb0333a4a..13c54cd2b 100644
--- a/packages/llamaindex/src/readers/JSONReader.ts
+++ b/packages/llamaindex/src/readers/JSONReader.ts
@@ -185,7 +185,7 @@ export class JSONReader<T extends JSONValue> extends FileReader {
       return jsonStr;
     } catch (e) {
       throw new JSONStringifyError(
-        `Error stringifying JSON: ${e} in "${data}"`,
+        `Error stringifying JSON: ${e} in "${JSON.stringify(data)}"`,
       );
     }
   }
-- 
GitLab