>ZeusDB 是一款由 Rust 提供支持的高性能向量数据库,提供产品量化、持久存储和企业级日志记录等高级功能。
本文档展示了如何使用 ZeusDB 为您的 LangChain 应用程序带来企业级向量搜索能力。
快速开始
安装
pip install langchain-zeusdb
uv add langchain-zeusdb
开始使用
此示例使用 *OpenAIEmbeddings*,需要 OpenAI API 密钥 - 在此获取您的 OpenAI API 密钥
如果您愿意,也可以将此包与任何其他嵌入提供程序(Hugging Face、Cohere、自定义函数等)配合使用。
pip install langchain-openai
os.environ['OPENAI_API_KEY'] = getpass.getpass('OpenAI API Key:')
基本用法
from langchain_zeusdb import ZeusDBVectorStore
from langchain_openai import OpenAIEmbeddings
from zeusdb import VectorDatabase
# Initialize embeddings
embeddings = OpenAIEmbeddings(model="text-embedding-3-small")
# Create ZeusDB index
vdb = VectorDatabase()
index = vdb.create(
index_type="hnsw",
dim=1536,
space="cosine"
)
# Create vector store
vector_store = ZeusDBVectorStore(
zeusdb_index=index,
embedding=embeddings
)
# Add documents
from langchain_core.documents import Document
docs = [
Document(page_content="ZeusDB is fast", metadata={"source": "docs"}),
Document(page_content="LangChain is powerful", metadata={"source": "docs"}),
]
vector_store.add_documents(docs)
# Search
results = vector_store.similarity_search("fast database", k=2)
print(f"Found the following {len(results)} results:")
print(results)
预期结果:
Found the following 2 results:
[Document(id='ea2b4f13-b0b7-4cef-bb91-0fc4f4c41295', metadata={'source': 'docs'}, page_content='ZeusDB is fast'), Document(id='33dc1e87-a18a-4827-a0df-6ee47eabc7b2', metadata={'source': 'docs'}, page_content='LangChain is powerful')]
工厂方法
为方便起见,您可以一步创建并填充向量存储:
示例 1:从文本创建(一步创建索引并添加文本)
vector_store_texts = ZeusDBVectorStore.from_texts(
texts=["Hello world", "Goodbye world"],
embedding=embeddings,
metadatas=[{"source": "text1"}, {"source": "text2"}]
)
print("texts store count:", vector_store_texts.get_vector_count()) # -> 2
print("texts store peek:", vector_store_texts.zeusdb_index.list(2)) # [('id1', {...}), ('id2', {...})]
# Search the texts-based store
results = vector_store_texts.similarity_search("Hello", k=1)
print(f"Found in texts store: {results[0].page_content}") # -> "Hello world"
预期结果:
texts store count: 2
texts store peek: [('e9c39b44-b610-4e00-91f3-bf652e9989ac', {'source': 'text1', 'text': 'Hello world'}), ('d33f210c-ed53-4006-a64a-a9eee397fec9', {'source': 'text2', 'text': 'Goodbye world'})]
Found in texts store: Hello world
示例 2:从文档创建(一步创建索引并添加文档)
new_docs = [
Document(page_content="Python is great", metadata={"source": "python"}),
Document(page_content="JavaScript is flexible", metadata={"source": "js"}),
]
vector_store_docs = ZeusDBVectorStore.from_documents(
documents=new_docs,
embedding=embeddings
)
print("docs store count:", vector_store_docs.get_vector_count()) # -> 2
print("docs store peek:", vector_store_docs.zeusdb_index.list(2)) # [('id3', {...}), ('id4', {...})]
# Search the documents-based store
results = vector_store_docs.similarity_search("Python", k=1)
print(f"Found in docs store: {results[0].page_content}") # -> "Python is great"
预期结果:
docs store count: 2
docs store peek: [('aab2d1c1-7e02-4817-8dd8-6fb03570bb6f', {'text': 'Python is great', 'source': 'python'}), ('9a8a82cb-0e70-456c-9db2-556e464de14e', {'text': 'JavaScript is flexible', 'source': 'js'})]
Found in docs store: Python is great
高级功能
ZeusDB 的企业级功能已完全集成到 LangChain 生态系统中,提供量化、持久化、高级搜索功能以及许多其他企业级能力。
使用量化的内存高效设置
对于大型数据集,使用产品量化来减少内存使用:
# Create quantized index for memory efficiency
quantization_config = {
'type': 'pq',
'subvectors': 8,
'bits': 8,
'training_size': 10000
}
vdb = VectorDatabase()
index = vdb.create(
index_type="hnsw",
dim=1536,
space="cosine",
quantization_config=quantization_config
)
vector_store = ZeusDBVectorStore(
zeusdb_index=index,
embedding=embeddings
)
请参阅我们的 文档 获取有关设置量化的有用配置指南和建议。
持久化
ZeusDB 持久化功能让您能够将完全填充的索引保存到磁盘,并在之后加载它并完整恢复状态。这包括向量、元数据、HNSW 图,以及(如果启用了)产品量化模型。
保存的内容:
- - 向量和 ID
- - 元数据
- - HNSW 图结构
- - 量化配置、质心和训练状态(如果启用了 PQ)
如何保存向量存储
# Save index
vector_store.save_index("my_index.zdb")
如何加载向量存储
# Load index
loaded_store = ZeusDBVectorStore.load_index(
path="my_index.zdb",
embedding=embeddings
)
# Verify after load
print("vector count:", loaded_store.get_vector_count())
print("index info:", loaded_store.info())
print("store peek:", loaded_store.zeusdb_index.list(2))
注意事项
- - 路径是目录,不是单个文件。确保目标可写。
- - Saved indexes are cross-platform and include format/version info for compatibility checks.
- - 如果您使用了 PQ,压缩模型和状态都会被保留——加载后无需重新训练。
- - 您可以继续使用所有向量存储 API(相似度_搜索、检索器等)在加载的_store.
有关更多详细信息(包括文件结构和更多综合示例),请参阅 文档.
高级搜索选项
使用这些选项来控制搜索的评分、多样性、元数据过滤和检索器集成。
带分数的相似性搜索
返回 (Document, raw_distance) 来自 ZeusDB 的配对(距离越小 = 越相似)。 如果您偏好归一化的相关性 [0, 1],请使用 similarity_search_with_relevance_scores.
# Similarity search with scores
results_with_scores = vector_store.similarity_search_with_score(
query="machine learning",
k=5
)
print(results_with_scores)
预期结果:
[
(Document(id='ac0eaf5b-9f02-4ce2-8957-c369a7262c61', metadata={'source': 'docs'}, page_content='LangChain is powerful'), 0.8218843340873718),
(Document(id='faae3adf-7cf3-463c-b282-3790b096fa23', metadata={'source': 'docs'}, page_content='ZeusDB is fast'), 0.9140053391456604)
]
用于多样性的 MMR 搜索
MMR(最大边际相关性)在两个力量之间取得平衡:对查询的相关性和所选结果之间的多样性,减少近乎重复的答案。使用 lambda 参数控制权衡_(1.0 = 全部相关性,0.0 = 全部多样性)。
# MMR search for diversity
mmr_results = vector_store.max_marginal_relevance_search(
query="AI applications",
k=5,
fetch_k=20,
lambda_mult=0.7 # Balance relevance vs diversity
)
print(mmr_results)
使用元数据过滤进行搜索
使用添加文档时存储的文档元数据来过滤结果
# Search with metadata filtering
results = vector_store.similarity_search(
query="database performance",
k=3,
filter={"source": "documentation"}
)
有关支持的元数据查询类型和运算符,请参阅 文档.
作为检索器
将向量存储转换为检索器可为您提供标准 LangChain 接口,链(如 RetrievalQA)可以调用它来获取上下文。在底层它使用您选择的搜索类型(相似性或 MMR)和搜索_kwargs.
# Convert to retriever for use in chains
retriever = vector_store.as_retriever(
search_type="mmr",
search_kwargs={"k": 3, "lambda_mult": 0.8}
)
# Use with LangChain Expression Language (LCEL) - requires only langchain-core
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
from langchain_openai import ChatOpenAI
def format_docs(docs):
return "\n\n".join([d.page_content for d in docs])
template = """Answer the question based only on the following context:
{context}
Question: {question}
"""
prompt = ChatPromptTemplate.from_template(template)
llm = ChatOpenAI()
# Create a chain using LCEL
chain = (
{"context": retriever | format_docs, "question": RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)
# Use the chain
answer = chain.invoke("What is ZeusDB?")
print(answer)
预期结果:
ZeusDB is a fast database management system.
异步支持
ZeusDB 支持异步操作,用于非阻塞、并发向量操作。
何时使用异步: web servers (FastAPI/Starlette), agents/pipelines doing parallel searches, or notebooks where you want non-blocking/concurrent retrieval. If you're writing simple scripts, the sync methods are fine.
这些是 **异步操作** - the async/await versions of the regular synchronous methods. Here's what each one does:
await vector_store.aadd_documents(documents)- 异步向向量存储添加文档(add_documents())await vector_store.asimilarity_search("query", k=5)- 异步执行相似性搜索(similarity_search())await vector_store.adelete(ids=["doc1", "doc2"])- 异步按 ID 删除文档(delete())
异步版本在以下情况下很有用:
- - 您正在构建异步应用程序(使用
asyncio、FastAPI 等) - - 您需要可以并发运行的无阻塞操作
- - 您正在同时处理多个请求
- - You want better performance in I/O-bound applications
例如,不在添加文档时阻塞:
# Synchronous (blocking)
vector_store.add_documents(docs) # Blocks until complete
# Asynchronous (non-blocking)
await vector_store.aadd_documents(docs) # Can do other work while this runs
All operations support async/await:
**脚本版本(python my_script.py):**
from langchain_zeusdb import ZeusDBVectorStore
from langchain_openai import OpenAIEmbeddings
from langchain_core.documents import Document
from zeusdb import VectorDatabase
# Setup
embeddings = OpenAIEmbeddings(model="text-embedding-3-small")
vdb = VectorDatabase()
index = vdb.create(index_type="hnsw", dim=1536, space="cosine")
vector_store = ZeusDBVectorStore(zeusdb_index=index, embedding=embeddings)
docs = [
Document(page_content="ZeusDB is fast", metadata={"source": "docs"}),
Document(page_content="LangChain is powerful", metadata={"source": "docs"}),
]
async def main():
# Add documents asynchronously
ids = await vector_store.aadd_documents(docs)
print("Added IDs:", ids)
# Run multiple searches concurrently
results_fast, results_powerful = await asyncio.gather(
vector_store.asimilarity_search("fast", k=2),
vector_store.asimilarity_search("powerful", k=2),
)
print("Fast results:", [d.page_content for d in results_fast])
print("Powerful results:", [d.page_content for d in results_powerful])
# Delete documents asynchronously
deleted = await vector_store.adelete(ids=ids[:1])
print("Deleted first doc:", deleted)
if __name__ == "__main__":
asyncio.run(main())
**Colab/Notebook/Jupyter version (top-level await):**
from langchain_zeusdb import ZeusDBVectorStore
from langchain_openai import OpenAIEmbeddings
from langchain_core.documents import Document
from zeusdb import VectorDatabase
# Setup
embeddings = OpenAIEmbeddings(model="text-embedding-3-small")
vdb = VectorDatabase()
index = vdb.create(index_type="hnsw", dim=1536, space="cosine")
vector_store = ZeusDBVectorStore(zeusdb_index=index, embedding=embeddings)
docs = [
Document(page_content="ZeusDB is fast", metadata={"source": "docs"}),
Document(page_content="LangChain is powerful", metadata={"source": "docs"}),
]
# Add documents asynchronously
ids = await vector_store.aadd_documents(docs)
print("Added IDs:", ids)
# Run multiple searches concurrently
results_fast, results_powerful = await asyncio.gather(
vector_store.asimilarity_search("fast", k=2),
vector_store.asimilarity_search("powerful", k=2),
)
print("Fast results:", [d.page_content for d in results_fast])
print("Powerful results:", [d.page_content for d in results_powerful])
# Delete documents asynchronously
deleted = await vector_store.adelete(ids=ids[:1])
print("Deleted first doc:", deleted)
预期结果:
Added IDs: ['9c440918-715f-49ba-9b97-0d991d29e997', 'ad59c645-d3ba-4a4a-a016-49ed39514123']
Fast results: ['ZeusDB is fast', 'LangChain is powerful']
Powerful results: ['LangChain is powerful', 'ZeusDB is fast']
Deleted first doc: True
监控与可观测性
性能监控
# Get index statistics
stats = vector_store.get_zeusdb_stats()
print(f"Index size: {stats.get('total_vectors', '0')} vectors")
print(f"Dimension: {stats.get('dimension')} | Space: {stats.get('space')} | Index type: {stats.get('index_type')}")
# Benchmark search performance
performance = vector_store.benchmark_search_performance(
query_count=100,
max_threads=4
)
print(f"Search QPS: {performance.get('parallel_qps', 0):.0f}")
# Check quantization status
if vector_store.is_quantized():
progress = vector_store.get_training_progress()
print(f"Quantization training: {progress:.1f}% complete")
else:
print("Index is not quantized")
预期结果:
Index size: 2 vectors
Dimension: 1536 | Space: cosine | Index type: HNSW
Search QPS: 53807
Index is not quantized
企业级日志
ZeusDB 包含企业级结构化日志,可通过智能环境检测自动工作:
# ZeusDB automatically detects your environment and applies appropriate logging:
# - Development: Human-readable logs, WARNING level
# - Production: JSON structured logs, ERROR level
# - Testing: Minimal output, CRITICAL level
# - Jupyter: Clean readable logs, INFO level
# Operations are automatically logged with performance metrics
vector_store.add_documents(docs)
# Logs: {"operation":"vector_addition","total_inserted":2,"duration_ms":45}
# Control logging with environment variables if needed
# ZEUSDB_LOG_LEVEL=debug ZEUSDB_LOG_FORMAT=json python your_app.py
要了解更多关于 ZeusDB 企业级日志功能的完整特性,请阅读以下 文档.
配置选项
索引参数
vdb = VectorDatabase()
index = vdb.create(
index_type="hnsw", # Index algorithm
dim=1536, # Vector dimension
space="cosine", # Distance metric: cosine, l2, l1
m=16, # HNSW connectivity
ef_construction=200, # Build-time search width
expected_size=100000, # Expected number of vectors
quantization_config=None # Optional quantization
)
搜索参数
results = vector_store.similarity_search(
query="search query",
k=5, # Number of results
ef_search=None, # Runtime search width (auto if None)
filter={"key": "value"} # Metadata filter
)
错误处理
该集成包含全面的错误处理:
try:
results = vector_store.similarity_search("query")
print(results)
except Exception as e:
# Graceful degradation with logging
print(f"Search failed: {e}")
# Fallback logic here
要求
- Python:3.10 或更高版本
- ZeusDB:0.0.8 或更高版本
- LangChain Core: 0.3.74 或更高版本
源码安装
git clone https://github.com/zeusdb/langchain-zeusdb.git
cd langchain-zeusdb/libs/zeusdb
pip install -e .
使用场景
- RAG 应用: 用于问答的高性能检索
- 语义搜索: 在大型文档集合中快速进行相似性搜索
- 推荐系统: 基于向量的内容过滤和协同过滤
- 嵌入分析: 高维嵌入空间分析
- 实时应用: 生产系统的低延迟向量搜索
兼容性
LangChain 版本
- LangChain Core: 0.3.74+
距离度量
- 余弦: 默认值,归一化相似度
- 欧几里得 (L2): 几何距离
- 曼哈顿 (L1): 城市街区距离
嵌入模型
兼容任何嵌入提供者:
- - OpenAI (
text-embedding-3-small,text-embedding-3-large) - - Hugging Face Transformers
- - Cohere Embeddings
- - 自定义嵌入函数
支持
- 文档: docs.zeusdb.com
- 问题反馈: GitHub Issues
- 电子邮件: contact@zeusdb.com
*让向量搜索快速、可扩展且对开发者友好。*