Skip to main content

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:

  1. FAISS: Semantic search that uses dense vectors for similarity.
  2. BM25: Keyword-based search leveraging sparse vectors.
  3. Hybrid Search: A cascade method combining FAISS and BM25. Hybrid search first retrieves the top-k results using FAISS (high recall) and then applies BM25 (high precision) to re-rank the results without changing the FAISS's similarity scores.

Example usage:

results = vecsea.similarity_search_with_score(
    query="Quantum mechanics",
    k=3,
    method="bm25" | "faiss" | "cascade",
    lang="en" | "de"
)

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

denspa-0.0.7.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

denspa-0.0.7-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file denspa-0.0.7.tar.gz.

File metadata

  • Download URL: denspa-0.0.7.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for denspa-0.0.7.tar.gz
Algorithm Hash digest
SHA256 3e33470fe66830bd81c489702deec8c09e5841b12767f65b5cabcab71356ae9a
MD5 4a61f32feaf64cc9c2ae06fb45fa54ca
BLAKE2b-256 87f480a50366cecf94e1555c0e57cfcc1ea7e601c4e14d747d570f0ff33c76e5

See more details on using hashes here.

File details

Details for the file denspa-0.0.7-py3-none-any.whl.

File metadata

  • Download URL: denspa-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 11.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for denspa-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 472da8302dc014b8653ca292bf891d0cc67439b180a955c2e4dc8cbc3b4c1d87
MD5 08303cfcf1b74b61303c0bf6bc64cda6
BLAKE2b-256 b632dc48007b0b8e0c748ec43160213ff9ce1c777692ccca0cd7f08888402581

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page