Skip to main content

PGVectorStore

Compatibility

Only available on Node.js.

To enable vector search in generic PostgreSQL databases, LangChain.js supports using the 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.

Overviewโ€‹

Integration detailsโ€‹

ClassPackagePY supportPackage latest
PGVectorStore@langchain/communityโœ…NPM - Version

Setupโ€‹

To use PGVector vector stores, youโ€™ll need to set up a Postgres instance with the 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.

yarn add @langchain/community pg @langchain/openai uuid

Setting up an instanceโ€‹

There are many ways to connect to Postgres depending on how youโ€™ve set up your instance. Hereโ€™s one example of a local setup using a prebuilt Docker image provided by the pgvector team.

Create a file with the below content named docker-compose.yml:

# Run this command to start the database:
# docker-compose up --build
version: "3"
services:
db:
hostname: 127.0.0.1
image: pgvector/pgvector:pg16
ports:
- 5432:5432
restart: always
environment:
- POSTGRES_DB=api
- POSTGRES_USER=myuser
- POSTGRES_PASSWORD=ChangeMe
volumes:
- ./init.sql:/docker-entrypoint-initdb.d/init.sql

And then in the same directory, run docker compose up to start the container.

You can find more information on how to setup pgvector in the official repository.

Credentialsโ€‹

To connect to you Postgres instance, youโ€™ll need corresponding credentials. For a full list of supported options, see the node-postgres docs.

If you are using OpenAI embeddings for this guide, youโ€™ll need to set your OpenAI key as well:

process.env.OPENAI_API_KEY = "YOUR_API_KEY";

If you want to get automated tracing of your model calls you can also set your LangSmith API key by uncommenting below:

// process.env.LANGCHAIN_TRACING_V2="true"
// process.env.LANGCHAIN_API_KEY="your-api-key"

Instantiationโ€‹

To instantiate the vector store, call the .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.

Security

User-generated data such as usernames should not be used as input for table and column names. This may lead to SQL Injection!

import {
PGVectorStore,
DistanceStrategy,
} from "@langchain/community/vectorstores/pgvector";
import { OpenAIEmbeddings } from "@langchain/openai";
import { PoolConfig } from "pg";

const embeddings = new OpenAIEmbeddings({
model: "text-embedding-3-small",
});

// Sample config
const config = {
postgresConnectionOptions: {
type: "postgres",
host: "127.0.0.1",
port: 5433,
user: "myuser",
password: "ChangeMe",
database: "api",
} as PoolConfig,
tableName: "testlangchainjs",
columns: {
idColumnName: "id",
vectorColumnName: "vector",
contentColumnName: "content",
metadataColumnName: "metadata",
},
// supported distance strategies: cosine (default), innerProduct, or euclidean
distanceStrategy: "cosine" as DistanceStrategy,
};

const vectorStore = await PGVectorStore.initialize(embeddings, config);

Manage vector storeโ€‹

Add items to vector storeโ€‹

import { v4 as uuidv4 } from "uuid";
import type { Document } from "@langchain/core/documents";

const document1: Document = {
pageContent: "The powerhouse of the cell is the mitochondria",
metadata: { source: "https://example.com" },
};

const document2: Document = {
pageContent: "Buildings are made out of brick",
metadata: { source: "https://example.com" },
};

const document3: Document = {
pageContent: "Mitochondria are made out of lipids",
metadata: { source: "https://example.com" },
};

const document4: Document = {
pageContent: "The 2024 Olympics are in Paris",
metadata: { source: "https://example.com" },
};

const documents = [document1, document2, document3, document4];

const ids = [uuidv4(), uuidv4(), uuidv4(), uuidv4()];

await vectorStore.addDocuments(documents, { ids: ids });

Delete items from vector storeโ€‹

const id4 = ids[ids.length - 1];

await vectorStore.delete({ ids: [id4] });

Query vector storeโ€‹

Once your vector store has been created and the relevant documents have been added you will most likely wish to query it during the running of your chain or agent.

Query directlyโ€‹

Performing a simple similarity search can be done as follows:

const filter = { source: "https://example.com" };

const similaritySearchResults = await vectorStore.similaritySearch(
"biology",
2,
filter
);

for (const doc of similaritySearchResults) {
console.log(`* ${doc.pageContent} [${JSON.stringify(doc.metadata, null)}]`);
}
* The powerhouse of the cell is the mitochondria [{"source":"https://example.com"}]
* Mitochondria are made out of lipids [{"source":"https://example.com"}]

The above filter syntax supports exact match, but the following are also supported:

Using the in operatorโ€‹

{
"field": {
"in": ["value1", "value2"]
}
}

Using the arrayContains operatorโ€‹

{
"field": {
"arrayContains": ["value1", "value2"]
}
}

If you want to execute a similarity search and receive the corresponding scores you can run:

const similaritySearchWithScoreResults =
await vectorStore.similaritySearchWithScore("biology", 2, filter);

for (const [doc, score] of similaritySearchWithScoreResults) {
console.log(
`* [SIM=${score.toFixed(3)}] ${doc.pageContent} [${JSON.stringify(
doc.metadata
)}]`
);
}
* [SIM=0.835] The powerhouse of the cell is the mitochondria [{"source":"https://example.com"}]
* [SIM=0.852] Mitochondria are made out of lipids [{"source":"https://example.com"}]

Query by turning into retrieverโ€‹

You can also transform the vector store into a retriever for easier usage in your chains.

const retriever = vectorStore.asRetriever({
// Optional filter
filter: filter,
k: 2,
});
await retriever.invoke("biology");
[
Document {
pageContent: 'The powerhouse of the cell is the mitochondria',
metadata: { source: 'https://example.com' },
id: undefined
},
Document {
pageContent: 'Mitochondria are made out of lipids',
metadata: { source: 'https://example.com' },
id: undefined
}
]

Usage for retrieval-augmented generationโ€‹

For guides on how to use this vector store for retrieval-augmented generation (RAG), see the following sections:

Advanced: reusing connectionsโ€‹

You can reuse connections by creating a pool, then creating new PGVectorStore 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.

import { OpenAIEmbeddings } from "@langchain/openai";
import { PGVectorStore } from "@langchain/community/vectorstores/pgvector";
import pg from "pg";

// First, follow set-up instructions at
// https://js.langchain.com/docs/modules/indexes/vector_stores/integrations/pgvector

const reusablePool = new pg.Pool({
host: "127.0.0.1",
port: 5433,
user: "myuser",
password: "ChangeMe",
database: "api",
});

const originalConfig = {
pool: reusablePool,
tableName: "testlangchainjs",
collectionName: "sample",
collectionTableName: "collections",
columns: {
idColumnName: "id",
vectorColumnName: "vector",
contentColumnName: "content",
metadataColumnName: "metadata",
},
};

// Set up the DB.
// Can skip this step if you've already initialized the DB.
// await PGVectorStore.initialize(new OpenAIEmbeddings(), originalConfig);
const pgvectorStore = new PGVectorStore(new OpenAIEmbeddings(), originalConfig);

await pgvectorStore.addDocuments([
{ pageContent: "what's this", metadata: { a: 2 } },
{ pageContent: "Cat drinks milk", metadata: { a: 1 } },
]);

const results = await pgvectorStore.similaritySearch("water", 1);

console.log(results);

/*
[ Document { pageContent: 'Cat drinks milk', metadata: { a: 1 } } ]
*/

const pgvectorStore2 = new PGVectorStore(new OpenAIEmbeddings(), {
pool: reusablePool,
tableName: "testlangchainjs",
collectionTableName: "collections",
collectionName: "some_other_collection",
columns: {
idColumnName: "id",
vectorColumnName: "vector",
contentColumnName: "content",
metadataColumnName: "metadata",
},
});

const results2 = await pgvectorStore2.similaritySearch("water", 1);

console.log(results2);

/*
[]
*/

await reusablePool.end();

Create HNSW Indexโ€‹

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.

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.

For more info, see the Pgvector GitHub repo 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

import { OpenAIEmbeddings } from "@langchain/openai";
import {
DistanceStrategy,
PGVectorStore,
} from "@langchain/community/vectorstores/pgvector";
import { PoolConfig } from "pg";

// First, follow set-up instructions at
// https://js.langchain.com/docs/modules/indexes/vector_stores/integrations/pgvector

const hnswConfig = {
postgresConnectionOptions: {
type: "postgres",
host: "127.0.0.1",
port: 5433,
user: "myuser",
password: "ChangeMe",
database: "api",
} as PoolConfig,
tableName: "testlangchainjs",
columns: {
idColumnName: "id",
vectorColumnName: "vector",
contentColumnName: "content",
metadataColumnName: "metadata",
},
// supported distance strategies: cosine (default), innerProduct, or euclidean
distanceStrategy: "cosine" as DistanceStrategy,
};

const hnswPgVectorStore = await PGVectorStore.initialize(
new OpenAIEmbeddings(),
hnswConfig
);

// create the index
await hnswPgVectorStore.createHnswIndex({
dimensions: 1536,
efConstruction: 64,
m: 16,
});

await hnswPgVectorStore.addDocuments([
{ pageContent: "what's this", metadata: { a: 2, b: ["tag1", "tag2"] } },
{ pageContent: "Cat drinks milk", metadata: { a: 1, b: ["tag2"] } },
]);

const model = new OpenAIEmbeddings();
const query = await model.embedQuery("water");
const hnswResults = await hnswPgVectorStore.similaritySearchVectorWithScore(
query,
1
);

console.log(hnswResults);

await pgvectorStore.end();

Closing connectionsโ€‹

Make sure you close the connection when you are finished to avoid excessive resource consumption:

await vectorStore.end();

API referenceโ€‹

For detailed documentation of all PGVectorStore features and configurations head to the API reference.


Was this page helpful?


You can also leave detailed feedback on GitHub.