Spaces:
Running
Running
File size: 3,772 Bytes
0231daa 79829bb 0231daa 90528a8 0231daa |
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 |
"""
Custom exceptions for the Unified Embedding API.
This module defines all custom exceptions used throughout the application,
providing clear error messages and proper HTTP status code mapping.
"""
class EmbeddingAPIException(Exception):
"""Base exception for all API errors."""
def __init__(self, message: str, status_code: int = 500):
self.message = message
self.status_code = status_code
super().__init__(self.message)
class ModelNotFoundError(EmbeddingAPIException):
"""Raised when a requested model is not found in configuration."""
def __init__(self, model_id: str):
message = f"Model '{model_id}' not found in configuration"
super().__init__(message, status_code=404)
self.model_id = model_id
class ModelNotLoadedError(EmbeddingAPIException):
"""Raised when attempting to use a model that is not loaded."""
def __init__(self, model_id: str):
message = f"Model '{model_id}' is not loaded. Please wait for initialization."
super().__init__(message, status_code=503)
self.model_id = model_id
class ModelTypeError(EmbeddingAPIException):
"""Raise when model not configuration"""
def __init__(self, config, model_id: str, expected_type: str):
message = (
f"Model '{model_id}' is not a {expected_type.replace('-', ' ')} "
f"model. Detected type: {config.type}"
)
super().__init__(message, status_code=404)
self.model_id = model_id
class ModelLoadError(EmbeddingAPIException):
"""Raised when a model fails to load."""
def __init__(self, model_id: str, reason: str):
message = f"Failed to load model '{model_id}': {reason}"
super().__init__(message, status_code=500)
self.model_id = model_id
self.reason = reason
class ConfigurationError(EmbeddingAPIException):
"""Raised when there's an error in the configuration file."""
def __init__(self, reason: str):
message = f"Configuration error: {reason}"
super().__init__(message, status_code=500)
self.reason = reason
class ValidationError(EmbeddingAPIException):
"""Raised when input validation fails."""
def __init__(self, field: str, reason: str):
message = f"Validation error for '{field}': {reason}"
super().__init__(message, status_code=400)
self.field = field
self.reason = reason
class BatchTooLargeError(ValidationError):
"""Raised when batch size exceeds maximum allowed."""
def __init__(self, batch_size: int, max_batch_size: int):
reason = f"Batch size ({batch_size}) exceeds maximum ({max_batch_size})"
super().__init__("texts", reason)
self.batch_size = batch_size
self.max_batch_size = max_batch_size
class EmbeddingGenerationError(EmbeddingAPIException):
"""Raised when embedding generation fails."""
def __init__(self, model_id: str, reason: str):
message = f"Failed to generate embedding with model '{model_id}': {reason}"
super().__init__(message, status_code=500)
self.model_id = model_id
self.reason = reason
class RerankingDocumentError(EmbeddingAPIException):
"""Raised when reranking document fails."""
def __init__(self, model_id: str, reason: str):
message = f"Failed to reranking document with model '{model_id}': {reason}"
super().__init__(message, status_code=500)
self.model_id = model_id
self.reason = reason
class ServerNotReadyError(EmbeddingAPIException):
"""Raised when server is not fully initialized."""
def __init__(self):
message = "Server is not ready. Please wait for model initialization."
super().__init__(message, status_code=503)
|