Phillnet-2 / ConsciousnessSystem /example_usage.py
ayjays132's picture
Upload 478 files
101858b verified
"""
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!")