2026-04-16 03:21:38 +08:00
|
|
|
|
"""
|
|
|
|
|
|
前端配置管理模块
|
|
|
|
|
|
集中管理所有配置项,支持环境变量覆盖
|
2026-04-21 18:41:14 +08:00
|
|
|
|
需要类型转换的配置在此处理
|
2026-04-16 03:21:38 +08:00
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
from dataclasses import dataclass
|
2026-04-20 14:05:57 +08:00
|
|
|
|
from typing import Optional
|
2026-04-16 03:21:38 +08:00
|
|
|
|
from dotenv import load_dotenv
|
|
|
|
|
|
|
|
|
|
|
|
# 加载 .env 文件
|
|
|
|
|
|
load_dotenv()
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-04-21 18:41:14 +08:00
|
|
|
|
# ========== 辅助函数:类型转换 ==========
|
|
|
|
|
|
def _get_str(key: str) -> str | None:
|
|
|
|
|
|
"""获取字符串配置"""
|
|
|
|
|
|
return os.getenv(key)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _get_int(key: str, default: int = 0) -> int:
|
|
|
|
|
|
"""获取整数配置,自动转换"""
|
|
|
|
|
|
value = os.getenv(key)
|
|
|
|
|
|
if value is not None:
|
|
|
|
|
|
try:
|
|
|
|
|
|
return int(value)
|
|
|
|
|
|
except (ValueError, TypeError):
|
|
|
|
|
|
pass
|
|
|
|
|
|
return default
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _get_bool(key: str, default: bool = False) -> bool:
|
|
|
|
|
|
"""获取布尔配置,自动转换"""
|
|
|
|
|
|
value = os.getenv(key)
|
|
|
|
|
|
if value is not None:
|
|
|
|
|
|
return value.lower() in ("true", "1", "yes", "on")
|
|
|
|
|
|
return default
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-04-16 03:21:38 +08:00
|
|
|
|
@dataclass
|
|
|
|
|
|
class FrontendConfig:
|
|
|
|
|
|
"""前端配置类 - 统一管理所有配置项"""
|
|
|
|
|
|
|
|
|
|
|
|
# ==================== API 配置 ====================
|
|
|
|
|
|
api_base: str = ""
|
|
|
|
|
|
|
2026-04-21 18:41:14 +08:00
|
|
|
|
# ==================== 页面配置(固定值,无需环境变量) ====================
|
2026-04-16 03:21:38 +08:00
|
|
|
|
page_title: str = "AI 个人助手"
|
|
|
|
|
|
page_icon: str = "🤖"
|
|
|
|
|
|
layout: str = "wide"
|
|
|
|
|
|
|
2026-04-21 18:41:14 +08:00
|
|
|
|
# ==================== 模型配置(固定值,无需环境变量) ====================
|
2026-04-24 21:57:15 +08:00
|
|
|
|
default_model: str = "zhipu"
|
2026-04-20 14:05:57 +08:00
|
|
|
|
model_options: Optional[dict] = None
|
2026-04-16 03:21:38 +08:00
|
|
|
|
|
2026-04-21 18:41:14 +08:00
|
|
|
|
# ==================== 用户配置(固定值,无需环境变量) ====================
|
2026-04-16 03:21:38 +08:00
|
|
|
|
default_user_id: str = "default_user"
|
|
|
|
|
|
|
2026-04-21 18:41:14 +08:00
|
|
|
|
# ==================== 历史记录配置(固定值,无需环境变量) ====================
|
2026-04-16 03:21:38 +08:00
|
|
|
|
history_limit: int = 50
|
|
|
|
|
|
summary_max_length: int = 30
|
|
|
|
|
|
|
2026-04-21 18:41:14 +08:00
|
|
|
|
# ==================== 流式响应配置(固定值,无需环境变量) ====================
|
2026-04-16 03:21:38 +08:00
|
|
|
|
stream_timeout: int = 120
|
|
|
|
|
|
|
2026-04-21 18:41:14 +08:00
|
|
|
|
# ==================== 日志配置 ====================
|
|
|
|
|
|
log_level: str = ""
|
|
|
|
|
|
debug: bool = False
|
|
|
|
|
|
|
2026-04-16 03:21:38 +08:00
|
|
|
|
def __post_init__(self):
|
|
|
|
|
|
"""初始化后处理 - 设置默认值和加载环境变量"""
|
|
|
|
|
|
if self.model_options is None:
|
|
|
|
|
|
self.model_options = {
|
2026-04-24 21:57:15 +08:00
|
|
|
|
"zhipu": "智谱 GLM-4.7-Flash(在线)",
|
2026-04-21 18:41:14 +08:00
|
|
|
|
"local": "本地 llama.cpp(Gemma-4)",
|
2026-04-24 21:57:15 +08:00
|
|
|
|
"deepseek": "DeepSeek V3.2(在线)"
|
2026-04-16 03:21:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-21 18:41:14 +08:00
|
|
|
|
# 从环境变量加载配置(优先级最高)
|
2026-04-16 03:21:38 +08:00
|
|
|
|
self._load_from_env()
|
|
|
|
|
|
|
|
|
|
|
|
def _load_from_env(self):
|
2026-04-21 18:41:14 +08:00
|
|
|
|
"""从环境变量加载配置(仅加载必要的配置项)"""
|
2026-04-16 03:21:38 +08:00
|
|
|
|
# API 地址(移除 /chat 后缀)
|
2026-04-21 18:41:14 +08:00
|
|
|
|
api_url = _get_str("API_URL")
|
|
|
|
|
|
if api_url:
|
|
|
|
|
|
self.api_base = api_url.replace("/chat", "").rstrip("/")
|
|
|
|
|
|
|
2026-04-21 11:02:16 +08:00
|
|
|
|
# 日志配置
|
2026-04-21 18:41:14 +08:00
|
|
|
|
log_level = _get_str("LOG_LEVEL")
|
|
|
|
|
|
if log_level:
|
|
|
|
|
|
self.log_level = log_level.upper()
|
|
|
|
|
|
|
|
|
|
|
|
self.debug = _get_bool("DEBUG", False)
|
2026-04-16 03:21:38 +08:00
|
|
|
|
|
2026-04-21 11:02:16 +08:00
|
|
|
|
|
2026-04-16 03:21:38 +08:00
|
|
|
|
# 全局配置实例(单例模式)
|
2026-04-21 18:41:14 +08:00
|
|
|
|
config = FrontendConfig()
|