Skip to main content

Production-grade RAG for Python: multi-LLM, query rewriting, reranking, guardrails, and evaluation.

Project description

rag-python

PyPI version PyPI downloads Python 3.10+ License: MIT GitHub

rag-python is a production-oriented Python library for Retrieval-Augmented Generation (RAG).

Ingest your documents, ask questions, get grounded answers — with query rewriting, multi-query retrieval, reranking, guardrails, and multi-LLM support.

Author: Raghav Singla
Repository: github.com/RaghavOG/rag-python


Features

  • Document pipeline: loaders → cleaning → chunking → embeddings → ChromaDB
  • Query pipeline: rewriting → multi-query / hybrid retrieval → reranking
  • Generation with guardrails (prompt injection + hallucination checks)
  • Evaluation scores + self-correction retry loop
  • LLM providers: OpenAI, Azure OpenAI, Anthropic, Gemini, Ollama
  • Loaders: TXT, MD, PDF, DOCX, CSV, JSON, HTML

Install

pip install rag-python
# or from source
pip install -e .
# with reranking + extra providers
pip install -e ".[rerank,local,hybrid,anthropic,gemini,all]"

Quickstart

from rag_python import RAG

rag = RAG(
    llm_provider="openai",
    llm_model="gpt-4o-mini",
    embedding_provider="openai",
    embedding_model="text-embedding-3-small",
)

rag.ingest(["./data"], reindex=True)
answer = rag.query("How many days of annual leave?")
print(answer.text)

Hybrid search + metadata filter

from rag_python import RAG, SearchConfig

rag = RAG(
    retriever="hybrid",  # pip install rag-python[hybrid]
    metadata_filter={"filename": "leave-policy.pdf"},
)
rag.ingest(["./policies/leave-policy.pdf", "./policies/handbook.pdf"])
answer = rag.query("How many days of annual leave?")

CLI

export OPENAI_API_KEY=sk-...
rag-python ingest ./data --reindex
rag-python query "How many days of annual leave?" -v
rag-python query "leave policy" --retriever hybrid --metadata-filter '{"filename": "leave-policy.pdf"}'

Environment variables

Variable Required Description
OPENAI_API_KEY For OpenAI Default LLM + embeddings
ANTHROPIC_API_KEY For Claude LLM only
GEMINI_API_KEY For Gemini LLM only
AZURE_OPENAI_ENDPOINT For Azure Azure OpenAI
AZURE_OPENAI_API_KEY For Azure Azure OpenAI
OPENAI_API_VERSION Azure Default 2023-09-01-preview
OLLAMA_BASE_URL Ollama Default http://localhost:11434
RAG_PYTHON_DATA_DIR Optional Default ./data
RAG_PYTHON_CHROMA_DIR Optional Default ./chroma_db

See .env.example for all tuning options.


Project structure

.
├── src/rag_python/      # Installable package (PyPI: rag-python)
│   ├── client.py        # High-level RAG API
│   ├── rag_pipeline.py  # Full pipeline
│   └── providers/       # OpenAI, Azure, Anthropic, Gemini, Ollama
├── tests/
├── examples/
├── docs/
├── data/                # Sample documents
├── pyproject.toml
└── main.py              # Local dev CLI wrapper

Docs


License

MIT © Raghav Singla

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

rag_python-0.3.0.tar.gz (29.1 kB view details)

Uploaded Source

Built Distribution

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

rag_python-0.3.0-py3-none-any.whl (33.5 kB view details)

Uploaded Python 3

File details

Details for the file rag_python-0.3.0.tar.gz.

File metadata

  • Download URL: rag_python-0.3.0.tar.gz
  • Upload date:
  • Size: 29.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.0

File hashes

Hashes for rag_python-0.3.0.tar.gz
Algorithm Hash digest
SHA256 50121386f68a6d5fc40d9c1c2beee4d040477ac0013133c71905a6a0098a9d1c
MD5 84f77cbea21c23de0819e9a21a95d9f6
BLAKE2b-256 17d48b37b9020b26f6b9aa1601fcee3ea0a2b713e8f3dacbec566901e87f021a

See more details on using hashes here.

File details

Details for the file rag_python-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: rag_python-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 33.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.0

File hashes

Hashes for rag_python-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3abfa2d2f37228e88503119ff97203886ccf4da036a6ecfe6ad3a93624696f2d
MD5 906774876a20e3df03f5643c57c4b66e
BLAKE2b-256 5d5b779e601619c0dd951cde5769012ea152e58284c01928a844ae770bded188

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