forked from langchain-ai/langchainjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for generic PGVector vector store (langchain-ai#2657)
* 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
1 parent
983135a
commit abb5824
Showing
15 changed files
with
487 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,4 +31,6 @@ Chinook_Sqlite.sql | |
.node-version | ||
|
||
firebase-debug.log | ||
firestore-debug.log | ||
firestore-debug.log | ||
|
||
tmp/ |
35 changes: 35 additions & 0 deletions
35
docs/extras/modules/data_connection/vectorstores/integrations/pgvector.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
examples/src/indexes/vector_stores/pgvector_vectorstore/docker-compose.example.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
44 changes: 44 additions & 0 deletions
44
examples/src/indexes/vector_stores/pgvector_vectorstore/pgvector.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.