-
Notifications
You must be signed in to change notification settings - Fork 0
/
chroma-db.js
90 lines (77 loc) · 2.31 KB
/
chroma-db.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
module.exports = function (RED) {
function ChromaDBNode(config) {
RED.nodes.createNode(this, config);
var node = this;
const { ChromaClient } = require("chromadb");
const chroma = new ChromaClient({
path: config.dbIp + ":" + config.dbPort,
});
async function execute(msg) {
switch (config.operation) {
case "list":
msg.payload = await chroma.listCollections();
break;
case "create":
var collection = await chroma.getOrCreateCollection({
name: config.dbName,
metadata: {
"hnsw:space": config.distance,
},
});
msg.result = "Collection successfully created";
break;
case "insert":
var collection = await chroma.getCollection({
name: config.dbName,
});
const embeddings = Array.from(msg.payload);
const count = (await collection.count()) + 1;
var len = 1;
if (Array.isArray(embeddings[0])) {
len = embeddings.length;
}
var ids = [];
for (var i = 0; i < len; i++) {
ids.push("id-" + (count + i));
}
await collection.add({
ids: ids,
embeddings: embeddings,
});
msg.result = ids;
break;
case "query":
var collection = await chroma.getCollection({
name: config.dbName,
});
msg.payload = await collection.query({
queryEmbeddings: Array.from(msg.payload),
nResults: config.nResults,
});
break;
case "delete":
var collection = await chroma.getCollection({
name: config.dbName,
});
msg.payload = await collection.delete({
ids: Array.from(msg.payload),
});
break;
case "drop":
msg.payload = await chroma.deleteCollection({
name: config.dbName,
});
msg.result = "Collection successfully droped";
break;
}
}
node.on("input", function (msg) {
execute(msg)
.catch((error) => console.error(error))
.then(() => {
node.send(msg);
});
});
}
RED.nodes.registerType("good-chroma-db", ChromaDBNode);
};