Multi-Document RAG
On this page
Overview
MultiDocumentRAGSystem manages a collection of independently-chunked
documents, letting queries be scoped to a subset (filter_docs) and
providing per-document lifecycle operations (add/update/remove) on top of
shared vector_db, chunker, and context_manager collaborators.
MultiDocumentRAGSystem
Constructor
def __init__(
self,
vector_db: Any,
llm: Any,
chunker: Any,
context_manager: Any,
config: Optional[Any] = None,
)
All four collaborators are required positional dependencies — this class
does not construct them internally. chunker and context_manager are
externally-defined objects not present in this archive; see
Installation for their inferred contract.
Document lifecycle
_detect_language(text) -> str auto-detects language when not explicitly
provided to add_document.
query(query, top_k=None, filter_docs=None, include_sources=True, language="ar", **llm_kwargs) -> Dict[str, Any]
Internally builds an enhanced prompt via _build_prompt(query, context, language).
Introspection
Persistence
def save(self, path: str) -> None: ...
@classmethod
def load(cls, path: str, vector_db, llm, chunker, context_manager, config=None) -> "MultiDocumentRAGSystem": ...
Note that load() still requires the four collaborator objects to be
supplied fresh — only the document/chunk state is restored from path, not
the collaborators themselves.
Async API
agenerate(query, **kwargs) -> str, aretrieve(query, **kwargs), plus
__aenter__/__aexit__.
Complete example
from zerda.rag.types.multi_doc_rag import MultiDocumentRAGSystem
system = MultiDocumentRAGSystem(vector_db, llm, chunker, context_manager)
system.add_documents({
"handbook_2024": handbook_text,
"policy_v2": policy_text,
})
result = system.query(
"What is the remote-work policy?",
filter_docs=["policy_v2"],
language="en",
)
print(result["answer"])
print(system.list_documents())
system.save("state/multi_doc.json")
Best practices
- Use
filter_docsrather than maintaining separateMultiDocumentRAGSysteminstances per document subset — it reuses the same vector index and collaborators. - Call
get_document_stats_summary()periodically in long-running services to catch documents that silently failed chunking.
Common mistakes
- Calling
load()without re-supplying the samechunker/context_managerconfiguration used originally — behavior on subsequentadd_documentcalls may differ if the collaborator objects changed. - Passing a document ID in
filter_docsthat hasn't been added — the query will run against an effectively empty filtered set with no chunks returned.
Related APIs
- Domain-Specific RAG — orthogonal filtering (topic vs. document identity); the two can be composed.