-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathindex.ts
More file actions
172 lines (143 loc) · 4.81 KB
/
index.ts
File metadata and controls
172 lines (143 loc) · 4.81 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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import Database from 'better-sqlite3';
import vectorlite from 'vectorlite';
import { OpenAI } from 'openai';
import { join } from 'path';
import { existsSync, mkdirSync } from 'fs';
export class VectorDB {
private db!: Database.Database;
private openai!: OpenAI;
private nextRowId!: number;
private readonly indexFilePath: string;
private readonly dbFilePath: string;
private maxElements: number;
private static readonly DEFAULT_INDEX_FILE = 'index_file.bin';
private static readonly DEFAULT_DB_FILE = 'vector_store.db';
private static readonly DEFAULT_DATA_DIR = join(process.cwd(), 'data', 'vector-db');
constructor(
dataDir?: string,
indexFilePath: string = VectorDB.DEFAULT_INDEX_FILE,
dbFilePath: string = VectorDB.DEFAULT_DB_FILE,
maxElements: number = 100000,
) {
const targetDir = dataDir ? join(process.cwd(), dataDir) : VectorDB.DEFAULT_DATA_DIR;
if (!existsSync(targetDir)) {
mkdirSync(targetDir, { recursive: true });
}
this.indexFilePath = join(targetDir, indexFilePath);
this.dbFilePath = join(targetDir, dbFilePath);
this.nextRowId = 1;
this.maxElements = maxElements;
this.openai = new OpenAI({
apiKey: <OpenAI API Key>,
});
this.initializeDatabase();
}
private initializeDatabase(): void {
const extensionPath = vectorlite.vectorlitePath();
this.db = new Database(this.dbFilePath);
this.db.loadExtension(extensionPath);
this.createTables();
const maxRowIdResult = this.db
.prepare(
`
SELECT MAX(rowid) as maxId FROM content_store
`,
)
.get() as { maxId: number | null };
this.nextRowId = (maxRowIdResult?.maxId || 0) + 1;
}
private createTables(): void {
this.db.exec(`
CREATE VIRTUAL TABLE IF NOT EXISTS embeddings_index USING vectorlite(
embedding_vector float32[1536],
hnsw(max_elements=${this.maxElements}),
'${this.indexFilePath}'
);
CREATE TABLE IF NOT EXISTS content_store (
rowid INTEGER PRIMARY KEY,
content TEXT
);
`);
}
private async getEmbedding(text: string): Promise<number[]> {
try {
const response = await this.openai.embeddings.create({
model: 'text-embedding-ada-002',
input: text,
});
return response.data[0].embedding;
} catch (error) {
throw error;
}
}
async insert(content: string): Promise<boolean> {
if (!content || content.trim().length === 0) {
throw new Error('Content cannot be empty');
}
const embedding = await this.getEmbedding(content);
this.db.exec('BEGIN');
try {
if (this.nextRowId > this.maxElements) {
const oldestRowId = this.db
.prepare(
`
SELECT MIN(rowid) as min_id FROM content_store
`,
)
.get() as { min_id: number };
const oldestId = Number(oldestRowId.min_id);
try {
this.db.exec(`DELETE FROM embeddings_index WHERE rowid = ${oldestId}`);
this.db.exec(`DELETE FROM content_store WHERE rowid = ${oldestId}`);
} catch (deleteError) {
throw deleteError;
}
this.nextRowId = oldestId;
}
const currentRowId = Number(this.nextRowId);
const vectorStmt = this.db.prepare(`
INSERT INTO embeddings_index (rowid, embedding_vector)
VALUES (?, ?)
`);
const contentStmt = this.db.prepare(`
INSERT INTO content_store (rowid, content)
VALUES (?, ?)
`);
vectorStmt.run(currentRowId, Buffer.from(new Float32Array(embedding).buffer));
contentStmt.run(currentRowId, content);
this.nextRowId++;
this.db.exec('COMMIT');
} catch (error) {
this.db.exec('ROLLBACK');
throw error;
}
return true;
}
async search(
content: string,
limit: number = 5,
): Promise<Array<{ rowid: number; distance: number; content: string }>> {
if (!content || content.trim().length === 0) {
throw new Error('Search query cannot be empty');
}
const embedding = await this.getEmbedding(content);
const integerLimit = parseInt(limit.toString(), 10);
const stmt = this.db.prepare(`
SELECT v.rowid, v.distance, c.content
FROM (
SELECT rowid, distance
FROM embeddings_index
WHERE knn_search(embedding_vector, knn_param(?, ${integerLimit}))
) v
JOIN content_store c ON v.rowid = c.rowid
`);
return stmt.all(Buffer.from(new Float32Array(embedding).buffer)) as Array<{
rowid: number;
distance: number;
content: string;
}>;
}
getDatabase(): Database.Database {
return this.db;
}
}