-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembedding.js
More file actions
47 lines (39 loc) · 1.13 KB
/
embedding.js
File metadata and controls
47 lines (39 loc) · 1.13 KB
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
const { ChromaClient } = require('chromadb');
let chroma = null;
const getRandomUUID = (count) => {
const ids = [];
for(let i=0; i<count; i++){
ids.push(crypto.randomUUID());
}
return ids;
}
const getChromaClient = () => {
if(!chroma){
chroma = new ChromaClient({
host: process.env.CHROMA_HOST,
port: process.env.CHROMA_PORT,
});
}
return chroma;
}
const getCollection = async (collectionName) => {
const chromaClient = getChromaClient();
const collection = await chromaClient.getOrCreateCollection({ name: collectionName });
return collection;
}
const addDocument = async (documents, collection) => {
const chromaClient = getChromaClient();
await collection.upsert({
documents,
ids: getRandomUUID(documents.length)
});
}
const getQuery = async (query, collectionName) => {
const collection = await getCollection(collectionName);
const response = await collection.query({
queryTexts: [query],
nResults: 5,
});
return response;
}
module.exports = { getChromaClient, getCollection, addDocument, getQuery };