pgvector
Postgres extension.
This guide provides a quick overview for getting started with PGVector vector stores. For detailed documentation of all PGVectorStore
features and configurations head to the API reference.
Class | Package | PY support | Package latest |
---|---|---|---|
PGVectorStore | @langchain/community | ✅ |
pgvector
extension enabled. You’ll also need to install the @langchain/community
integration package with the pg
package as a peer dependency.
This guide will also use OpenAI embeddings, which require you to install the @langchain/openai
integration package. You can also use other supported embeddings models if you wish.
We’ll also use the uuid
package to generate ids in the required format.
pgvector
team.
Create a file with the below content named docker-compose.yml:
docker compose up
to start the container.
You can find more information on how to setup pgvector in the official repository.
node-postgres
docs.
If you are using OpenAI embeddings for this guide, you’ll need to set your OpenAI key as well:
.initialize()
static method. This will automatically check for the presence of a table, given by tableName
in the passed config
. If it is not there, it will create it with the required columns.
in
operatornotIn
operatorarrayContains
operatorPGVectorStore
instances directly via the constructor.
Note that you should call .initialize()
to set up your database at least once to set up your tables properly before using the constructor.
similaritySearchVectorWithScore
execution time. To create the HNSW index on your vector column, use the createHnswIndex()
method.
The method parameters include:
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.
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.
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.
distanceFunction?
: The distance function name you want to use, is automatically selected based on the distanceStrategy.
PGVectorStore
features and configurations head to the API reference.