@langchain/google-cloud-sql-pg 0.0.2 → 1.0.1

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.
Files changed (48) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/LICENSE +6 -6
  3. package/README.md +1 -1
  4. package/dist/_virtual/rolldown_runtime.cjs +25 -0
  5. package/dist/chat_message_history.cjs +104 -137
  6. package/dist/chat_message_history.cjs.map +1 -0
  7. package/dist/chat_message_history.d.cts +57 -0
  8. package/dist/chat_message_history.d.ts +53 -43
  9. package/dist/chat_message_history.js +103 -133
  10. package/dist/chat_message_history.js.map +1 -0
  11. package/dist/engine.cjs +188 -242
  12. package/dist/engine.cjs.map +1 -0
  13. package/dist/engine.d.cts +138 -0
  14. package/dist/engine.d.ts +102 -80
  15. package/dist/engine.js +186 -234
  16. package/dist/engine.js.map +1 -0
  17. package/dist/index.cjs +21 -20
  18. package/dist/index.d.cts +6 -0
  19. package/dist/index.d.ts +6 -4
  20. package/dist/index.js +7 -4
  21. package/dist/indexes.cjs +96 -168
  22. package/dist/indexes.cjs.map +1 -0
  23. package/dist/indexes.d.cts +68 -0
  24. package/dist/indexes.d.ts +50 -47
  25. package/dist/indexes.js +90 -161
  26. package/dist/indexes.js.map +1 -0
  27. package/dist/loader.cjs +159 -242
  28. package/dist/loader.cjs.map +1 -0
  29. package/dist/loader.d.cts +101 -0
  30. package/dist/loader.d.ts +40 -26
  31. package/dist/loader.js +157 -237
  32. package/dist/loader.js.map +1 -0
  33. package/dist/utils/utils.cjs +36 -65
  34. package/dist/utils/utils.cjs.map +1 -0
  35. package/dist/utils/utils.js +36 -62
  36. package/dist/utils/utils.js.map +1 -0
  37. package/dist/vectorstore.cjs +438 -593
  38. package/dist/vectorstore.cjs.map +1 -0
  39. package/dist/vectorstore.d.cts +300 -0
  40. package/dist/vectorstore.d.ts +147 -130
  41. package/dist/vectorstore.js +436 -588
  42. package/dist/vectorstore.js.map +1 -0
  43. package/package.json +41 -48
  44. package/dist/utils/utils.d.ts +0 -22
  45. package/index.cjs +0 -1
  46. package/index.d.cts +0 -1
  47. package/index.d.ts +0 -1
  48. package/index.js +0 -1
@@ -1,597 +1,442 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PostgresVectorStore = void 0;
4
- const vectorstores_1 = require("@langchain/core/vectorstores");
5
- const documents_1 = require("@langchain/core/documents");
6
- const uuid_1 = require("uuid");
7
- const math_1 = require("@langchain/core/utils/math");
8
- const indexes_js_1 = require("./indexes.cjs");
9
- const utils_js_1 = require("./utils/utils.cjs");
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
- * 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
- class PostgresVectorStore extends vectorstores_1.VectorStore {
157
- /**
158
- * Initializes a new vector store with embeddings and database configuration.
159
- *
160
- * @param embeddings - Instance of `EmbeddingsInterface` used to embed queries.
161
- * @param dbConfig - Configuration settings for the database or storage system.
162
- */
163
- constructor(embeddings, dbConfig) {
164
- super(embeddings, dbConfig);
165
- Object.defineProperty(this, "engine", {
166
- enumerable: true,
167
- configurable: true,
168
- writable: true,
169
- value: void 0
170
- });
171
- Object.defineProperty(this, "embeddings", {
172
- enumerable: true,
173
- configurable: true,
174
- writable: true,
175
- value: void 0
176
- });
177
- Object.defineProperty(this, "tableName", {
178
- enumerable: true,
179
- configurable: true,
180
- writable: true,
181
- value: void 0
182
- });
183
- Object.defineProperty(this, "schemaName", {
184
- enumerable: true,
185
- configurable: true,
186
- writable: true,
187
- value: void 0
188
- });
189
- Object.defineProperty(this, "contentColumn", {
190
- enumerable: true,
191
- configurable: true,
192
- writable: true,
193
- value: void 0
194
- });
195
- Object.defineProperty(this, "embeddingColumn", {
196
- enumerable: true,
197
- configurable: true,
198
- writable: true,
199
- value: void 0
200
- });
201
- Object.defineProperty(this, "metadataColumns", {
202
- enumerable: true,
203
- configurable: true,
204
- writable: true,
205
- value: void 0
206
- });
207
- Object.defineProperty(this, "idColumn", {
208
- enumerable: true,
209
- configurable: true,
210
- writable: true,
211
- value: void 0
212
- });
213
- Object.defineProperty(this, "metadataJsonColumn", {
214
- enumerable: true,
215
- configurable: true,
216
- writable: true,
217
- value: void 0
218
- });
219
- Object.defineProperty(this, "distanceStrategy", {
220
- enumerable: true,
221
- configurable: true,
222
- writable: true,
223
- value: void 0
224
- });
225
- Object.defineProperty(this, "k", {
226
- enumerable: true,
227
- configurable: true,
228
- writable: true,
229
- value: void 0
230
- });
231
- Object.defineProperty(this, "fetchK", {
232
- enumerable: true,
233
- configurable: true,
234
- writable: true,
235
- value: void 0
236
- });
237
- Object.defineProperty(this, "lambdaMult", {
238
- enumerable: true,
239
- configurable: true,
240
- writable: true,
241
- value: void 0
242
- });
243
- Object.defineProperty(this, "indexQueryOptions", {
244
- enumerable: true,
245
- configurable: true,
246
- writable: true,
247
- value: void 0
248
- });
249
- this.embeddings = embeddings;
250
- this.engine = dbConfig.engine;
251
- this.tableName = dbConfig.tableName;
252
- this.schemaName = dbConfig.schemaName;
253
- this.contentColumn = dbConfig.contentColumn;
254
- this.embeddingColumn = dbConfig.embeddingColumn;
255
- this.metadataColumns = dbConfig.metadataColumns
256
- ? dbConfig.metadataColumns
257
- : [];
258
- this.idColumn = dbConfig.idColumn;
259
- this.metadataJsonColumn = dbConfig.metadataJsonColumn;
260
- this.distanceStrategy = dbConfig.distanceStrategy;
261
- this.k = dbConfig.k;
262
- this.fetchK = dbConfig.fetchK;
263
- this.lambdaMult = dbConfig.lambdaMult;
264
- this.indexQueryOptions = dbConfig.indexQueryOptions;
265
- }
266
- /**
267
- * Create a new PostgresVectorStore instance.
268
- * @param {PostgresEngine} engine Required - Connection pool engine for managing connections to Cloud SQL for PostgreSQL database.
269
- * @param {Embeddings} embeddings Required - Text embedding model to use.
270
- * @param {string} tableName Required - Name of an existing table or table to be created.
271
- * @param {string} schemaName Database schema name of the table. Defaults to "public".
272
- * @param {string} contentColumn Column that represent a Document's page_content. Defaults to "content".
273
- * @param {string} embeddingColumn Column for embedding vectors. The embedding is generated from the document value. Defaults to "embedding".
274
- * @param {Array<string>} metadataColumns Column(s) that represent a document's metadata.
275
- * @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.
276
- * @param {string} idColumn Column that represents the Document's id. Defaults to "langchain_id".
277
- * @param {string} metadataJsonColumn Optional - Column to store metadata as JSON. Defaults to "langchain_metadata".
278
- * @param {DistanceStrategy} distanceStrategy Distance strategy to use for vector similarity search. Defaults to COSINE_DISTANCE.
279
- * @param {number} k Number of Documents to return from search. Defaults to 4.
280
- * @param {number} fetchK Number of Documents to fetch to pass to MMR algorithm.
281
- * @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.
282
- * @param {QueryOptions} indexQueryOptions Optional - Index query option.
283
- * @returns PostgresVectorStore instance.
284
- */
285
- static async initialize(engine, embeddings, tableName, { schemaName = "public", contentColumn = "content", embeddingColumn = "embedding", metadataColumns = [], ignoreMetadataColumns, idColumn = "langchain_id", metadataJsonColumn = "langchain_metadata", distanceStrategy = indexes_js_1.DEFAULT_DISTANCE_STRATEGY, k = 4, fetchK = 20, lambdaMult = 0.5, indexQueryOptions, } = {}) {
286
- if (metadataColumns !== undefined && ignoreMetadataColumns !== undefined) {
287
- throw Error("Can not use both metadata_columns and ignore_metadata_columns.");
288
- }
289
- const { rows } = await engine.pool.raw(`SELECT column_name, data_type FROM information_schema.columns WHERE table_name = '${tableName}' AND table_schema = '${schemaName}'`);
290
- const columns = {};
291
- for (const index in rows) {
292
- if (rows[index]) {
293
- const row = rows[index];
294
- columns[row.column_name] = row.data_type;
295
- }
296
- }
297
- if (!Object.prototype.hasOwnProperty.call(columns, idColumn)) {
298
- throw Error(`Id column: ${idColumn}, does not exist.`);
299
- }
300
- if (!Object.prototype.hasOwnProperty.call(columns, contentColumn)) {
301
- throw Error(`Content column: ${contentColumn}, does not exist.`);
302
- }
303
- const contentType = columns[contentColumn];
304
- if (contentType !== "text" && !contentType.includes("char")) {
305
- throw Error(`Content column: ${contentColumn}, is type: ${contentType}. It must be a type of character string.`);
306
- }
307
- if (!Object.prototype.hasOwnProperty.call(columns, embeddingColumn)) {
308
- throw Error(`Embedding column: ${embeddingColumn}, does not exist.`);
309
- }
310
- if (columns[embeddingColumn] !== "USER-DEFINED") {
311
- throw Error(`Embedding column: ${embeddingColumn} is not of type Vector.`);
312
- }
313
- const jsonColumn = Object.prototype.hasOwnProperty.call(columns, metadataJsonColumn)
314
- ? metadataJsonColumn
315
- : "";
316
- for (const column of metadataColumns) {
317
- if (!Object.prototype.hasOwnProperty.call(columns, column)) {
318
- throw Error(`Metadata column: ${column}, does not exist.`);
319
- }
320
- }
321
- const allColumns = columns;
322
- let allMetadataColumns = [];
323
- if (ignoreMetadataColumns !== undefined &&
324
- ignoreMetadataColumns.length > 0) {
325
- for (const column of ignoreMetadataColumns) {
326
- delete allColumns[column];
327
- }
328
- delete allColumns[idColumn];
329
- delete allColumns[contentColumn];
330
- delete allColumns[embeddingColumn];
331
- allMetadataColumns = Object.keys(allColumns);
332
- }
333
- else {
334
- for (const column of metadataColumns) {
335
- if (Object.prototype.hasOwnProperty.call(allColumns, column)) {
336
- allMetadataColumns.push(column);
337
- }
338
- }
339
- }
340
- return new PostgresVectorStore(embeddings, {
341
- engine,
342
- tableName,
343
- schemaName,
344
- contentColumn,
345
- embeddingColumn,
346
- metadataColumns: allMetadataColumns,
347
- idColumn,
348
- metadataJsonColumn: jsonColumn,
349
- distanceStrategy,
350
- k,
351
- fetchK,
352
- lambdaMult,
353
- indexQueryOptions,
354
- });
355
- }
356
- static async fromTexts(texts, metadatas, embeddings, dbConfig) {
357
- const documents = [];
358
- for (let i = 0; i < texts.length; i += 1) {
359
- const doc = new documents_1.Document({
360
- pageContent: texts[i],
361
- metadata: Array.isArray(metadatas) ? metadatas[i] : metadatas,
362
- });
363
- documents.push(doc);
364
- }
365
- return PostgresVectorStore.fromDocuments(documents, embeddings, dbConfig);
366
- }
367
- static async fromDocuments(docs, embeddings, dbConfig) {
368
- const { engine } = dbConfig;
369
- const { tableName } = dbConfig;
370
- const config = dbConfig.dbConfig;
371
- const vectorStore = await this.initialize(engine, embeddings, tableName, config);
372
- await vectorStore.addDocuments(docs);
373
- return vectorStore;
374
- }
375
- async addVectors(vectors, documents, options) {
376
- let ids = [];
377
- const metadatas = [];
378
- if (vectors.length !== documents.length) {
379
- throw new Error("The number of vectors must match the number of documents provided.");
380
- }
381
- if (options?.ids) {
382
- ids = options.ids;
383
- }
384
- else {
385
- documents.forEach((document) => {
386
- if (document.id !== undefined) {
387
- ids.push(document.id);
388
- }
389
- else {
390
- ids.push((0, uuid_1.v4)());
391
- }
392
- });
393
- }
394
- if (options && options.ids && options.ids.length !== documents.length) {
395
- throw new Error("The number of ids must match the number of documents provided.");
396
- }
397
- documents.forEach((document) => {
398
- metadatas.push(document.metadata);
399
- });
400
- const tuples = (0, utils_js_1.customZip)(ids, documents, vectors, metadatas);
401
- // Insert embeddings
402
- for (const [id, document, embedding, metadata] of tuples) {
403
- const metadataColNames = this.metadataColumns.length > 0
404
- ? `, "${this.metadataColumns.join('","')}"`
405
- : "";
406
- let stmt = `INSERT INTO "${this.schemaName}"."${this.tableName}"("${this.idColumn}", "${this.contentColumn}", "${this.embeddingColumn}" ${metadataColNames}`;
407
- const values = {
408
- id,
409
- content: document.pageContent,
410
- embedding: `[${embedding.toString()}]`,
411
- };
412
- let valuesStmt = " VALUES (:id, :content, :embedding";
413
- // Add metadata
414
- const extra = metadata;
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
- const { rows } = await this.engine.pool.raw(stmt);
575
- return rows.length === 1;
576
- }
577
- /**
578
- * Drop the vector index
579
- * @param {string} indexName Optional - index name
580
- */
581
- async dropVectorIndex(indexName) {
582
- const idxName = indexName || this.tableName + indexes_js_1.DEFAULT_INDEX_NAME_SUFFIX;
583
- const query = `DROP INDEX IF EXISTS ${idxName};`;
584
- await this.engine.pool.raw(query);
585
- }
586
- /**
587
- * Re-index the vector store table
588
- * @param {string} indexName Optional - index name
589
- */
590
- async reIndex(indexName) {
591
- const idxName = indexName || this.tableName + indexes_js_1.DEFAULT_INDEX_NAME_SUFFIX;
592
- const query = `REINDEX INDEX ${idxName};`;
593
- await this.engine.pool.raw(query);
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
- exports.default = PostgresVectorStore;
442
+ //# sourceMappingURL=vectorstore.cjs.map