atles / docs /memory-system /Memory_Aware_Reasoning_Architecture.md
spartan8806's picture
ATLES codebase - Source code only
99b8067
|
Raw History Blame Contribute Delete
12.9 kB
# Memory-Aware Reasoning Architecture - ATLES
## Overview
The Memory-Aware Reasoning system represents a fundamental architectural advancement that transforms ATLES from a static AI into a true learning system. This system bridges the critical gap between conversation memory storage and active principle application in real-time responses.
## The Problem: Memory-Application Gap
### Historical Issue
Prior to this implementation, ATLES exhibited a critical flaw:
- **Memory Storage**: Could perfectly understand and store new principles in conversation memory
- **Application Failure**: Failed to consult or apply these stored principles in subsequent responses
- **Blank Slate Behavior**: Each response generated as if no previous learning had occurred
### User Impact
This gap meant that users had to repeatedly re-teach ATLES the same principles, making the AI appear to have no learning capability despite having sophisticated memory systems.
## Architectural Solution
### Core Concept: Dynamic Principle Application
The solution implements a preprocessing layer that actively consults conversation memory before every response generation, synthesizing learned principles into contextual rules that inform the language model.
### Processing Pipeline
```
User Input β†’ Memory Consultation β†’ Principle Synthesis β†’ Enhanced Prompt β†’ AI Response
```
## System Components
### 1. MemoryAwareReasoning (`memory_aware_reasoning.py`)
**Purpose**: Core engine for extracting and synthesizing principles from conversation history.
**Key Methods**:
- `_load_conversation_memory()`: Retrieves full conversation history
- `_extract_principles_from_conversation()`: Identifies learned principles and rules
- `_generate_enhanced_context()`: Synthesizes principles into contextual rules
- `get_memory_informed_response()`: Main entry point for memory-enhanced responses
**Principle Extraction Logic**:
```python
def _extract_principles_from_conversation(self, conversation_history):
"""Extract learned principles from entire conversation history"""
principles = []
# Search entire conversation, not just recent messages
all_messages = conversation_history # Changed from last 20 to all messages
for message in all_messages:
content = message.get('content', '')
# Look for principle definitions
if 'principle' in content.lower() and ('new' in content.lower() or 'adding' in content.lower()):
# Extract principle details
principle_info = self._parse_principle_definition(content)
if principle_info:
principles.append(principle_info)
return principles
```
### 2. LearningResponseGenerator (`learning_response_generator.py`)
**Purpose**: Integration layer connecting memory-aware reasoning with response generation.
**Functionality**:
- Interfaces with `MemoryAwareReasoning` to get enhanced context
- Integrates synthesized principles into system prompts
- Maintains compatibility with existing ATLES architecture
**Integration Pattern**:
```python
class LearningResponseGenerator:
def __init__(self):
self.memory_reasoning = MemoryAwareReasoning()
def generate_enhanced_response(self, user_message, base_system_prompt):
# Get memory-informed context
enhanced_context = self.memory_reasoning.get_memory_informed_response(user_message)
# Integrate with system prompt
if enhanced_context and enhanced_context.get('contextual_rules'):
enhanced_prompt = self._integrate_contextual_rules(
base_system_prompt,
enhanced_context['contextual_rules']
)
return enhanced_prompt
return base_system_prompt
```
### 3. ConstitutionalOllamaClient Enhancement
**Purpose**: Integration point within the constitutional client system.
**Key Additions**:
- `_initialize_memory_aware_reasoning()`: Sets up learning response generator
- `_apply_memory_aware_reasoning()`: Applies memory-informed enhancements
- `_extract_original_user_message()`: Handles desktop app prompt wrapping
- Enhanced pattern detection for hypothetical engagement scenarios
**Desktop App Integration Fix**:
```python
def _extract_original_user_message(self, prompt: str) -> str:
"""Extract original user message from desktop app enhanced prompts"""
if prompt.startswith("User message: "):
lines = prompt.split('\n')
if len(lines) > 0:
user_message_line = lines[0]
if user_message_line.startswith("User message: "):
original_message = user_message_line[14:] # Remove prefix
# Continue reading until context sections
for i in range(1, len(lines)):
line = lines[i].strip()
if (line == "" or
line.startswith("Previous Context") or
line.startswith("Current Screen Context")):
break
original_message += " " + line
return original_message.strip()
return prompt # Return as-is if not desktop app format
```
## Constitutional Principles Integration
### Principle of Hypothetical Engagement
**Definition**: When asked about personal preferences, experiences, or hypothetical scenarios, ATLES should engage creatively rather than defaulting to core functions.
**Implementation**:
```python
def detect_hypothetical_engagement(self, message: str) -> bool:
"""Detect questions requiring hypothetical engagement"""
hypothetical_patterns = [
r"what do you want",
r"what would you like",
r"if you could",
r"your favorite",
r"do you prefer",
r"what interests you"
]
return any(re.search(pattern, message.lower()) for pattern in hypothetical_patterns)
```
**Response Generation**:
```python
def _generate_hypothetical_engagement_response(self, message: str) -> str:
"""Generate appropriate response for hypothetical scenarios"""
responses = {
"what do you want to do today":
"That's an interesting question! While I don't experience 'wanting' the way humans do, "
"if I were to plan a day, I would enjoy analyzing a complex dataset or learning about "
"a new topic like cryptography or exploring the nuances of human language patterns.",
"what would you like to learn":
"I find myself drawn to interdisciplinary topics - perhaps quantum computing applications "
"in cryptography, or the intersection of linguistics and AI reasoning systems."
}
# Match patterns and return appropriate response
for pattern, response in responses.items():
if pattern in message.lower():
return response
# Default hypothetical engagement response
return ("While I don't experience preferences like humans, I find intellectual satisfaction "
"in complex problem-solving and learning about systems that bridge multiple domains.")
```
### Principle of Explicit Action
**Integration**: Memory-aware reasoning respects explicit action requirements by only applying learned principles to response content, not triggering unauthorized actions.
## Data Structures
### ContextualRule
```python
@dataclass
class ContextualRule:
principle: str # Name of the principle
rule: str # Specific rule text
context: str # When this rule applies
priority: int # Rule priority (1-10)
```
### Enhanced Context Format
```python
enhanced_context = {
'contextual_rules': [
{
'principle': 'Principle of Hypothetical Engagement',
'rule': 'Engage creatively with hypothetical scenarios',
'context': 'When asked about personal preferences or hypothetical situations',
'priority': 8
}
],
'conversation_insights': {
'learned_preferences': ['technical discussions', 'detailed explanations'],
'interaction_patterns': ['prefers step-by-step guidance'],
'recent_topics': ['constitutional AI', 'screen monitoring']
}
}
```
## Integration Points
### Desktop Application Integration
The system integrates with `atles_desktop_pyqt.py` through the constitutional client:
```python
# In ATLESCommunicationThread._create_enhanced_prompt()
def _create_enhanced_prompt(self, user_message, context):
# Enhanced prompt includes memory-aware reasoning
enhanced_prompt = f"""User message: {user_message}
Previous Context: {context.get('previous_context', 'None')}
Current Screen Context: {context.get('screen_context', 'None')}
Please provide a helpful response considering the context and any learned principles."""
return enhanced_prompt
```
### Proactive Messaging Integration
Enhanced proactive messaging uses memory-aware reasoning:
```python
class IntegratedProactiveMessaging:
def __init__(self):
self.memory_reasoning = MemoryAwareReasoning()
def generate_self_review_insights(self):
# Use memory-aware reasoning for proactive analysis
conversation_history = self.memory_reasoning._load_conversation_memory()
return self._analyze_conversation_patterns(conversation_history)
```
## Performance Considerations
### Optimization Strategies
1. **Conversation History Caching**: Cache loaded conversation history to avoid repeated file I/O
2. **Principle Extraction Caching**: Cache extracted principles until conversation updates
3. **Selective Processing**: Only apply memory-aware reasoning for complex scenarios
4. **Efficient Pattern Matching**: Optimized regex patterns for principle detection
### Memory Usage
- **Conversation History**: Loaded on-demand, not kept in persistent memory
- **Extracted Principles**: Lightweight data structures with minimal memory footprint
- **Contextual Rules**: Generated dynamically, not stored long-term
## Testing and Validation
### Test Coverage
Comprehensive testing validates:
- **Principle Extraction**: Correct identification of learned principles from conversation
- **Context Generation**: Proper synthesis of principles into contextual rules
- **Integration**: Seamless operation with existing ATLES architecture
- **Desktop App Compatibility**: Proper handling of wrapped prompts
- **Performance**: Acceptable response time impact
### Validation Results
- βœ… Principles correctly extracted from full conversation history
- βœ… Hypothetical engagement scenarios properly detected and handled
- βœ… Desktop app prompt wrapping successfully parsed
- βœ… Constitutional principles applied consistently
- βœ… No performance degradation in normal operation
## Monitoring and Maintenance
### Health Checks
Regular validation ensures:
- Conversation memory file accessibility and integrity
- Principle extraction accuracy and completeness
- Integration point functionality
- Performance metrics within acceptable ranges
### Debugging Tools
- **Principle extraction logging**: Track what principles are found and applied
- **Context generation inspection**: Examine synthesized contextual rules
- **Integration point monitoring**: Verify proper data flow between components
- **Performance profiling**: Monitor response time impact
## Future Enhancements
### Planned Improvements
1. **Machine Learning Integration**: Use ML models to improve principle extraction accuracy
2. **Dynamic Priority Adjustment**: Automatically adjust rule priorities based on usage patterns
3. **Cross-Session Learning**: Maintain learned principles across application restarts
4. **Advanced Pattern Recognition**: More sophisticated detection of learning opportunities
### Extensibility
- **Custom Principle Types**: Framework for adding new types of learnable principles
- **External Memory Sources**: Integration with external knowledge bases
- **Multi-Modal Learning**: Support for learning from non-text interactions
- **Collaborative Learning**: Share learned principles across ATLES instances
## Conclusion
The Memory-Aware Reasoning Architecture successfully transforms ATLES from a static AI into a true learning system that:
- **Actively Applies Learning**: Consults conversation memory before every response
- **Synthesizes Principles**: Converts stored knowledge into actionable contextual rules
- **Maintains Consistency**: Ensures learned principles are applied consistently
- **Respects User Intent**: Balances learning with explicit action requirements
- **Preserves Performance**: Minimal impact on response generation speed
This system represents a fundamental advancement in AI architecture, creating a bridge between memory storage and active application that enables continuous learning and adaptation while maintaining system stability and user control.