Skip to main content
This is Part 2 of 3. If you haven’t completed Part 1, start there: Part 1: Getting Started
  • Time to complete: 15-20 minutes
  • What you’ll build: Agent with monitoring, sessions, and custom capabilities
  • What you’ll learn: Tool mixins, file watching, persistence, and extensibility patterns

Building It: The Step-by-Step Journey (Continued)

Step 4: Add Tool Mixins

Use GAIA’s built-in mixins instead of implementing tools from scratch.
Document operations:
  • query_documents(query) - Semantic search across all indexed docs
  • query_specific_file(file_path, query) - Search specific document
  • index_document(file_path) - Add document to index
  • index_directory(directory_path) - Index all files in directory
  • list_indexed_documents() - List currently indexed files
  • rag_status() - Get index statistics
Import: from gaia.agents.tools import RAGToolsMixin
What you have: An agent using GAIA’s built-in mixins. This reduces code duplication and provides tested tool implementations.

Step 5: Add File Monitoring (Auto-Indexing)

Add file system monitoring to automatically reindex documents when they change.
Watchdog library implementation:
Implementation details:
  • File events are async (non-blocking)
  • Detection latency: ~1 second
  • Runs in dedicated thread
  • Debouncing: 2-second window per file
  • Memory: LRU eviction at 1000 tracked files
  • Supported types: .pdf, .txt, .md, .py, .json, etc.
What you have: Reactive file monitoring. The index automatically updates when documents are created or modified.

Step 6: Add Session Persistence

Implement session persistence to avoid re-indexing on every restart.
Data structure:
Save process:
Load process:
Use cases:
  • Avoid re-indexing large document collections
  • Maintain conversation context across restarts
  • Share pre-indexed state with team (requires file permissions)
  • Support long-running research sessions
What you have: Persistent sessions via JSON serialization. State (indexed docs, conversation) survives restarts.

The Complete Agent

The ChatAgent class combines all components. Here’s how to configure and use it:
complete_agent.py
Run it:
Full execution sequence:
  1. Initialization (Startup):
  2. First Query: “What documents are indexed?”
  3. Second Query: “Find safety manual…”
  4. Background: File Added to ./documents/
The file watcher handles this automatically. Note that initial indexing still requires manual setup.

Making It Your Own (Extensibility)

The agent uses Python class inheritance, so you can override methods or add new tools.

Add Custom Tools

Extend the agent by adding domain-specific tools.
custom_tools.py

Customize RAG Behavior


Create Specialized Agents

Override system prompts to create domain-specific behavior.
research_agent.py

Real-World Examples

Example 1: Research Assistant

Use case: Searching across multiple academic papers for common themes.
research_assistant.py

Example 2: Company Knowledge Base

Use case: Shared document search across organizational documentation.
knowledge_base.py

Example 3: Personal Document Assistant

Use case: Interactive CLI for querying personal documents.
personal_assistant.py

Next Steps

You’ve built an agent with monitoring, sessions, and customization! Continue with Part 3 to learn deployment and optimization:

Part 3: Deployment & Optimization

Master the agent’s intelligence, learn advanced patterns, deploy as API/CLI, and optimize performance.