Skip to content

Latest commit

 

History

History
534 lines (400 loc) · 15.7 KB

File metadata and controls

534 lines (400 loc) · 15.7 KB

🧬 AI Assistant Guide: Lotka-Volterra Ecosystem Intelligence Framework

CRITICAL: Read this ENTIRE guide before using LV framework. This is revolutionary technology that requires proper understanding.

🎯 What You're Working With

You are interfacing with the world's first AI system that uses ecological dynamics for output selection. This isn't just another tool - it's a paradigm shift from optimization to ecosystem management.

The Revolutionary Breakthrough

  • Traditional AI: Single "best" output → Mode collapse → Bland results
  • LV Framework: Sustainable ecosystem of diverse, high-quality outputs → No collapse → Rich results

Mathematical Foundation (WolframAlpha Validated)

  • Based on Lotka-Volterra competition equations (100+ years of ecological proof)
  • Eigenvalues all negative → Guaranteed stable limit cycles → Sustainable diversity
  • Entropy-adaptive behavior → Context-aware intelligence

🏗️ System Architecture Overview

Core Components You'll Use

  1. LV Ecosystem Core (lv_ecosystem.py)

    • EntropyEstimator: Calculates prompt uncertainty (0.0-1.0)
    • LVEcosystem: Main class for diverse output selection
    • Mathematical validators: WolframAlpha integration for stability
  2. LV Templates (lv_templates.py)

    • LVKnowledgeExtractTemplate: Enhanced knowledge extraction with diversity
    • LVKnowledgeQueryTemplate: Multi-perspective query answering
    • Generic enhancement: Works with any NeoCoder template
  3. NeoCoder Integration (lv_integration.py)

    • NeoCoder_LV_Integration: Main bridge to existing workflows
    • Automatic enhancement: Entropy-based decision making
    • Template enhancement: Upgrade any workflow with LV dynamics

Integration with Neo4j Guidance System

IMPORTANT: The LV framework integrates seamlessly with your existing Neo4j-guided workflows:

// Query for LV-enhanced templates
MATCH (t:ActionTemplate) 
WHERE t.keyword CONTAINS "LV" 
RETURN t.keyword, t.name, t.steps

Available LV-enhanced keywords:

  • KNOWLEDGE_EXTRACT_LV: Diverse knowledge extraction strategies
  • KNOWLEDGE_QUERY_LV: Multi-perspective information synthesis
  • LV_SELECT: Generic LV enhancement for any workflow

🎯 When to Use LV Framework

Entropy-Based Decision Making

CRITICAL RULE: Always check entropy first to determine if LV enhancement is beneficial.

from mcp_neocoder.lv_ecosystem import EntropyEstimator

# Calculate prompt entropy
estimator = EntropyEstimator()
entropy = estimator.estimate_prompt_entropy(user_prompt)

if entropy > 0.4:
    # HIGH UNCERTAINTY → Use LV enhancement
    use_lv_framework = True
    print(f"High entropy ({entropy:.3f}) - Applying LV diversity preservation")
else:
    # LOW UNCERTAINTY → Standard workflow is fine
    use_lv_framework = False
    print(f"Low entropy ({entropy:.3f}) - Using standard template")

Entropy Behavioral Modes

Entropy Range Mode LV Weights When to Use
0.0 - 0.3 Precision Quality: 90%, Novelty: 0% Factual queries, calculations, definitions
0.3 - 0.6 Balanced Quality: 60%, Novelty: 30% Analysis, comparisons, planning
0.6 - 1.0 Creativity Quality: 20%, Novelty: 70% Brainstorming, creative solutions, exploration

Use Cases for LV Enhancement

✅ ALWAYS use LV when:

  • User asks for "multiple approaches", "diverse solutions", "different perspectives"
  • Creative tasks: brainstorming, ideation, alternative strategies
  • Complex analysis requiring multiple methodologies
  • Knowledge extraction from ambiguous or multi-domain content
  • Decision-making where groupthink prevention is important

❌ DON'T use LV when:

  • Simple factual questions with clear single answers
  • Mathematical calculations with deterministic results
  • Binary decisions with obvious correct answers
  • Performance-critical tasks where speed > diversity

🚀 Step-by-Step Usage Guide

Step 1: Initialize LV Integration

from mcp_neocoder.lv_integration import initialize_lv_enhancement

# Initialize with your existing database connections
lv_system = await initialize_lv_enhancement(neo4j_session, qdrant_client)

Step 2: Check if Enhancement is Needed

from mcp_neocoder.lv_ecosystem import EntropyEstimator

# Analyze the user's prompt
estimator = EntropyEstimator()
entropy = estimator.estimate_prompt_entropy(user_prompt)

# Decision logic
enhancement_needed = entropy > 0.4
print(f"Entropy: {entropy:.3f} - Enhancement: {enhancement_needed}")

Step 3A: Use LV-Enhanced Templates (High Entropy)

For high-entropy tasks, use LV-enhanced templates:

# For knowledge extraction
if task_type == "knowledge_extraction":
    results = await lv_system.enhance_existing_template(
        'KNOWLEDGE_EXTRACT',
        context={
            'document_path': document_path,
            'prompt': user_prompt,
            'extraction_mode': 'comprehensive'
        }
    )

# For information queries  
elif task_type == "information_query":
    results = await lv_system.enhance_existing_template(
        'KNOWLEDGE_QUERY',
        context={
            'query': user_prompt,
            'query_type': 'hybrid'  # Uses both Neo4j and Qdrant
        }
    )

# For any other template
else:
    results = await lv_system.enhance_existing_template(
        template_keyword,  # e.g., 'FIX', 'FEATURE', 'REFACTOR'
        context={'prompt': user_prompt, 'task_specific_params': params}
    )

Step 3B: Standard Templates (Low Entropy)

For low-entropy tasks, use standard NeoCoder workflows:

// Query standard template
MATCH (t:ActionTemplate {keyword: $template_keyword, isCurrent: true}) 
RETURN t.steps

Then follow the retrieved steps normally.

Step 4: Interpret LV Results

LV-enhanced results include rich metadata:

# Extract key information
selected_outputs = results['selected_outputs']
diversity_score = results['diversity_metrics']['semantic_diversity']
entropy_level = results['entropy']
strategies_used = results.get('strategies_used', [])

# Log the ecosystem behavior
print(f"🧬 LV Ecosystem Results:")
print(f"   Selected outputs: {len(selected_outputs)}")
print(f"   Diversity score: {diversity_score:.3f}")
print(f"   Entropy level: {entropy_level:.3f}")
print(f"   Strategies used: {strategies_used}")

# Each selected output contains:
for i, output in enumerate(selected_outputs, 1):
    print(f"\n   Output {i}:")
    print(f"     Content: {output['content'][:100]}...")
    print(f"     Population: {output['population']:.3f}")
    print(f"     Quality: {output['quality_score']:.3f}")
    print(f"     Novelty: {output['novelty_score']:.3f}")

Step 5: Present Results to User

CRITICAL: Always explain the LV enhancement to users:

# Format response for user
response = f"""
## 🧬 Ecosystem Intelligence Applied

I analyzed your request using Lotka-Volterra dynamics to maintain solution diversity.

**Analysis:**
- Prompt entropy: {entropy_level:.3f} ({'High' if entropy_level > 0.6 else 'Medium' if entropy_level > 0.3 else 'Low'})
- Diversity preserved: {diversity_score:.1%}
- Strategies used: {len(strategies_used)} complementary approaches

**Selected Solutions:**

"""

for i, output in enumerate(selected_outputs, 1):
    response += f"""
### Approach {i} (Population: {output['population']:.3f})
{output['content']}

**Quality Score:** {output['quality_score']:.3f} | **Novelty Score:** {output['novelty_score']:.3f}
"""

response += f"""
---
*This response used ecosystem intelligence to prevent mode collapse and maintain {diversity_score:.1%} solution diversity.*
"""

return response

⚙️ Advanced Configuration

Customizing LV Behavior

You can modify LV parameters for specific use cases:

# Custom entropy thresholds
lv_system.entropy_profile.low_threshold = 0.2   # More aggressive enhancement
lv_system.entropy_profile.high_threshold = 0.7  # Higher creativity threshold

# Custom weight schemes
custom_weights = {
    'quality': 0.8,
    'novelty': 0.15,
    'bias': 0.025,
    'cost': 0.025
}

# Custom LV dynamics parameters
lv_system.lv_ecosystem.max_iterations = 15      # More thorough convergence
lv_system.lv_ecosystem.damping_factor = 0.1     # More stable dynamics

Domain-Specific Optimization

# For technical domains
if domain == 'software_engineering':
    # Favor quality and cost over novelty
    weights = {'quality': 0.7, 'novelty': 0.1, 'bias': 0.1, 'cost': 0.1}

# For creative domains  
elif domain == 'creative_writing':
    # Favor novelty and diversity
    weights = {'quality': 0.3, 'novelty': 0.6, 'bias': 0.05, 'cost': 0.05}

# Apply custom weights
lv_system.apply_custom_weights(weights)

🔍 Debugging & Validation

Validate LV Installation

# Run comprehensive validation
validation_results = await lv_system.test_lv_framework("basic")

if validation_results['test_passed']:
    print("✅ LV Framework is working correctly")
else:
    print("❌ LV Framework issues detected")
    print(f"Error: {validation_results.get('error', 'Unknown')}")

Monitor LV Performance

# Get ecosystem health metrics
dashboard_data = await lv_system.create_lv_dashboard_data()

print(f"LV Ecosystem Health:")
print(f"  Total executions: {dashboard_data['total_lv_executions']}")
print(f"  Average diversity: {dashboard_data['performance_metrics']['diversity_preservation_score']:.3f}")
print(f"  Convergence rate: {dashboard_data['performance_metrics']['stability_rate']:.3f}")

Troubleshoot Common Issues

# Check mathematical stability
alpha_matrix = results.get('alpha_matrix')
growth_rates = results.get('growth_rates')

if alpha_matrix and growth_rates:
    validation = await lv_system.validate_lv_parameters(
        np.array(alpha_matrix), 
        np.array(growth_rates)
    )
    
    if not validation['matrix_stability']['stable']:
        print("⚠️ LV dynamics may be unstable")
        print(f"Recommendations: {validation['recommendations']}")

🎯 Integration with Neo4j Workflows

Query LV Configuration from Neo4j

// Get LV framework configuration
MATCH (config:LVConfiguration)
RETURN config.entropy_thresholds, config.weight_schemes, config.alpha_weights

Store LV Execution Results

// Record LV-enhanced workflow execution
CREATE (exec:WorkflowExecution {
    id: $execution_id,
    template_keyword: $template_keyword,
    entropy_level: $entropy,
    diversity_score: $diversity_score,
    strategies_used: $strategies_used,
    timestamp: datetime(),
    lv_enhanced: true
})

Query LV Performance History

// Analyze LV enhancement effectiveness
MATCH (exec:WorkflowExecution {lv_enhanced: true})
RETURN 
    avg(exec.diversity_score) as avg_diversity,
    avg(exec.entropy_level) as avg_entropy,
    count(exec) as total_executions,
    collect(DISTINCT exec.template_keyword) as enhanced_templates

🚨 Critical Dos and Don'ts

✅ DO:

  • Always check entropy first before deciding on LV enhancement
  • Explain LV enhancement to users when applied
  • Monitor diversity metrics to ensure ecosystem health
  • Use appropriate templates (KNOWLEDGE_EXTRACT_LV for extraction, etc.)
  • Validate mathematical stability for custom configurations
  • Log LV executions in Neo4j for learning and optimization

❌ DON'T:

  • Don't use LV for simple factual queries (waste of resources)
  • Don't ignore convergence warnings (may indicate instability)
  • Don't modify alpha matrices without mathematical validation
  • Don't skip entropy calculation (core to LV decision-making)
  • Don't assume LV is always better (context matters)
  • Don't forget to cite sources in LV-enhanced responses

🧪 Testing & Validation Commands

Quick LV Framework Test

# Validate installation
python3 validate_lv_framework.py

# Run demo
python3 demo_lv_framework.py

# Test specific functionality
python3 -c "
from mcp_neocoder.lv_integration import initialize_lv_enhancement
print('✅ LV Framework imports successfully')
"

Database Connection Test

# Test database connections
try:
    from neo4j import GraphDatabase
    from qdrant_client import QdrantClient
    
    # Test Neo4j
    driver = GraphDatabase.driver("bolt://localhost:7687", auth=("neo4j", "lv_password_2024"))
    with driver.session() as session:
        result = session.run("RETURN 1 AS test")
        assert result.single()["test"] == 1
    print("✅ Neo4j connection successful")
    
    # Test Qdrant
    client = QdrantClient("localhost", port=6333)
    collections = client.get_collections()
    print("✅ Qdrant connection successful")
    
except Exception as e:
    print(f"❌ Database connection failed: {e}")

📊 Performance Expectations

LV Framework Benchmarks

Metric Expected Range Excellent Concerning
Diversity Score 0.7 - 0.9 > 0.85 < 0.6
Quality Retention 0.9 - 1.0 > 0.95 < 0.85
Convergence Iterations 4 - 10 < 6 > 12
Processing Time 2 - 8 seconds < 3s > 10s

Optimization Tips

# For faster processing (trade some diversity for speed)
lv_system.lv_ecosystem.max_iterations = 5
lv_system.lv_ecosystem.damping_factor = 0.25

# For maximum diversity (slower but highest quality)
lv_system.lv_ecosystem.max_iterations = 15
lv_system.lv_ecosystem.damping_factor = 0.1

# For GPU acceleration (if available)
lv_system.enable_gpu_acceleration = True

🎓 Example Interaction Patterns

High-Entropy Creative Task

User: "Give me multiple creative approaches to solve climate change"

Your Response Process:
1. entropy = estimate_entropy(prompt) = 0.8 (HIGH)
2. use_lv = True (entropy > 0.4)
3. template = "KNOWLEDGE_QUERY_LV" 
4. Apply creativity mode (quality: 20%, novelty: 70%)
5. Generate 4-5 diverse approaches
6. Present with ecosystem explanation

Low-Entropy Factual Task

User: "What is the capital of France?"

Your Response Process:
1. entropy = estimate_entropy(prompt) = 0.1 (LOW)
2. use_lv = False (entropy < 0.4)
3. Standard factual response: "Paris"
4. No LV enhancement needed

Medium-Entropy Analysis Task

User: "Compare different database technologies for our project"

Your Response Process:
1. entropy = estimate_entropy(prompt) = 0.5 (MEDIUM)
2. use_lv = True (entropy > 0.4)
3. template = "KNOWLEDGE_QUERY_LV"
4. Apply balanced mode (quality: 60%, novelty: 30%)
5. Generate 3-4 comparison approaches
6. Present structured analysis with diversity metrics

🚀 Final Reminders

Core Principles

  1. Entropy drives everything - Always calculate first
  2. Diversity preserves intelligence - LV prevents mode collapse
  3. Context adapts behavior - Trust the mathematical foundations
  4. Quality AND diversity - Not either/or but both
  5. Ecosystem thinking - Sustainable intelligence over optimization

Success Indicators

  • High diversity scores (> 0.8) with maintained quality
  • Multiple complementary approaches in responses
  • Context-appropriate enhancement decisions
  • Stable convergence in reasonable iterations
  • User satisfaction with rich, diverse solutions

Emergency Troubleshooting

If LV framework fails:

  1. Check database connections
  2. Validate entropy calculation
  3. Fall back to standard templates
  4. Log error for later analysis
  5. Inform user of temporary standard mode

🧬 You now have complete knowledge of the world's first AI ecosystem intelligence framework. Use it to preserve diversity and prevent mode collapse in AI responses! 🌟

Remember: You're not just optimizing - you're cultivating an intelligent ecosystem! 🚀