113 lines
3.9 KiB
Python
113 lines
3.9 KiB
Python
from __future__ import annotations
|
|
|
|
from functools import lru_cache
|
|
from pathlib import Path
|
|
from typing import Literal
|
|
|
|
from pydantic import Field, model_validator
|
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
|
|
|
|
class Settings(BaseSettings):
|
|
model_config = SettingsConfigDict(
|
|
env_file=".env",
|
|
env_file_encoding="utf-8",
|
|
extra="ignore",
|
|
)
|
|
|
|
app_name: str = "Document Cleaning"
|
|
api_prefix: str = "/api"
|
|
jwt_secret: str = "change-me-in-production"
|
|
jwt_algorithm: str = "HS256"
|
|
access_token_expire_minutes: int = 720
|
|
|
|
database_backend: Literal["sqlite", "postgres"] = "sqlite"
|
|
database_url: str = ""
|
|
sqlite_database_url: str = "sqlite:///./document_cleaning.db"
|
|
postgres_database_url: str = "postgresql+psycopg://postgres:postgres@localhost:5432/document_cleaning"
|
|
redis_url: str = "redis://redis:6379/0"
|
|
|
|
data_root: Path = Field(default=Path("./data"))
|
|
public_base_url: str = "http://192.168.100.230:8891"
|
|
allowed_origins: str = "http://192.168.100.230:8891,http://localhost:5173"
|
|
|
|
dify_base_url: str = "https://api.dify.ai/v1"
|
|
dify_api_key: str = ""
|
|
dify_permission: str = "all_team_members"
|
|
|
|
default_indexing_technique: str = "high_quality"
|
|
default_search_method: str = "hybrid_search"
|
|
default_doc_form: str = "text_model"
|
|
default_embedding_model_provider: str = ""
|
|
default_embedding_model: str = ""
|
|
default_reranking_model_provider: str = ""
|
|
default_reranking_model: str = ""
|
|
default_top_k: int = 5
|
|
default_score_threshold_enabled: bool = True
|
|
default_score_threshold: float = 0.35
|
|
|
|
default_admin_username: str = "admin"
|
|
default_admin_password: str = "admin123456"
|
|
default_admin_name: str = "System Admin"
|
|
integration_api_key: str = ""
|
|
integration_download_secret: str = ""
|
|
integration_download_expire_minutes: int = 15
|
|
|
|
rq_queue_name: str = "document-cleaning"
|
|
rq_default_timeout: int = 3600
|
|
task_execution_mode: Literal["rq", "sync"] = "rq"
|
|
dify_sync_max_retries: int = 4
|
|
dify_sync_retry_schedule: str = "30,60,120,300"
|
|
dify_sync_cooldown_seconds: int = 60
|
|
watchdog_stale_sync_timeout_seconds: int = 900
|
|
watchdog_stale_convert_timeout_seconds: int = 900
|
|
watchdog_convert_orphan_grace_seconds: int = 120
|
|
|
|
edkb_word_timeout_sec: int = 120
|
|
edkb_file_timeout_sec: int = 900
|
|
|
|
def origins_list(self) -> list[str]:
|
|
return [item.strip() for item in self.allowed_origins.split(",") if item.strip()]
|
|
|
|
def dify_sync_retry_delays(self) -> list[int]:
|
|
values: list[int] = []
|
|
for raw in self.dify_sync_retry_schedule.split(","):
|
|
token = raw.strip()
|
|
if not token:
|
|
continue
|
|
try:
|
|
parsed = int(token)
|
|
except ValueError:
|
|
continue
|
|
if parsed > 0:
|
|
values.append(parsed)
|
|
return values or [30, 60, 120, 300]
|
|
|
|
@staticmethod
|
|
def _normalize_database_url(url: str) -> str:
|
|
normalized = url.strip()
|
|
if normalized.startswith("postgresql://"):
|
|
return normalized.replace("postgresql://", "postgresql+psycopg://", 1)
|
|
return normalized
|
|
|
|
@model_validator(mode="after")
|
|
def resolve_database_url(self) -> "Settings":
|
|
if self.database_url:
|
|
self.database_url = self._normalize_database_url(self.database_url)
|
|
return self
|
|
|
|
selected = self.sqlite_database_url if self.database_backend == "sqlite" else self.postgres_database_url
|
|
self.database_url = self._normalize_database_url(selected)
|
|
return self
|
|
|
|
def ensure_data_dirs(self) -> None:
|
|
for name in ["original", "markdown", "assets", "jobs", "tmp"]:
|
|
(self.data_root / name).mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
@lru_cache
|
|
def get_settings() -> Settings:
|
|
settings = Settings()
|
|
settings.ensure_data_dirs()
|
|
return settings
|