-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
community[patch]: Add pgvector index using HNSW (#5564)
* Add pgvector hnsw * Update hnsw index name to use column name * Fix import typo * Fix pgvector index test * Refactor, set dimensions for hnsw mandatory, fix docs * Add pg-format, refactor create hnsw index with sql identifiers pg-format * Revert docs gitignore * Revert docs gitignore * Revert pg-format * Revert gitignore docs
- Loading branch information
Showing
4 changed files
with
220 additions
and
27 deletions.
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
54 changes: 54 additions & 0 deletions
54
examples/src/indexes/vector_stores/pgvector_vectorstore/pgvector_hnsw.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,54 @@ | ||
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 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", | ||
}, | ||
// supported distance strategies: cosine (default), innerProduct, or euclidean | ||
distanceStrategy: "cosine" as DistanceStrategy, | ||
}; | ||
|
||
const pgvectorStore = await PGVectorStore.initialize( | ||
new OpenAIEmbeddings(), | ||
config | ||
); | ||
|
||
// create the index | ||
await pgvectorStore.createHnswIndex({ | ||
dimensions: 1536, | ||
efConstruction: 64, | ||
m: 16, | ||
}); | ||
|
||
await pgvectorStore.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 results = await pgvectorStore.similaritySearchVectorWithScore(query, 1); | ||
|
||
console.log(results); | ||
|
||
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