From 7bace207698e32d5a656d36366d210fe9cd8b6f2 Mon Sep 17 00:00:00 2001
From: timothycarambat <rambat1010@gmail.com>
Date: Wed, 22 May 2024 13:42:48 -0500
Subject: [PATCH] Improve VoyageAI error responses and textChunk handler
 resolves #1491

---
 server/utils/EmbeddingEngines/voyageAi/index.js | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/server/utils/EmbeddingEngines/voyageAi/index.js b/server/utils/EmbeddingEngines/voyageAi/index.js
index fe2a39643..65126613b 100644
--- a/server/utils/EmbeddingEngines/voyageAi/index.js
+++ b/server/utils/EmbeddingEngines/voyageAi/index.js
@@ -38,7 +38,10 @@ class VoyageAiEmbedder {
       Array.isArray(textInput) ? textInput : [textInput],
       { modelName: this.model }
     );
-    return result || [];
+
+    // If given an array return the native Array[Array] format since that should be the outcome.
+    // But if given a single string, we need to flatten it so that we have a 1D array.
+    return (Array.isArray(textInput) ? result : result.flat()) || [];
   }
 
   async embedChunks(textChunks = []) {
@@ -50,6 +53,12 @@ class VoyageAiEmbedder {
       return embeddings;
     } catch (error) {
       console.error("Voyage AI Failed to embed:", error);
+      if (
+        error.message.includes(
+          "Cannot read properties of undefined (reading '0')"
+        )
+      )
+        throw new Error("Voyage AI failed to embed: Rate limit reached");
       throw error;
     }
   }