Skip to main content

Caching based RAG primitives

Project description

rager

Composable RAG primitives with caching baked in.

Install

uv add rager

Getting started

uv run pytest
uv run prek install

For GPU (CUDA/ROCm) torch, add the matching PyTorch index to your own project and install torch from it — those builds aren't on PyPI.

Example

Wire the primitives together yourself — there is no hidden pipeline. This chunks documents, embeds and indexes each chunk under its own key, then answers a query from the nearest chunk:

chunker = SemanticChunker()
embedder = SentenceTransformerDenseEmbedder()
index = FaissIndex(384, MemoryStore())
chunks = MemoryStore()
generator = TransformersGenerator()

key = 0
for document in documents:
    for chunk in chunker.chunks(document):
        await index.set(key, await embedder.embed(chunk))
        chunks[key] = chunk
        key += 1

query = "Why do cats purr?"
(key,) = await index.similar(await embedder.embed(query), embedding_results=1)
answer = await generator.prompt(f"Answer using only the context.\n{chunks[key]}\n{query}")

tests/application/ has full dense and hybrid recipes.

API

Every stage is a Protocol with concrete implementations. Each module also ships an abstract Base* helper that derives the protocol from a few core operations, so you can drop in your own implementation. async methods batch concurrent calls; model-backed methods cache results under .jar/.

Parsers — extract text units from files

  • Parser — protocol: units(file) returns text units, id(file) returns the content Hash.
  • BaseParser — derives a cached units() from _jar, _parse, and id.
  • UnstructuredFileParser — parses any file supported by unstructured.
  • PdfFileParser, MarkdownFileParser, CsvFileParser — aliases of UnstructuredFileParser for readable call sites.

Chunkers — split units into chunks

  • Chunker — protocol: chunks(unit) -> list[str].
  • BaseChunker — derives a cached chunks() from _jar and _split.
  • SemanticChunker — splits on semantic boundaries with a token budget: SemanticChunker(model_name="gpt-3.5-turbo", chunk_size=1000, overlap=0).

Embedders — turn chunks into vectors

  • Embedder[E] — protocol: async embed(chunk) -> E.
  • BaseEmbedder — derives a cached embed() from _jar and _encode.
  • SentenceTransformerDenseEmbedder — dense, L2-normalized DenseEmbedding via SentenceTransformers (default all-MiniLM-L6-v2).
  • SpladeSparseEmbedder — sparse SparseEmbedding via a SPLADE encoder (default prithivida/Splade_PP_en_v1).

Indexes — store vectors and search by similarity

  • Index[K, E] — protocol: a Store[K, E] of embeddings plus async similar(embedding, embedding_results=100) -> list[key]. Ranks by inner product (equals cosine for L2-normalized vectors).
  • FaissIndex(dimensions, key_map) — flat FAISS index; seals embeddings on disk under .jar/ and records each key's FAISS id in the injected key_map store.
  • SparseIndex(embedding_map, token_map) — inner-product search over sparse weight maps through an inverted token index; the injected stores decide whether embeddings live in memory or on disk, and search only loads the posting lists of the query's tokens.

Fusers — merge ranked lists

  • Fuser[V] — protocol: fuse(*rankings) -> list[V].
  • BaseFuser — derives fuse() from _weight(rank, size).
  • ReciprocalRankFuser — reciprocal rank fusion with smoothing constant k (default 60).
  • BordaCountFuser — Borda count fusion.

Scorers — rerank chunks against a query

  • Scorer — protocol: async score(query, chunk) -> float.
  • BaseScorer — derives a cached score() from _jar and _predict.
  • CrossEncoderScorer — cross-encoder reranker (default cross-encoder/ms-marco-MiniLM-L6-v2).

Generators — produce an answer

  • Generator — protocol: async prompt(query) -> str.
  • BaseGenerator — derives a cached prompt() from _jar and _generate.
  • TransformersGenerator — local Transformers text-generation model (default HuggingFaceTB/SmolLM2-135M-Instruct, max_new_tokens=512).

Stores — map keys to data

  • Store[K, V] — protocol: mapping-style access (store[key], del store[key], in, len(), iteration) plus set(key, value), get(key) -> value | None, remove(key), clear(), and keys().
  • BaseStore — derives the full protocol from keys, __setitem__, __getitem__, and __delitem__.
  • MemoryStore[K, V] — in-memory map from key to value (chunk text, embeddings, metadata, ...).
  • FileStore[K, V] — like MemoryStore, but seals values on disk under .jar/, keeping only keys and digests in memory.

Types

  • Hash — a blake3 hasher; the content ID returned by parsers.
  • DenseEmbeddinglist[float].
  • SparseEmbeddingdict[int, float] mapping token id to weight.

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

rager-0.3.2.tar.gz (15.5 kB view details)

Uploaded Source

Built Distribution

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

rager-0.3.2-py3-none-any.whl (20.6 kB view details)

Uploaded Python 3

File details

Details for the file rager-0.3.2.tar.gz.

File metadata

  • Download URL: rager-0.3.2.tar.gz
  • Upload date:
  • Size: 15.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"CachyOS Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for rager-0.3.2.tar.gz
Algorithm Hash digest
SHA256 2b875e63670524e222c43b588a97c4b3e1de3cd66fcab800194c4e6dccf4c0ae
MD5 b9a5c4786e962f2a48dd1f97c4ea2c68
BLAKE2b-256 59d51b815e44ba95a088cd57d475dfde2945e706ec0d843554e78448c2dc7276

See more details on using hashes here.

File details

Details for the file rager-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: rager-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 20.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"CachyOS Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for rager-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 79aae19997043f252438811caa5c5090a66a902f24f67b46c389fe926d08e061
MD5 27b59908743aab39dd24f33d4976c75f
BLAKE2b-256 ea246709a677fa83355b7dc24b4cff7fb1510c3c0a7b755eadb90461cb24e5ba

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