Files
LLMWiki/src/config.py
2026-06-04 09:58:45 +08:00

63 lines
1.4 KiB
Python

"""Configuration loader."""
import json
from pathlib import Path
CONFIG_PATH = Path(__file__).parent.parent / "config.json"
DEFAULT_CONFIG: dict = {
"language": "Chinese",
"raw_dir": "raw",
"wiki_dir": "wiki",
}
DEFAULT_LLM_CONFIG: dict = {
"model": "qwen/qwen3.6-27b",
"api_base": "http://100.123.83.113:1234/v1",
"api_key": "123456",
}
def load_config() -> dict:
"""
加载项目配置。
返回
-------
dict
配置字典,包含 language、raw_dir、wiki_dir 等键。
"""
if CONFIG_PATH.is_file():
with CONFIG_PATH.open(encoding="utf-8") as f:
user_config = json.load(f)
config = {**DEFAULT_CONFIG, **user_config}
else:
config = DEFAULT_CONFIG.copy()
return config
def get_language() -> str | None:
"""获取配置的语言设置,"auto" 返回 None 以启用自动检测。"""
config = load_config()
language = config.get("language", "auto")
if language.lower() == "auto":
return None
return language
def get_llm_config() -> dict[str, str]:
"""
获取 LLM 配置。
返回
-------
dict[str, str]
包含 model, api_base, api_key 的字典。
"""
config = load_config()
llm_raw = config.get("llm", {})
# 过滤空值,避免空字符串覆盖默认配置
llm_filtered = {k: v for k, v in llm_raw.items() if v}
return {**DEFAULT_LLM_CONFIG, **llm_filtered}