openGauss VectorStore
本笔记本介绍了如何开始使用 openGauss VectorStore。openGauss 是一个高性能的关系型数据库,具有原生的向量存取能力。这种集成支持在 LangChain 应用程序中实现符合 ACID 的向量作,将传统的 SQL 功能与现代 AI 驱动的相似性搜索相结合。 向量存储。
设置
启动 openGauss Container
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 二进制
凭据
使用 openGauss 凭证
初始化
选择嵌入模型:
pip install -qU langchain-openai
import getpass
import os
if not os.environ.get("OPENAI_API_KEY"):
os.environ["OPENAI_API_KEY"] = getpass.getpass("Enter API key for OpenAI: ")
from langchain_openai import OpenAIEmbeddings
embeddings = OpenAIEmbeddings(model="text-embedding-3-large")
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"])
API 参考:文档
更新 vector store 中的项
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 中删除项目
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}]")
通过转换为 retriever 进行查询
您还可以将 vector store 转换为检索器,以便在您的链中更轻松地使用。
- TODO:编辑然后运行代码单元以生成输出
retriever = vector_store.as_retriever(search_type="mmr", search_kwargs={"k": 1})
retriever.invoke("thud")
用于检索增强生成
有关如何使用此向量存储进行检索增强生成 (RAG) 的指南,请参阅以下部分:
配置
连接设置
| 参数 | 违约 | 描述 |
|---|---|---|
host | localhost | Database server address |
port | 8888 | Database connection port |
user | gaussdb | Database username |
password | - | Complex password string |
database | postgres | Default database name |
min_connections | 1 | Connection pool minimum size |
max_connections | 5 | Connection pool maximum size |
table_name | langchain_docs | Name of the table for storing vector data and metadata |
index_type | IndexType.HNSW | Vector index algorithm type. Options: HNSW or IVFFLAT\nDefault is HNSW. |
vector_type | VectorType.vector | Type of vector representation to use. Default is Vector. |
distance_strategy | DistanceStrategy.COSINE | Vector similarity metric to use for retrieval. Options: euclidean (L2 distance), cosine (angular distance, ideal for text embeddings), manhattan (L1 distance for sparse data), negative_inner_product (dot product for normalized vectors).\n Default is cosine. |
embedding_dimension | 1536 | Dimensionality of the vector embeddings. |
支持的组合
| 向量类型 | 尺寸 | 索引类型 | 支持的距离策略 |
|---|---|---|---|
| vector | ≤2000 | HNSW/IVFFLAT | COSINE/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)
局限性
bit和sparsevec目前正在开发的向量类型- 最大矢量维度:2000
vector类型
API 参考
有关所有 __ModuleName__VectorStore 功能和配置的详细文档,请访问 API 参考:https://python.langchain.com/api_reference/en/latest/vectorstores/opengauss.OpenGuass.html