Skip to main content

ragrefine

Evidence-first post-retrieval refinement for RAG systems.

CI Python 3.12 License: MIT

ragrefine improves an existing retriever's candidate set before it reaches an LLM. It reranks, fuses, deduplicates, and selects context while preserving the original evidence and recording an auditable trace.

retriever -> Top-N candidates -> ragrefine -> refined Top-K + trace -> LLM

The core library is framework- and retriever-agnostic. Haystack support is available as an optional integration.

Core deliverables

Capability Delivered behaviour
Candidate contract Immutable candidates retain IDs, text, metadata, retrieval rank, and retrieval score
Ranking channels Independent original, neural, lexical, and configurable pattern rankings over the same candidate pool
Rank fusion Deterministic reciprocal-rank fusion without mixing incompatible raw-score scales
Context refinement Exact/near-duplicate suppression and rank-preserving Top-K/token-budget selection
Traceability Per-channel evidence, fusion contributions, stage timings, configuration, and explicit failure records
Neural reranking Optional SentenceTransformers CrossEncoder adapter with batched inference
Haystack integration Serializable component profiles and a retriever-to-generator pipeline builder
Evaluation Frozen BEIR candidate pools, checksummed artifacts, reproducibility commands, and a paired Ragas harness

Output candidates always originate from the supplied input pool; ragrefine does not invent or rewrite evidence.

Quick start

Python 3.12+ is required. From this checkout:

uv sync
from ragrefine import Candidate, CandidateSet, Refiner

candidates = CandidateSet(
    name="my-retriever",
    candidates=(
        Candidate(
            id="doc-1",
            text="Python 3.12 improves f-string parsing.",
            retrieval_rank=1,
            retrieval_score=0.91,
        ),
        Candidate(
            id="doc-2",
            text="Python 3.11 introduced exception groups.",
            retrieval_rank=2,
            retrieval_score=0.84,
        ),
    ),
)

result = Refiner().refine(
    query="Which Python version improves f-string parsing?",
    candidate_set=candidates,
    top_k=1,
)

print(result.candidates[0].candidate.id)
print(result.trace)

Refiner() preserves the original ranking. Explicit RefinerConfig settings enable lexical, pattern, neural, or fused profiles. See the technical design for the configuration contract.

Haystack integration

uv sync --extra haystack

The optional integration provides document conversion, a serializable RagRefineComponent, registered refiner profiles, prompt construction, and a complete Haystack pipeline builder. The RAG pipeline guide contains the runnable recipe and customization points.

Evaluation summary

Post-retrieval comparisons reuse the same frozen first-stage Top-50 candidates. Retrieval is not regenerated between profiles, and candidate-pool Recall@50 is a fixed ceiling rather than a refinement gain.

Experiment SciFact result Decision
B0 — frozen dense baseline nDCG@5 0.4592 Baseline
B1-reference — MiniLM-L6/CUDA nDCG@5 0.6262 Retained neural profile
B1-light — TinyBERT-L2/CPU nDCG@5 0.6112 Retained measured profile
B2-L — lexical/CPU nDCG@5 0.5432 Retained lightweight profile
B2-P — patterns/CPU nDCG@5 0.4559 Not retained
B3 — equal-weight fusion Best profile nDCG@5 0.5715 Not retained; below B1-reference

The end-to-end Ragas A/B used 13 paired DeepSeek queries. It is statistically inconclusive: the positive directional score differences have 95% paired bootstrap confidence intervals spanning zero, and both context metrics are saturated at 1.0. The run establishes neither an end-to-end benefit nor a regression. See the retained A/B report.

Machine-readable artifacts are the source of truth. No experiment is described as improved unless its recorded measurements support that claim.

Documentation

Detailed material removed from this README remains in the repository's focused documents:

Scope

ragrefine owns post-retrieval candidate refinement, context selection, and tracing. It deliberately does not own ingestion, parsing, chunking, embeddings, vector databases, first-stage retrieval, LLM generation, agents, or deployment.

Development

uv sync --extra haystack --extra ragas
uv run ruff check .
uv run ruff format --check .
uv run mypy src/
uv run pytest
uv build

Heavy model and benchmark dependencies remain optional so the core package has no runtime dependencies.

License

MIT

Download files

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

Source Distribution

ragrefine-0.2.1.tar.gz (23.9 kB view details)

Uploaded Source

Built Distribution

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

ragrefine-0.2.1-py3-none-any.whl (36.2 kB view details)

Uploaded Python 3

File details

Details for the file ragrefine-0.2.1.tar.gz.

File metadata

  • Download URL: ragrefine-0.2.1.tar.gz
  • Upload date:
  • Size: 23.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for ragrefine-0.2.1.tar.gz
Algorithm Hash digest
SHA256 d14fb3b784d7aa8c97866182ff7ef588a110446d50a84c117129c16a75a0527e
MD5 28222bd89f387b97e6a21cb15b87cb03
BLAKE2b-256 cab67d2805a4e183849f94fe24829730626b7d5be60ffed8c3eed0a16b9b455c

See more details on using hashes here.

Provenance

The following attestation bundles were made for ragrefine-0.2.1.tar.gz:

Publisher: release.yml on Atharva01/ragrefine

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ragrefine-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: ragrefine-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 36.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for ragrefine-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5860194216d1463dce3071f13b51abb0353308550fce577c219b64b13cbbe0a9
MD5 9b00ec80de8ad8f7302bbf4f7b2cd875
BLAKE2b-256 15433e3a093f6252ffa7839b5ac183861ce558b43d74c8caeb8e3d3cd0c5b5fd

See more details on using hashes here.

Provenance

The following attestation bundles were made for ragrefine-0.2.1-py3-none-any.whl:

Publisher: release.yml on Atharva01/ragrefine

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page