Better RAG results from the database you already run.
BestRAG finds passages by meaning and by exact words, then merges the two lists. Use default local models or your provider's.
- Postgres or Qdrant, wherever they run. Any
Postgres with pgvector works, such as
AWS RDS,
Google Cloud SQL,
Azure,
Supabase or
Neon. So does
Qdrant, on your machine or on Qdrant Cloud.
- Finds exact words too. BM25 finds names and codes like
XJ-9042, even when search by meaning misses them. - Your own embeddings, or the built-in local model. Use OpenAI, Cohere, Gemini and more with no model download. By default, BestRAG runs a small model on your machine.
- PDFs and text. Every result keeps its page number. Indexing a name again replaces the old version.
- Fast. A hybrid search takes a few milliseconds, with no API calls by default.
- Optional reranker. A cross-encoder can put the best result first. It is off by default, because it is slower.
- Many languages. PDFs are read in reading order, including right-to-left scripts.
uv add bestrag # with uv
pip install bestrag # with pipYou also need a database. To try one on your machine, start it with Docker:
docker run -p 5432:5432 -e POSTGRES_PASSWORD=postgres pgvector/pgvector:pg17 # Postgres with pgvector
docker run -p 6333:6333 -p 6334:6334 qdrant/qdrant # or Qdrantfrom bestrag import BestRAG
# Free and local: BestRAG runs a small embedding model on your machine.
rag = BestRAG("postgresql://postgres:postgres@localhost:5432/postgres", "my-docs")
# or Qdrant, on its gRPC port 6334: BestRAG("http://localhost:6334", "my-docs")
# To use your provider without a model download, comment out the local line and uncomment these:
# from openai import OpenAI
# client = OpenAI()
# def embed(texts, query):
# return [d.embedding for d in client.embeddings.create(model="text-embedding-3-small", input=texts).data]
# rag = BestRAG("postgresql://postgres:postgres@localhost:5432/postgres", "my-docs", dense_model=embed)
rag.add_pdf("handbook.pdf")
rag.add_text("Part XJ-9042 replaces the filter in the 2019 pump.", "parts", {"team": "support"})
for hit in rag.search("which filter fits the old pump?", limit=5):
print(f"{hit['score']:.2f} {hit['source']} page {hit['page']} {hit['text'][:60]}")Results come best first. Each hit is a dict with text, score, source, page and metadata. The first run with the local model downloads about 130 MB. Embedding providers has recipes for OpenAI, Azure OpenAI, Gemini, Cohere, Voyage, Mistral, Bedrock and Ollama.
The documentation covers every option and each cloud. Its llms.txt makes the docs available to your AI coding agent.
On BEIR SciFact, hybrid search finds more of the right abstracts than dense or BM25 search alone. Postgres and Qdrant give the same quality. Benchmarks has the numbers for both.
Try BestRAG in your browser on Hugging Face Spaces, with your own Postgres or Qdrant URL.
Version 1.0 is a rewrite with a new API. It cannot read collections made by 0.3, so index your documents again. The old pure-Python code is kept at the v0.3.3 tag. It is no longer maintained.
