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 contentHash.BaseParser— derives a cachedunits()from_jar,_parse, andid.UnstructuredFileParser— parses any file supported byunstructured.PdfFileParser,MarkdownFileParser,CsvFileParser— aliases ofUnstructuredFileParserfor readable call sites.
Chunkers — split units into chunks
Chunker— protocol:chunks(unit) -> list[str].BaseChunker— derives a cachedchunks()from_jarand_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 cachedembed()from_jarand_encode.SentenceTransformerDenseEmbedder— dense, L2-normalizedDenseEmbeddingvia SentenceTransformers (defaultall-MiniLM-L6-v2).SpladeSparseEmbedder— sparseSparseEmbeddingvia a SPLADE encoder (defaultprithivida/Splade_PP_en_v1).
Indexes — store vectors and search by similarity
Index[K, E]— protocol: aStore[K, E]of embeddings plusasync 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 injectedkey_mapstore.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— derivesfuse()from_weight(rank, size).ReciprocalRankFuser— reciprocal rank fusion with smoothing constantk(default 60).BordaCountFuser— Borda count fusion.
Scorers — rerank chunks against a query
Scorer— protocol:async score(query, chunk) -> float.BaseScorer— derives a cachedscore()from_jarand_predict.CrossEncoderScorer— cross-encoder reranker (defaultcross-encoder/ms-marco-MiniLM-L6-v2).
Generators — produce an answer
Generator— protocol:async prompt(query) -> str.BaseGenerator— derives a cachedprompt()from_jarand_generate.TransformersGenerator— local Transformers text-generation model (defaultHuggingFaceTB/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) plusset(key, value),get(key) -> value | None,remove(key),clear(), andkeys().BaseStore— derives the full protocol fromkeys,__setitem__,__getitem__, and__delitem__.MemoryStore[K, V]— in-memory map from key to value (chunk text, embeddings, metadata, ...).FileStore[K, V]— likeMemoryStore, but seals values on disk under.jar/, keeping only keys and digests in memory.
Types
Hash— ablake3hasher; the content ID returned by parsers.DenseEmbedding—list[float].SparseEmbedding—dict[int, float]mapping token id to weight.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file rager-0.3.1.tar.gz.
File metadata
- Download URL: rager-0.3.1.tar.gz
- Upload date:
- Size: 15.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.24 {"installer":{"name":"uv","version":"0.11.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0482b78d86c0c92e55263acd417ba4eb6bb8188470c571c5540dc1b146d86c44
|
|
| MD5 |
bf4091b2adc0626dc5e9f932f663aa38
|
|
| BLAKE2b-256 |
2a4e47e01c7cdbeac1954275e3c5de9eab4e37582d19604b3f197eacd6e29a8b
|
File details
Details for the file rager-0.3.1-py3-none-any.whl.
File metadata
- Download URL: rager-0.3.1-py3-none-any.whl
- Upload date:
- Size: 20.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.24 {"installer":{"name":"uv","version":"0.11.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
398c7622b0c169f142954df8deec07ee4981b0f8c3355500db5ea61d15955a58
|
|
| MD5 |
58df97af2798815168a0962a81b059c7
|
|
| BLAKE2b-256 |
7d659b06114301a84a76d68dee1a956d31f6f3bb8fb107f474287fd27cb5ecf8
|