File size: 2,382 Bytes
e3892d4
 
 
 
 
 
 
ed0db0d
e3892d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed0db0d
 
 
 
 
e3892d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a7fd580
 
 
 
 
 
 
 
 
 
 
 
 
e3892d4
 
 
 
 
 
a7fd580
e3892d4
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Centralized application settings loaded from environment variables.

Uses pydantic-settings so both backend and frontend can share defaults and
override them through a `.env` file or process environment variables.
"""

from functools import lru_cache
from typing import Optional
from pydantic_settings import BaseSettings, SettingsConfigDict


class APISettings(BaseSettings):
    """Configuration for the FastAPI backend."""

    host: str = "0.0.0.0"
    port: int = 8000
    log_level: str = "INFO"

    model_config = SettingsConfigDict(
        env_prefix="API_",
        env_file=".env",
        env_file_encoding="utf-8",
        extra="ignore",
    )


class LLMSettings(BaseSettings):
    """Configuration for the language model backend."""

    backend: str = "ollama"
    host: str = "http://localhost:11434"
    model: str = "llama3.2:latest"
    timeout: int = 120
    max_retries: int = 3
    retry_delay: float = 1.0
    api_key: Optional[str] = None
    site_url: Optional[str] = None
    app_name: Optional[str] = None

    model_config = SettingsConfigDict(
        env_prefix="LLM_",
        env_file=".env",
        env_file_encoding="utf-8",
        extra="ignore",
    )


class FrontendSettings(BaseSettings):
    """Configuration for the Gradio frontend."""

    backend_base_url: str = "http://localhost:8000"
    websocket_url: str = "ws://localhost:8000/ws/conversation"

    model_config = SettingsConfigDict(
        env_prefix="FRONTEND_",
        env_file=".env",
        env_file_encoding="utf-8",
        extra="ignore",
    )


class DBSettings(BaseSettings):
    """Configuration for persistent storage."""

    path: str = ".localdata/converta.db"

    model_config = SettingsConfigDict(
        env_prefix="DB_",
        env_file=".env",
        env_file_encoding="utf-8",
        extra="ignore",
    )


class AppSettings(BaseSettings):
    """Aggregate configuration exposed to the application."""

    api: APISettings = APISettings()
    llm: LLMSettings = LLMSettings()
    frontend: FrontendSettings = FrontendSettings()
    db: DBSettings = DBSettings()
    log_level: str = "INFO"

    model_config = SettingsConfigDict(
        env_file=".env",
        env_file_encoding="utf-8",
        extra="ignore",
    )


@lru_cache
def get_settings() -> AppSettings:
    """Return the singleton settings instance."""
    return AppSettings()