Skip to main content

fabricatio-rag

MIT Python Versions PyPI Version PyPI Downloads PyPI Downloads Build Tool: uv

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 documents
  • afetch_document(query, config) — retrieve documents by semantic similarity
  • arefined_query(question, **kwargs) — refine user queries via a configurable template before retrieval
  • arank_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").

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 vector
  • from_txt_files(files, chunk_size, overlap) -> List[Self] — chunk text files using the Rust-backed split_into_chunks, creating one model instance per chunk
  • with_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 result
  • as_prompt() -> str — render as prompt text (from AsPrompt mixin)
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

Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

fabricatio_rag-0.6.2-py3-none-any.whl (25.2 kB view details)

Uploaded Python 3

File details

Details for the file fabricatio_rag-0.6.2-py3-none-any.whl.

File metadata

  • Download URL: fabricatio_rag-0.6.2-py3-none-any.whl
  • Upload date:
  • Size: 25.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","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}

File hashes

Hashes for fabricatio_rag-0.6.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8f53e33e7baddd06f9de7b753d0dbbb1be19d42a73ea257be7dd6545a135739b
MD5 c87e90baeb1f7d87e69e175233d04c1a
BLAKE2b-256 879a0b3fab0ff3385f2bf3d8dc4de10f46c840c8c10e72a0036c71cb28df864a

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.6.2 This release

1 file

0.6.1

1 file

0.6.0

1 file

0.4.3

1 file

0.4.1

1 file

0.4.0

1 file

0.3.1

1 file

0.3.0

1 file

0.2.4

18 files

0.2.3

12 files

0.2.2

12 files

0.2.0

12 files

0.1.13

12 files

0.1.12

12 files

0.1.11

12 files

0.1.10

8 files

0.1.9

8 files

0.1.8

8 files

0.1.7

8 files

0.1.6

4 files

0.1.5

3 files

0.1.4

4 files

0.1.3

4 files

0.1.2

4 files

0.1.0

5 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page