@langchain/google-cloud-sql-pg 0.0.2 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +17 -0
- package/LICENSE +6 -6
- package/README.md +1 -1
- package/dist/_virtual/rolldown_runtime.cjs +25 -0
- package/dist/chat_message_history.cjs +104 -137
- package/dist/chat_message_history.cjs.map +1 -0
- package/dist/chat_message_history.d.cts +57 -0
- package/dist/chat_message_history.d.cts.map +1 -0
- package/dist/chat_message_history.d.ts +53 -43
- package/dist/chat_message_history.d.ts.map +1 -0
- package/dist/chat_message_history.js +103 -133
- package/dist/chat_message_history.js.map +1 -0
- package/dist/engine.cjs +188 -242
- package/dist/engine.cjs.map +1 -0
- package/dist/engine.d.cts +138 -0
- package/dist/engine.d.cts.map +1 -0
- package/dist/engine.d.ts +102 -80
- package/dist/engine.d.ts.map +1 -0
- package/dist/engine.js +186 -234
- package/dist/engine.js.map +1 -0
- package/dist/index.cjs +21 -20
- package/dist/index.d.cts +6 -0
- package/dist/index.d.ts +6 -4
- package/dist/index.js +7 -4
- package/dist/indexes.cjs +96 -168
- package/dist/indexes.cjs.map +1 -0
- package/dist/indexes.d.cts +68 -0
- package/dist/indexes.d.cts.map +1 -0
- package/dist/indexes.d.ts +50 -47
- package/dist/indexes.d.ts.map +1 -0
- package/dist/indexes.js +90 -161
- package/dist/indexes.js.map +1 -0
- package/dist/loader.cjs +159 -242
- package/dist/loader.cjs.map +1 -0
- package/dist/loader.d.cts +101 -0
- package/dist/loader.d.cts.map +1 -0
- package/dist/loader.d.ts +40 -26
- package/dist/loader.d.ts.map +1 -0
- package/dist/loader.js +157 -237
- package/dist/loader.js.map +1 -0
- package/dist/utils/utils.cjs +36 -65
- package/dist/utils/utils.cjs.map +1 -0
- package/dist/utils/utils.js +36 -62
- package/dist/utils/utils.js.map +1 -0
- package/dist/vectorstore.cjs +438 -593
- package/dist/vectorstore.cjs.map +1 -0
- package/dist/vectorstore.d.cts +300 -0
- package/dist/vectorstore.d.cts.map +1 -0
- package/dist/vectorstore.d.ts +147 -130
- package/dist/vectorstore.d.ts.map +1 -0
- package/dist/vectorstore.js +436 -588
- package/dist/vectorstore.js.map +1 -0
- package/package.json +38 -47
- package/dist/utils/utils.d.ts +0 -22
- package/index.cjs +0 -1
- package/index.d.cts +0 -1
- package/index.d.ts +0 -1
- package/index.js +0 -1
package/dist/vectorstore.cjs
CHANGED
|
@@ -1,597 +1,442 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_utils = require('./utils/utils.cjs');
|
|
3
|
+
const require_indexes = require('./indexes.cjs');
|
|
4
|
+
const __langchain_core_vectorstores = require_rolldown_runtime.__toESM(require("@langchain/core/vectorstores"));
|
|
5
|
+
const __langchain_core_documents = require_rolldown_runtime.__toESM(require("@langchain/core/documents"));
|
|
6
|
+
const uuid = require_rolldown_runtime.__toESM(require("uuid"));
|
|
7
|
+
const __langchain_core_utils_math = require_rolldown_runtime.__toESM(require("@langchain/core/utils/math"));
|
|
8
|
+
|
|
9
|
+
//#region src/vectorstore.ts
|
|
10
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
|
-
class PostgresVectorStore extends
|
|
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
|
-
for (const metadataColumn of this.metadataColumns) {
|
|
416
|
-
if (Object.prototype.hasOwnProperty.call(metadata, metadataColumn)) {
|
|
417
|
-
valuesStmt += `, :${metadataColumn}`;
|
|
418
|
-
values[metadataColumn] = metadata[metadataColumn];
|
|
419
|
-
delete extra[metadataColumn];
|
|
420
|
-
}
|
|
421
|
-
else {
|
|
422
|
-
valuesStmt += " ,null";
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
// Add JSON column and/or close statement
|
|
426
|
-
stmt += this.metadataJsonColumn ? `, ${this.metadataJsonColumn})` : ")";
|
|
427
|
-
if (this.metadataJsonColumn) {
|
|
428
|
-
valuesStmt += ", :extra)";
|
|
429
|
-
Object.assign(values, { extra: JSON.stringify(extra) });
|
|
430
|
-
}
|
|
431
|
-
else {
|
|
432
|
-
valuesStmt += ")";
|
|
433
|
-
}
|
|
434
|
-
const query = stmt + valuesStmt;
|
|
435
|
-
await this.engine.pool.raw(query, values);
|
|
436
|
-
}
|
|
437
|
-
return options?.ids;
|
|
438
|
-
}
|
|
439
|
-
_vectorstoreType() {
|
|
440
|
-
return "cloudsqlpostgresql";
|
|
441
|
-
}
|
|
442
|
-
/**
|
|
443
|
-
* Adds documents to the vector store, embedding them first through the
|
|
444
|
-
* `embeddings` instance.
|
|
445
|
-
*
|
|
446
|
-
* @param documents - Array of documents to embed and add.
|
|
447
|
-
* @param options - Optional configuration for embedding and storing documents.
|
|
448
|
-
* @returns A promise resolving to an array of document IDs or void, based on implementation.
|
|
449
|
-
* @abstract
|
|
450
|
-
*/
|
|
451
|
-
async addDocuments(documents, options) {
|
|
452
|
-
const texts = [];
|
|
453
|
-
for (const doc of documents) {
|
|
454
|
-
texts.push(doc.pageContent);
|
|
455
|
-
}
|
|
456
|
-
const embeddings = await this.embeddings.embedDocuments(texts);
|
|
457
|
-
const results = await this.addVectors(embeddings, documents, options);
|
|
458
|
-
return results;
|
|
459
|
-
}
|
|
460
|
-
/**
|
|
461
|
-
* Deletes documents from the vector store based on the specified ids.
|
|
462
|
-
*
|
|
463
|
-
* @param params - Flexible key-value pairs defining conditions for document deletion.
|
|
464
|
-
* @param ids - Optional: Property of {params} that contains the array of ids to be deleted
|
|
465
|
-
* @returns A promise that resolves once the deletion is complete.
|
|
466
|
-
*/
|
|
467
|
-
async delete(params) {
|
|
468
|
-
if (params.ids === undefined)
|
|
469
|
-
return;
|
|
470
|
-
const idList = params.ids.map((id) => `'${id}'`).join(", ");
|
|
471
|
-
const query = `DELETE FROM "${this.schemaName}"."${this.tableName}" WHERE "${this.idColumn}" in (${idList})`;
|
|
472
|
-
await this.engine.pool.raw(query);
|
|
473
|
-
}
|
|
474
|
-
async similaritySearchVectorWithScore(embedding, k, filter) {
|
|
475
|
-
const results = await this.queryCollection(embedding, k, filter);
|
|
476
|
-
const documentsWithScores = [];
|
|
477
|
-
for (const row of results) {
|
|
478
|
-
const metadata = this.metadataJsonColumn && row[this.metadataJsonColumn]
|
|
479
|
-
? row[this.metadataJsonColumn]
|
|
480
|
-
: {};
|
|
481
|
-
for (const col of this.metadataColumns) {
|
|
482
|
-
metadata[col] = row[col];
|
|
483
|
-
}
|
|
484
|
-
documentsWithScores.push([
|
|
485
|
-
new documents_1.Document({ pageContent: row[this.contentColumn], metadata }),
|
|
486
|
-
row.distance,
|
|
487
|
-
]);
|
|
488
|
-
}
|
|
489
|
-
return documentsWithScores;
|
|
490
|
-
}
|
|
491
|
-
async queryCollection(embedding, k, filter) {
|
|
492
|
-
const fetchK = k ?? this.k;
|
|
493
|
-
const { operator } = this.distanceStrategy;
|
|
494
|
-
const { searchFunction } = this.distanceStrategy;
|
|
495
|
-
const _filter = filter !== undefined ? `WHERE ${filter}` : "";
|
|
496
|
-
const metadataColNames = this.metadataColumns.length > 0
|
|
497
|
-
? `, "${this.metadataColumns.join('","')}"`
|
|
498
|
-
: "";
|
|
499
|
-
const metadataJsonColName = this.metadataJsonColumn
|
|
500
|
-
? `, "${this.metadataJsonColumn}"`
|
|
501
|
-
: "";
|
|
502
|
-
const query = `SELECT "${this.idColumn}", "${this.contentColumn}", "${this.embeddingColumn}" ${metadataColNames} ${metadataJsonColName}, ${searchFunction}("${this.embeddingColumn}", '[${embedding}]') as distance FROM "${this.schemaName}"."${this.tableName}" ${_filter} ORDER BY "${this.embeddingColumn}" ${operator} '[${embedding}]' LIMIT ${fetchK};`;
|
|
503
|
-
if (this.indexQueryOptions) {
|
|
504
|
-
await this.engine.pool.raw(`SET LOCAL ${this.indexQueryOptions.to_string()}`);
|
|
505
|
-
}
|
|
506
|
-
const { rows } = await this.engine.pool.raw(query);
|
|
507
|
-
return rows;
|
|
508
|
-
}
|
|
509
|
-
async maxMarginalRelevanceSearch(query, options) {
|
|
510
|
-
const vector = await this.embeddings.embedQuery(query);
|
|
511
|
-
const results = await this.queryCollection(vector, options?.k, options?.filter);
|
|
512
|
-
const k = options?.k ? options.k : this.k;
|
|
513
|
-
const documentsWithScores = [];
|
|
514
|
-
let docsList = [];
|
|
515
|
-
const embeddingList = results.map((row) => JSON.parse(row[this.embeddingColumn]));
|
|
516
|
-
const mmrSelected = (0, math_1.maximalMarginalRelevance)(vector, embeddingList, options?.lambda, k);
|
|
517
|
-
for (const row of results) {
|
|
518
|
-
const metadata = this.metadataJsonColumn && row[this.metadataJsonColumn]
|
|
519
|
-
? row[this.metadataJsonColumn]
|
|
520
|
-
: {};
|
|
521
|
-
for (const col of this.metadataColumns) {
|
|
522
|
-
metadata[col] = row[col];
|
|
523
|
-
}
|
|
524
|
-
documentsWithScores.push([
|
|
525
|
-
new documents_1.Document({
|
|
526
|
-
pageContent: row[this.contentColumn],
|
|
527
|
-
metadata,
|
|
528
|
-
}),
|
|
529
|
-
row.distance,
|
|
530
|
-
]);
|
|
531
|
-
}
|
|
532
|
-
docsList = documentsWithScores
|
|
533
|
-
.filter((_, i) => mmrSelected.includes(i))
|
|
534
|
-
.map(([doc, _]) => doc);
|
|
535
|
-
return docsList;
|
|
536
|
-
}
|
|
537
|
-
/**
|
|
538
|
-
* Create an index on the vector store table
|
|
539
|
-
* @param {BaseIndex} index
|
|
540
|
-
* @param {string} name Optional
|
|
541
|
-
* @param {boolean} concurrently Optional
|
|
542
|
-
*/
|
|
543
|
-
async applyVectorIndex(index, name, concurrently = false) {
|
|
544
|
-
if (index.constructor.name === "ExactNearestNeighbor") {
|
|
545
|
-
await this.dropVectorIndex();
|
|
546
|
-
return;
|
|
547
|
-
}
|
|
548
|
-
const filter = index.partialIndexes && index.partialIndexes?.length > 0
|
|
549
|
-
? `WHERE (${index.partialIndexes})`
|
|
550
|
-
: "";
|
|
551
|
-
const params = `WITH ${index.indexOptions()}`;
|
|
552
|
-
const funct = index.distanceStrategy.indexFunction;
|
|
553
|
-
let indexName = name;
|
|
554
|
-
if (!indexName) {
|
|
555
|
-
if (!index.name) {
|
|
556
|
-
indexName = this.tableName + indexes_js_1.DEFAULT_INDEX_NAME_SUFFIX;
|
|
557
|
-
}
|
|
558
|
-
else {
|
|
559
|
-
indexName = index.name;
|
|
560
|
-
}
|
|
561
|
-
}
|
|
562
|
-
const stmt = `CREATE INDEX ${concurrently ? "CONCURRENTLY" : ""} ${indexName} ON "${this.schemaName}"."${this.tableName}" USING ${index.indexType} (${this.embeddingColumn} ${funct}) ${params} ${filter};`;
|
|
563
|
-
await this.engine.pool.raw(stmt);
|
|
564
|
-
}
|
|
565
|
-
/**
|
|
566
|
-
* Check if index exists in the table.
|
|
567
|
-
* @param {string} indexName Optional - index name
|
|
568
|
-
*/
|
|
569
|
-
async isValidIndex(indexName) {
|
|
570
|
-
const idxName = indexName || this.tableName + indexes_js_1.DEFAULT_INDEX_NAME_SUFFIX;
|
|
571
|
-
const stmt = `SELECT tablename, indexname
|
|
11
|
+
* Google Cloud SQL for PostgreSQL vector store integration.
|
|
12
|
+
*
|
|
13
|
+
* Setup:
|
|
14
|
+
* Install `@langchain/google-cloud-sql-pg`
|
|
15
|
+
*
|
|
16
|
+
* ```bash
|
|
17
|
+
* npm install @langchain/google-cloud-sql-pg
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* <details open>
|
|
21
|
+
* <summary><strong>Instantiate</strong></summary>
|
|
22
|
+
*
|
|
23
|
+
* ```typescript
|
|
24
|
+
* import { Column, PostgresEngine, PostgresEngineArgs, PostgresVectorStore, VectorStoreTableArgs } from "@langchain/google-cloud-sql-pg";
|
|
25
|
+
* // Or other embeddings
|
|
26
|
+
* import { OpenAIEmbeddings } from '@langchain/openai';
|
|
27
|
+
*
|
|
28
|
+
*
|
|
29
|
+
* const embeddings = new OpenAIEmbeddings({
|
|
30
|
+
* model: "text-embedding-3-small",
|
|
31
|
+
* });
|
|
32
|
+
*
|
|
33
|
+
* const pgArgs: PostgresEngineArgs = {
|
|
34
|
+
* user: "db-user",
|
|
35
|
+
* password: "password"
|
|
36
|
+
* }
|
|
37
|
+
* // Create a shared connection pool
|
|
38
|
+
* const engine: PostgresEngine = await PostgresEngine.fromInstance(
|
|
39
|
+
* "project-id",
|
|
40
|
+
* "region",
|
|
41
|
+
* "instance-name",
|
|
42
|
+
* "database-name",
|
|
43
|
+
* pgArgs
|
|
44
|
+
* );
|
|
45
|
+
* // (Optional) Specify metadata columns for filtering
|
|
46
|
+
* // All other metadata will be added to JSON
|
|
47
|
+
* const vectorStoreTableArgs: VectorStoreTableArgs = {
|
|
48
|
+
* metadataColumns: [new Column("baz", "TEXT")],
|
|
49
|
+
* };
|
|
50
|
+
* // Create a vector store table
|
|
51
|
+
* await engine.initVectorstoreTable("my-table", 768, vectorStoreTableArgs);
|
|
52
|
+
* // Customize the vector store
|
|
53
|
+
* const pvectorArgs: PostgresVectorStoreArgs = {
|
|
54
|
+
* idColumn: "ID_COLUMN",
|
|
55
|
+
* contentColumn: "CONTENT_COLUMN",
|
|
56
|
+
* embeddingColumn: "EMBEDDING_COLUMN",
|
|
57
|
+
* metadataColumns: ["baz"]
|
|
58
|
+
* }
|
|
59
|
+
*
|
|
60
|
+
* const vectorStore = await PostgresVectorStore.initialize(engine, embeddingService, "my-table", pvectorArgs);
|
|
61
|
+
* ```
|
|
62
|
+
* </details>
|
|
63
|
+
*
|
|
64
|
+
* <br />
|
|
65
|
+
*
|
|
66
|
+
* <details>
|
|
67
|
+
* <summary><strong>Add documents</strong></summary>
|
|
68
|
+
*
|
|
69
|
+
* ```typescript
|
|
70
|
+
* import type { Document } from '@langchain/core/documents';
|
|
71
|
+
*
|
|
72
|
+
* const document1 = { pageContent: "foo", metadata: { baz: "bar" } };
|
|
73
|
+
* const document2 = { pageContent: "thud", metadata: { bar: "baz" } };
|
|
74
|
+
* const document3 = { pageContent: "i will be deleted :(", metadata: {} };
|
|
75
|
+
*
|
|
76
|
+
* const documents: Document[] = [document1, document2, document3];
|
|
77
|
+
* const ids = ["1", "2", "3"];
|
|
78
|
+
* await vectorStore.addDocuments(documents, { ids });
|
|
79
|
+
* ```
|
|
80
|
+
* </details>
|
|
81
|
+
*
|
|
82
|
+
* <br />
|
|
83
|
+
*
|
|
84
|
+
* <details>
|
|
85
|
+
* <summary><strong>Delete documents</strong></summary>
|
|
86
|
+
*
|
|
87
|
+
* ```typescript
|
|
88
|
+
* await vectorStore.delete({ ids: ["3"] });
|
|
89
|
+
* ```
|
|
90
|
+
* </details>
|
|
91
|
+
*
|
|
92
|
+
* <br />
|
|
93
|
+
*
|
|
94
|
+
* <details>
|
|
95
|
+
* <summary><strong>Similarity search</strong></summary>
|
|
96
|
+
*
|
|
97
|
+
* ```typescript
|
|
98
|
+
* const results = await vectorStore.similaritySearch("thud", 1);
|
|
99
|
+
* for (const doc of results) {
|
|
100
|
+
* console.log(`* ${doc.pageContent} [${JSON.stringify(doc.metadata, null)}]`);
|
|
101
|
+
* }
|
|
102
|
+
* // Output:thud [{"baz":"bar"}]
|
|
103
|
+
* ```
|
|
104
|
+
* </details>
|
|
105
|
+
*
|
|
106
|
+
* <br />
|
|
107
|
+
*
|
|
108
|
+
*
|
|
109
|
+
* <details>
|
|
110
|
+
* <summary><strong>Similarity search with filter</strong></summary>
|
|
111
|
+
*
|
|
112
|
+
* ```typescript
|
|
113
|
+
* const resultsWithFilter = await vectorStore.similaritySearch("thud", 1, "baz = 'bar'");
|
|
114
|
+
*
|
|
115
|
+
* for (const doc of resultsWithFilter) {
|
|
116
|
+
* console.log(`* ${doc.pageContent} [${JSON.stringify(doc.metadata, null)}]`);
|
|
117
|
+
* }
|
|
118
|
+
* // Output:foo [{"baz":"bar"}]
|
|
119
|
+
* ```
|
|
120
|
+
* </details>
|
|
121
|
+
*
|
|
122
|
+
* <br />
|
|
123
|
+
*
|
|
124
|
+
*
|
|
125
|
+
* <details>
|
|
126
|
+
* <summary><strong>Similarity search with score</strong></summary>
|
|
127
|
+
*
|
|
128
|
+
* ```typescript
|
|
129
|
+
* const resultsWithScore = await vectorStore.similaritySearchWithScore("qux", 1);
|
|
130
|
+
* for (const [doc, score] of resultsWithScore) {
|
|
131
|
+
* console.log(`* [SIM=${score.toFixed(6)}] ${doc.pageContent} [${JSON.stringify(doc.metadata, null)}]`);
|
|
132
|
+
* }
|
|
133
|
+
* // Output:[SIM=0.000000] qux [{"bar":"baz","baz":"bar"}]
|
|
134
|
+
* ```
|
|
135
|
+
* </details>
|
|
136
|
+
*
|
|
137
|
+
* <br />
|
|
138
|
+
*
|
|
139
|
+
* <details>
|
|
140
|
+
* <summary><strong>As a retriever</strong></summary>
|
|
141
|
+
*
|
|
142
|
+
* ```typescript
|
|
143
|
+
* const retriever = vectorStore.asRetriever({
|
|
144
|
+
* searchType: "mmr", // Leave blank for standard similarity search
|
|
145
|
+
* k: 1,
|
|
146
|
+
* });
|
|
147
|
+
* const resultAsRetriever = await retriever.invoke("thud");
|
|
148
|
+
* console.log(resultAsRetriever);
|
|
149
|
+
*
|
|
150
|
+
* // Output: [Document({ metadata: { "baz":"bar" }, pageContent: "thud" })]
|
|
151
|
+
* ```
|
|
152
|
+
* </details>
|
|
153
|
+
*
|
|
154
|
+
* <br />
|
|
155
|
+
*/
|
|
156
|
+
var PostgresVectorStore = class PostgresVectorStore extends __langchain_core_vectorstores.VectorStore {
|
|
157
|
+
engine;
|
|
158
|
+
embeddings;
|
|
159
|
+
tableName;
|
|
160
|
+
schemaName;
|
|
161
|
+
contentColumn;
|
|
162
|
+
embeddingColumn;
|
|
163
|
+
metadataColumns;
|
|
164
|
+
idColumn;
|
|
165
|
+
metadataJsonColumn;
|
|
166
|
+
distanceStrategy;
|
|
167
|
+
k;
|
|
168
|
+
fetchK;
|
|
169
|
+
lambdaMult;
|
|
170
|
+
indexQueryOptions;
|
|
171
|
+
/**
|
|
172
|
+
* Initializes a new vector store with embeddings and database configuration.
|
|
173
|
+
*
|
|
174
|
+
* @param embeddings - Instance of `EmbeddingsInterface` used to embed queries.
|
|
175
|
+
* @param dbConfig - Configuration settings for the database or storage system.
|
|
176
|
+
*/
|
|
177
|
+
constructor(embeddings, dbConfig) {
|
|
178
|
+
super(embeddings, dbConfig);
|
|
179
|
+
this.embeddings = embeddings;
|
|
180
|
+
this.engine = dbConfig.engine;
|
|
181
|
+
this.tableName = dbConfig.tableName;
|
|
182
|
+
this.schemaName = dbConfig.schemaName;
|
|
183
|
+
this.contentColumn = dbConfig.contentColumn;
|
|
184
|
+
this.embeddingColumn = dbConfig.embeddingColumn;
|
|
185
|
+
this.metadataColumns = dbConfig.metadataColumns ? dbConfig.metadataColumns : [];
|
|
186
|
+
this.idColumn = dbConfig.idColumn;
|
|
187
|
+
this.metadataJsonColumn = dbConfig.metadataJsonColumn;
|
|
188
|
+
this.distanceStrategy = dbConfig.distanceStrategy;
|
|
189
|
+
this.k = dbConfig.k;
|
|
190
|
+
this.fetchK = dbConfig.fetchK;
|
|
191
|
+
this.lambdaMult = dbConfig.lambdaMult;
|
|
192
|
+
this.indexQueryOptions = dbConfig.indexQueryOptions;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Create a new PostgresVectorStore instance.
|
|
196
|
+
* @param {PostgresEngine} engine Required - Connection pool engine for managing connections to Cloud SQL for PostgreSQL database.
|
|
197
|
+
* @param {Embeddings} embeddings Required - Text embedding model to use.
|
|
198
|
+
* @param {string} tableName Required - Name of an existing table or table to be created.
|
|
199
|
+
* @param {string} schemaName Database schema name of the table. Defaults to "public".
|
|
200
|
+
* @param {string} contentColumn Column that represent a Document's page_content. Defaults to "content".
|
|
201
|
+
* @param {string} embeddingColumn Column for embedding vectors. The embedding is generated from the document value. Defaults to "embedding".
|
|
202
|
+
* @param {Array<string>} metadataColumns Column(s) that represent a document's metadata.
|
|
203
|
+
* @param {Array<string>} ignoreMetadataColumns Optional - Column(s) to ignore in pre-existing tables for a document's metadata. Can not be used with metadata_columns.
|
|
204
|
+
* @param {string} idColumn Column that represents the Document's id. Defaults to "langchain_id".
|
|
205
|
+
* @param {string} metadataJsonColumn Optional - Column to store metadata as JSON. Defaults to "langchain_metadata".
|
|
206
|
+
* @param {DistanceStrategy} distanceStrategy Distance strategy to use for vector similarity search. Defaults to COSINE_DISTANCE.
|
|
207
|
+
* @param {number} k Number of Documents to return from search. Defaults to 4.
|
|
208
|
+
* @param {number} fetchK Number of Documents to fetch to pass to MMR algorithm.
|
|
209
|
+
* @param {number} lambdaMult Number between 0 and 1 that determines the degree of diversity among the results with 0 corresponding to maximum diversity and 1 to minimum diversity. Defaults to 0.5.
|
|
210
|
+
* @param {QueryOptions} indexQueryOptions Optional - Index query option.
|
|
211
|
+
* @returns PostgresVectorStore instance.
|
|
212
|
+
*/
|
|
213
|
+
static async initialize(engine, embeddings, tableName, { schemaName = "public", contentColumn = "content", embeddingColumn = "embedding", metadataColumns = [], ignoreMetadataColumns, idColumn = "langchain_id", metadataJsonColumn = "langchain_metadata", distanceStrategy = require_indexes.DEFAULT_DISTANCE_STRATEGY, k = 4, fetchK = 20, lambdaMult = .5, indexQueryOptions } = {}) {
|
|
214
|
+
if (metadataColumns !== void 0 && ignoreMetadataColumns !== void 0) throw Error("Can not use both metadata_columns and ignore_metadata_columns.");
|
|
215
|
+
const { rows } = await engine.pool.raw(`SELECT column_name, data_type FROM information_schema.columns WHERE table_name = '${tableName}' AND table_schema = '${schemaName}'`);
|
|
216
|
+
const columns = {};
|
|
217
|
+
for (const index in rows) if (rows[index]) {
|
|
218
|
+
const row = rows[index];
|
|
219
|
+
columns[row.column_name] = row.data_type;
|
|
220
|
+
}
|
|
221
|
+
if (!Object.prototype.hasOwnProperty.call(columns, idColumn)) throw Error(`Id column: ${idColumn}, does not exist.`);
|
|
222
|
+
if (!Object.prototype.hasOwnProperty.call(columns, contentColumn)) throw Error(`Content column: ${contentColumn}, does not exist.`);
|
|
223
|
+
const contentType = columns[contentColumn];
|
|
224
|
+
if (contentType !== "text" && !contentType.includes("char")) throw Error(`Content column: ${contentColumn}, is type: ${contentType}. It must be a type of character string.`);
|
|
225
|
+
if (!Object.prototype.hasOwnProperty.call(columns, embeddingColumn)) throw Error(`Embedding column: ${embeddingColumn}, does not exist.`);
|
|
226
|
+
if (columns[embeddingColumn] !== "USER-DEFINED") throw Error(`Embedding column: ${embeddingColumn} is not of type Vector.`);
|
|
227
|
+
const jsonColumn = Object.prototype.hasOwnProperty.call(columns, metadataJsonColumn) ? metadataJsonColumn : "";
|
|
228
|
+
for (const column of metadataColumns) if (!Object.prototype.hasOwnProperty.call(columns, column)) throw Error(`Metadata column: ${column}, does not exist.`);
|
|
229
|
+
const allColumns = columns;
|
|
230
|
+
let allMetadataColumns = [];
|
|
231
|
+
if (ignoreMetadataColumns !== void 0 && ignoreMetadataColumns.length > 0) {
|
|
232
|
+
for (const column of ignoreMetadataColumns) delete allColumns[column];
|
|
233
|
+
delete allColumns[idColumn];
|
|
234
|
+
delete allColumns[contentColumn];
|
|
235
|
+
delete allColumns[embeddingColumn];
|
|
236
|
+
allMetadataColumns = Object.keys(allColumns);
|
|
237
|
+
} else for (const column of metadataColumns) if (Object.prototype.hasOwnProperty.call(allColumns, column)) allMetadataColumns.push(column);
|
|
238
|
+
return new PostgresVectorStore(embeddings, {
|
|
239
|
+
engine,
|
|
240
|
+
tableName,
|
|
241
|
+
schemaName,
|
|
242
|
+
contentColumn,
|
|
243
|
+
embeddingColumn,
|
|
244
|
+
metadataColumns: allMetadataColumns,
|
|
245
|
+
idColumn,
|
|
246
|
+
metadataJsonColumn: jsonColumn,
|
|
247
|
+
distanceStrategy,
|
|
248
|
+
k,
|
|
249
|
+
fetchK,
|
|
250
|
+
lambdaMult,
|
|
251
|
+
indexQueryOptions
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
static async fromTexts(texts, metadatas, embeddings, dbConfig) {
|
|
255
|
+
const documents = [];
|
|
256
|
+
for (let i = 0; i < texts.length; i += 1) {
|
|
257
|
+
const doc = new __langchain_core_documents.Document({
|
|
258
|
+
pageContent: texts[i],
|
|
259
|
+
metadata: Array.isArray(metadatas) ? metadatas[i] : metadatas
|
|
260
|
+
});
|
|
261
|
+
documents.push(doc);
|
|
262
|
+
}
|
|
263
|
+
return PostgresVectorStore.fromDocuments(documents, embeddings, dbConfig);
|
|
264
|
+
}
|
|
265
|
+
static async fromDocuments(docs, embeddings, dbConfig) {
|
|
266
|
+
const { engine } = dbConfig;
|
|
267
|
+
const { tableName } = dbConfig;
|
|
268
|
+
const config = dbConfig.dbConfig;
|
|
269
|
+
const vectorStore = await this.initialize(engine, embeddings, tableName, config);
|
|
270
|
+
await vectorStore.addDocuments(docs);
|
|
271
|
+
return vectorStore;
|
|
272
|
+
}
|
|
273
|
+
async addVectors(vectors, documents, options) {
|
|
274
|
+
let ids = [];
|
|
275
|
+
const metadatas = [];
|
|
276
|
+
if (vectors.length !== documents.length) throw new Error("The number of vectors must match the number of documents provided.");
|
|
277
|
+
if (options?.ids) ids = options.ids;
|
|
278
|
+
else documents.forEach((document) => {
|
|
279
|
+
if (document.id !== void 0) ids.push(document.id);
|
|
280
|
+
else ids.push((0, uuid.v4)());
|
|
281
|
+
});
|
|
282
|
+
if (options && options.ids && options.ids.length !== documents.length) throw new Error("The number of ids must match the number of documents provided.");
|
|
283
|
+
documents.forEach((document) => {
|
|
284
|
+
metadatas.push(document.metadata);
|
|
285
|
+
});
|
|
286
|
+
const tuples = require_utils.customZip(ids, documents, vectors, metadatas);
|
|
287
|
+
for (const [id, document, embedding, metadata] of tuples) {
|
|
288
|
+
const metadataColNames = this.metadataColumns.length > 0 ? `, "${this.metadataColumns.join("\",\"")}"` : "";
|
|
289
|
+
let stmt = `INSERT INTO "${this.schemaName}"."${this.tableName}"("${this.idColumn}", "${this.contentColumn}", "${this.embeddingColumn}" ${metadataColNames}`;
|
|
290
|
+
const values = {
|
|
291
|
+
id,
|
|
292
|
+
content: document.pageContent,
|
|
293
|
+
embedding: `[${embedding.toString()}]`
|
|
294
|
+
};
|
|
295
|
+
let valuesStmt = " VALUES (:id, :content, :embedding";
|
|
296
|
+
const extra = metadata;
|
|
297
|
+
for (const metadataColumn of this.metadataColumns) if (Object.prototype.hasOwnProperty.call(metadata, metadataColumn)) {
|
|
298
|
+
valuesStmt += `, :${metadataColumn}`;
|
|
299
|
+
values[metadataColumn] = metadata[metadataColumn];
|
|
300
|
+
delete extra[metadataColumn];
|
|
301
|
+
} else valuesStmt += " ,null";
|
|
302
|
+
stmt += this.metadataJsonColumn ? `, ${this.metadataJsonColumn})` : ")";
|
|
303
|
+
if (this.metadataJsonColumn) {
|
|
304
|
+
valuesStmt += ", :extra)";
|
|
305
|
+
Object.assign(values, { extra: JSON.stringify(extra) });
|
|
306
|
+
} else valuesStmt += ")";
|
|
307
|
+
const query = stmt + valuesStmt;
|
|
308
|
+
await this.engine.pool.raw(query, values);
|
|
309
|
+
}
|
|
310
|
+
return options?.ids;
|
|
311
|
+
}
|
|
312
|
+
_vectorstoreType() {
|
|
313
|
+
return "cloudsqlpostgresql";
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Adds documents to the vector store, embedding them first through the
|
|
317
|
+
* `embeddings` instance.
|
|
318
|
+
*
|
|
319
|
+
* @param documents - Array of documents to embed and add.
|
|
320
|
+
* @param options - Optional configuration for embedding and storing documents.
|
|
321
|
+
* @returns A promise resolving to an array of document IDs or void, based on implementation.
|
|
322
|
+
* @abstract
|
|
323
|
+
*/
|
|
324
|
+
async addDocuments(documents, options) {
|
|
325
|
+
const texts = [];
|
|
326
|
+
for (const doc of documents) texts.push(doc.pageContent);
|
|
327
|
+
const embeddings = await this.embeddings.embedDocuments(texts);
|
|
328
|
+
const results = await this.addVectors(embeddings, documents, options);
|
|
329
|
+
return results;
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* Deletes documents from the vector store based on the specified ids.
|
|
333
|
+
*
|
|
334
|
+
* @param params - Flexible key-value pairs defining conditions for document deletion.
|
|
335
|
+
* @param ids - Optional: Property of {params} that contains the array of ids to be deleted
|
|
336
|
+
* @returns A promise that resolves once the deletion is complete.
|
|
337
|
+
*/
|
|
338
|
+
async delete(params) {
|
|
339
|
+
if (params.ids === void 0) return;
|
|
340
|
+
const idList = params.ids.map((id) => `'${id}'`).join(", ");
|
|
341
|
+
const query = `DELETE FROM "${this.schemaName}"."${this.tableName}" WHERE "${this.idColumn}" in (${idList})`;
|
|
342
|
+
await this.engine.pool.raw(query);
|
|
343
|
+
}
|
|
344
|
+
async similaritySearchVectorWithScore(embedding, k, filter) {
|
|
345
|
+
const results = await this.queryCollection(embedding, k, filter);
|
|
346
|
+
const documentsWithScores = [];
|
|
347
|
+
for (const row of results) {
|
|
348
|
+
const metadata = this.metadataJsonColumn && row[this.metadataJsonColumn] ? row[this.metadataJsonColumn] : {};
|
|
349
|
+
for (const col of this.metadataColumns) metadata[col] = row[col];
|
|
350
|
+
documentsWithScores.push([new __langchain_core_documents.Document({
|
|
351
|
+
pageContent: row[this.contentColumn],
|
|
352
|
+
metadata
|
|
353
|
+
}), row.distance]);
|
|
354
|
+
}
|
|
355
|
+
return documentsWithScores;
|
|
356
|
+
}
|
|
357
|
+
async queryCollection(embedding, k, filter) {
|
|
358
|
+
const fetchK = k ?? this.k;
|
|
359
|
+
const { operator } = this.distanceStrategy;
|
|
360
|
+
const { searchFunction } = this.distanceStrategy;
|
|
361
|
+
const _filter = filter !== void 0 ? `WHERE ${filter}` : "";
|
|
362
|
+
const metadataColNames = this.metadataColumns.length > 0 ? `, "${this.metadataColumns.join("\",\"")}"` : "";
|
|
363
|
+
const metadataJsonColName = this.metadataJsonColumn ? `, "${this.metadataJsonColumn}"` : "";
|
|
364
|
+
const query = `SELECT "${this.idColumn}", "${this.contentColumn}", "${this.embeddingColumn}" ${metadataColNames} ${metadataJsonColName}, ${searchFunction}("${this.embeddingColumn}", '[${embedding}]') as distance FROM "${this.schemaName}"."${this.tableName}" ${_filter} ORDER BY "${this.embeddingColumn}" ${operator} '[${embedding}]' LIMIT ${fetchK};`;
|
|
365
|
+
if (this.indexQueryOptions) await this.engine.pool.raw(`SET LOCAL ${this.indexQueryOptions.to_string()}`);
|
|
366
|
+
const { rows } = await this.engine.pool.raw(query);
|
|
367
|
+
return rows;
|
|
368
|
+
}
|
|
369
|
+
async maxMarginalRelevanceSearch(query, options) {
|
|
370
|
+
const vector = await this.embeddings.embedQuery(query);
|
|
371
|
+
const results = await this.queryCollection(vector, options?.k, options?.filter);
|
|
372
|
+
const k = options?.k ? options.k : this.k;
|
|
373
|
+
const documentsWithScores = [];
|
|
374
|
+
let docsList = [];
|
|
375
|
+
const embeddingList = results.map((row) => JSON.parse(row[this.embeddingColumn]));
|
|
376
|
+
const mmrSelected = (0, __langchain_core_utils_math.maximalMarginalRelevance)(vector, embeddingList, options?.lambda, k);
|
|
377
|
+
for (const row of results) {
|
|
378
|
+
const metadata = this.metadataJsonColumn && row[this.metadataJsonColumn] ? row[this.metadataJsonColumn] : {};
|
|
379
|
+
for (const col of this.metadataColumns) metadata[col] = row[col];
|
|
380
|
+
documentsWithScores.push([new __langchain_core_documents.Document({
|
|
381
|
+
pageContent: row[this.contentColumn],
|
|
382
|
+
metadata
|
|
383
|
+
}), row.distance]);
|
|
384
|
+
}
|
|
385
|
+
docsList = documentsWithScores.filter((_, i) => mmrSelected.includes(i)).map(([doc, _]) => doc);
|
|
386
|
+
return docsList;
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Create an index on the vector store table
|
|
390
|
+
* @param {BaseIndex} index
|
|
391
|
+
* @param {string} name Optional
|
|
392
|
+
* @param {boolean} concurrently Optional
|
|
393
|
+
*/
|
|
394
|
+
async applyVectorIndex(index, name, concurrently = false) {
|
|
395
|
+
if (index.constructor.name === "ExactNearestNeighbor") {
|
|
396
|
+
await this.dropVectorIndex();
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
const filter = index.partialIndexes && index.partialIndexes?.length > 0 ? `WHERE (${index.partialIndexes})` : "";
|
|
400
|
+
const params = `WITH ${index.indexOptions()}`;
|
|
401
|
+
const funct = index.distanceStrategy.indexFunction;
|
|
402
|
+
let indexName = name;
|
|
403
|
+
if (!indexName) if (!index.name) indexName = this.tableName + require_indexes.DEFAULT_INDEX_NAME_SUFFIX;
|
|
404
|
+
else indexName = index.name;
|
|
405
|
+
const stmt = `CREATE INDEX ${concurrently ? "CONCURRENTLY" : ""} ${indexName} ON "${this.schemaName}"."${this.tableName}" USING ${index.indexType} (${this.embeddingColumn} ${funct}) ${params} ${filter};`;
|
|
406
|
+
await this.engine.pool.raw(stmt);
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* Check if index exists in the table.
|
|
410
|
+
* @param {string} indexName Optional - index name
|
|
411
|
+
*/
|
|
412
|
+
async isValidIndex(indexName) {
|
|
413
|
+
const idxName = indexName || this.tableName + require_indexes.DEFAULT_INDEX_NAME_SUFFIX;
|
|
414
|
+
const stmt = `SELECT tablename, indexname
|
|
572
415
|
FROM pg_indexes
|
|
573
416
|
WHERE tablename = '${this.tableName}' AND schemaname = '${this.schemaName}' AND indexname = '${idxName}';`;
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
}
|
|
417
|
+
const { rows } = await this.engine.pool.raw(stmt);
|
|
418
|
+
return rows.length === 1;
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* Drop the vector index
|
|
422
|
+
* @param {string} indexName Optional - index name
|
|
423
|
+
*/
|
|
424
|
+
async dropVectorIndex(indexName) {
|
|
425
|
+
const idxName = indexName || this.tableName + require_indexes.DEFAULT_INDEX_NAME_SUFFIX;
|
|
426
|
+
const query = `DROP INDEX IF EXISTS ${idxName};`;
|
|
427
|
+
await this.engine.pool.raw(query);
|
|
428
|
+
}
|
|
429
|
+
/**
|
|
430
|
+
* Re-index the vector store table
|
|
431
|
+
* @param {string} indexName Optional - index name
|
|
432
|
+
*/
|
|
433
|
+
async reIndex(indexName) {
|
|
434
|
+
const idxName = indexName || this.tableName + require_indexes.DEFAULT_INDEX_NAME_SUFFIX;
|
|
435
|
+
const query = `REINDEX INDEX ${idxName};`;
|
|
436
|
+
await this.engine.pool.raw(query);
|
|
437
|
+
}
|
|
438
|
+
};
|
|
439
|
+
|
|
440
|
+
//#endregion
|
|
596
441
|
exports.PostgresVectorStore = PostgresVectorStore;
|
|
597
|
-
|
|
442
|
+
//# sourceMappingURL=vectorstore.cjs.map
|