File size: 2,910 Bytes
2129c29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Server configuration for NLProxy

Author: IntelliDeep Labs Team
License: BSL 1.1
"""

from __future__ import annotations

from typing import Any, Literal
from pydantic import BaseModel, ConfigDict, field_validator

from nlproxy.llm.client import LLMProvider


class Settings(BaseModel):
    """Runtime settings loaded from environment variables."""

    model_config = ConfigDict(env_prefix="NLPROXY_", case_sensitive=False)

    host: str = "0.0.0.0"
    port: int = 8000
    workers: int = 4
    log_level: Literal["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"] = "INFO"

    redis_url: str = "redis://localhost:6379"
    redis_max_connections: int = 50
    redis_socket_timeout: float = 5.0

    enable_semantic_cache: bool = True
    cache_similarity_threshold: float = 0.92
    cache_default_ttl: int = 3600
    cache_embedding_dim: int = 384

    default_aggressiveness: float = 0.2
    max_compression_timeout: float = 60.0
    compression_max_retries: int = 3

    default_llm_provider: str = "openai"
    default_llm_model: str = "gpt-4"
    llm_request_timeout: float = 30.0
    llm_max_retries: int = 3
    enable_llm_fallback: bool = True

    min_confidence_threshold: float = 0.6
    max_regeneration_attempts: int = 3
    enable_auto_correction: bool = True
    privacy_mode_default: bool = False

    enable_metrics: bool = True
    metrics_path: str = "/metrics"
    enable_tracing: bool = False
    trace_sample_rate: float = 0.1

    enable_nli_verification: bool = True
    enable_perplexity_check: bool = False
    enable_semantic_drift: bool = True

    @field_validator("log_level")
    @classmethod
    def validate_log_level(cls, value: str) -> str:
        allowed = {"DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"}
        normalized = value.upper().strip()
        if normalized not in allowed:
            raise ValueError(f"log_level must be one of {sorted(allowed)}")
        return normalized

    @field_validator("default_llm_provider")
    @classmethod
    def validate_llm_provider(cls, value: str) -> str:
        allowed = {p.value for p in LLMProvider}
        normalized = value.lower().strip()
        if normalized not in allowed:
            raise ValueError(
                f"Invalid LLM provider: {value}. Must be one of {sorted(allowed)}"
            )
        return normalized

    @field_validator(
        "enable_semantic_cache",
        "enable_llm_fallback",
        "enable_metrics",
        "enable_tracing",
        "enable_nli_verification",
        "enable_perplexity_check",
        "enable_semantic_drift",
        "enable_auto_correction",
        "privacy_mode_default",
    )
    @classmethod
    def parse_boolean_flags(cls, value: Any) -> bool:
        if isinstance(value, str):
            normalized = value.strip().lower()
            return normalized in {"1", "true", "yes", "on"}
        return bool(value)


settings = Settings()