File size: 5,415 Bytes
101858b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
"""

Example usage of ConsciousnessSystem.



Demonstrates consciousness processing using Qwen.

"""

import torch
import logging
from ConsciousnessSystem import ConsciousnessSystem, ConsciousnessSystemConfig

logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')
logger = logging.getLogger(__name__)


def example_basic_consciousness():
    """Basic consciousness processing example."""
    print("=" * 80)
    print("ConsciousnessSystem - Basic Consciousness Processing")
    print("=" * 80)
    
    # Create configuration
    config = ConsciousnessSystemConfig(
        use_fp16=True,
        device='cuda' if torch.cuda.is_available() else 'cpu',
        enable_memory_update=True,
        enable_thought_analysis=True,
    )
    
    # Initialize system
    print("\nInitializing ConsciousnessSystem...")
    consciousness_system = ConsciousnessSystem(config)
    print("ConsciousnessSystem initialized!")
    print(f"   Using Qwen model: {config.model_name}")
    print(f"   All consciousness processing uses Qwen!")
    print(f"   Zero extra parameters - all from Qwen!")
    
    # Create sample hidden states
    batch_size = 2
    seq_len = 20
    hidden_size = consciousness_system.qwen_processor.hidden_size
    
    hidden_states = torch.randn(batch_size, seq_len, hidden_size).to(consciousness_system.device)
    if config.use_fp16:
        hidden_states = hidden_states.half()
    
    # Process through consciousness system
    print("\nProcessing consciousness...")
    metrics = consciousness_system.forward(hidden_states)
    
    print(f"\nConsciousness Metrics:")
    print(f"   Creativity: {metrics['creativity']:.4f}")
    print(f"   Coherence: {metrics['coherence']:.4f}")
    if metrics['delta_creativity'] is not None:
        print(f"   Delta Creativity: {metrics['delta_creativity']:+.4f}")
    if metrics['delta_coherence'] is not None:
        print(f"   Delta Coherence: {metrics['delta_coherence']:+.4f}")
    print(f"   Memory Updated: {metrics['memory_updated']}")
    if 'memory_state' in metrics:
        print(f"   Memory Nodes: {metrics['memory_state']['total_nodes']}")


def example_thought_analysis():
    """Thought analysis example."""
    print("\n" + "=" * 80)
    print("ConsciousnessSystem - Thought Analysis")
    print("=" * 80)
    
    config = ConsciousnessSystemConfig(use_fp16=True, enable_thought_analysis=True)
    consciousness_system = ConsciousnessSystem(config)
    
    # Add thoughts
    thoughts = [
        "I need to solve this problem step by step",
        "First, I should understand the requirements",
        "Then, I can design a solution",
    ]
    
    print("\nAdding thoughts...")
    for thought in thoughts:
        consciousness_system.add_thought(thought)
        print(f"   Added: {thought}")
    
    # Process with thought history
    hidden_states = torch.randn(1, 10, consciousness_system.qwen_processor.hidden_size).to(consciousness_system.device)
    if config.use_fp16:
        hidden_states = hidden_states.half()
    
    print("\nProcessing with thought analysis...")
    metrics = consciousness_system.forward(hidden_states, thought_history=thoughts)
    
    print(f"   Creativity: {metrics['creativity']:.4f}")
    print(f"   Coherence: {metrics['coherence']:.4f}")
    if metrics['plan_embedding'] is not None:
        print(f"   Plan Embedding Shape: {metrics['plan_embedding'].shape}")


def example_memory_management():
    """Memory management example."""
    print("\n" + "=" * 80)
    print("ConsciousnessSystem - Memory Management")
    print("=" * 80)
    
    config = ConsciousnessSystemConfig(use_fp16=True, enable_memory_update=True)
    consciousness_system = ConsciousnessSystem(config)
    
    # Add memories
    print("\nAdding memories...")
    for i in range(5):
        embedding = torch.randn(consciousness_system.qwen_processor.hidden_size).to(consciousness_system.device)
        if config.use_fp16:
            embedding = embedding.half()
        
        node_id = consciousness_system.memory.add_memory(
            embedding,
            metadata={'index': i, 'content': f'Memory {i}'},
            score=0.5 + i * 0.1,
        )
        print(f"   Added memory node {node_id}")
    
    # Analyze memory state
    memory_state = consciousness_system.memory.analyze_memory_state()
    print(f"\nMemory State:")
    print(f"   Total Nodes: {memory_state['total_nodes']}")
    print(f"   Average Score: {memory_state['avg_score']:.4f}")
    print(f"   Utilization: {memory_state['utilization']:.2%}")
    
    # Prune memory
    print("\nPruning memory...")
    prune_result = consciousness_system.memory.prune_memory()
    print(f"   Pruned: {prune_result['pruned_count']} nodes")
    print(f"   Retained: {prune_result['retained_count']} nodes")
    
    # Get consciousness state
    state = consciousness_system.get_consciousness_state()
    print(f"\nConsciousness State:")
    print(f"   Creativity: {state['creativity']}")
    print(f"   Coherence: {state['coherence']}")
    print(f"   Memory Nodes: {state['memory_state']['total_nodes']}")


if __name__ == "__main__":
    example_basic_consciousness()
    example_thought_analysis()
    example_memory_management()
    print("\nAll examples completed successfully!")