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

2. Graceful Degradation

3. Development-Friendly

๐Ÿ”ง 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:

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:

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:

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:

๐Ÿงช Testing & Verification

Test Script (test_graceful_degradation.py)

Comprehensive test suite that verifies:

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

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

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

  1. 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

โœ… Partial API Key Configuration

โœ… Full API Key Configuration

โœ… API Key Changes at Runtime

๐Ÿ”„ 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

For Production

For Hackathons

๐Ÿ”ง 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

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:

The system works perfectly for hackathons, development, testing, and production with any combination of API keys!


All DocumentsDocument IndexThe Book of mindXImprovement JournalAPI Reference