跳到主要內容

vlite

VLite 是一個簡單且速度極快的向量資料庫,可讓您使用嵌入以語意方式儲存和檢索資料。VLite 使用 numpy 構建,是一個輕量級、包含完整功能的資料庫,用於在您的專案中實作 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 方法,根據文件 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()

此頁面是否對您有幫助?