Spaces:
Paused
Paused
File size: 5,113 Bytes
5eb4857 be01d40 5eb4857 b7980f7 5eb4857 97655dc fb361f9 17bb84f 97655dc 5eb4857 17bb84f 5eb4857 fb361f9 cc4d4a7 17bb84f 5eb4857 5df0bfe 5eb4857 5df0bfe 5eb4857 91ebb5b | 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 | """
Application configuration loaded from environment variables.
Uses pydantic-settings for type-safe config with .env file support.
"""
import os
from pydantic_settings import BaseSettings
from pydantic import field_validator
from functools import lru_cache
class Settings(BaseSettings):
"""Application settings from environment variables."""
# App
APP_NAME: str = "Nexus Master Gateway"
APP_VERSION: str = "1.0.0"
DEBUG: bool = False
API_V1_STR: str = "/api/v1"
# Super Admin Configuration
SUPER_ADMIN_EMAIL: str = "admin@3sc.com"
SUPER_ADMIN_NAME: str = "Nexus Super Admin"
# Database
DATABASE_URL: str | None = None
DB_USER: str = "postgres"
DB_PASS: str = "postgres"
DB_HOST: str = "localhost"
DB_PORT: int = 5432
DB_NAME: str = "customer_portal"
@property
def async_database_url(self) -> str:
"""Constructs or ensures the +asyncpg prefix exists for SQLAlchemy."""
# Priority: Explicit environment variable DATABASE_URL (Cleaned)
url = self.DATABASE_URL.strip() if self.DATABASE_URL else None
if url:
if "localhost" in url or "127.0.0.1" in url:
# If we are in the cloud (Linux) but hitting localhost, something is wrong
import platform
if platform.system() != "Windows":
print("--- WARNING: CLOUD ENVIRONMENT DETECTED LOCALHOST DB ---")
if url.startswith("postgresql://"):
return url.replace("postgresql://", "postgresql+asyncpg://", 1)
return url
# Fallback: Construct from individual parts
return f"postgresql+asyncpg://{self.DB_USER}:{self.DB_PASS}@{self.DB_HOST}:{self.DB_PORT}/{self.DB_NAME}"
@property
def clean_redis_url(self) -> str:
"""Returns a sanitized Redis URL (no trailing newlines or spaces)."""
return self.REDIS_URL.strip().replace("\n", "").replace("\r", "")
@property
def database_host(self) -> str:
"""Returns the hostname of the database for diagnostic logs."""
if self.DATABASE_URL:
try:
from urllib.parse import urlparse
return urlparse(self.DATABASE_URL).hostname or "unspecified"
except: return "error-parsing"
return self.DB_HOST
REDIS_URL: str = "redis://localhost:6379"
CACHE_REDIS_URL: str = "redis://localhost:6379"
# JWT Auth
SECRET_KEY: str = "your-secret-key-change-in-production"
ALGORITHM: str = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES: int = 30
REFRESH_TOKEN_EXPIRE_DAYS: int = 7
# AI Settings (100% Ollama-Only Transition)
AI_PROVIDER: str = "ollama"
AI_MODEL: str = "kimi-k2.5:cloud"
# AI Providers (Hybrid Switch)
AI_IMAGE_PROVIDER: str = "local"
AI_TEXT_PROVIDER: str = "ollama"
# Decoupled Embeddings
AI_TEXT_EMBEDDING_PROVIDER: str = "google" # options: "google", "ollama", "openai"
AI_TEXT_EMBEDDING_MODEL: str = "models/embedding-001"
AI_TEXT_EMBEDDING_DIM: int = 768
AI_IMAGE_EMBEDDING_MODEL: str = "clip-ViT-B-32"
AI_IMAGE_EMBEDDING_DIM: int = 512
# Vector Safety
AI_FORCE_DIMENSION_COMPATIBILITY: bool = True
# Deprecated (Keeping for soft migration/compat)
AI_EMBEDDING_MODEL: str = "nomic-embed-text:latest"
AI_EMBEDDING_DIM: int = 768
OLLAMA_BASE_URL: str = "http://host.docker.internal:11434"
@field_validator("OLLAMA_BASE_URL")
@classmethod
def sanitize_ollama_url(cls, v: str) -> str:
"""Removes trailing slashes to prevent double-slash API paths."""
if v and v.endswith("/"):
return v.rstrip("/")
return v
# Cloud AI Providers
GROQ_API_KEY: str | None = None
OPENAI_API_KEY: str | None = None
ANTHROPIC_API_KEY: str | None = None
GOOGLE_API_KEY: str | None = None
# LangGraph Settings
LANGCHAIN_TRACING_V2: bool = False
LANGCHAIN_API_KEY: str | None = None
LANGCHAIN_PROJECT: str = "customer-portal"
# Storage & Logging Settings
STORAGE_BACKEND: str = "local" # options: "local", "s3"
UPLOAD_DIR: str = "uploads"
LOG_DIR: str = "logs"
# S3 Configuration (Required if STORAGE_BACKEND="s3")
S3_ACCESS_KEY: str | None = None
S3_SECRET_KEY: str | None = None
S3_BUCKET: str | None = None
S3_REGION: str = "us-east-1"
S3_ENDPOINT_URL: str | None = None # For Cloudflare R2 or Minio
class Config:
case_sensitive = True
env_file = ".env"
extra = "ignore" # Add this to prevent future validation errors from extra env vars
@lru_cache()
def get_settings() -> Settings:
return Settings()
def get_redis_settings() -> "RedisSettings":
"""Returns hardened RedisSettings for Upstash/Cloud stability."""
from arq.connections import RedisSettings
settings = get_settings()
rs = RedisSettings.from_dsn(settings.clean_redis_url)
rs.conn_timeout = 60
rs.conn_retries = 20
rs.socket_keepalive = True
rs.retry_on_timeout = True
return rs
|