api-embedding / src /config /settings.py
fahmiaziz98
[UPDATE]: Change version + add prefix rank endpoint
3b88f19
raw
history blame
2.98 kB
"""
Application settings and configuration.
This module uses Pydantic Settings for environment variable management
and configuration validation.
"""
from functools import lru_cache
from pathlib import Path
from pydantic_settings import BaseSettings, SettingsConfigDict
class Settings(BaseSettings):
"""
Application settings loaded from environment variables.
All settings can be overridden via environment variables with the
same name. Example: APP_NAME="My API" in .env file.
"""
# Application Info
APP_NAME: str = "Unified Embedding API - Dense, Sparse & Reranking"
VERSION: str = "3.5.1"
DEBUG: bool = False
ENVIRONMENT: str = "development" # development, staging, production
# Server Configuration
HOST: str = "0.0.0.0"
PORT: int = 7860
WORKERS: int = 1
RELOAD: bool = False
# Model Configuration
MODEL_CONFIG_PATH: str = "src/config/models.yaml"
MODEL_CACHE_DIR: str = "./model_cache"
PRELOAD_MODELS: bool = True
# Request Limits
MAX_TEXT_LENGTH: int = 32000
MAX_BATCH_SIZE: int = 100
REQUEST_TIMEOUT: int = 30
# Logging
LOG_LEVEL: str = "INFO" # DEBUG, INFO, WARNING, ERROR, CRITICAL
LOG_FILE: bool = True # Write logs to file
LOG_DIR: str = "logs"
CORS_ENABLED: bool = False
CORS_ORIGINS: list[str] = ["*"]
# Model Settings
DEVICE: str = "cpu" # "cpu" or "cuda
TRUST_REMOTE_CODE: bool = True # For models requiring remote code
model_config = SettingsConfigDict(
env_file=".env",
env_file_encoding="utf-8",
case_sensitive=True,
extra="ignore",
)
@property
def model_config_file(self) -> Path:
"""Get Path object for model configuration file."""
return Path(self.MODEL_CONFIG_PATH)
@property
def is_production(self) -> bool:
"""Check if running in production environment."""
return self.ENVIRONMENT.lower() == "production"
@property
def is_development(self) -> bool:
"""Check if running in development environment."""
return self.ENVIRONMENT.lower() == "development"
def validate_paths(self) -> None:
"""
Validate that required paths exist.
Raises:
FileNotFoundError: If model config file is not found
"""
if not self.model_config_file.exists():
raise FileNotFoundError(
f"Model configuration file not found: {self.MODEL_CONFIG_PATH}"
)
Path(self.MODEL_CACHE_DIR).mkdir(parents=True, exist_ok=True)
if self.LOG_FILE:
Path(self.LOG_DIR).mkdir(parents=True, exist_ok=True)
@lru_cache()
def get_settings() -> Settings:
"""
Get cached settings instance.
Uses lru_cache to ensure settings are loaded only once
and reused across the application.
Returns:
Settings instance
"""
settings = Settings()
settings.validate_paths()
return settings