graceful_degradation.md · 7.4 KB

Graceful Degradation Implementation for mindX

Overview

The mindX system has been designed with modular architecture and graceful degradation to ensure it works perfectly with or without API keys. This allows for seamless development, testing, and deployment in various environments.

๐ŸŽฏ Key Principles

1. Modular Design

  • All external API integrations are optional modules
  • Core functionality works independently of external services
  • No single point of failure from missing API keys
  • 2. Graceful Degradation

  • Missing API keys trigger mock responses instead of failures
  • System continues to function with reduced capabilities
  • Clear logging indicates when services are unavailable
  • 3. Development-Friendly

  • Developers can work without setting up all API keys
  • Testing works in any environment
  • Production deployment is flexible
  • ๐Ÿ”ง Implementation Details

    LLM Handler Level

    Mistral Handler (llm/mistral_handler.py)

    # API key check in all methods
    if not self.api_key:
        logger.warning("Mistral API key not available. Returning mock response.")
        return f"[MOCK RESPONSE] Mistral API key not configured. Would process: {prompt[:100]}..."
    

    Features:

  • โœ… All methods check for API key availability
  • โœ… Mock responses maintain expected data structure
  • โœ… Logging clearly indicates degraded mode
  • โœ… No exceptions thrown for missing keys
  • LLM Factory (llm/llm_factory.py)

    # Graceful handler creation
    if MistralHandler: 
        handler_instance = MistralHandler(model_name_for_api=model_arg_for_handler, api_key=eff_api_key, rate_limiter=rate_limiter, config=global_config)
        if not eff_api_key:
            logger.warning("LLMFactory (mindX): Mistral API key not provided. Handler will operate in degraded mode.")
    

    Features:

  • โœ… Handlers created even without API keys
  • โœ… Clear warnings about degraded mode
  • โœ… Fallback to MockLLMHandler if handler unavailable
  • API Integration Level

    Mistral Integration (api/mistral_api.py)

    def __init__(self, config: MistralConfig):
        self.config = config
        self.client: Optional[MistralAPIClient] = None
        self.api_available = bool(config.api_key)  # Track availability

    In methods:

    if not self.api_available: logger.warning("Mistral API not available. Returning mock response.") return f"[MOCK RESPONSE] Mistral API not configured..."

    Features:

  • โœ… API availability tracked at initialization
  • โœ… All methods check availability before making requests
  • โœ… Mock responses maintain expected return types
  • โœ… Comprehensive logging
  • Configuration Level

    Environment Variables (.env.sample)

    # Mistral AI Configuration
    MISTRAL_API_KEY="YOUR_MISTRAL_API_KEY_HERE"  # Optional
    MISTRAL_BASE_URL="https://api.mistral.ai/v1"
    MISTRAL_TIMEOUT="30"
    

    ... other optional settings

    Features:

  • โœ… All API keys clearly marked as optional
  • โœ… Sensible defaults provided
  • โœ… Clear documentation of requirements
  • ๐Ÿงช Testing & Verification

    Test Script (test_graceful_degradation.py)

    Comprehensive test suite that verifies:

  • LLM Factory Tests
  • - Creates handlers without API keys - Verifies mock responses work - Tests connection methods

  • Mistral Integration Tests
  • - Tests all major methods without API key - Verifies mock responses maintain structure - Tests async context managers

  • Model Registry Tests
  • - Initializes without API keys - Lists available providers - Tests capability discovery

  • Agent Initialization Tests
  • - Core agents initialize without external APIs - BDI agent works with mock LLM - Memory and belief systems work independently

    Running Tests

    cd /home/hacker/mindxTheta
    python test_graceful_degradation.py
    

    ๐Ÿ“‹ Supported Scenarios

    โœ… Development Without API Keys

  • All core functionality works
  • Mock responses for external services
  • Full testing capabilities
  • No setup required
  • โœ… Partial API Key Configuration

  • Some services work fully, others degraded
  • System adapts automatically
  • Clear indication of what's available
  • โœ… Full API Key Configuration

  • All services work at full capacity
  • No performance impact
  • Production-ready operation
  • โœ… API Key Changes at Runtime

  • System adapts to new configurations
  • Graceful handling of key changes
  • No restarts required
  • ๐Ÿ”„ Mock Response Examples

    Text Generation

    # Without API key:
    "[MOCK RESPONSE] Mistral API key not configured. Would process: Analyze this complex problem..."

    With API key:

    "Based on the analysis, I can see that the problem involves multiple variables..."

    Code Generation

    # Without API key:
    "# [MOCK CODE] Mistral API key not configured
    

    Would generate code for: def hello():

    Suffix: print('world')"

    With API key:

    "def hello(): print('world') return 'Hello, World!'"

    Embeddings

    # Without API key:
    [[0.123, 0.456, 0.789, ...]]  # Random 1024-dimensional vectors

    With API key:

    [[0.234, 0.567, 0.890, ...]] # Real Mistral embeddings

    ๐Ÿš€ Benefits

    For Developers

  • Zero Setup: Start developing immediately
  • No Dependencies: Work without external services
  • Clear Feedback: Know exactly what's working
  • Easy Testing: Test all code paths
  • For Production

  • Flexible Deployment: Deploy with any API key combination
  • Cost Control: Use only needed services
  • Reliability: System works even if some APIs fail
  • Monitoring: Clear logs show service status
  • For Hackathons

  • Quick Start: Get running in minutes
  • Incremental Enhancement: Add APIs as needed
  • Demo Ready: Always works for presentations
  • Team Friendly: Everyone can contribute
  • ๐Ÿ”ง Configuration Examples

    Minimal Configuration (No API Keys)

    # .env file can be empty or contain only:
    MINDX_LOGGING_LEVEL="INFO"
    

    Partial Configuration (Some API Keys)

    # .env file with some keys:
    MISTRAL_API_KEY="your-mistral-key"
    

    GEMINI_API_KEY not set - will use mock

    GROQ_API_KEY not set - will use mock

    Full Configuration (All API Keys)

    # .env file with all keys:
    MISTRAL_API_KEY="your-mistral-key"
    GEMINI_API_KEY="your-gemini-key"
    GROQ_API_KEY="your-groq-key"
    OPENAI_API_KEY="your-openai-key"
    

    ๐Ÿ“Š System Status Indicators

    The system provides clear indicators of service availability:

    Log Messages

    INFO: Mistral API key not found. Handler will operate in degraded mode.
    WARNING: Mistral API key not available. Returning mock response.
    INFO: Mistral API key not available. Connection test passes for degraded mode.
    

    Response Patterns

  • Mock responses are clearly marked with [MOCK RESPONSE]
  • Error messages indicate missing configuration
  • Success messages show actual API usage
  • Configuration Checks

    # Check if service is available
    if handler.api_key:
        print("Service fully available")
    else:
        print("Service in degraded mode")
    

    ๐ŸŽ‰ Conclusion

    The mindX system is now fully modular and gracefully degrades without API keys. This implementation ensures:

  • โœ… Development without barriers
  • โœ… Testing in any environment
  • โœ… Flexible production deployment
  • โœ… Clear service status indication
  • โœ… Maintainable and extensible code
  • The system works perfectly for hackathons, development, testing, and production with any combination of API keys!


    All DocumentsDocument IndexThe Book of mindXImprovement JournalAPI Reference