vlite
VLite 是一個簡單且極速的向量資料庫,可讓您使用嵌入語意化地儲存和檢索資料。VLite 以 numpy 打造,是一個輕量級、功能齊全的資料庫,可在您的專案中實作 RAG、相似性搜尋和嵌入。
您需要安裝 langchain-community
,使用 pip install -qU 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)
API 參考:TextLoader | CharacterTextSplitter
新增文字和文件
您可以使用 add_texts
和 add_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_document
和 delete
方法,在 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
方法,根據 ID 或中繼資料從 VLite 向量資料庫檢索文件。
# 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()