Skip to main content
Component: RAGToolsMixin Module: gaia.agents.tools.rag_tools Import: from gaia.agents.tools.rag_tools import RAGToolsMixin

Overview

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)
Search Strategy:
  1. Semantic embeddings search with multiple query reformulations
  2. Keyword boost for exact term matches
  3. Hash-based deduplication
  4. Adaptive max chunks (5-25) based on document size
  5. 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

  1. 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
  2. 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 filename error with a candidates list instead of guessing (summarize_document and dump_document resolve files the same way)
  3. Text Search (search_indexed_chunks)
    • Exact text pattern matching in RAG chunks
    • Case-insensitive search
    • Limited to 100 matches for performance
  4. Retrieval Evaluation (evaluate_retrieval)
    • Keyword overlap calculation
    • Sufficiency assessment
    • Confidence scoring
    • Next-step recommendations
  5. Document Management
    • Index single documents with statistics
    • Index entire directories (recursive option)
    • List indexed documents
    • Export cached extracted text
    • RAG system status reporting
  6. 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

  1. 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
  2. Quality
    • Citation-ready with page numbers
    • Structured instruction format for LLM
    • Debug info for retrieval analysis
    • Graceful degradation on failures
  3. 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

External:
  • 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


RAGToolsMixin Technical Specification