Skip to content

Commit

Permalink
feat: Add support for generic PGVector vector store (langchain-ai#2657)
Browse files Browse the repository at this point in the history
* feat: added pgevtor class

* feat: finished pgvector integration

* chore: update create-entrypoints with pgvector vectorstore

* chore: add docs

* nit: final cleanups

* fix: correct metadata type

* fix: allow for optional columns & add defaults

* fix: update doc demo code to new config schema

* chore: lint/format

* Close connections properly

* Small docs fixes

---------

Co-authored-by: jacoblee93 <[email protected]>
  • Loading branch information
bracesproul and jacoblee93 authored Sep 19, 2023
1 parent 983135a commit abb5824
Show file tree
Hide file tree
Showing 15 changed files with 487 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ Chinook_Sqlite.sql
.node-version

firebase-debug.log
firestore-debug.log
firestore-debug.log

tmp/
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# PGVector

To enable vector search in a generic PostgreSQL database, LangChain.js supports using the [`pgvector`](https://github.com/pgvector/pgvector) Postgres extension.

## Setup

To work with PGVector, you need to install the `pg` package:

```bash npm2yarn
npm install pg
```

### Setup a `pgvector` self hosted instance with `docker-compose`

`pgvector` provides a prebuilt Docker image that can be used to quickly setup a self-hosted Postgres instance.
Create a file below named `docker-compose.yml`:

import CodeBlock from "@theme/CodeBlock";
import DockerExample from "@examples/indexes/vector_stores/pgvector_vectorstore/docker-compose.example.yml";

<CodeBlock language="yml" name="docker-compose.yml">
{DockerExample}
</CodeBlock>

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](https://github.com/pgvector/pgvector).

## Usage

import Example from "@examples/indexes/vector_stores/pgvector_vectorstore/pgvector.ts";

One complete example of using `PGVectorStore` is the following:

<CodeBlock language="typescript">{Example}</CodeBlock>
1 change: 1 addition & 0 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"langchain": "workspace:*",
"ml-distance": "^4.0.0",
"mongodb": "^5.2.0",
"pg": "^8.11.0",
"pickleparser": "^0.1.0",
"prisma": "^4.11.0",
"redis": "^4.6.6",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
services:
db:
image: ankane/pgvector
ports:
- 5433:5432
volumes:
- ./data:/var/lib/postgresql/data
environment:
- POSTGRES_PASSWORD=ChangeMe
- POSTGRES_USER=myuser
- POSTGRES_DB=api
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { OpenAIEmbeddings } from "langchain/embeddings/openai";
import { PGVectorStore } from "langchain/vectorstores/pgvector";
import { PoolConfig } from "pg";

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

const config = {
postgresConnectionOptions: {
type: "postgres",
host: "127.0.0.1",
port: 5433,
user: "myuser",
password: "ChangeMe",
database: "api",
} as PoolConfig,
tableName: "testlangchain",
columns: {
idColumnName: "id",
vectorColumnName: "vector",
contentColumnName: "content",
metadataColumnName: "metadata",
},
};

const pgvectorStore = await PGVectorStore.initialize(
new OpenAIEmbeddings(),
config
);

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 } } ]
*/

await pgvectorStore.end();
3 changes: 3 additions & 0 deletions langchain/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ vectorstores/supabase.d.ts
vectorstores/opensearch.cjs
vectorstores/opensearch.js
vectorstores/opensearch.d.ts
vectorstores/pgvector.cjs
vectorstores/pgvector.js
vectorstores/pgvector.d.ts
vectorstores/milvus.cjs
vectorstores/milvus.js
vectorstores/milvus.d.ts
Expand Down
8 changes: 8 additions & 0 deletions langchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@
"vectorstores/opensearch.cjs",
"vectorstores/opensearch.js",
"vectorstores/opensearch.d.ts",
"vectorstores/pgvector.cjs",
"vectorstores/pgvector.js",
"vectorstores/pgvector.d.ts",
"vectorstores/milvus.cjs",
"vectorstores/milvus.js",
"vectorstores/milvus.d.ts",
Expand Down Expand Up @@ -1447,6 +1450,11 @@
"import": "./vectorstores/opensearch.js",
"require": "./vectorstores/opensearch.cjs"
},
"./vectorstores/pgvector": {
"types": "./vectorstores/pgvector.d.ts",
"import": "./vectorstores/pgvector.js",
"require": "./vectorstores/pgvector.cjs"
},
"./vectorstores/milvus": {
"types": "./vectorstores/milvus.d.ts",
"import": "./vectorstores/milvus.js",
Expand Down
2 changes: 2 additions & 0 deletions langchain/scripts/create-entrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ const entrypoints = {
"vectorstores/qdrant": "vectorstores/qdrant",
"vectorstores/supabase": "vectorstores/supabase",
"vectorstores/opensearch": "vectorstores/opensearch",
"vectorstores/pgvector": "vectorstores/pgvector",
"vectorstores/milvus": "vectorstores/milvus",
"vectorstores/prisma": "vectorstores/prisma",
"vectorstores/typeorm": "vectorstores/typeorm",
Expand Down Expand Up @@ -302,6 +303,7 @@ const requiresOptionalDependency = [
"vectorstores/qdrant",
"vectorstores/supabase",
"vectorstores/opensearch",
"vectorstores/pgvector",
"vectorstores/typeorm",
"vectorstores/milvus",
"vectorstores/myscale",
Expand Down
1 change: 1 addition & 0 deletions langchain/src/load/import_constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export const optionalImportEntrypoints = [
"langchain/vectorstores/qdrant",
"langchain/vectorstores/supabase",
"langchain/vectorstores/opensearch",
"langchain/vectorstores/pgvector",
"langchain/vectorstores/milvus",
"langchain/vectorstores/typeorm",
"langchain/vectorstores/myscale",
Expand Down
3 changes: 3 additions & 0 deletions langchain/src/load/import_type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ export interface OptionalImportMap {
"langchain/vectorstores/opensearch"?:
| typeof import("../vectorstores/opensearch.js")
| Promise<typeof import("../vectorstores/opensearch.js")>;
"langchain/vectorstores/pgvector"?:
| typeof import("../vectorstores/pgvector.js")
| Promise<typeof import("../vectorstores/pgvector.js")>;
"langchain/vectorstores/milvus"?:
| typeof import("../vectorstores/milvus.js")
| Promise<typeof import("../vectorstores/milvus.js")>;
Expand Down
Loading

0 comments on commit abb5824

Please sign in to comment.