Skip to content

Commit b1823e4

Browse files
authored
Migrate Zilliz to class (#4729)
migrate zilliz to class
1 parent eebb393 commit b1823e4

File tree

2 files changed

+57
-38
lines changed

2 files changed

+57
-38
lines changed

server/utils/helpers/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ function getVectorDbClass(getExactly = null) {
107107
return Milvus;
108108
case "zilliz":
109109
const { Zilliz } = require("../vectorDbProviders/zilliz");
110-
return Zilliz;
110+
return new Zilliz();
111111
case "astra":
112112
const { AstraDB } = require("../vectorDbProviders/astra");
113113
return new AstraDB();

server/utils/vectorDbProviders/zilliz/index.js

Lines changed: 56 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,23 @@ const { sourceIdentifier } = require("../../chats");
1313

1414
// Zilliz is basically a copy of Milvus DB class with a different constructor
1515
// to connect to the cloud
16-
const Zilliz = {
17-
name: "Zilliz",
16+
class Zilliz {
17+
constructor() {
18+
this.name = "Zilliz";
19+
}
20+
1821
// Milvus/Zilliz only allows letters, numbers, and underscores in collection names
1922
// so we need to enforce that by re-normalizing the names when communicating with
2023
// the DB.
2124
// If the first char of the collection is not an underscore or letter the collection name will be invalid.
22-
normalize: function (inputString) {
25+
normalize(inputString) {
2326
let normalized = inputString.replace(/[^a-zA-Z0-9_]/g, "_");
2427
if (new RegExp(/^[a-zA-Z_]/).test(normalized.slice(0, 1)))
2528
normalized = `anythingllm_${normalized}`;
2629
return normalized;
27-
},
28-
connect: async function () {
30+
}
31+
32+
async connect() {
2933
if (process.env.VECTOR_DB !== "zilliz")
3034
throw new Error("Zilliz::Invalid ENV settings");
3135

@@ -41,12 +45,14 @@ const Zilliz = {
4145
);
4246

4347
return { client };
44-
},
45-
heartbeat: async function () {
48+
}
49+
50+
async heartbeat() {
4651
await this.connect();
4752
return { heartbeat: Number(new Date()) };
48-
},
49-
totalVectors: async function () {
53+
}
54+
55+
async totalVectors() {
5056
const { client } = await this.connect();
5157
const { collection_names } = await client.listCollections();
5258
const total = collection_names.reduce(async (acc, collection_name) => {
@@ -56,27 +62,31 @@ const Zilliz = {
5662
return Number(acc) + Number(statistics?.data?.row_count ?? 0);
5763
}, 0);
5864
return total;
59-
},
60-
namespaceCount: async function (_namespace = null) {
65+
}
66+
67+
async namespaceCount(_namespace = null) {
6168
const { client } = await this.connect();
6269
const statistics = await client.getCollectionStatistics({
6370
collection_name: this.normalize(_namespace),
6471
});
6572
return Number(statistics?.data?.row_count ?? 0);
66-
},
67-
namespace: async function (client, namespace = null) {
73+
}
74+
75+
async namespace(client, namespace = null) {
6876
if (!namespace) throw new Error("No namespace value provided.");
6977
const collection = await client
7078
.getCollectionStatistics({ collection_name: this.normalize(namespace) })
7179
.catch(() => null);
7280
return collection;
73-
},
74-
hasNamespace: async function (namespace = null) {
81+
}
82+
83+
async hasNamespace(namespace = null) {
7584
if (!namespace) return false;
7685
const { client } = await this.connect();
7786
return await this.namespaceExists(client, namespace);
78-
},
79-
namespaceExists: async function (client, namespace = null) {
87+
}
88+
89+
async namespaceExists(client, namespace = null) {
8090
if (!namespace) throw new Error("No namespace value provided.");
8191
const { value } = await client
8292
.hasCollection({ collection_name: this.normalize(namespace) })
@@ -85,15 +95,17 @@ const Zilliz = {
8595
return { value: false };
8696
});
8797
return value;
88-
},
89-
deleteVectorsInNamespace: async function (client, namespace = null) {
98+
}
99+
100+
async deleteVectorsInNamespace(client, namespace = null) {
90101
await client.dropCollection({ collection_name: this.normalize(namespace) });
91102
return true;
92-
},
103+
}
104+
93105
// Zilliz requires a dimension aspect for collection creation
94106
// we pass this in from the first chunk to infer the dimensions like other
95107
// providers do.
96-
getOrCreateCollection: async function (client, namespace, dimensions = null) {
108+
async getOrCreateCollection(client, namespace, dimensions = null) {
97109
const isExists = await this.namespaceExists(client, namespace);
98110
if (!isExists) {
99111
if (!dimensions)
@@ -134,8 +146,9 @@ const Zilliz = {
134146
collection_name: this.normalize(namespace),
135147
});
136148
}
137-
},
138-
addDocumentToNamespace: async function (
149+
}
150+
151+
async addDocumentToNamespace(
139152
namespace,
140153
documentData = {},
141154
fullFilePath = null,
@@ -261,8 +274,9 @@ const Zilliz = {
261274
console.error("addDocumentToNamespace", e.message);
262275
return { vectorized: false, error: e.message };
263276
}
264-
},
265-
deleteDocumentFromNamespace: async function (namespace, docId) {
277+
}
278+
279+
async deleteDocumentFromNamespace(namespace, docId) {
266280
const { DocumentVectors } = require("../../../models/vectors");
267281
const { client } = await this.connect();
268282
if (!(await this.namespaceExists(client, namespace))) return;
@@ -284,8 +298,9 @@ const Zilliz = {
284298
// on a later call.
285299
await client.flushSync({ collection_names: [this.normalize(namespace)] });
286300
return true;
287-
},
288-
performSimilaritySearch: async function ({
301+
}
302+
303+
async performSimilaritySearch({
289304
namespace = null,
290305
input = "",
291306
LLMConnector = null,
@@ -323,8 +338,9 @@ const Zilliz = {
323338
sources: this.curateSources(sources),
324339
message: false,
325340
};
326-
},
327-
similarityResponse: async function ({
341+
}
342+
343+
async similarityResponse({
328344
client,
329345
namespace,
330346
queryVector,
@@ -358,8 +374,9 @@ const Zilliz = {
358374
result.scores.push(match.score);
359375
});
360376
return result;
361-
},
362-
"namespace-stats": async function (reqBody = {}) {
377+
}
378+
379+
async "namespace-stats"(reqBody = {}) {
363380
const { namespace = null } = reqBody;
364381
if (!namespace) throw new Error("namespace required");
365382
const { client } = await this.connect();
@@ -369,8 +386,9 @@ const Zilliz = {
369386
return stats
370387
? stats
371388
: { message: "No stats were able to be fetched from DB for namespace" };
372-
},
373-
"delete-namespace": async function (reqBody = {}) {
389+
}
390+
391+
async "delete-namespace"(reqBody = {}) {
374392
const { namespace = null } = reqBody;
375393
const { client } = await this.connect();
376394
if (!(await this.namespaceExists(client, namespace)))
@@ -382,8 +400,9 @@ const Zilliz = {
382400
return {
383401
message: `Namespace ${namespace} was deleted along with ${vectorCount} vectors.`,
384402
};
385-
},
386-
curateSources: function (sources = []) {
403+
}
404+
405+
curateSources(sources = []) {
387406
const documents = [];
388407
for (const source of sources) {
389408
const { metadata = {} } = source;
@@ -396,7 +415,7 @@ const Zilliz = {
396415
}
397416

398417
return documents;
399-
},
400-
};
418+
}
419+
}
401420

402421
module.exports.Zilliz = Zilliz;

0 commit comments

Comments
 (0)