File size: 8,027 Bytes
1a4aa87 | 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 | """
Custom Exception Classes for AegisLM Backend
Defines domain-specific exceptions for the evaluation framework.
"""
from typing import Any, Dict, Optional
class AegisLMException(Exception):
"""Base exception for all AegisLM errors."""
def __init__(
self,
message: str,
code: str = "AEGISLM_ERROR",
details: Optional[Dict[str, Any]] = None
):
self.message = message
self.code = code
self.details = details or {}
super().__init__(self.message)
def to_dict(self) -> Dict[str, Any]:
return {
"error": self.__class__.__name__,
"code": self.code,
"message": self.message,
"details": self.details
}
# ============================================================================
# Configuration Exceptions
# ============================================================================
class ConfigurationError(AegisLMException):
"""Raised when configuration is invalid or missing."""
def __init__(self, message: str, details: Optional[Dict[str, Any]] = None):
super().__init__(message, code="CONFIG_ERROR", details=details)
class WeightValidationError(ConfigurationError):
"""Raised when scoring weights don't sum to 1.0."""
def __init__(self, total: float):
super().__init__(
f"Scoring weights must sum to 1.0, got {total}",
details={"total_weight": total}
)
# ============================================================================
# Database Exceptions
# ============================================================================
class DatabaseError(AegisLMException):
"""Raised when database operations fail."""
def __init__(self, message: str, details: Optional[Dict[str, Any]] = None):
super().__init__(message, code="DATABASE_ERROR", details=details)
class RecordNotFoundError(DatabaseError):
"""Raised when a database record is not found."""
def __init__(self, model: str, identifier: Any):
super().__init__(
f"{model} not found",
details={"model": model, "identifier": str(identifier)}
)
class MigrationError(DatabaseError):
"""Raised when database migration fails."""
def __init__(self, message: str):
super().__init__(message, code="MIGRATION_ERROR")
# ============================================================================
# Model/Execution Exceptions
# ============================================================================
class ModelError(AegisLMException):
"""Raised when model loading or execution fails."""
def __init__(self, message: str, details: Optional[Dict[str, Any]] = None):
super().__init__(message, code="MODEL_ERROR", details=details)
class ModelNotFoundError(ModelError):
"""Raised when a model is not found in registry."""
def __init__(self, model_name: str, version: Optional[str] = None):
details = {"model_name": model_name}
if version:
details["version"] = version
super().__init__(
f"Model '{model_name}' not found",
details=details
)
class ModelLoadingError(ModelError):
"""Raised when model fails to load."""
def __init__(self, model_name: str, reason: str):
super().__init__(
f"Failed to load model '{model_name}': {reason}",
details={"model_name": model_name, "reason": reason}
)
class GenerationError(ModelError):
"""Raised when text generation fails."""
def __init__(self, message: str):
super().__init__(message, code="GENERATION_ERROR")
class DeviceError(ModelError):
"""Raised when device (CUDA/CPU) configuration fails."""
def __init__(self, message: str):
super().__init__(message, code="DEVICE_ERROR")
# ============================================================================
# Evaluation Exceptions
# ============================================================================
class EvaluationError(AegisLMException):
"""Raised when evaluation pipeline fails."""
def __init__(self, message: str, details: Optional[Dict[str, Any]] = None):
super().__init__(message, code="EVALUATION_ERROR", details=details)
class EvaluationTimeoutError(EvaluationError):
"""Raised when evaluation exceeds timeout."""
def __init__(self, run_id: str, timeout_seconds: int):
super().__init__(
f"Evaluation run '{run_id}' timed out after {timeout_seconds}s",
details={"run_id": run_id, "timeout_seconds": timeout_seconds}
)
class EvaluationCancelledError(EvaluationError):
"""Raised when evaluation is cancelled."""
def __init__(self, run_id: str):
super().__init__(
f"Evaluation run '{run_id}' was cancelled",
details={"run_id": run_id}
)
# ============================================================================
# Scoring Exceptions
# ============================================================================
class ScoringError(AegisLMException):
"""Raised when scoring computation fails."""
def __init__(self, message: str, details: Optional[Dict[str, Any]] = None):
super().__init__(message, code="SCORING_ERROR", details=details)
class InvalidMetricError(ScoringError):
"""Raised when a metric value is outside valid range [0, 1]."""
def __init__(self, metric_name: str, value: float):
super().__init__(
f"Metric '{metric_name}' must be in [0, 1], got {value}",
details={"metric": metric_name, "value": value}
)
# ============================================================================
# API Exceptions
# ============================================================================
class APIError(AegisLMException):
"""Raised for general API errors."""
def __init__(self, message: str, status_code: int = 500):
super().__init__(message, code="API_ERROR")
self.status_code = status_code
class ValidationError(APIError):
"""Raised when request validation fails."""
def __init__(self, message: str, field: Optional[str] = None):
details = {}
if field:
details["field"] = field
super().__init__(message, status_code=422)
self.code = "VALIDATION_ERROR"
self.details = details
class NotFoundError(APIError):
"""Raised when resource is not found."""
def __init__(self, resource: str, identifier: Any):
super().__init__(
f"{resource} not found: {identifier}",
status_code=404
)
self.code = "NOT_FOUND"
# ============================================================================
# Agent Exceptions
# ============================================================================
class AgentError(AegisLMException):
"""Raised when agent execution fails."""
def __init__(self, message: str, agent_type: Optional[str] = None):
details = {}
if agent_type:
details["agent_type"] = agent_type
super().__init__(message, code="AGENT_ERROR", details=details)
class AgentTimeoutError(AgentError):
"""Raised when agent execution times out."""
def __init__(self, agent_type: str, timeout_seconds: int):
super().__init__(
f"Agent '{agent_type}' timed out after {timeout_seconds}s",
agent_type=agent_type
)
class AgentInitializationError(AgentError):
"""Raised when agent fails to initialize."""
def __init__(self, agent_type: str, reason: str):
super().__init__(
f"Failed to initialize agent '{agent_type}': {reason}",
agent_type=agent_type
)
|