跳到主要内容
ZeusDB 是一个由 Rust 提供支持的高性能向量数据库,提供乘积量化、持久存储和企业级日志等高级功能。
本文档展示了如何使用 ZeusDB 为您的 LangChain 应用程序带来企业级向量搜索功能。

设置

从 PyPi 安装 ZeusDB LangChain 集成包
pip install -qU langchain-zeusdb
在 Jupyter Notebooks 中设置
💡 提示:如果您在 Jupyter 或 Google Colab 中工作,请使用 %pip magic 命令,以便将包安装到活动内核中
pip install -qU langchain-zeusdb

入门

本示例使用 OpenAIEmbeddings,这需要一个 OpenAI API 密钥:在此处获取您的 OpenAI API 密钥 如果您愿意,也可以将此包与任何其他嵌入提供商(Hugging Face、Cohere、自定义函数等)一起使用。从 PyPi 安装 LangChain OpenAI 集成包
pip install -qU langchain-openai

# Use this command if inside Jupyter Notebooks
#pip install -qU langchain-openai

请选择以下选项以集成您的 OpenAI 密钥

选项 1:🔑 每次输入您的 API 密钥 在 Jupyter 中使用 getpass 安全地输入您的密钥以供当前会话使用
import os
import getpass

os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:")
选项 2:🗂️ 使用 .env 文件 将您的密钥保存在本地 .env 文件中,并使用 python-dotenv 自动加载
from dotenv import load_dotenv

load_dotenv()  # reads .env and sets OPENAI_API_KEY
🎉 太棒了!您已准备就绪。

初始化

# Import required Packages and Classes
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)

管理向量存储

2.1 将项目添加到向量存储

from langchain_core.documents import Document

document_1 = Document(
    page_content="ZeusDB is a high-performance vector database",
    metadata={"source": "https://docs.zeusdb.com"},
)

document_2 = Document(
    page_content="Product Quantization reduces memory usage significantly",
    metadata={"source": "https://docs.zeusdb.com"},
)

document_3 = Document(
    page_content="ZeusDB integrates seamlessly with LangChain",
    metadata={"source": "https://docs.zeusdb.com"},
)

documents = [document_1, document_2, document_3]

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

2.2 更新向量存储中的项目

updated_document = Document(
    page_content="ZeusDB now supports advanced Product Quantization with 4x-256x compression",
    metadata={"source": "https://docs.zeusdb.com", "updated": True},
)

vector_store.add_documents([updated_document], ids=["1"])

2.3 从向量存储中删除项目

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

查询向量存储

3.1 直接查询

执行简单的相似性搜索
results = vector_store.similarity_search(query="high performance database", k=2)

for doc in results:
    print(f"* {doc.page_content} [{doc.metadata}]")
如果您想执行相似性搜索并接收相应的分数
results = vector_store.similarity_search_with_score(query="memory optimization", k=2)

for doc, score in results:
    print(f"* [SIM={score:.3f}] {doc.page_content} [{doc.metadata}]")

3.2 通过转换为检索器进行查询

您还可以将向量存储转换为检索器,以便在您的链中更轻松地使用
retriever = vector_store.as_retriever(search_type="mmr", search_kwargs={"k": 2})

retriever.invoke("vector database features")

ZeusDB 特有功能

4.1 通过乘积量化实现内存高效设置

对于大型数据集,使用乘积量化来减少内存使用
# Create memory-optimized vector store
quantization_config = {"type": "pq", "subvectors": 8, "bits": 8, "training_size": 10000}

vdb_quantized = VectorDatabase()
quantized_index = vdb_quantized.create(
    index_type="hnsw", dim=1536, quantization_config=quantization_config
)

quantized_vector_store = ZeusDBVectorStore(
    zeusdb_index=quantized_index, embedding=embeddings
)

print(f"Created quantized store: {quantized_index.info()}")

4.2 持久化

保存和加载您的向量存储到磁盘:如何保存您的向量存储
# Save the vector store
vector_store.save_index("my_zeusdb_index.zdb")
如何加载您的向量存储
# Load the vector store
loaded_store = ZeusDBVectorStore.load_index(
    path="my_zeusdb_index.zdb", embedding=embeddings
)

print(f"Loaded store with {loaded_store.get_vector_count()} vectors")

用于检索增强生成的使用

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

API 参考

有关所有 ZeusDBVectorStore 功能和配置的详细文档,请访问 ZeusDB 文档
以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.