Files
hunjing/yuanqi/src/config.py
T

93 lines
3.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
元炁配置管理。
从环境变量加载配置,遵循魂镜统一配置规范(所有配置通过环境变量注入,不使用 .env 文件)。
模型文件统一存放在项目根目录 models/ 下,各引擎从对应子目录加载。
"""
from __future__ import annotations
from pathlib import Path
from pydantic_settings import BaseSettings, SettingsConfigDict
_PROJECT_ROOT = Path(__file__).resolve().parent.parent.parent
_DEFAULT_MODELS_DIR = _PROJECT_ROOT / "models"
class Settings(BaseSettings):
"""元炁应用配置。"""
# 服务
host: str = "0.0.0.0"
port: int = 11434
# ── 模型文件路径 ────────────────────────────────────
models_dir: str = str(_DEFAULT_MODELS_DIR)
# 下载源:auto / huggingface / modelscope
# auto 表示优先 ModelScope(国内快),失败后切 HuggingFace。
model_source: str = "auto"
# LLM:GGUF 模型文件路径。默认扫描 $models_dir/gguf/ 下第一个 .gguf
llm_model_path: str = ""
# Embedding:HuggingFace 模型名称。首次调用自动下载到 models/FlagModel/<模型名>。
embedding_model: str = "BAAI/bge-m3"
# Reranker:HuggingFace 模型名称。首次调用自动下载到 models/transformers/<模型名>。
reranker_model: str = "BAAI/bge-reranker-v2-m3"
# Vision(多模态):GGUF 多模态模型路径。默认扫描含 vision / multimodal / llava 的 .gguf
vision_model_path: str = "microsoft/Mage-VL"
# ASR(语音识别):FunASR 模型名称。自动下载到 models/funasr/<模型名>。
asr_model: str = "iic/SenseVoiceSmall"
# 推理设备:auto / cuda / mps / cpu。
# auto 表示自动检测最优可用加速器(CUDA > MPS > CPU)。
device: str = "auto"
# LLM 默认参数
llm_max_tokens: int = 2048
llm_temperature: float = 0.7
llm_ctx_size: int = 4096
# 日志级别
log_level: str = "info"
@property
def models_dir_resolved(self) -> Path:
return Path(self.models_dir).resolve()
@property
def llm_model_path_resolved(self) -> str:
if self.llm_model_path:
return str(Path(self.llm_model_path).resolve())
gguf_dir = self.models_dir_resolved / "gguf"
if gguf_dir.exists():
files = list(gguf_dir.glob("*.gguf"))
if files:
return str(files[0])
return ""
@property
def vision_model_path_resolved(self) -> str:
if self.vision_model_path:
return str(Path(self.vision_model_path).resolve())
gguf_dir = self.models_dir_resolved / "gguf"
if gguf_dir.exists():
for pat in ("*vision*", "*multimodal*", "*llava*", "*mmproj*"):
matches = list(gguf_dir.glob(pat))
if matches:
return str(matches[0])
return ""
model_config = SettingsConfigDict(
env_prefix="yuanqi_",
env_file=None,
)
settings = Settings()