-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvectorDB.js
35 lines (31 loc) · 1.01 KB
/
vectorDB.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
import weaviate from 'weaviate-ts-client';
import crypto from 'crypto';
import dotenv from 'dotenv';
dotenv.config()
const client = weaviate.client({
scheme: 'https',
host: process.env.WEAVIATE_HOSTNAME,
apiKey: new weaviate.ApiKey(process.env.WEAVIATE_API_KEY),
});
export async function upsertVector(collectionName, vector, text) {
return await client.data.creator()
.withClassName(collectionName)
.withId(crypto.createHash('md5').update(text).digest("hex"))
.withProperties({ text })
.withVector(vector)
.do()
.catch((e) => { console.log('Got An error, skipping'); console.error(e) })
}
export async function getClosestEmbeddings(collectionName, embedding, limit = 3) {
const res = await client.graphql
.get()
.withClassName(collectionName)
.withFields('text')
.withLimit(limit)
.withNearVector(
{
vector: embedding,
})
.do()
return res.data.Get[collectionName]
}