DenSpa is an open-source package designed for hybrid search, enabling seamless integration into RAG frameworks.
Project description
DenSpa
DenSpa is an open-source package designed for hybrid search, enabling seamless integration into Retrieval-Augmented Generation (RAG) frameworks. The package combines dense and sparse vector embeddings to perform efficient searches on document corpora.
- Dense-vector-based search leverages the FAISS vector database to manage and query collections.
- Sparse-vector-based search utilizes a custom implementation of BM25, enhanced with pre-processing techniques like stemming to optimize the index.
Installation
To get started, clone this repository and install the dependencies or use pip:
pip install DenSpa
Quick Start
Initializing the Vector Search Engine
You can easily initialize the vector search engine using the following code:
from denspa import VectorSearch
from langchain.embeddings import HuggingFaceEmbeddings
import os
embedding_function = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2")
INDEX_PATH = "database/index"
if not os.path.exists(INDEX_PATH):
os.makedirs(INDEX_PATH)
vecsea = VectorSearch(
folder_path=INDEX_PATH,
index_name="denspa",
embedding_function=embedding_function,
bm25_options={"k1": 1.25, "b": 0}
)
Indexing Documents
DenSpa supports indexing documents in both English and German. Documents can be added to the search engine like this:
from langchain.docstore.document import Document
documents = [Document(page_content="There are many variations...", metadata={"source": "lecture.pdf"})]
# Indexing with FAISS
vecsea.add_documents(documents, lang="en", engine="faiss")
# Indexing with BM25
vecsea.add_documents(documents, lang="en", engine="bm25")
# Save the index locally
vecsea.save_local()
Deleting Indexed Documents
To remove a specific document from the index, use the removeByMetadata function:
vecsea.removeByMetadata({"source": "lecture.pdf"})
vecsea.save_local()
Deleting Indexes
To remove the indexes, use the delete_local function:
vecsea.delete_local()
Search Methods
DenSpa currently supports three search methods:
- FAISS: Semantic search that uses dense vectors for similarity.
- BM25: Keyword-based search leveraging sparse vectors.
- Hybrid Search: A cascade method combining FAISS and BM25. Hybrid search first retrieves the top results using FAISS (high recall) and then applies BM25 on the top-3*k results to refine the selection for the final top-k results (higher precision).
Example usage:
results = vecsea.similarity_search_with_score(
query="Quantum mechanics",
k=3,
method="bm25",
lang="en"
)
Features
- Dense and Sparse Search: Utilize semantic embeddings and keyword-based indexing for versatile search capabilities.
- Hybrid Search Strategy: Combine the strengths of both FAISS and BM25 for balanced recall and precision.
- Customizable: Easily configure embeddings, BM25 parameters, and storage paths.
- Language Support: Works with English and German document corpora.
Contributions
Contributions are welcome! Please feel free to open an issue or submit a pull request if you have suggestions or improvements.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file denspa-0.0.5.tar.gz.
File metadata
- Download URL: denspa-0.0.5.tar.gz
- Upload date:
- Size: 8.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
114662b639af9efcf67112561f92e9845d92958584a1a219b6b86f3b4bda9935
|
|
| MD5 |
840a5e3ab8ede14e6c9ce78096f8594a
|
|
| BLAKE2b-256 |
37b1fb007ed63f0f32dfb0f351fdc0f1755501b9b64078358eefae5a893f8742
|
File details
Details for the file denspa-0.0.5-py3-none-any.whl.
File metadata
- Download URL: denspa-0.0.5-py3-none-any.whl
- Upload date:
- Size: 10.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
86e67888e3e29202bd13c5935262bf5ce01285c9c38fa27188861ea9520634a1
|
|
| MD5 |
5ba241a1536e517ec748430d496bdf2b
|
|
| BLAKE2b-256 |
1b57a0bdba82c5266659f02be34ad24349d8f83894c35f1dd6e4f77f55586478
|