File size: 14,454 Bytes
23804b3 |
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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 |
"""
Online Learning System for Cyber-LLM
Enables real-time model updates from operational feedback and new threat intelligence.
Author: Muzan Sano <[email protected]>
"""
import asyncio
import json
import logging
from datetime import datetime, timedelta
from typing import Dict, List, Optional, Tuple, Any
from dataclasses import dataclass, asdict
from enum import Enum
import numpy as np
import torch
from torch.utils.data import DataLoader, Dataset
from transformers import AutoTokenizer, AutoModelForCausalLM
import redis
from pydantic import BaseModel, Field
from ..utils.logging_system import CyberLLMLogger
from ..utils.secrets_manager import SecretsManager
# Configure logging
logger = CyberLLMLogger(__name__).get_logger()
class LearningEventType(Enum):
"""Types of learning events that can trigger model updates"""
FEEDBACK_POSITIVE = "feedback_positive"
FEEDBACK_NEGATIVE = "feedback_negative"
NEW_THREAT_INTELLIGENCE = "new_threat_intel"
SECURITY_INCIDENT = "security_incident"
AGENT_SUCCESS = "agent_success"
AGENT_FAILURE = "agent_failure"
OPSEC_VIOLATION = "opsec_violation"
FALSE_POSITIVE = "false_positive"
@dataclass
class LearningEvent:
"""Structure for learning events"""
event_id: str
event_type: LearningEventType
timestamp: datetime
source: str # Which agent or system generated this event
context: Dict[str, Any] # Relevant context for learning
feedback_score: Optional[float] = None # Human feedback score (0-1)
confidence: float = 1.0 # Confidence in this event
priority: int = 1 # Priority level (1=low, 5=critical)
def to_dict(self) -> Dict[str, Any]:
data = asdict(self)
data['timestamp'] = self.timestamp.isoformat()
data['event_type'] = self.event_type.value
return data
class OnlineDataset(Dataset):
"""Dataset for online learning from streaming events"""
def __init__(self, events: List[LearningEvent], tokenizer, max_length: int = 512):
self.events = events
self.tokenizer = tokenizer
self.max_length = max_length
def __len__(self) -> int:
return len(self.events)
def __getitem__(self, idx: int) -> Dict[str, torch.Tensor]:
event = self.events[idx]
# Convert learning event to training sample
context_text = self._event_to_text(event)
# Tokenize
encoding = self.tokenizer(
context_text,
truncation=True,
padding='max_length',
max_length=self.max_length,
return_tensors='pt'
)
return {
'input_ids': encoding['input_ids'].squeeze(),
'attention_mask': encoding['attention_mask'].squeeze(),
'labels': encoding['input_ids'].squeeze(),
'event_weight': torch.tensor(event.confidence * event.priority, dtype=torch.float32)
}
def _event_to_text(self, event: LearningEvent) -> str:
"""Convert learning event to training text"""
if event.event_type == LearningEventType.FEEDBACK_POSITIVE:
return f"[POSITIVE_FEEDBACK] Context: {event.context.get('query', '')} Response: {event.context.get('response', '')} Score: {event.feedback_score}"
elif event.event_type == LearningEventType.FEEDBACK_NEGATIVE:
return f"[NEGATIVE_FEEDBACK] Context: {event.context.get('query', '')} Response: {event.context.get('response', '')} Score: {event.feedback_score}"
elif event.event_type == LearningEventType.NEW_THREAT_INTELLIGENCE:
return f"[THREAT_INTEL] {event.context.get('threat_description', '')} TTPs: {event.context.get('ttps', [])}"
elif event.event_type == LearningEventType.SECURITY_INCIDENT:
return f"[INCIDENT] {event.context.get('incident_description', '')} Response: {event.context.get('response_actions', [])}"
else:
return f"[{event.event_type.value.upper()}] {json.dumps(event.context)}"
class OnlineLearningManager:
"""Manages online learning process for Cyber-LLM"""
def __init__(self,
model_name: str = "microsoft/DialoGPT-medium",
redis_host: str = "localhost",
redis_port: int = 6379,
learning_rate: float = 1e-5,
batch_size: int = 4,
update_frequency: int = 100, # Update after N events
max_events_memory: int = 10000):
self.model_name = model_name
self.learning_rate = learning_rate
self.batch_size = batch_size
self.update_frequency = update_frequency
self.max_events_memory = max_events_memory
# Initialize components
self.secrets_manager = SecretsManager()
self.redis_client = redis.Redis(host=redis_host, port=redis_port, decode_responses=True)
# Load model and tokenizer
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
self.model = AutoModelForCausalLM.from_pretrained(model_name)
if self.tokenizer.pad_token is None:
self.tokenizer.pad_token = self.tokenizer.eos_token
# Learning state
self.learning_events: List[LearningEvent] = []
self.total_events_processed = 0
self.last_update_time = datetime.now()
# Performance tracking
self.learning_metrics = {
'total_updates': 0,
'successful_updates': 0,
'failed_updates': 0,
'average_loss': 0.0,
'learning_rate_history': []
}
logger.info(f"OnlineLearningManager initialized with model: {model_name}")
async def add_learning_event(self, event: LearningEvent) -> None:
"""Add a new learning event to the queue"""
try:
# Store event in memory
self.learning_events.append(event)
# Store event in Redis for persistence
event_key = f"learning_event:{event.event_id}"
await self._store_event_redis(event_key, event)
# Maintain memory limit
if len(self.learning_events) > self.max_events_memory:
self.learning_events.pop(0)
self.total_events_processed += 1
logger.info(f"Added learning event: {event.event_type.value} from {event.source}")
# Trigger update if threshold reached
if len(self.learning_events) >= self.update_frequency:
await self.trigger_model_update()
except Exception as e:
logger.error(f"Error adding learning event: {str(e)}")
async def trigger_model_update(self) -> Dict[str, Any]:
"""Trigger an online model update based on accumulated events"""
if not self.learning_events:
logger.warning("No learning events available for model update")
return {'success': False, 'reason': 'no_events'}
try:
logger.info(f"Starting online model update with {len(self.learning_events)} events")
# Prepare dataset
dataset = OnlineDataset(self.learning_events, self.tokenizer)
dataloader = DataLoader(dataset, batch_size=self.batch_size, shuffle=True)
# Configure optimizer
optimizer = torch.optim.AdamW(self.model.parameters(), lr=self.learning_rate)
# Training loop
self.model.train()
total_loss = 0.0
num_batches = 0
for batch in dataloader:
optimizer.zero_grad()
outputs = self.model(
input_ids=batch['input_ids'],
attention_mask=batch['attention_mask'],
labels=batch['labels']
)
# Apply event weights to loss
loss = outputs.loss * batch['event_weight'].mean()
loss.backward()
torch.nn.utils.clip_grad_norm_(self.model.parameters(), 1.0)
optimizer.step()
total_loss += loss.item()
num_batches += 1
avg_loss = total_loss / num_batches if num_batches > 0 else 0.0
# Update metrics
self.learning_metrics['total_updates'] += 1
self.learning_metrics['successful_updates'] += 1
self.learning_metrics['average_loss'] = avg_loss
self.learning_metrics['learning_rate_history'].append(self.learning_rate)
# Clear processed events
self.learning_events.clear()
self.last_update_time = datetime.now()
logger.info(f"Online model update completed. Average loss: {avg_loss:.4f}")
# Store updated model (in production, would save to model registry)
await self._save_model_checkpoint()
return {
'success': True,
'average_loss': avg_loss,
'events_processed': num_batches * self.batch_size,
'timestamp': self.last_update_time.isoformat()
}
except Exception as e:
self.learning_metrics['failed_updates'] += 1
logger.error(f"Online model update failed: {str(e)}")
return {'success': False, 'error': str(e)}
async def process_feedback(self,
query: str,
response: str,
feedback_score: float,
source: str = "human_feedback") -> None:
"""Process human feedback for online learning"""
event_type = LearningEventType.FEEDBACK_POSITIVE if feedback_score > 0.5 else LearningEventType.FEEDBACK_NEGATIVE
event = LearningEvent(
event_id=f"feedback_{datetime.now().timestamp()}",
event_type=event_type,
timestamp=datetime.now(),
source=source,
context={
'query': query,
'response': response,
'feedback_score': feedback_score
},
feedback_score=feedback_score,
priority=3 if abs(feedback_score - 0.5) > 0.3 else 2 # Higher priority for strong feedback
)
await self.add_learning_event(event)
async def process_threat_intelligence(self,
threat_data: Dict[str, Any],
source: str = "threat_intel") -> None:
"""Process new threat intelligence for online learning"""
event = LearningEvent(
event_id=f"threat_{datetime.now().timestamp()}",
event_type=LearningEventType.NEW_THREAT_INTELLIGENCE,
timestamp=datetime.now(),
source=source,
context=threat_data,
priority=4, # High priority for new threats
confidence=threat_data.get('confidence', 0.8)
)
await self.add_learning_event(event)
async def process_agent_performance(self,
agent_name: str,
task: str,
success: bool,
performance_data: Dict[str, Any]) -> None:
"""Process agent performance data for online learning"""
event_type = LearningEventType.AGENT_SUCCESS if success else LearningEventType.AGENT_FAILURE
event = LearningEvent(
event_id=f"agent_{agent_name}_{datetime.now().timestamp()}",
event_type=event_type,
timestamp=datetime.now(),
source=agent_name,
context={
'task': task,
'performance_data': performance_data,
'success': success
},
priority=2 if success else 3, # Higher priority for failures to learn from
confidence=performance_data.get('confidence', 0.9)
)
await self.add_learning_event(event)
async def get_learning_statistics(self) -> Dict[str, Any]:
"""Get comprehensive learning statistics"""
return {
'total_events_processed': self.total_events_processed,
'current_events_in_memory': len(self.learning_events),
'last_update_time': self.last_update_time.isoformat(),
'metrics': self.learning_metrics,
'event_type_distribution': self._get_event_type_distribution(),
'learning_rate': self.learning_rate,
'update_frequency': self.update_frequency
}
def _get_event_type_distribution(self) -> Dict[str, int]:
"""Get distribution of event types in current memory"""
distribution = {}
for event in self.learning_events:
event_type = event.event_type.value
distribution[event_type] = distribution.get(event_type, 0) + 1
return distribution
async def _store_event_redis(self, key: str, event: LearningEvent) -> None:
"""Store learning event in Redis for persistence"""
try:
event_data = json.dumps(event.to_dict())
self.redis_client.setex(key, timedelta(days=7), event_data)
except Exception as e:
logger.warning(f"Failed to store event in Redis: {str(e)}")
async def _save_model_checkpoint(self) -> None:
"""Save model checkpoint after online learning update"""
try:
checkpoint_path = f"models/online_learning_checkpoint_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
self.model.save_pretrained(checkpoint_path)
self.tokenizer.save_pretrained(checkpoint_path)
logger.info(f"Model checkpoint saved to {checkpoint_path}")
except Exception as e:
logger.error(f"Failed to save model checkpoint: {str(e)}")
# Factory function for easy instantiation
def create_online_learning_manager(**kwargs) -> OnlineLearningManager:
"""Factory function to create OnlineLearningManager with default configuration"""
return OnlineLearningManager(**kwargs)
|