Files
TrulyMEM-TrueHumanMEM/ui/models/config.py

47 lines
1.3 KiB
Python

"""配置数据模型"""
import json
import os
from dataclasses import dataclass, asdict
from pathlib import Path
from typing import Optional
@dataclass
class AppConfig:
"""应用配置"""
api_key: str = ""
model: str = "deepseek-chat"
base_url: str = "https://api.deepseek.com"
@classmethod
def from_env(cls) -> "AppConfig":
"""从环境变量加载配置"""
return cls(
api_key=os.getenv("DEEPSEEK_API_KEY", ""),
model=os.getenv("MODEL_NAME", "deepseek-chat"),
base_url=os.getenv("DEEPSEEK_BASE_URL", "https://api.deepseek.com"),
)
@classmethod
def from_file(cls, path: Path) -> "AppConfig":
"""从文件加载配置"""
if not path.exists():
return cls()
with open(path, "r", encoding="utf-8") as f:
data = json.load(f)
return cls(
api_key=data.get("api_key", ""),
model=data.get("model", "deepseek-chat"),
base_url=data.get("base_url", "https://api.deepseek.com"),
)
def save(self, path: Path) -> None:
"""保存配置到文件"""
path.parent.mkdir(parents=True, exist_ok=True)
with open(path, "w", encoding="utf-8") as f:
json.dump(asdict(self), f, indent=2, ensure_ascii=False)