Source Code:
src/gaia/agents/tools/rag_tools.pyComponent: RAGToolsMixin
Module:
gaia.agents.tools.rag_tools
Import: from gaia.agents.tools.rag_tools import RAGToolsMixinOverview
RAGToolsMixin provides comprehensive document retrieval and query capabilities for the Chat Agent, implementing hybrid search (semantic + keyword), document summarization, and quality evaluation tools. Key Features:- Hybrid semantic + keyword search for optimal retrieval
- Per-file targeted search for fast lookups
- Adaptive chunk retrieval based on document size
- Multi-section iterative summarization for large documents
- Retrieval quality evaluation
- Document management (indexing, listing, dumping)
- Semantic embeddings search with multiple query reformulations
- Keyword boost for exact term matches
- Hash-based deduplication
- Adaptive max chunks (5-25) based on document size
- Page number extraction with lookback for citation
Host Agent Contract
RAGToolsMixin is a mixin, not a base class — it reads state off self that it
never initializes itself. A class mixing it in must set all five attributes
below before _register_tools() runs, or every tool call hits an
AttributeError internally the moment it touches self.rag — caught and
returned as a generic status: "error" rather than naming the missing
attribute (see below):
Nothing sets these for you — not the base
Agent class, not RAGToolsMixin
itself. ChatAgent (hub/agents/chat/python/gaia_agent_chat/agent.py)
is the reference implementation: it sets all five before calling
super().__init__(), because _register_tools() runs inside that call. The
custom agent guide shows the same pattern for a
minimal example.
A host may also override _no_documents_message(), the text every document
tool reports when nothing is indexed (default: "No documents are currently indexed."). ChatAgent overrides it in an Agent UI chat, where documents are
attached per chat, to say that nothing is attached to this chat rather than
that the library is empty.
A document only joins self.rag.indexed_files once it is searchable. While one
is still indexing in the background, documents_still_indexing(self.rag) (in
gaia.agents.tools.rag_tools) returns its path; ChatAgent uses it to tell
the model each turn which documents are indexed and which are not ready yet.
Of the five, only self.rag is enforced at the point of use: query_documents
and query_specific_file read it through a helper that raises a
MissingHostAttributeError naming the host class, the attribute, and this
section — instead of letting a bare AttributeError get swallowed into a
generic error result. None remains a valid,
intentional “RAG disabled” value and does not raise. The other four
attributes (indexed_files, max_chunks, current_session,
session_manager) are still enforced by convention, matching the reasoning
in #3316: a construction-time
check was considered and rejected — it would run before skill-injected
tools exist and require
inspecting a process-global tool registry. See the issue for the full
analysis.
Requirements
Functional Requirements
-
Document Query (query_documents)
- Multi-key semantic search with reformulation
- Keyword boost for exact matches
- Adaptive chunk limits (5-25 based on doc size)
- Page extraction for citations
- Debug mode with full retrieval metrics
-
File-Specific Query (query_specific_file)
- Fast per-file retrieval
- Same hybrid search strategy as query_documents
- File resolution prefers an exact path, then an exact filename / trailing path, then a substring;
if several indexed files still match it returns an
Ambiguous filenameerror with acandidateslist instead of guessing (summarize_documentanddump_documentresolve files the same way)
-
Text Search (search_indexed_chunks)
- Exact text pattern matching in RAG chunks
- Case-insensitive search
- Limited to 100 matches for performance
-
Retrieval Evaluation (evaluate_retrieval)
- Keyword overlap calculation
- Sufficiency assessment
- Confidence scoring
- Next-step recommendations
-
Document Management
- Index single documents with statistics
- Index entire directories (recursive option)
- List indexed documents
- Export cached extracted text
- RAG system status reporting
-
Document Summarization (summarize_document)
- Multi-section iterative approach for large docs
- Three summary types: brief, detailed, bullets
- Page-based section boundaries
- Overlap between sections for context
- Structured output with metadata
Non-Functional Requirements
-
Performance
- Hash-based deduplication (O(1) instead of O(N))
- Adaptive chunk limits prevent context overflow
- Cached text reuse (no VLM re-extraction)
- Timeout handling for long summarizations
-
Quality
- Citation-ready with page numbers
- Structured instruction format for LLM
- Debug info for retrieval analysis
- Graceful degradation on failures
-
Usability
- Clear status messages
- Numbered chunk IDs for reference
- File statistics on indexing
- Helpful error hints
API Specification
File Location
Public Interface
Implementation Highlights
Hybrid Search Architecture
Iterative Summarization
Testing Requirements
File:tests/agents/chat/test_rag_tools_mixin.py
Key test scenarios:
- Hybrid search with keyword boost
- Per-file targeted search
- Exact text search in chunks
- Retrieval evaluation metrics
- Document indexing with statistics
- Directory indexing (recursive)
- Iterative summarization for large docs
- Page extraction with lookback
- Debug mode output validation
- Graceful degradation on failures
Dependencies
- RAG SDK for indexing and retrieval
- Agent SDK for summarization
- SessionManager for path validation
Usage Examples
Example 1: Query Documents with Debug
Example 2: Summarize Large Document
Example 3: Targeted File Search
RAGToolsMixin Technical Specification