Skip to content

Commit ceb415f

Browse files
authored
Migrate Astra to class (#4722)
migrate astra to class
1 parent b96988a commit ceb415f

File tree

2 files changed

+60
-40
lines changed

2 files changed

+60
-40
lines changed

server/utils/helpers/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ function getVectorDbClass(getExactly = null) {
110110
return Zilliz;
111111
case "astra":
112112
const { AstraDB } = require("../vectorDbProviders/astra");
113-
return AstraDB;
113+
return new AstraDB();
114114
case "pgvector":
115115
const { PGVector } = require("../vectorDbProviders/pgvector");
116116
return PGVector;

server/utils/vectorDbProviders/astra/index.js

Lines changed: 59 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@ const collectionExists = async function (client, namespace) {
2727
}
2828
};
2929

30-
const AstraDB = {
31-
name: "AstraDB",
32-
connect: async function () {
30+
class AstraDB {
31+
constructor() {
32+
this.name = "AstraDB";
33+
}
34+
35+
async connect() {
3336
if (process.env.VECTOR_DB !== "astra")
3437
throw new Error("AstraDB::Invalid ENV settings");
3538

@@ -38,21 +41,24 @@ const AstraDB = {
3841
process?.env?.ASTRA_DB_ENDPOINT
3942
);
4043
return { client };
41-
},
42-
heartbeat: async function () {
44+
}
45+
46+
async heartbeat() {
4347
return { heartbeat: Number(new Date()) };
44-
},
48+
}
49+
4550
// Astra interface will return a valid collection object even if the collection
4651
// does not actually exist. So we run a simple check which will always throw
4752
// when the table truly does not exist. Faster than iterating all collections.
48-
isRealCollection: async function (astraCollection = null) {
53+
async isRealCollection(astraCollection = null) {
4954
if (!astraCollection) return false;
5055
return await astraCollection
5156
.countDocuments()
5257
.then(() => true)
5358
.catch(() => false);
54-
},
55-
totalVectors: async function () {
59+
}
60+
61+
async totalVectors() {
5662
const { client } = await this.connect();
5763
const collectionNames = await this.allNamespaces(client);
5864
var totalVectors = 0;
@@ -62,13 +68,15 @@ const AstraDB = {
6268
totalVectors += count ? count : 0;
6369
}
6470
return totalVectors;
65-
},
66-
namespaceCount: async function (_namespace = null) {
71+
}
72+
73+
async namespaceCount(_namespace = null) {
6774
const { client } = await this.connect();
6875
const namespace = await this.namespace(client, _namespace);
6976
return namespace?.vectorCount || 0;
70-
},
71-
namespace: async function (client, namespace = null) {
77+
}
78+
79+
async namespace(client, namespace = null) {
7280
if (!namespace) throw new Error("No namespace value provided.");
7381
const sanitizedNamespace = sanitizeNamespace(namespace);
7482
const collection = await client
@@ -86,27 +94,31 @@ const AstraDB = {
8694
...collection,
8795
vectorCount: typeof count === "number" ? count : 0,
8896
};
89-
},
90-
hasNamespace: async function (namespace = null) {
97+
}
98+
99+
async hasNamespace(namespace = null) {
91100
if (!namespace) return false;
92101
const { client } = await this.connect();
93102
return await this.namespaceExists(client, namespace);
94-
},
95-
namespaceExists: async function (client, namespace = null) {
103+
}
104+
105+
async namespaceExists(client, namespace = null) {
96106
if (!namespace) throw new Error("No namespace value provided.");
97107
const sanitizedNamespace = sanitizeNamespace(namespace);
98108
const collection = await client.collection(sanitizedNamespace);
99109
return await this.isRealCollection(collection);
100-
},
101-
deleteVectorsInNamespace: async function (client, namespace = null) {
110+
}
111+
112+
async deleteVectorsInNamespace(client, namespace = null) {
102113
const sanitizedNamespace = sanitizeNamespace(namespace);
103114
await client.dropCollection(sanitizedNamespace);
104115
return true;
105-
},
116+
}
117+
106118
// AstraDB requires a dimension aspect for collection creation
107119
// we pass this in from the first chunk to infer the dimensions like other
108120
// providers do.
109-
getOrCreateCollection: async function (client, namespace, dimensions = null) {
121+
async getOrCreateCollection(client, namespace, dimensions = null) {
110122
const sanitizedNamespace = sanitizeNamespace(namespace);
111123
try {
112124
const exists = await collectionExists(client, sanitizedNamespace);
@@ -138,8 +150,9 @@ const AstraDB = {
138150
);
139151
throw error;
140152
}
141-
},
142-
addDocumentToNamespace: async function (
153+
}
154+
155+
async addDocumentToNamespace(
143156
namespace,
144157
documentData = {},
145158
fullFilePath = null,
@@ -269,8 +282,9 @@ const AstraDB = {
269282
console.error("addDocumentToNamespace", e.message);
270283
return { vectorized: false, error: e.message };
271284
}
272-
},
273-
deleteDocumentFromNamespace: async function (namespace, docId) {
285+
}
286+
287+
async deleteDocumentFromNamespace(namespace, docId) {
274288
const { DocumentVectors } = require("../../../models/vectors");
275289
const { client } = await this.connect();
276290
namespace = sanitizeNamespace(namespace);
@@ -293,8 +307,9 @@ const AstraDB = {
293307
const indexes = knownDocuments.map((doc) => doc.id);
294308
await DocumentVectors.deleteIds(indexes);
295309
return true;
296-
},
297-
performSimilaritySearch: async function ({
310+
}
311+
312+
async performSimilaritySearch({
298313
namespace = null,
299314
input = "",
300315
LLMConnector = null,
@@ -336,8 +351,9 @@ const AstraDB = {
336351
sources: this.curateSources(sources),
337352
message: false,
338353
};
339-
},
340-
similarityResponse: async function ({
354+
}
355+
356+
async similarityResponse({
341357
client,
342358
namespace,
343359
queryVector,
@@ -380,8 +396,9 @@ const AstraDB = {
380396
result.scores.push(response.$similarity);
381397
});
382398
return result;
383-
},
384-
allNamespaces: async function (client) {
399+
}
400+
401+
async allNamespaces(client) {
385402
try {
386403
let header = new Headers();
387404
header.append("Token", client?.httpClient?.applicationToken);
@@ -406,8 +423,9 @@ const AstraDB = {
406423
console.error("Astra::AllNamespace", e);
407424
return [];
408425
}
409-
},
410-
"namespace-stats": async function (reqBody = {}) {
426+
}
427+
428+
async "namespace-stats"(reqBody = {}) {
411429
const { namespace = null } = reqBody;
412430
if (!namespace) throw new Error("namespace required");
413431
const { client } = await this.connect();
@@ -417,8 +435,9 @@ const AstraDB = {
417435
return stats
418436
? stats
419437
: { message: "No stats were able to be fetched from DB for namespace" };
420-
},
421-
"delete-namespace": async function (reqBody = {}) {
438+
}
439+
440+
async "delete-namespace"(reqBody = {}) {
422441
const { namespace = null } = reqBody;
423442
const { client } = await this.connect();
424443
if (!(await this.namespaceExists(client, namespace)))
@@ -431,8 +450,9 @@ const AstraDB = {
431450
details?.vectorCount || "all"
432451
} vectors.`,
433452
};
434-
},
435-
curateSources: function (sources = []) {
453+
}
454+
455+
curateSources(sources = []) {
436456
const documents = [];
437457
for (const source of sources) {
438458
if (Object.keys(source).length > 0) {
@@ -446,7 +466,7 @@ const AstraDB = {
446466
}
447467

448468
return documents;
449-
},
450-
};
469+
}
470+
}
451471

452472
module.exports.AstraDB = AstraDB;

0 commit comments

Comments
 (0)