61 lines
1.6 KiB
Python
61 lines
1.6 KiB
Python
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
import os
|
|
from pathlib import Path
|
|
|
|
|
|
PROJECT_DIR = Path(__file__).resolve().parent.parent
|
|
ENV_PATH = PROJECT_DIR / ".env"
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class DeepSeekConfig:
|
|
api_key: str
|
|
base_url: str
|
|
model: str
|
|
|
|
|
|
def read_env_file(path: Path = ENV_PATH) -> dict[str, str]:
|
|
if not path.exists():
|
|
return {}
|
|
|
|
values: dict[str, str] = {}
|
|
for raw_line in path.read_text(encoding="utf-8").splitlines():
|
|
line = raw_line.strip()
|
|
if not line or line.startswith("#") or "=" not in line:
|
|
continue
|
|
key, value = line.split("=", 1)
|
|
values[key.strip()] = value.strip().strip('"').strip("'")
|
|
return values
|
|
|
|
|
|
def get_config_value(key: str, default: str = "") -> str:
|
|
env_values = read_env_file()
|
|
return os.getenv(key, env_values.get(key, default)).strip()
|
|
|
|
|
|
def load_deepseek_config() -> DeepSeekConfig:
|
|
return DeepSeekConfig(
|
|
api_key=get_config_value("DEEPSEEK_API_KEY"),
|
|
base_url=get_config_value("DEEPSEEK_BASE_URL", "https://api.deepseek.com").rstrip("/"),
|
|
model=get_config_value("DEEPSEEK_MODEL", "deepseek-chat"),
|
|
)
|
|
|
|
|
|
def get_ai_status() -> dict[str, object]:
|
|
config = load_deepseek_config()
|
|
if not config.api_key:
|
|
return {
|
|
"configured": False,
|
|
"provider": "deepseek",
|
|
"model": config.model,
|
|
"message": "未配置 DEEPSEEK_API_KEY,请在 .env 或环境变量中填写。",
|
|
}
|
|
return {
|
|
"configured": True,
|
|
"provider": "deepseek",
|
|
"model": config.model,
|
|
"message": "DeepSeek API 已配置。",
|
|
}
|