Skip to main content
本笔记本介绍如何开始使用 openGauss VectorStore。openGauss 是一款具备原生向量存储与检索能力的高性能关系型数据库。该集成在 LangChain 应用中实现符合 ACID 标准的向量操作,将传统 SQL 功能与现代 AI 驱动的相似性搜索相结合。 vector store.

配置

启动 openGauss 容器

docker run --name opengauss \
  -d \
  -e GS_PASSWORD='MyStrongPass@123' \
  -p 8888:5432 \
  opengauss/opengauss-server:latest

安装 langchain-opengauss

pip install langchain-opengauss
系统要求
  • openGauss ≥ 7.0.0
  • Python ≥ 3.8
  • psycopg2-binary

凭据

使用你的 openGauss 凭据

初始化

from langchain_opengauss import OpenGauss, OpenGaussSettings

# Configure with schema validation
config = OpenGaussSettings(
    table_name="test_langchain",
    embedding_dimension=384,
    index_type="HNSW",
    distance_strategy="COSINE",
)
vector_store = OpenGauss(embedding=embeddings, config=config)

管理向量存储

向向量存储添加项目

from langchain_core.documents import Document

document_1 = Document(page_content="foo", metadata={"source": "https://example.com"})

document_2 = Document(page_content="bar", metadata={"source": "https://example.com"})

document_3 = Document(page_content="baz", metadata={"source": "https://example.com"})

documents = [document_1, document_2, document_3]

vector_store.add_documents(documents=documents, ids=["1", "2", "3"])

更新向量存储中的项目

updated_document = Document(
    page_content="qux", metadata={"source": "https://another-example.com"}
)

# If the id is already exist, will update the document
vector_store.add_documents(document_id="1", document=updated_document)

从向量存储中删除项目

vector_store.delete(ids=["3"])

查询向量存储

创建向量存储并添加相关文档后,通常需要在运行链或代理时对其进行查询。

直接查询

执行简单的相似性搜索,方法如下:
  • TODO: 编辑后运行代码单元以生成输出
results = vector_store.similarity_search(
    query="thud", k=1, filter={"source": "https://another-example.com"}
)
for doc in results:
    print(f"* {doc.page_content} [{doc.metadata}]")
如果要执行相似性搜索并获取对应的分数,可以运行:
results = vector_store.similarity_search_with_score(
    query="thud", k=1, filter={"source": "https://example.com"}
)
for doc, score in results:
    print(f"* [SIM={score:3f}] {doc.page_content} [{doc.metadata}]")

转换为检索器后查询

你也可以将向量存储转换为检索器,以便在链中更便捷地使用。
  • TODO: 编辑后运行代码单元以生成输出
retriever = vector_store.as_retriever(search_type="mmr", search_kwargs={"k": 1})
retriever.invoke("thud")

用于检索增强生成

有关如何将此向量存储用于检索增强生成(RAG)的指南,请参阅以下章节:

配置参数

连接设置

参数默认值描述
hostlocalhost数据库服务器地址
port8888数据库连接端口
usergaussdb数据库用户名
password-复杂密码字符串
databasepostgres默认数据库名称
min_connections1连接池最小连接数
max_connections5连接池最大连接数
table_namelangchain_docs用于存储向量数据和元数据的表名
index_typeIndexType.HNSW向量索引算法类型。可选:HNSW 或 IVFFLAT\n默认为 HNSW。
vector_typeVectorType.vector使用的向量表示类型。默认为 Vector。
distance_strategyDistanceStrategy.COSINE用于检索的向量相似度度量。可选:euclidean(L2 距离)、cosine(角距离,适合文本嵌入)、manhattan(稀疏数据的 L1 距离)、negative_inner_product(归一化向量的点积)。\n默认为 cosine。
embedding_dimension1536向量嵌入的维度数。

支持的组合

向量类型维度索引类型支持的距离策略
vector≤2000HNSW/IVFFLATCOSINE/EUCLIDEAN/MANHATTAN/INNER_PROD

性能优化

索引调优指南

HNSW 参数
  • m:16-100(在召回率和内存之间取得平衡)
  • ef_construction:64-1000(必须 > 2*m)
IVFFLAT 建议
import math

lists = min(
    int(math.sqrt(total_rows)) if total_rows > 1e6 else int(total_rows / 1000),
    2000,  # openGauss maximum
)

连接池

OpenGaussSettings(min_connections=3, max_connections=20)

限制

  • bitsparsevec 向量类型目前正在开发中
  • vector 类型的最大向量维度:2000

API 参考

有关所有 __ModuleName__VectorStore 功能和配置的详细文档,请参阅 API 参考:python.langchain.com/api_reference/en/latest/vectorstores/opengauss.OpenGuass.html