-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
faiss.ts
470 lines (432 loc) Β· 14.9 KB
/
faiss.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
import type { IndexFlatL2 } from "faiss-node";
import type { NameRegistry, Parser } from "pickleparser";
import * as uuid from "uuid";
import type { EmbeddingsInterface } from "@langchain/core/embeddings";
import { SaveableVectorStore } from "@langchain/core/vectorstores";
import { Document } from "@langchain/core/documents";
import { SynchronousInMemoryDocstore } from "../stores/doc/in_memory.js";
/**
* Interface for the arguments required to initialize a FaissStore
* instance.
*/
export interface FaissLibArgs {
docstore?: SynchronousInMemoryDocstore;
index?: IndexFlatL2;
mapping?: Record<number, string>;
}
/**
* A class that wraps the FAISS (Facebook AI Similarity Search) vector
* database for efficient similarity search and clustering of dense
* vectors.
*/
export class FaissStore extends SaveableVectorStore {
_index?: IndexFlatL2;
_mapping: Record<number, string>;
docstore: SynchronousInMemoryDocstore;
args: FaissLibArgs;
_vectorstoreType(): string {
return "faiss";
}
getMapping(): Record<number, string> {
return this._mapping;
}
getDocstore(): SynchronousInMemoryDocstore {
return this.docstore;
}
constructor(embeddings: EmbeddingsInterface, args: FaissLibArgs) {
super(embeddings, args);
this.args = args;
this._index = args.index;
this._mapping = args.mapping ?? {};
this.embeddings = embeddings;
this.docstore = args?.docstore ?? new SynchronousInMemoryDocstore();
}
/**
* Adds an array of Document objects to the store.
* @param documents An array of Document objects.
* @returns A Promise that resolves when the documents have been added.
*/
async addDocuments(documents: Document[], options?: { ids?: string[] }) {
const texts = documents.map(({ pageContent }) => pageContent);
return this.addVectors(
await this.embeddings.embedDocuments(texts),
documents,
options
);
}
public get index(): IndexFlatL2 {
if (!this._index) {
throw new Error(
"Vector store not initialised yet. Try calling `fromTexts`, `fromDocuments` or `fromIndex` first."
);
}
return this._index;
}
private set index(index: IndexFlatL2) {
this._index = index;
}
/**
* Adds an array of vectors and their corresponding Document objects to
* the store.
* @param vectors An array of vectors.
* @param documents An array of Document objects corresponding to the vectors.
* @returns A Promise that resolves with an array of document IDs when the vectors and documents have been added.
*/
async addVectors(
vectors: number[][],
documents: Document[],
options?: { ids?: string[] }
) {
if (vectors.length === 0) {
return [];
}
if (vectors.length !== documents.length) {
throw new Error(`Vectors and documents must have the same length`);
}
const dv = vectors[0].length;
if (!this._index) {
const { IndexFlatL2 } = await FaissStore.importFaiss();
this._index = new IndexFlatL2(dv);
}
const d = this.index.getDimension();
if (dv !== d) {
throw new Error(
`Vectors must have the same length as the number of dimensions (${d})`
);
}
const docstoreSize = this.index.ntotal();
const documentIds = options?.ids ?? documents.map(() => uuid.v4());
for (let i = 0; i < vectors.length; i += 1) {
const documentId = documentIds[i];
const id = docstoreSize + i;
this.index.add(vectors[i]);
this._mapping[id] = documentId;
this.docstore.add({ [documentId]: documents[i] });
}
return documentIds;
}
/**
* Performs a similarity search in the vector store using a query vector
* and returns the top k results along with their scores.
* @param query A query vector.
* @param k The number of top results to return.
* @returns A Promise that resolves with an array of tuples, each containing a Document and its corresponding score.
*/
async similaritySearchVectorWithScore(query: number[], k: number) {
const d = this.index.getDimension();
if (query.length !== d) {
throw new Error(
`Query vector must have the same length as the number of dimensions (${d})`
);
}
if (k > this.index.ntotal()) {
const total = this.index.ntotal();
console.warn(
`k (${k}) is greater than the number of elements in the index (${total}), setting k to ${total}`
);
// eslint-disable-next-line no-param-reassign
k = total;
}
const result = this.index.search(query, k);
return result.labels.map((id, index) => {
const uuid = this._mapping[id];
return [this.docstore.search(uuid), result.distances[index]] as [
Document,
number
];
});
}
/**
* Saves the current state of the FaissStore to a specified directory.
* @param directory The directory to save the state to.
* @returns A Promise that resolves when the state has been saved.
*/
async save(directory: string) {
const fs = await import("node:fs/promises");
const path = await import("node:path");
await fs.mkdir(directory, { recursive: true });
await Promise.all([
this.index.write(path.join(directory, "faiss.index")),
await fs.writeFile(
path.join(directory, "docstore.json"),
JSON.stringify([
Array.from(this.docstore._docs.entries()),
this._mapping,
])
),
]);
}
/**
* Method to delete documents.
* @param params Object containing the IDs of the documents to delete.
* @returns A promise that resolves when the deletion is complete.
*/
async delete(params: { ids: string[] }) {
const documentIds = params.ids;
if (documentIds == null) {
throw new Error("No documentIds provided to delete.");
}
const mappings = new Map(
Object.entries(this._mapping).map(([key, value]) => [
parseInt(key, 10),
value,
])
);
const reversedMappings = new Map(
Array.from(mappings, (entry) => [entry[1], entry[0]])
);
const missingIds = new Set(
documentIds.filter((id) => !reversedMappings.has(id))
);
if (missingIds.size > 0) {
throw new Error(
`Some specified documentIds do not exist in the current store. DocumentIds not found: ${Array.from(
missingIds
).join(", ")}`
);
}
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const indexIdToDelete = documentIds.map((id) => reversedMappings.get(id)!);
// remove from index
this.index.removeIds(indexIdToDelete);
// remove from docstore
documentIds.forEach((id) => {
this.docstore._docs.delete(id);
});
// remove from mappings
indexIdToDelete.forEach((id) => {
mappings.delete(id);
});
this._mapping = { ...Array.from(mappings.values()) };
}
/**
* Merges the current FaissStore with another FaissStore.
* @param targetIndex The FaissStore to merge with.
* @returns A Promise that resolves with an array of document IDs when the merge is complete.
*/
async mergeFrom(targetIndex: FaissStore) {
const targetIndexDimensions = targetIndex.index.getDimension();
if (!this._index) {
const { IndexFlatL2 } = await FaissStore.importFaiss();
this._index = new IndexFlatL2(targetIndexDimensions);
}
const d = this.index.getDimension();
if (targetIndexDimensions !== d) {
throw new Error("Cannot merge indexes with different dimensions.");
}
const targetMapping = targetIndex.getMapping();
const targetDocstore = targetIndex.getDocstore();
const targetSize = targetIndex.index.ntotal();
const documentIds = [];
const currentDocstoreSize = this.index.ntotal();
for (let i = 0; i < targetSize; i += 1) {
const targetId = targetMapping[i];
documentIds.push(targetId);
const targetDocument = targetDocstore.search(targetId);
const id = currentDocstoreSize + i;
this._mapping[id] = targetId;
this.docstore.add({ [targetId]: targetDocument });
}
this.index.mergeFrom(targetIndex.index);
return documentIds;
}
/**
* Loads a FaissStore from a specified directory.
* @param directory The directory to load the FaissStore from.
* @param embeddings An Embeddings object.
* @returns A Promise that resolves with a new FaissStore instance.
*/
static async load(directory: string, embeddings: EmbeddingsInterface) {
const fs = await import("node:fs/promises");
const path = await import("node:path");
const readStore = (directory: string) =>
fs
.readFile(path.join(directory, "docstore.json"), "utf8")
.then(JSON.parse) as Promise<
[Map<string, Document>, Record<number, string>]
>;
const readIndex = async (directory: string) => {
const { IndexFlatL2 } = await this.importFaiss();
return IndexFlatL2.read(path.join(directory, "faiss.index"));
};
const [[docstoreFiles, mapping], index] = await Promise.all([
readStore(directory),
readIndex(directory),
]);
const docstore = new SynchronousInMemoryDocstore(new Map(docstoreFiles));
return new this(embeddings, { docstore, index, mapping });
}
static async loadFromPython(
directory: string,
embeddings: EmbeddingsInterface
) {
const fs = await import("node:fs/promises");
const path = await import("node:path");
const { Parser, NameRegistry } = await this.importPickleparser();
class PyDocument extends Map {
toDocument(): Document {
return new Document({
pageContent: this.get("page_content"),
metadata: this.get("metadata"),
});
}
}
class PyInMemoryDocstore {
_dict: Map<string, PyDocument>;
toInMemoryDocstore(): SynchronousInMemoryDocstore {
const s = new SynchronousInMemoryDocstore();
for (const [key, value] of Object.entries(this._dict)) {
s._docs.set(key, value.toDocument());
}
return s;
}
}
const readStore = async (directory: string) => {
const pkl = await fs.readFile(
path.join(directory, "index.pkl"),
"binary"
);
const buffer = Buffer.from(pkl, "binary");
const registry = new NameRegistry()
.register(
"langchain.docstore.in_memory",
"InMemoryDocstore",
PyInMemoryDocstore
)
.register(
"langchain_community.docstore.in_memory",
"InMemoryDocstore",
PyInMemoryDocstore
)
.register("langchain.schema", "Document", PyDocument)
.register("langchain.docstore.document", "Document", PyDocument)
.register("langchain.schema.document", "Document", PyDocument)
.register("langchain_core.documents.base", "Document", PyDocument)
.register("pathlib", "WindowsPath", (...args) => args.join("\\"))
.register("pathlib", "PosixPath", (...args) => args.join("/"));
const pickleparser = new Parser({
nameResolver: registry,
});
const [rawStore, mapping] =
pickleparser.parse<[PyInMemoryDocstore, Record<number, string>]>(
buffer
);
const store = rawStore.toInMemoryDocstore();
return { store, mapping };
};
const readIndex = async (directory: string) => {
const { IndexFlatL2 } = await this.importFaiss();
return IndexFlatL2.read(path.join(directory, "index.faiss"));
};
const [store, index] = await Promise.all([
readStore(directory),
readIndex(directory),
]);
return new this(embeddings, {
docstore: store.store,
index,
mapping: store.mapping,
});
}
/**
* Creates a new FaissStore from an array of texts, their corresponding
* metadata, and an Embeddings object.
* @param texts An array of texts.
* @param metadatas An array of metadata corresponding to the texts, or a single metadata object to be used for all texts.
* @param embeddings An Embeddings object.
* @param dbConfig An optional configuration object for the document store.
* @returns A Promise that resolves with a new FaissStore instance.
*/
static async fromTexts(
texts: string[],
metadatas: object[] | object,
embeddings: EmbeddingsInterface,
dbConfig?: {
docstore?: SynchronousInMemoryDocstore;
}
): Promise<FaissStore> {
const docs: Document[] = [];
for (let i = 0; i < texts.length; i += 1) {
const metadata = Array.isArray(metadatas) ? metadatas[i] : metadatas;
const newDoc = new Document({
pageContent: texts[i],
metadata,
});
docs.push(newDoc);
}
return this.fromDocuments(docs, embeddings, dbConfig);
}
/**
* Creates a new FaissStore from an array of Document objects and an
* Embeddings object.
* @param docs An array of Document objects.
* @param embeddings An Embeddings object.
* @param dbConfig An optional configuration object for the document store.
* @returns A Promise that resolves with a new FaissStore instance.
*/
static async fromDocuments(
docs: Document[],
embeddings: EmbeddingsInterface,
dbConfig?: {
docstore?: SynchronousInMemoryDocstore;
}
): Promise<FaissStore> {
const args: FaissLibArgs = {
docstore: dbConfig?.docstore,
};
const instance = new this(embeddings, args);
await instance.addDocuments(docs);
return instance;
}
/**
* Creates a new FaissStore from an existing FaissStore and an Embeddings
* object.
* @param targetIndex An existing FaissStore.
* @param embeddings An Embeddings object.
* @param dbConfig An optional configuration object for the document store.
* @returns A Promise that resolves with a new FaissStore instance.
*/
static async fromIndex(
targetIndex: FaissStore,
embeddings: EmbeddingsInterface,
dbConfig?: {
docstore?: SynchronousInMemoryDocstore;
}
): Promise<FaissStore> {
const args: FaissLibArgs = {
docstore: dbConfig?.docstore,
};
const instance = new this(embeddings, args);
await instance.mergeFrom(targetIndex);
return instance;
}
static async importFaiss(): Promise<{ IndexFlatL2: typeof IndexFlatL2 }> {
try {
const {
default: { IndexFlatL2 },
} = await import("faiss-node");
return { IndexFlatL2 };
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (err: any) {
throw new Error(
`Could not import faiss-node. Please install faiss-node as a dependency with, e.g. \`npm install -S faiss-node\`.\n\nError: ${err?.message}`
);
}
}
static async importPickleparser(): Promise<{
Parser: typeof Parser;
NameRegistry: typeof NameRegistry;
}> {
try {
const {
default: { Parser, NameRegistry },
} = await import("pickleparser");
return { Parser, NameRegistry };
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (err: any) {
throw new Error(
`Could not import pickleparser. Please install pickleparser as a dependency with, e.g. \`npm install -S pickleparser\`.\n\nError: ${err?.message}`
);
}
}
}