-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
usearch.ts
223 lines (205 loc) Β· 7.41 KB
/
usearch.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
import usearch from "usearch";
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 that defines the arguments that can be passed to the
* `USearch` constructor. It includes optional properties for a
* `docstore`, `index`, and `mapping`.
*/
export interface USearchArgs {
docstore?: SynchronousInMemoryDocstore;
index?: usearch.Index;
mapping?: Record<number, string>;
}
/**
* Class that extends `SaveableVectorStore` and provides methods for
* adding documents and vectors to a `usearch` index, performing
* similarity searches, and saving the index.
*/
export class USearch extends SaveableVectorStore {
_index?: usearch.Index;
_mapping: Record<number, string>;
docstore: SynchronousInMemoryDocstore;
args: USearchArgs;
_vectorstoreType(): string {
return "usearch";
}
constructor(embeddings: EmbeddingsInterface, args: USearchArgs) {
super(embeddings, args);
this.args = args;
this._index = args.index;
this._mapping = args.mapping ?? {};
this.embeddings = embeddings;
this.docstore = args?.docstore ?? new SynchronousInMemoryDocstore();
}
/**
* Method that adds documents to the `usearch` index. It generates
* embeddings for the documents and adds them to the index.
* @param documents An array of `Document` instances to be added to the index.
* @returns A promise that resolves with an array of document IDs.
*/
async addDocuments(documents: Document[]) {
const texts = documents.map(({ pageContent }) => pageContent);
return this.addVectors(
await this.embeddings.embedDocuments(texts),
documents
);
}
public get index(): usearch.Index {
if (!this._index) {
throw new Error(
"Vector store not initialised yet. Try calling `fromTexts` or `fromDocuments` first."
);
}
return this._index;
}
private set index(index: usearch.Index) {
this._index = index;
}
/**
* Method that adds vectors to the `usearch` index. It also updates the
* mapping between vector IDs and document IDs.
* @param vectors An array of vectors to be added to the index.
* @param documents An array of `Document` instances corresponding to the vectors.
* @returns A promise that resolves with an array of document IDs.
*/
async addVectors(vectors: number[][], documents: Document[]) {
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) {
this._index = new usearch.Index({
metric: "l2sq",
connectivity: BigInt(16),
dimensions: BigInt(dv),
});
}
const d = this.index.dimensions();
if (BigInt(dv) !== d) {
throw new Error(
`Vectors must have the same length as the number of dimensions (${d})`
);
}
const docstoreSize = this.index.size();
const documentIds = [];
for (let i = 0; i < vectors.length; i += 1) {
const documentId = uuid.v4();
documentIds.push(documentId);
const id = Number(docstoreSize) + i;
this.index.add(BigInt(id), new Float32Array(vectors[i]));
this._mapping[id] = documentId;
this.docstore.add({ [documentId]: documents[i] });
}
return documentIds;
}
/**
* Method that performs a similarity search in the `usearch` index. It
* returns the `k` most similar documents to a given query vector, along
* with their similarity scores.
* @param query The query vector.
* @param k The number of most similar documents to return.
* @returns A promise that resolves with an array of tuples, each containing a `Document` and its similarity score.
*/
async similaritySearchVectorWithScore(query: number[], k: number) {
const d = this.index.dimensions();
if (BigInt(query.length) !== d) {
throw new Error(
`Query vector must have the same length as the number of dimensions (${d})`
);
}
if (k > this.index.size()) {
const total = this.index.size();
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 = Number(total);
}
const result = this.index.search(new Float32Array(query), BigInt(k));
const return_list: [Document, number][] = [];
for (let i = 0; i < result.count; i += 1) {
const uuid = this._mapping[Number(result.keys[i])];
return_list.push([this.docstore.search(uuid), result.distances[i]]);
}
return return_list;
}
/**
* Method that saves the `usearch` index and the document store to disk.
* @param directory The directory where the index and document store should be saved.
* @returns A promise that resolves when the save operation is complete.
*/
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.save(path.join(directory, "usearch.index")),
await fs.writeFile(
path.join(directory, "docstore.json"),
JSON.stringify([
Array.from(this.docstore._docs.entries()),
this._mapping,
])
),
]);
}
/**
* Static method that creates a new `USearch` instance from a list of
* texts. It generates embeddings for the texts and adds them to the
* `usearch` index.
* @param texts An array of texts to be added to the index.
* @param metadatas Metadata associated with the texts.
* @param embeddings An instance of `Embeddings` used to generate embeddings for the texts.
* @param dbConfig Optional configuration for the document store.
* @returns A promise that resolves with a new `USearch` instance.
*/
static async fromTexts(
texts: string[],
metadatas: object[] | object,
embeddings: EmbeddingsInterface,
dbConfig?: {
docstore?: SynchronousInMemoryDocstore;
}
): Promise<USearch> {
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);
}
/**
* Static method that creates a new `USearch` instance from a list of
* documents. It generates embeddings for the documents and adds them to
* the `usearch` index.
* @param docs An array of `Document` instances to be added to the index.
* @param embeddings An instance of `Embeddings` used to generate embeddings for the documents.
* @param dbConfig Optional configuration for the document store.
* @returns A promise that resolves with a new `USearch` instance.
*/
static async fromDocuments(
docs: Document[],
embeddings: EmbeddingsInterface,
dbConfig?: {
docstore?: SynchronousInMemoryDocstore;
}
): Promise<USearch> {
const args: USearchArgs = {
docstore: dbConfig?.docstore,
};
const instance = new this(embeddings, args);
await instance.addDocuments(docs);
return instance;
}
}