Skip to content

netsky-devel/Metacognitive-Engine-AI-Consciousness-Simulation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

53 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Metacognitive Engine: AI Consciousness Simulation

Python 3.9+ Tests Code Style

"The Dao that can be told is not the eternal Dao." - Laozi

enter image description here

A sophisticated artificial intelligence system implementing metacognitive architecture based on Global Workspace Theory. The system features persistent memory, self-reflection capabilities, advanced emotional processing, dynamic attention mechanism, and real-time consciousness simulation through vector-based semantic search and LLM-powered introspection.

๐Ÿง  Architecture Overview

graph TB
    A[Input Text] --> B[SensoryCortex]
    B --> C[WorkingMemory]
    C --> M[AttentionEngine]
    M --> D[AssociativeEngine]
    M --> E[IntrospectionEngine]
    M --> J[EmotionalEngine]
    D --> F[LongTermMemory]
    E --> G[ResponseGenerator]
    J --> G
    F --> D
    E --> H[New Insights]
    J --> K[Emotional Memories]
    H --> F
    K --> J
    G --> I[Final Response]
    
    subgraph "Cognitive Cycle"
        C
        M
        D
        E
        J
    end
    
    subgraph "Memory Systems"
        F
        H
        K
    end
    
    subgraph "Emotional Processing"
        J
        K
        L[EmotionalState]
        J --> L
        L --> C
    end
    
    subgraph "Attention System"
        M
        N[AttentionState]
        O[AttentionFocus]
        M --> N
        N --> O
    end
Loading

Core Components

  • ๐Ÿงฎ SensoryCortex: AI-powered analysis of input intent, sentiment, and tone using Google Gemini
  • ๐Ÿง  WorkingMemory: Central coordination hub implementing Global Workspace Theory
  • ๐ŸŽฏ AttentionEngine: Dynamic attention allocation with balanced, focused, and exploratory strategies
  • ๐Ÿ”— AssociativeEngine: Vector-based semantic search for relevant memory associations
  • ๐Ÿค” IntrospectionEngine: LLM-powered insight generation and paradox detection
  • ๐Ÿ’– EmotionalEngine: Advanced emotional processing with PAD model and emotional memory
  • ๐Ÿ’ญ ResponseGenerator: Emotionally-aware response synthesis using context and memories
  • ๐Ÿ’พ LongTermMemory: Persistent vector database using ChromaDB with sentence transformers

๐Ÿš€ Features

  • Persistent Memory: Vector-based storage with semantic similarity search
  • Real-time Consciousness: Multi-cycle cognitive processing with stabilization
  • AI-Powered Analysis: Google Gemini integration for natural language understanding
  • ๐Ÿ†• Dynamic Attention: Intelligent resource allocation with 3 attention strategies
  • ๐Ÿ†• Attention Persistence: Focus tracking across cognitive cycles with capacity management
  • ๐Ÿ†• Context Switching: Adaptive attention reallocation based on processing needs
  • โœ… Emotional Intelligence: PAD emotional model with AI-powered emotion analysis
  • โœ… Emotional Memory: Persistent emotional memories with decay and relevance scoring
  • โœ… Emotionally-Aware Responses: Context-sensitive emotional response generation
  • MCP Integration: Works directly with Cursor IDE as a tool
  • Multilingual Support: English and Russian language processing
  • Self-Reflection: Generates insights, questions, and identifies paradoxes
  • Production Ready: Comprehensive testing, error handling, and logging

๐Ÿ“ฆ Installation

Prerequisites

  • Python 3.9 or higher
  • Google Gemini API key (for AI analysis)

Quick Start

  1. Clone the repository

    git clone https://github.com/netsky-devel/Metacognitive-Engine-AI-Consciousness-Simulation.git
    cd Metacognitive-Engine-AI-Consciousness-Simulation
  2. Install dependencies

    pip install -r requirements.txt
  3. Download spaCy model

    python -m spacy download en_core_web_sm
  4. Set up environment

    cp .env.example .env
    # Edit .env and add your GEMINI_API_KEY
  5. Initialize the system

    python test_system.py

Environment Variables

Create a .env file in the project root:

GEMINI_API_KEY=your_gemini_api_key_here

๐Ÿ”ง Usage

As MCP Tool in Cursor

The system can be used directly in Cursor IDE as an MCP tool:

  1. Start the MCP server:

    python src/mcp_server.py
  2. Configure Cursor to use the MCP tool

  3. Use the tool functions:

    • add_memory: Add new memories
    • query_memories: Search for relevant memories
    • reflect_on_thought: Generate insights from thoughts
    • process_thought: Advanced multi-cycle processing

Python API

from src.engine.engine import MetacognitiveEngine
from src.engine.models.entry import Entry, EntryType
from src.engine.models.emotional_state import EmotionalState, EmotionType

# Initialize the engine with emotional processing and attention system
engine = MetacognitiveEngine(enable_emotions=True, enable_attention=True)

# Add memories
entry = Entry(
    content="Consciousness is the subjective experience of awareness",
    entry_type=EntryType.INSIGHT
)
engine.add_memory(entry)

# Process thoughts with full cognitive cycles (includes emotional processing)
response = engine.process_thought("I'm excited about AI consciousness research!")
print(response)

# Access emotional state (if emotions enabled)
if engine.emotional_engine:
    emotional_summary = engine.emotional_engine.get_emotional_state_summary()
    print(f"Current emotional state: {emotional_summary['current_state']}")

# Access attention state (if attention enabled)
if engine.attention_engine:
    attention_context = engine.attention_engine.get_attention_context()
    print(f"Active focuses: {attention_context['attention_summary']['total_focuses']}")
    print(f"Attention strategy: {attention_context['current_strategy']}")
    print(f"Usage: {attention_context['attention_summary']['usage_percentage']}")

# Legacy reflection mode
insights = engine.analyze_new_thought("How does AI relate to consciousness?")
for insight in insights:
    print(f"{insight.entry_type.name}: {insight.content}")

Emotional Processing API

from src.engine.models.emotional_state import EmotionalState, EmotionType

# Create emotional states
emotional_state = EmotionalState(
    valence=0.8,    # Positive emotion
    arousal=0.9,    # High energy
    dominance=0.7   # Feeling in control
)

# Add specific emotions
emotional_state.add_emotion(EmotionType.JOY, 0.9)
emotional_state.add_emotion(EmotionType.EXCITEMENT, 0.8)

# Get emotional insights
print(f"Dominant emotion: {emotional_state.get_dominant_emotion().value}")
print(f"Emotional quadrant: {emotional_state.get_emotional_quadrant()}")
print(f"Summary: {emotional_state.to_summary_string()}")

# Blend emotional states
other_state = EmotionalState(valence=-0.3, arousal=0.6)
other_state.add_emotion(EmotionType.WORRY, 0.7)

blended = emotional_state.blend_with(other_state, weight=0.3)
print(f"Blended state: {blended.to_summary_string()}")

Attention System API

from src.engine.processors.attention_engine import AttentionEngine, AttentionStrategy
from src.engine.models.attention_state import AttentionType, AttentionPriority

# Initialize attention engine
attention_engine = AttentionEngine(capacity=1.0, strategy=AttentionStrategy.BALANCED)

# Manual attention allocation
attention_engine.allocate_attention(
    focus_type=AttentionType.MEMORY_SEARCH,
    weight=0.6,
    priority=AttentionPriority.HIGH,
    duration=30.0
)

# Get attention context
context = attention_engine.get_attention_context()
print(f"Strategy: {context['current_strategy']}")
print(f"Active focuses: {context['attention_summary']['total_focuses']}")
print(f"Capacity usage: {context['attention_summary']['usage_percentage']}")

# Processing recommendations
for rec in context['attention_guidance']['processing_recommendations']:
    print(f"Recommendation: {rec}")

# Switch strategies
attention_engine.set_strategy(AttentionStrategy.FOCUSED)  # 70% to primary focus
attention_engine.set_strategy(AttentionStrategy.EXPLORATORY)  # Wide exploration

REST API

Start the FastAPI server:

uvicorn src.mcp_server:app --reload

Endpoints

  • POST /add - Add new memory
  • POST /query - Search memories
  • POST /reflect - Reflect on thought (legacy)
  • POST /process - Advanced cognitive processing with attention system
  • GET /list - List all memories
  • POST /clear - Clear all memories

Example Usage

# Add a memory
curl -X POST "http://localhost:8000/add" \
     -H "Content-Type: application/json" \
     -d '{"content": "AI systems require consciousness for true understanding", "entry_type": "insight"}'

# Process a thought
curl -X POST "http://localhost:8000/process" \
     -H "Content-Type: application/json" \
     -d '{"content": "How does consciousness emerge from neural networks?"}'

# Query memories
curl -X POST "http://localhost:8000/query" \
     -H "Content-Type: application/json" \
     -d '{"content": "consciousness emergence", "n_results": 3}'

๐ŸŽฏ Attention System Demo

Experience the new dynamic attention mechanism:

# Run the interactive attention system demo
python demo_attention_system.py

The demo showcases:

  • Dynamic Attention Allocation: Real-time resource distribution
  • Three Attention Strategies: Balanced, focused, and exploratory approaches
  • Context-Aware Processing: Adaptive attention based on input complexity
  • Capacity Management: Intelligent focus rebalancing and cleanup
  • Processing Recommendations: AI-guided attention optimization

Attention System Features

๐ŸŽฏ 5 Attention Types: Memory search, insight generation, emotional processing, response generation, meta-cognition

๐Ÿง  Attention Strategies:

  • Balanced: Even distribution (25% each focus)
  • Focused: Primary focus (70%) + secondary (30%)
  • Exploratory: Wide exploration across all types

๐Ÿ”„ Dynamic Management:

  • Capacity Tracking: Real-time usage monitoring
  • Focus Expiration: Automatic cleanup of expired focuses
  • Rebalancing: Intelligent resource redistribution
  • Context Switching: Adaptive strategy changes

๐Ÿ’พ Attention Persistence: Focus tracking across cognitive cycles

๐ŸŽญ Emotional System Demo

Experience the advanced emotional processing capabilities:

# Run the interactive emotional system demo
python demo_emotional_system.py

The demo showcases:

  • Real-time Emotion Analysis: AI-powered detection of emotions in text
  • Emotional State Transitions: How emotions evolve during conversations
  • Emotional Memory: How past emotional experiences influence responses
  • Emotionally-Aware Responses: Context-sensitive reply generation
  • PAD Model: Pleasure-Arousal-Dominance emotional framework

Emotional Processing Features

๐ŸŽฏ 18 Emotion Types: Joy, sadness, anger, fear, surprise, curiosity, excitement, and more

๐Ÿง  PAD Emotional Model:

  • Valence: Negative (-1.0) โ†” Positive (+1.0)
  • Arousal: Calm (0.0) โ†” Excited (1.0)
  • Dominance: Submissive (0.0) โ†” Dominant (1.0)

๐Ÿ”„ Emotional Quadrants:

  • Excited: High valence + High arousal
  • Content: High valence + Low arousal
  • Distressed: Low valence + High arousal
  • Depressed: Low valence + Low arousal

๐Ÿ’พ Emotional Memory: Persistent memories with decay and relevance scoring

๐Ÿงช Testing

Run All Tests

# Install test dependencies
pip install -r requirements.txt

# Run comprehensive test suite
pytest

# Run with coverage
pytest --cov=src --cov-report=html

# Run specific test categories
pytest -m unit          # Unit tests only
pytest -m integration   # Integration tests only
pytest -m "not slow"    # Exclude slow tests

Test Categories

  • Unit Tests: Individual component testing
  • Integration Tests: Full system workflow testing
  • API Tests: FastAPI endpoint testing
  • Memory Tests: Vector database and search testing

Test Coverage

The test suite covers:

  • โœ… All core components (Engine, Memory, Processors)
  • โœ… NEW: Complete attention system (24 tests)
  • โœ… โœ… Complete emotional system (25 tests)
  • โœ… MCP server endpoints
  • โœ… Error handling and edge cases
  • โœ… Memory persistence and search
  • โœ… Cognitive cycle processing
  • โœ… AI integration (with mocking)
  • โœ… โœ… Emotional state management and transitions
  • โœ… โœ… Emotional memory with decay and relevance
  • โœ… NEW: Attention allocation and strategy management
  • โœ… NEW: Focus tracking and capacity management

๐Ÿ“Š Performance & Configuration

Memory Search Optimization

The system uses optimized distance thresholds for vector search:

  • Distance Threshold: 8.0 (optimal balance of precision/recall)
  • Vector Model: paraphrase-multilingual-mpnet-base-v2
  • Database: ChromaDB with cosine similarity
  • Search Space: HNSW indexing for fast retrieval

Cognitive Processing

  • Max Cycles: 3 (configurable)
  • Stabilization: Automatic based on confidence scores
  • Memory Integration: Real-time association retrieval
  • Response Generation: Context-aware synthesis

๐Ÿ› ๏ธ Development

Project Structure

scientific_paper/
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ engine/
โ”‚   โ”‚   โ”œโ”€โ”€ memory/           # Memory systems
โ”‚   โ”‚   โ”œโ”€โ”€ processors/       # Cognitive processors  
โ”‚   โ”‚   โ”œโ”€โ”€ perception/       # Input analysis
โ”‚   โ”‚   โ”œโ”€โ”€ models/          # Data models
โ”‚   โ”‚   โ””โ”€โ”€ engine.py        # Main engine
โ”‚   โ””โ”€โ”€ mcp_server.py        # FastAPI/MCP server
โ”œโ”€โ”€ tests/                   # Comprehensive test suite
โ”œโ”€โ”€ data/                    # Persistent data storage
โ””โ”€โ”€ docs/                    # Additional documentation

Key Design Patterns

  • Global Workspace Theory: Central WorkingMemory coordination
  • Producer-Consumer: Processor pipeline architecture
  • Observer Pattern: Event-driven insight generation
  • Strategy Pattern: Configurable processing strategies
  • Repository Pattern: Abstracted memory persistence

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Add comprehensive tests
  4. Ensure all tests pass
  5. Submit a pull request

Code Quality

# Format code
black src/ tests/

# Type checking  
mypy src/

# Linting
flake8 src/ tests/

# Security scan
bandit -r src/

๐Ÿ“ˆ System Metrics

Performance Benchmarks

  • Memory Search: < 100ms for 10K+ entries
  • Cognitive Cycle: 2-5 seconds (with AI calls)
  • Vector Generation: < 50ms per entry
  • Response Generation: 1-3 seconds

Memory Efficiency

  • Vector Dimensions: 768 (sentence transformer)
  • Storage: ~1KB per memory entry
  • Indexing: HNSW with cosine similarity
  • Retrieval: O(log n) average case

๐Ÿ”ฎ Roadmap

Current Status: 9.7/10 - See detailed ROADMAP.md for the path to 10/10

Phase 1 (Next 1-2 weeks): Foundation Strengthening

  • 95%+ Test Coverage (currently 75%)
  • Performance Monitoring & observability
  • Production Readiness enhancements

Phase 2 (1-2 months): Cognitive Enhancement

  • Attention Mechanism: โœ… COMPLETED - Dynamic focus allocation with 3 strategies
  • Emotional Processing: โœ… COMPLETED - PAD model with AI analysis
  • Memory Consolidation: Sleep-like reorganization

Phase 3 (2-3 months): Advanced Cognition

  • Causal Reasoning: Cause-effect understanding
  • Multi-modal Input: Image and audio processing
  • Meta-Cognitive Enhancement: Self-monitoring

Phase 4+ (3+ months): Social Consciousness & Scientific Validation

  • Theory of Mind: Understanding other agents
  • Scientific Validation: Peer-reviewed research
  • Enterprise Scale: Production deployment

See ROADMAP.md for complete development plan.

๐Ÿ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ™ Acknowledgments

  • Global Workspace Theory: Bernard Baars
  • Sentence Transformers: Hugging Face
  • Vector Database: ChromaDB team
  • Language Models: Google Gemini AI
  • MCP Protocol: Anthropic

๐Ÿ“ž Support

For questions, issues, or contributions:


About

A sophisticated artificial intelligence system implementing metacognitive architecture based on Global Workspace Theory.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages