-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
typesense.ts
323 lines (287 loc) Β· 8.79 KB
/
typesense.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
import type { Client } from "typesense";
import type { MultiSearchRequestSchema } from "typesense/lib/Typesense/MultiSearch.js";
import type {
SearchResponseHit,
DocumentSchema,
} from "typesense/lib/Typesense/Documents.js";
import type { EmbeddingsInterface } from "@langchain/core/embeddings";
import { VectorStore } from "@langchain/core/vectorstores";
import { Document } from "@langchain/core/documents";
import {
AsyncCaller,
AsyncCallerParams,
} from "@langchain/core/utils/async_caller";
/**
* Interface for the response hit from a vector search in Typesense.
*/
interface VectorSearchResponseHit<T extends DocumentSchema>
extends SearchResponseHit<T> {
vector_distance?: number;
}
/**
* Typesense vector store configuration.
*/
export interface TypesenseConfig extends AsyncCallerParams {
/**
* Typesense client.
*/
typesenseClient: Client;
/**
* Typesense schema name in which documents will be stored and searched.
*/
schemaName: string;
/**
* Typesense search parameters.
* @default { q: '*', per_page: 5, query_by: '' }
*/
searchParams?: MultiSearchRequestSchema;
/**
* Column names.
*/
columnNames?: {
/**
* Vector column name.
* @default 'vec'
*/
vector?: string;
/**
* Page content column name.
* @default 'text'
*/
pageContent?: string;
/**
* Metadata column names.
* @default []
*/
metadataColumnNames?: string[];
};
/**
* Replace default import function.
* Default import function will update documents if there is a document with the same id.
* @param data
* @param collectionName
*/
import?<T extends Record<string, unknown> = Record<string, unknown>>(
data: T[],
collectionName: string
): Promise<void>;
}
/**
* Typesense vector store.
*/
export class Typesense extends VectorStore {
declare FilterType: Partial<MultiSearchRequestSchema>;
private client: Client;
private schemaName: string;
private searchParams: MultiSearchRequestSchema;
private vectorColumnName: string;
private pageContentColumnName: string;
private metadataColumnNames: string[];
private caller: AsyncCaller;
private import: (
data: Record<string, unknown>[],
collectionName: string
) => Promise<void>;
_vectorstoreType(): string {
return "typesense";
}
constructor(embeddings: EmbeddingsInterface, config: TypesenseConfig) {
super(embeddings, config);
// Assign config values to class properties.
this.client = config.typesenseClient;
this.schemaName = config.schemaName;
this.searchParams = config.searchParams || {
q: "*",
per_page: 5,
query_by: "",
};
this.vectorColumnName = config.columnNames?.vector || "vec";
this.pageContentColumnName = config.columnNames?.pageContent || "text";
this.metadataColumnNames = config.columnNames?.metadataColumnNames || [];
// Assign import function.
this.import = config.import || this.importToTypesense.bind(this);
this.caller = new AsyncCaller(config);
}
/**
* Default function to import data to typesense
* @param data
* @param collectionName
*/
private async importToTypesense<
T extends Record<string, unknown> = Record<string, unknown>
>(data: T[], collectionName: string) {
const chunkSize = 2000;
for (let i = 0; i < data.length; i += chunkSize) {
const chunk = data.slice(i, i + chunkSize);
await this.caller.call(async () => {
await this.client
.collections<T>(collectionName)
.documents()
.import(chunk, { action: "emplace", dirty_values: "drop" });
});
}
}
/**
* Transform documents to Typesense records.
* @param documents
* @returns Typesense records.
*/
_documentsToTypesenseRecords(
documents: Document[],
vectors: number[][]
): Record<string, unknown>[] {
const metadatas = documents.map((doc) => doc.metadata);
const typesenseDocuments = documents.map((doc, index) => {
const metadata = metadatas[index];
const objectWithMetadatas: Record<string, unknown> = {};
this.metadataColumnNames.forEach((metadataColumnName) => {
objectWithMetadatas[metadataColumnName] = metadata[metadataColumnName];
});
return {
[this.pageContentColumnName]: doc.pageContent,
[this.vectorColumnName]: vectors[index],
...objectWithMetadatas,
};
});
return typesenseDocuments;
}
/**
* Transform the Typesense records to documents.
* @param typesenseRecords
* @returns documents
*/
_typesenseRecordsToDocuments(
typesenseRecords:
| { document?: Record<string, unknown>; vector_distance: number }[]
| undefined
): [Document, number][] {
const documents: [Document, number][] =
typesenseRecords?.map((hit) => {
const objectWithMetadatas: Record<string, unknown> = {};
const hitDoc = hit.document || {};
this.metadataColumnNames.forEach((metadataColumnName) => {
objectWithMetadatas[metadataColumnName] = hitDoc[metadataColumnName];
});
const document: Document = {
pageContent: (hitDoc[this.pageContentColumnName] as string) || "",
metadata: objectWithMetadatas,
};
return [document, hit.vector_distance];
}) || [];
return documents;
}
/**
* Add documents to the vector store.
* Will be updated if in the metadata there is a document with the same id if is using the default import function.
* Metadata will be added in the columns of the schema based on metadataColumnNames.
* @param documents Documents to add.
*/
async addDocuments(documents: Document[]) {
const typesenseDocuments = this._documentsToTypesenseRecords(
documents,
await this.embeddings.embedDocuments(
documents.map((doc) => doc.pageContent)
)
);
await this.import(typesenseDocuments, this.schemaName);
}
/**
* Adds vectors to the vector store.
* @param vectors Vectors to add.
* @param documents Documents associated with the vectors.
*/
async addVectors(vectors: number[][], documents: Document[]) {
const typesenseDocuments = this._documentsToTypesenseRecords(
documents,
vectors
);
await this.import(typesenseDocuments, this.schemaName);
}
/**
* Search for similar documents with their similarity score.
* @param vectorPrompt vector to search for
* @param k amount of results to return
* @returns similar documents with their similarity score
*/
async similaritySearchVectorWithScore(
vectorPrompt: number[],
k?: number,
filter: this["FilterType"] = {}
) {
const amount = k || this.searchParams.per_page || 5;
const vector_query = `${this.vectorColumnName}:([${vectorPrompt}], k:${amount})`;
const typesenseResponse = await this.client.multiSearch.perform(
{
searches: [
{
...this.searchParams,
...filter,
per_page: amount,
vector_query,
collection: this.schemaName,
},
],
},
{}
);
const results = typesenseResponse.results[0].hits;
const hits = results?.map((hit: VectorSearchResponseHit<object>) => ({
document: hit?.document || {},
vector_distance: hit?.vector_distance || 2,
})) as
| { document: Record<string, unknown>; vector_distance: number }[]
| undefined;
return this._typesenseRecordsToDocuments(hits);
}
/**
* Delete documents from the vector store.
* @param documentIds ids of the documents to delete
*/
async deleteDocuments(documentIds: string[]) {
await this.client
.collections(this.schemaName)
.documents()
.delete({
filter_by: `id:=${documentIds.join(",")}`,
});
}
/**
* Create a vector store from documents.
* @param docs documents
* @param embeddings embeddings
* @param config Typesense configuration
* @returns Typesense vector store
* @warning You can omit this method, and only use the constructor and addDocuments.
*/
static async fromDocuments(
docs: Document[],
embeddings: EmbeddingsInterface,
config: TypesenseConfig
): Promise<Typesense> {
const instance = new Typesense(embeddings, config);
await instance.addDocuments(docs);
return instance;
}
/**
* Create a vector store from texts.
* @param texts
* @param metadatas
* @param embeddings
* @param config
* @returns Typesense vector store
*/
static async fromTexts(
texts: string[],
metadatas: object[],
embeddings: EmbeddingsInterface,
config: TypesenseConfig
) {
const instance = new Typesense(embeddings, config);
const documents: Document[] = texts.map((text, i) => ({
pageContent: text,
metadata: metadatas[i] || {},
}));
await instance.addDocuments(documents);
return instance;
}
}