From 35362a79408c4a2d307c36e91136bbab934d61c9 Mon Sep 17 00:00:00 2001 From: Clemens Peters Date: Fri, 2 Aug 2024 18:11:01 +0200 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=A8=20Add=20namespace=20to=20allow=20?= =?UTF-8?q?indexing=20multiple=20tables=20within=20the=20same=20schema?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/vectorstores/pgvector.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/libs/langchain-community/src/vectorstores/pgvector.ts b/libs/langchain-community/src/vectorstores/pgvector.ts index 6a58c4ba5cd0..a37d8d1d6a6c 100644 --- a/libs/langchain-community/src/vectorstores/pgvector.ts +++ b/libs/langchain-community/src/vectorstores/pgvector.ts @@ -685,6 +685,7 @@ export class PGVectorStore extends VectorStore { * @param m - The max number of connections per layer (16 by default). Index build time improves with smaller values, while higher values can speed up search queries. * @param efConstruction - The size of the dynamic candidate list for constructing the graph (64 by default). A higher value can potentially improve the index quality at the cost of index build time. * @param distanceFunction - The distance function name you want to use, is automatically selected based on the distanceStrategy. + * @param namespace - The namespace is used to create the index with a specific name. This is useful when you want to create multiple indexes on the same database schema (within the same schema in PostgreSQL, the index name must be unique across all tables). * @returns Promise that resolves with the query response of creating the index. */ async createHnswIndex(config: { @@ -692,8 +693,10 @@ export class PGVectorStore extends VectorStore { m?: number; efConstruction?: number; distanceFunction?: string; + namespace?: string; }): Promise { let idxDistanceFunction = config?.distanceFunction || "vector_cosine_ops"; + const prefix = config?.namespace ? `${config.namespace}_` : ""; switch (this.distanceStrategy) { case "cosine": @@ -709,12 +712,10 @@ export class PGVectorStore extends VectorStore { throw new Error(`Unknown distance strategy: ${this.distanceStrategy}`); } - const createIndexQuery = `CREATE INDEX IF NOT EXISTS ${ - this.vectorColumnName - }_embedding_hnsw_idx - ON ${this.computedTableName} USING hnsw ((${ - this.vectorColumnName - }::vector(${config.dimensions})) ${idxDistanceFunction}) + const createIndexQuery = `CREATE INDEX IF NOT EXISTS ${prefix}${this.vectorColumnName + }_embedding_hnsw_idx + ON ${this.computedTableName} USING hnsw ((${this.vectorColumnName + }::vector(${config.dimensions})) ${idxDistanceFunction}) WITH ( m=${config?.m || 16}, ef_construction=${config?.efConstruction || 64} From 15225eb8028a368d010abefb7516a664f4a8c66b Mon Sep 17 00:00:00 2001 From: Clemens Peters Date: Fri, 2 Aug 2024 23:07:19 +0200 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=9A=A8=20Auto=20style=20fixes=20with?= =?UTF-8?q?=20`yarn=20format`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/langchain-community/src/vectorstores/pgvector.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/libs/langchain-community/src/vectorstores/pgvector.ts b/libs/langchain-community/src/vectorstores/pgvector.ts index a37d8d1d6a6c..8f4e564f2caf 100644 --- a/libs/langchain-community/src/vectorstores/pgvector.ts +++ b/libs/langchain-community/src/vectorstores/pgvector.ts @@ -712,10 +712,12 @@ export class PGVectorStore extends VectorStore { throw new Error(`Unknown distance strategy: ${this.distanceStrategy}`); } - const createIndexQuery = `CREATE INDEX IF NOT EXISTS ${prefix}${this.vectorColumnName - }_embedding_hnsw_idx - ON ${this.computedTableName} USING hnsw ((${this.vectorColumnName - }::vector(${config.dimensions})) ${idxDistanceFunction}) + const createIndexQuery = `CREATE INDEX IF NOT EXISTS ${prefix}${ + this.vectorColumnName + }_embedding_hnsw_idx + ON ${this.computedTableName} USING hnsw ((${ + this.vectorColumnName + }::vector(${config.dimensions})) ${idxDistanceFunction}) WITH ( m=${config?.m || 16}, ef_construction=${config?.efConstruction || 64}