feat: improve llm settings ui and douyu daily fallback
This commit is contained in:
@@ -19,6 +19,24 @@ system_bp = Blueprint('system', __name__)
|
||||
APP_START_TIME = time.time()
|
||||
|
||||
|
||||
def _system_config_path() -> str:
|
||||
return os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..', 'config.yaml'))
|
||||
|
||||
|
||||
def _load_system_yaml() -> dict:
|
||||
config_path = _system_config_path()
|
||||
if not os.path.exists(config_path):
|
||||
return {}
|
||||
with open(config_path, 'r', encoding='utf-8') as f:
|
||||
return yaml.safe_load(f) or {}
|
||||
|
||||
|
||||
def _save_system_yaml(config_obj: dict) -> None:
|
||||
config_path = _system_config_path()
|
||||
with open(config_path, 'w', encoding='utf-8') as f:
|
||||
yaml.safe_dump(config_obj, f, allow_unicode=True, sort_keys=False)
|
||||
|
||||
|
||||
@system_bp.route('/api_docs')
|
||||
@login_required
|
||||
def api_docs():
|
||||
@@ -162,7 +180,7 @@ def get_current_user_info():
|
||||
def get_system_config_raw():
|
||||
try:
|
||||
server = current_app.dashboard_server
|
||||
config_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..', 'config.yaml'))
|
||||
config_path = _system_config_path()
|
||||
with open(config_path, 'r', encoding='utf-8') as f:
|
||||
config_text = f.read()
|
||||
robot_config = getattr(getattr(server, "robot", None), "config", None)
|
||||
@@ -190,7 +208,7 @@ def update_system_config():
|
||||
return jsonify({"success": False, "message": "缺少配置内容"}), 400
|
||||
|
||||
yaml.safe_load(config_text)
|
||||
config_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..', 'config.yaml'))
|
||||
config_path = _system_config_path()
|
||||
with open(config_path, 'w', encoding='utf-8') as f:
|
||||
f.write(config_text)
|
||||
|
||||
@@ -203,6 +221,84 @@ def update_system_config():
|
||||
return jsonify({"success": False, "message": str(e)}), 500
|
||||
|
||||
|
||||
@system_bp.route('/api/system/llm_config', methods=['GET'])
|
||||
@login_required
|
||||
def get_system_llm_config():
|
||||
try:
|
||||
config_obj = _load_system_yaml()
|
||||
llm_config = config_obj.get("llm", {}) or {}
|
||||
backends = llm_config.get("backends", {}) or {}
|
||||
backend_list = []
|
||||
for name, backend in backends.items():
|
||||
if not isinstance(backend, dict):
|
||||
continue
|
||||
item = dict(backend)
|
||||
item["name"] = name
|
||||
backend_list.append(item)
|
||||
backend_list.sort(key=lambda item: item.get("name", ""))
|
||||
return jsonify({
|
||||
"success": True,
|
||||
"data": {
|
||||
"default_backend": llm_config.get("default_backend", ""),
|
||||
"backends": backend_list,
|
||||
"config_path": _system_config_path(),
|
||||
}
|
||||
})
|
||||
except Exception as e:
|
||||
logger.error(f"读取全局 LLM 配置失败: {e}")
|
||||
return jsonify({"success": False, "message": str(e)}), 500
|
||||
|
||||
|
||||
@system_bp.route('/api/system/llm_config', methods=['POST'])
|
||||
@login_required
|
||||
def update_system_llm_config():
|
||||
try:
|
||||
server = current_app.dashboard_server
|
||||
data = request.get_json() or {}
|
||||
default_backend = str(data.get("default_backend") or "").strip()
|
||||
backend_list = data.get("backends", []) or []
|
||||
if not isinstance(backend_list, list):
|
||||
return jsonify({"success": False, "message": "backends 格式不正确"}), 400
|
||||
|
||||
normalized_backends = {}
|
||||
for raw in backend_list:
|
||||
if not isinstance(raw, dict):
|
||||
continue
|
||||
name = str(raw.get("name") or "").strip()
|
||||
if not name:
|
||||
continue
|
||||
item = {}
|
||||
for key, value in raw.items():
|
||||
if key == "name":
|
||||
continue
|
||||
if value is None:
|
||||
continue
|
||||
if isinstance(value, str):
|
||||
value = value.strip()
|
||||
if value == "":
|
||||
continue
|
||||
item[key] = value
|
||||
normalized_backends[name] = item
|
||||
|
||||
if default_backend and default_backend not in normalized_backends:
|
||||
return jsonify({"success": False, "message": "默认后端不存在"}), 400
|
||||
|
||||
config_obj = _load_system_yaml()
|
||||
config_obj["llm"] = {
|
||||
"default_backend": default_backend,
|
||||
"backends": normalized_backends,
|
||||
}
|
||||
_save_system_yaml(config_obj)
|
||||
|
||||
if getattr(server, "robot", None) and getattr(server.robot, "config", None):
|
||||
server.robot.config.reload()
|
||||
|
||||
return jsonify({"success": True, "message": "全局 LLM 配置已保存"})
|
||||
except Exception as e:
|
||||
logger.error(f"保存全局 LLM 配置失败: {e}")
|
||||
return jsonify({"success": False, "message": str(e)}), 500
|
||||
|
||||
|
||||
@system_bp.route('/api/restart_service', methods=['POST'])
|
||||
@login_required
|
||||
def restart_service():
|
||||
|
||||
Reference in New Issue
Block a user