An implementation of LangChain vectorstore abstraction usingThe code lives in an integration package called: langchain-postgres.postgresas the backend and utilizing thepgvectorextension.
Status
This code has been ported over fromlangchain-community into a dedicated package called langchain-postgres. The following changes have been made:
langchain-postgresworks only with psycopg3. Please update your connnecion strings frompostgresql+psycopg2://...topostgresql+psycopg://langchain:langchain@...(yes, it’s the driver name ispsycopgnotpsycopg3, but it’ll usepsycopg3.- The schema of the embedding store and collection have been changed to make add_documents work correctly with user specified ids.
- One has to pass an explicit connection object now.
设置
First download the partner package:pgvector extension:
凭证
There are no credentials needed to run this notebook, just make sure you downloaded thelangchain-postgres package and correctly started the postgres container.
如果您希望获得一流的模型调用自动追踪功能,还可以通过取消注释以下代码来设置 LangSmith API 密钥:
实例化
管理向量存储
向向量存储添加项目
Note that adding documents by ID will over-write any existing documents that match that ID.从向量存储删除项目
查询向量存储
一旦创建了向量存储并添加了相关文档,您很可能希望在链或智能体运行期间对其进行查询。Filtering support
The vectorstore supports a set of filters that can be applied against the metadata fields of the documents.直接查询
可以按以下方式执行简单的相似度搜索:PGVector vector store, please refer to the API reference.
转换为检索器进行查询
您还可以将向量存储转换为检索器,以便在链中更方便地使用。用于检索增强生成
有关如何将此向量存储用于检索增强生成 (RAG) 的指南,请参阅以下部分:API 参考
For detailed documentation of all PGVector VectorStore features and configurations head to the API reference连接这些文档到 Claude、VSCode 等工具,通过 MCP 获取实时答案。

