Skip to content

Latest commit

 

History

History
207 lines (170 loc) · 8.53 KB

File metadata and controls

207 lines (170 loc) · 8.53 KB

Backup/Fallback System Implementation

Overview

A comprehensive backup/fallback system has been implemented to provide graceful degradation when live AI models are unavailable due to API failures, network issues, or service outages. The system maintains a good user experience by seamlessly switching to pre-generated responses.

Components Created/Modified

1. demo-backup/backup-loader.ts (Enhanced)

  • Purpose: Loads pre-generated backup responses from JSON files
  • Key Features:
    • loadBackupResponses(): Fetches backup responses for specific questions
    • findMatchingBackupQuestion(): Intelligent keyword matching to find relevant backup responses
    • getGenericFallbackResponse(): Provides persona-specific generic responses when no specific backup is available
    • Pre-defined demo questions with IDs for easy reference

2. src/services/backup.ts (New)

  • Purpose: Core backup service with automatic failure detection and fallback logic
  • Key Features:
    • Backup Modes:
      • disabled: No backup responses
      • auto: Automatically enables after consecutive failures (default: 2 failures)
      • always: Always use backup responses (useful for demos)
    • Failure Tracking: Records consecutive failures and successes
    • Auto-Recovery: Automatically disables backup mode when LLM recovers
    • State Management: Listeners for UI updates
    • Streaming Support: Simulates streaming for backup responses with word-by-word delivery
    • Status Reporting: Provides detailed status information for UI display

3. src/services/llm.ts (Modified)

  • Changes:
    • Added personaId to ChatRequest type for backup response matching
    • Integrated backup service into chatWithLLM() and chatWithLLMStreaming()
    • Added extractUserQuestion() helper to extract user's question from message history
    • Automatic failure recording and success tracking
    • Seamless fallback to backup responses when LLM fails
    • Pre-check for backup mode before attempting LLM calls

4. src/App.tsx (Modified)

  • Changes:
    • Added backup mode state (backupMode, backupStatus)
    • Imported backup service functions
    • Added backup state listener for real-time status updates
    • Updated all chatWithLLM() and chatWithLLMStreaming() calls to include personaId
    • Added backup mode settings to localStorage persistence
    • UI Controls (in Settings):
      • Backup mode selector (disabled/auto/always)
      • Real-time status display with visual indicators
      • Contextual help text explaining each mode

5. public/demo-backup/ (New Directory)

  • Purpose: Stores pre-generated backup responses as JSON files
  • Files:
    • q1-technical-bottlenecks.json
    • q2-model-selection.json
    • q3-data-sovereignty.json
    • q4-community-model.json
    • q5-debugging-approach.json
    • q6-future-architecture.json
  • Structure: Each file contains a question and responses from all personas (maya, otto, sarah, marcus, jessica)

How It Works

Automatic Failure Detection

  1. When an LLM API call fails, recordFailure() is called
  2. The failure counter increments
  3. If in auto mode and failures reach threshold (default: 2), backup mode activates
  4. When an LLM call succeeds, recordSuccess() resets the counter and deactivates backup mode

Backup Response Flow

  1. Check Backup Mode: Before making LLM call, check if backup mode is active
  2. Extract Question: Get the user's question from message history
  3. Find Match: Use keyword matching to find relevant pre-generated response
  4. Fallback Chain:
    • Try to find matching pre-generated response
    • If not found, use persona-specific generic response
    • If all else fails, use ultimate fallback message

Streaming Simulation

  • Backup responses are split into words
  • Words are delivered progressively with small delays (30ms default)
  • Provides natural streaming experience matching live LLM behavior
  • Works seamlessly with TTS and UI updates

Integration with Persona System

The backup system is fully integrated with the existing persona system:

  • Each backup response is persona-specific
  • Generic fallbacks reflect each persona's voice and expertise
  • Persona IDs are passed through the entire call chain
  • Works with both streaming and non-streaming modes

UI/UX Features

Settings Panel

Located in Settings → General → Backup/Fallback System:

  • Mode Selector: Dropdown to choose backup mode
  • Status Display: Real-time status with color-coded background
    • Gray: Ready/Monitoring
    • Yellow: Active (using backups)
  • Help Text: Context-sensitive explanations for each mode

Status Messages

  • "Backup mode: Ready" - Auto mode, no failures
  • "Backup mode: Monitoring (1/2 failures)" - Tracking failures
  • "Backup mode: Active (2 failures detected)" - Using backups
  • "Backup mode: Always active" - Always mode enabled
  • "Backup mode: Disabled" - No backup support

Testing the System

Test Scenario 1: Auto Mode with LLM Failure

  1. Set backup mode to "Auto"
  2. Stop your LLM service (e.g., Ollama)
  3. Ask a question
  4. After 2 failed attempts, backup mode activates
  5. Subsequent questions use backup responses
  6. Restart LLM service
  7. Next successful response deactivates backup mode

Test Scenario 2: Always Mode (Demo Mode)

  1. Set backup mode to "Always"
  2. Ask questions matching demo topics (e.g., "What are the technical bottlenecks?")
  3. Receive pre-generated responses immediately
  4. Useful for demos when LLM is unavailable

Test Scenario 3: Generic Fallback

  1. Set backup mode to "Always"
  2. Ask a question that doesn't match any demo questions
  3. Receive persona-specific generic response

Pre-Generated Response Topics

The system includes pre-generated responses for these topics:

  1. Technical Bottlenecks - LLM+GeoAI integration challenges
  2. Model Selection - Choosing models for LIDAR processing
  3. Data Sovereignty - Architecture for data governance
  4. Community Model - PostGIS LLM development
  5. Debugging Approach - Complex geometry processing
  6. Future Architecture - Evolution of geospatial AI systems

Configuration

Backup Mode Settings

type BackupMode = 'disabled' | 'auto' | 'always';

interface BackupConfig {
  mode: BackupMode;
  enabled: boolean;
  consecutiveFailures: number;
  autoEnableThreshold: number; // Default: 2
}

Customization

  • Failure Threshold: Modify autoEnableThreshold in backup.ts
  • Streaming Delay: Adjust delayMs parameter in streamBackupResponse()
  • Add New Responses: Create new JSON files in public/demo-backup/
  • Update Matching: Modify findMatchingBackupQuestion() logic

Benefits

  1. Reliability: Application remains functional even when LLM services fail
  2. User Experience: Seamless fallback without error messages
  3. Demo Mode: Perfect for presentations when LLM is unavailable
  4. Graceful Degradation: Maintains conversation flow with relevant responses
  5. Automatic Recovery: Returns to live LLM when service recovers
  6. Transparency: Clear status indicators show when backup mode is active

Future Enhancements

Potential improvements:

  1. More Backup Responses: Expand the library of pre-generated responses
  2. Better Matching: Use semantic similarity instead of keyword matching
  3. Response Caching: Cache recent LLM responses as backups
  4. Partial Failures: Handle individual persona failures differently
  5. Analytics: Track backup usage patterns
  6. Custom Responses: Allow users to add their own backup responses

Maintenance

Adding New Backup Responses

  1. Create a new JSON file in public/demo-backup/
  2. Follow the existing structure:
{
  "question": "Your question here",
  "responses": {
    "maya": { "content": "...", "timestamp": "..." },
    "otto": { "content": "...", "timestamp": "..." },
    "sarah": { "content": "...", "timestamp": "..." },
    "marcus": { "content": "...", "timestamp": "..." },
    "jessica": { "content": "...", "timestamp": "..." }
  }
}
  1. Add the question to DEMO_QUESTIONS array in backup-loader.ts
  2. Update matching logic in findMatchingBackupQuestion() if needed

Updating Generic Fallbacks

Edit the genericResponses object in getGenericFallbackResponse() in backup-loader.ts

Conclusion

The backup/fallback system provides a robust safety net for the application, ensuring users always receive meaningful responses even when AI services are unavailable. The system is transparent, configurable, and integrates seamlessly with existing features including streaming, TTS, and the persona system.