From 718062d0333dff1c8266183c24e755f424f9ccd7 Mon Sep 17 00:00:00 2001
From: timothycarambat <rambat1010@gmail.com>
Date: Wed, 3 Apr 2024 12:34:23 -0700
Subject: [PATCH] patch milvus/zilliz auto-generated collection name resolves
 #1027

---
 server/utils/vectorDbProviders/milvus/index.js | 6 +++++-
 server/utils/vectorDbProviders/zilliz/index.js | 6 +++++-
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/server/utils/vectorDbProviders/milvus/index.js b/server/utils/vectorDbProviders/milvus/index.js
index 3dac303fe..a304e8714 100644
--- a/server/utils/vectorDbProviders/milvus/index.js
+++ b/server/utils/vectorDbProviders/milvus/index.js
@@ -18,8 +18,12 @@ const Milvus = {
   // Milvus/Zilliz only allows letters, numbers, and underscores in collection names
   // so we need to enforce that by re-normalizing the names when communicating with
   // the DB.
+  // If the first char of the collection is not an underscore or letter the collection name will be invalid.
   normalize: function (inputString) {
-    return inputString.replace(/[^a-zA-Z0-9_]/g, "_");
+    let normalized = inputString.replace(/[^a-zA-Z0-9_]/g, "_");
+    if (new RegExp(/^[a-zA-Z_]/).test(normalized.slice(0, 1)))
+      normalized = `anythingllm_${normalized}`;
+    return normalized;
   },
   connect: async function () {
     if (process.env.VECTOR_DB !== "milvus")
diff --git a/server/utils/vectorDbProviders/zilliz/index.js b/server/utils/vectorDbProviders/zilliz/index.js
index 9b1d5a221..be0e9e7d4 100644
--- a/server/utils/vectorDbProviders/zilliz/index.js
+++ b/server/utils/vectorDbProviders/zilliz/index.js
@@ -20,8 +20,12 @@ const Zilliz = {
   // Milvus/Zilliz only allows letters, numbers, and underscores in collection names
   // so we need to enforce that by re-normalizing the names when communicating with
   // the DB.
+  // If the first char of the collection is not an underscore or letter the collection name will be invalid.
   normalize: function (inputString) {
-    return inputString.replace(/[^a-zA-Z0-9_]/g, "_");
+    let normalized = inputString.replace(/[^a-zA-Z0-9_]/g, "_");
+    if (new RegExp(/^[a-zA-Z_]/).test(normalized.slice(0, 1)))
+      normalized = `anythingllm_${normalized}`;
+    return normalized;
   },
   connect: async function () {
     if (process.env.VECTOR_DB !== "zilliz")
-- 
GitLab