Use this file to discover all available pages before exploring further.
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.
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.
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 upservices: 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
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.
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:
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 { 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 });
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.
* 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:
* [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"}]
[ 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 }]
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.
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.