Skip to main content
Open In ColabOpen on GitHub

vlite

VLite 是一个简单且快速的向量数据库,它允许你使用嵌入(embeddings)来语义化地存储和检索数据。基于 numpy 构建,VLite 是一个轻量级的、自带功能的数据库,可以将 RAG、相似性搜索和嵌入功能轻松集成到你的项目中。

您需要使用 pip install -qU langchain-community 安装 langchain-community 才能使用此集成

安装

要在 LangChain 中使用 VLite,你需要安装 vlite 包:

!pip install vlite

导入 VLite

from langchain_community.vectorstores import VLite
API 参考:VLite

基础示例

在这个基础示例中,我们加载一个文本文档,并将其存储在 VLite 向量数据库中。然后,我们执行相似性搜索,根据查询检索相关文档。

VLite 会为你处理文本的分块和嵌入,你也可以通过预先对文本进行分块以及将这些分块嵌入到 VLite 数据库中来更改这些参数。

from langchain.document_loaders import TextLoader
from langchain.text_splitter import CharacterTextSplitter

# Load the document and split it into chunks
loader = TextLoader("path/to/document.txt")
documents = loader.load()

# Create a VLite instance
vlite = VLite(collection="my_collection")

# Add documents to the VLite vector database
vlite.add_documents(documents)

# Perform a similarity search
query = "What is the main topic of the document?"
docs = vlite.similarity_search(query)

# Print the most relevant document
print(docs[0].page_content)

添加文本和文档

您可以使用add_textsadd_documents方法分别将文本或文档添加到VLite向量数据库中。

# Add texts to the VLite vector database
texts = ["This is the first text.", "This is the second text."]
vlite.add_texts(texts)

# Add documents to the VLite vector database
documents = [Document(page_content="This is a document.", metadata={"source": "example.txt"})]
vlite.add_documents(documents)

VLite 提供了在存储的文档上执行相似性搜索的方法。

# Perform a similarity search
query = "What is the main topic of the document?"
docs = vlite.similarity_search(query, k=3)

# Perform a similarity search with scores
docs_with_scores = vlite.similarity_search_with_score(query, k=3)

VLite还支持最大边际相关性(MMR)搜索,它在优化查询相似度的同时,也考虑了检索文档之间的多样性。

# Perform an MMR search
docs = vlite.max_marginal_relevance_search(query, k=3)

更新和删除文档

您可以使用 update_documentdelete 方法更新或删除 VLite 向量数据库中的文档。

# Update a document
document_id = "doc_id_1"
updated_document = Document(page_content="Updated content", metadata={"source": "updated.txt"})
vlite.update_document(document_id, updated_document)

# Delete documents
document_ids = ["doc_id_1", "doc_id_2"]
vlite.delete(document_ids)

检索文档

你可以使用 get 方法从 VLite 向量数据库中根据文档的 ID 或元数据检索文档。

# Retrieve documents by IDs
document_ids = ["doc_id_1", "doc_id_2"]
docs = vlite.get(ids=document_ids)

# Retrieve documents by metadata
metadata_filter = {"source": "example.txt"}
docs = vlite.get(where=metadata_filter)

创建 VLite 实例

你可以使用多种方法创建VLite实例:

# Create a VLite instance from texts
vlite = VLite.from_texts(texts)

# Create a VLite instance from documents
vlite = VLite.from_documents(documents)

# Create a VLite instance from an existing index
vlite = VLite.from_existing_index(collection="existing_collection")

附加功能

VLite 提供了用于管理向量数据库的附加功能:

from langchain.vectorstores import VLite
vlite = VLite(collection="my_collection")

# Get the number of items in the collection
count = vlite.count()

# Save the collection
vlite.save()

# Clear the collection
vlite.clear()

# Get collection information
vlite.info()

# Dump the collection data
data = vlite.dump()