Zum Inhalt springen

BestRAG - Hybrid RAG Storage & Retrieval Cheatsheet

BestRAG - Hybrid RAG Storage & Retrieval Cheatsheet

BestRAG (Best Retrieval Augmented Generation) ist eine Python-Bibliothek zum Erstellen von Hybrid RAG Pipelines. Sie kombiniert Dense-, Sparse- und Late-Interaction-Embeddings zum effizienten Speichern und Durchsuchen von Dokumenten mit Qdrant als Vektor-Store und FastEmbed für die Embeddings. Die Idee: Speichere jeden Chunk einmal mit mehreren Darstellungen, dann hole mit kombinierter Relevanz ab, damit du beide die semantische Erinnerung von Dense Vektoren und die Exakt-Term-Präzision von Sparse (BM25-Stil) Suche erhältst.

Installation

MethodCommand
pippip install bestrag
RequirementsEine Qdrant-Instanz (Local Docker oder Qdrant Cloud)
EmbeddingsVerwendet FastEmbed unter der Haube
Verifypython -c "import bestrag; print('ok')"

Why Hybrid

Retrieval typeStark beiSchwach bei
Dense (vector)Bedeutung, UmformulierungExakte IDs, Zahlen, seltene Begriffe
Sparse (BM25)Exakte Begriffe, IdentifikatorenSynonyme, Umformulierung
Late interactionFeines Token-MatchingKosten
Fused (hybrid)Bestes von Dense + Sparse

BestRAG speichert alle Darstellungen, damit eine einzelne Abfrage von all ihnen profitiert.

Basic Usage

from bestrag import BestRAG

rag = BestRAG(
    url="http://localhost:6333",     # Qdrant
    api_key="...",                   # if using Qdrant Cloud
    collection_name="docs",
)

# Ingest documents (embeds dense + sparse + late-interaction, upserts to Qdrant)
rag.store_pdf("handbook.pdf")
rag.store_text("Nick prefers concise answers.", metadata={"source": "prefs"})

# Retrieve with fused hybrid relevance
results = rag.search("what does the refund policy say?", limit=5)
for r in results:
    print(r.score, r.text)
MethodBeschreibung
BestRAG(url, collection_name, ...)Verbinde mit Qdrant, wähle eine Collection
store_pdf(path)Parse + Chunk + Embedde + Upsert eine PDF
store_text(text, metadata=...)Absorbiere Rohtext
search(query, limit=k)Hybrid (Fused) Retrieval
delete(...)Entferne Dokumente

What Happens on Ingest

StepDetail
ParseExtrahiere Text (z.B. aus PDF)
ChunkTeile in Passagen
EmbedDense + Sparse + Late-Interaction-Vektoren pro Chunk
UpsertSpeichere alle Vektoren + Payload in Qdrant
StepDetail
Embed queryDense + Sparse (+ Late Interaction)
Query QdrantAbrufen von Kandidaten pro Darstellung
FuseKombiniere Rankings (RRF-Stil) in eine Liste
ReturnTop-K Passagen mit Scores + Metadaten

Feeding Results to an LLM

# Retrieve, then build a grounded prompt
hits = rag.search(user_question, limit=5)
context = "\n\n".join(h.text for h in hits)
prompt = f"Answer using only this context:\n{context}\n\nQ: {user_question}"
# → send prompt to your LLM

Tuning

LeverEffekt
Chunk size/overlapRetrieval Granularität
Dense modelSemantische Qualität (über FastEmbed)
Sparse modelExact-Term Matching (BM25)
limit (k)Erinnerung vs Prompt Size
Metadata filtersRestrict Search Scope / Access Control

BestRAG vs Rolling Your Own

AspectBestRAGManual (LangChain/LlamaIndex)
Hybrid setupBuilt-in (Dense+Sparse+LI)Manual Wiring
StoreQdrant, OpinionatedAny, Flexible
ControlSimpler, Fewer KnobsVollständige Kontrolle
Best forFast Hybrid RAG StartCustom Pipelines

Gebaut auf FastEmbed und Qdrant; greife zu LangChain oder LlamaIndex wenn du vollständige Pipeline-Kontrolle brauchst.

Resources