fabricatio-rag
Abstract framework for building Retrieval-Augmented Generation (RAG) pipelines on top of Fabricatio's agent architecture. Provides typed base classes, document models, and workflow actions for embedding, storing, retrieving, and reranking documents.
Requires Python 3.12+.
Installation
pip install fabricatio[rag]
# or
uv pip install fabricatio[rag]
Key Components
RAG Base Class (RAG)
Type-parameterized abstract class inheriting UseEmbedding, UseReranker, and UseLLM from fabricatio-core.
Defines the core RAG contract that concrete implementations must fulfill:
add_document(data, config)— embed and store documentsafetch_document(query, config)— retrieve documents by semantic similarityarefined_query(question, **kwargs)— refine user queries via a configurable template before retrievalarank_documents(query, documents, **kwargs)— rerank previously retrieved documents by relevance
Built-in refinement uses TEMPLATE_MANAGER.render_template with the template named in RagConfig.refined_query_template
(default: "built-in/refined_query"). The question opens the rendered prompt verbatim, so a caller that prepends a
run-wide prefix — its skills section, say — keeps that head identical across calls and inside the provider's prefix cache.
from fabricatio_rag.capabilities.rag import RAG, RAGConfigBase
from fabricatio_rag.models.document import StoredDocumentModel, SearchedDocumentModel
class MyRAG(
RAG[MyStoredDoc, MySearchedDoc, MyAddConfig, MyFetchConfig]
):
async def add_document(self, data, config=None):
# embed with self.aembedding(...), store in vector db
...
async def afetch_document(self, query, config=None):
# embed query, search vector db, return results
...
Document Models (StoredDocumentModel, SearchedDocumentModel)
Generic abstract base classes for document representations.
StoredDocumentModel[ST] extends Base and Vectorizable. Key methods:
prepare_insertion(vector) -> ST— produce a database-ready record from an embedding vectorfrom_txt_files(files, chunk_size, overlap) -> List[Self]— chunk text files using the Rust-backedsplit_into_chunks, creating one model instance per chunkwith_text_chunk(chunk) -> Self— create an instance from a single text chunk (subclass must implement)
SearchedDocumentModel[SD] extends Base and AsPrompt. Key methods:
from_raw(raw) -> Self— construct from raw database resultas_prompt() -> str— render as prompt text (fromAsPromptmixin)
from fabricatio_rag.models.document import StoredDocumentModel, SearchedDocumentModel
class MyStoredDoc(StoredDocumentModel[dict]):
content: str
def prepare_insertion(self, vector):
return {"text": self.content, "vector": vector}
@classmethod
def with_text_chunk(cls, chunk):
return cls(content=chunk)
class MySearchedDoc(SearchedDocumentModel[dict]):
content: str
@classmethod
def from_raw(cls, raw):
return cls(content=raw["text"])
Workflow Actions (StoreTextFile, StoreDocuments)
Ready-to-use Action subclasses that bridge the Fabricatio workflow engine with RAG storage.
StoreTextFile — ingests a list of Path objects, chunks them according to chunk_size (default 512) and
chunk_overlap_ratio (default 0.3), then stores the resulting chunks via add_document.
StoreDocuments — stores pre-built model instances directly, without any chunking step.
Both accept an optional store_config for passing configuration to the underlying add_document call.
from fabricatio_rag.actions.db import StoreTextFile
class MyStoreAction(StoreTextFile[MyStoredDoc, MySearchedDoc, MyAddConfig, MyFetchConfig]):
store_model = MyStoredDoc
chunk_size = 1024
chunk_overlap_ratio = 0.2
store_config = MyAddConfig(collection="docs")
Configuration
All options below are read through the fabricatio configuration chain (see the
Configuration Guide). Set them under the
[ext.rag] table in fabricatio.toml, equivalently under
[tool.fabricatio.ext.rag] in pyproject.toml, or via
FABRICATIO_EXT__RAG__<FIELD_UPPER> environment variables.
[ext.rag]
refined_query_template = "built-in/refined_query"
| Option | Type | Default | Description |
|---|---|---|---|
refined_query_template |
str |
"built-in/refined_query" |
The name of the refined query template which will be used to refine a query. |
precise_chunk_template |
str |
"built-in/precise_chunk" |
— |
enrich_qa_template |
str |
"built-in/enrich_qa" |
Template for generating question-answer pairs from text chunks. |
mini_chunk_size |
int |
128 |
— |
Access at runtime: from fabricatio_rag.config import rag_config.
Package Structure
fabricatio-rag/
├── python/fabricatio_rag/
│ ├── capabilities/ - RAG abstract base class and config
│ ├── actions/ - StoreTextFile, StoreDocuments workflow actions
│ ├── models/ - StoredDocumentModel, SearchedDocumentModel
│ ├── workflows/ - Workflow definitions (extend here)
│ ├── config.py - RagConfig dataclass
│ └── __init__.py
└── pyproject.toml
Dependencies
fabricatio-core— LLM routing, embedding, reranking, event system, workflow engine, and Rust text-chunking utilities
License
MIT — see LICENSE
Metadata
Release files for fabricatio-rag 0.6.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fabricatio_rag-0.6.4-py3-none-any.whl | Python 3 | none | any | Details |
Release files / fabricatio_rag-0.6.4-py3-none-any.whl
| Download URL | fabricatio_rag-0.6.4-py3-none-any.whl |
|---|---|
| Size | 25.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c2be05c5fba9309ec7b6d27722705e0ac165f8e13e1fd3053af56f0b2a2c3604
|
|
BLAKE2b-256 checksum How to use checksums |
acba68bfedbec858b2665a3281c018859233e93af1bad3f39efb2f2dd7e6c519
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.17 {"installer":{"name":"uv","version":"0.12.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|