-
Notifications
You must be signed in to change notification settings - Fork 1
/
vectorStore.ts
39 lines (32 loc) · 1.24 KB
/
vectorStore.ts
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
import { PGVectorStore } from '@langchain/community/vectorstores/pgvector';
import { PgVectorStoreConfig } from '@/lib/db/vectorStoreConfig';
import HuggingFaceEmbeddingSingleton from './embeddings/huggingfaceEmbeddings';
// import OllamaEmbeddingSingleton from './ollamaEmbeddings';
const getVectorStore = () =>
class VectorStoreSingleton {
static instance: PGVectorStore | null = null;
static async getInstance() {
if (this.instance === null) {
const embeddings = await HuggingFaceEmbeddingSingleton.getInstance();
// const embeddings = await OllamaEmbeddingSingleton.getInstance();
this.instance = await PGVectorStore.initialize(embeddings, PgVectorStoreConfig);
// Initialise cleanup on initial
process.on('beforeExit', () => {
this.instance?.end();
this.instance = null;
});
}
return this.instance;
}
};
export type TVectorStore = ReturnType<typeof getVectorStore>;
let VectorStore: TVectorStore;
if (process.env.NODE_ENV !== 'production') {
if (!global.VectorStoreSingleton) {
global.VectorStoreSingleton = getVectorStore();
}
VectorStore = global.VectorStoreSingleton;
} else {
VectorStore = getVectorStore();
}
export default VectorStore;