You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
By default, the extension performs a sequential scan search, with 100% recall. You might consider creating an HNSW index for approximate nearest neighbor (ANN) search to speed up similaritySearchVectorWithScore execution time. To create the HNSW index on your vector column, use the `createHnswIndex()` method:
81
+
82
+
The method parameters include:
83
+
84
+
**dimensions**: Defines the number of dimensions in your vector data type, up to 2000. For example, use 1536 for OpenAI's `text-embedding-ada-002` and Amazon's `amazon.titan-embed-text-v1` models.
85
+
86
+
**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.
87
+
88
+
**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.
89
+
90
+
**distanceFunction?**: The distance function name you want to use, is automatically selected based on the distanceStrategy.
91
+
92
+
More info at the [`Pgvector GitHub project`](https://github.com/pgvector/pgvector?tab=readme-ov-file#hnsw) and the HNSW paper from Malkov Yu A. and Yashunin D. A.. 2020. [`Efficient and robust approximate nearest neighbor search using hierarchical navigable small world graphs`](https://arxiv.org/pdf/1603.09320)
93
+
94
+
import HnswExample from "@examples/indexes/vector_stores/pgvector_vectorstore/pgvector_hnsw.ts";
Copy file name to clipboardExpand all lines: libs/langchain-community/src/vectorstores/pgvector.ts
+51Lines changed: 51 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -677,4 +677,55 @@ export class PGVectorStore extends VectorStore {
677
677
this.client?.release();
678
678
returnthis.pool.end();
679
679
}
680
+
681
+
/**
682
+
* Method to create the HNSW index on the vector column.
683
+
*
684
+
* @param dimensions - Defines the number of dimensions in your vector data type, up to 2000. For example, use 1536 for OpenAI's text-embedding-ada-002 and Amazon's amazon.titan-embed-text-v1 models.
685
+
* @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.
686
+
* @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.
687
+
* @param distanceFunction - The distance function name you want to use, is automatically selected based on the distanceStrategy.
688
+
* @returns Promise that resolves with the query response of creating the index.
0 commit comments