refactor: 大模型配置改为列表选择模式
- 移除主配置表单,改为从列表选择默认接口 - 新增 is_default 字段标记默认使用的接口 - 新增 max_tokens/chunk_size/timeout 配置参数 - 点击"设为默认"按钮即可切换当前使用的接口 - get_llm_config() 从默认接口获取配置 - 默认接口不可删除,必须有至少一个默认
This commit is contained in:
106
admin.py
106
admin.py
@@ -674,22 +674,15 @@ def api_user_add_package(user_id):
|
||||
@admin_required
|
||||
def llm_config():
|
||||
"""LLM配置页面"""
|
||||
from config import LLM_CONFIG
|
||||
# 获取所有大模型配置
|
||||
llm_configs = BackupLLMConfig.query.order_by(BackupLLMConfig.sort_order).all()
|
||||
|
||||
# 从数据库获取配置,如果没有则使用默认值
|
||||
config = {
|
||||
'api_base': DynamicConfig.get('llm_api_base', LLM_CONFIG.get('api_base')),
|
||||
'api_key': DynamicConfig.get('llm_api_key', LLM_CONFIG.get('api_key')),
|
||||
'model': DynamicConfig.get('llm_model', LLM_CONFIG.get('model')),
|
||||
'max_tokens': DynamicConfig.get('llm_max_tokens', LLM_CONFIG.get('max_tokens')),
|
||||
'chunk_size': DynamicConfig.get('llm_chunk_size', LLM_CONFIG.get('chunk_size')),
|
||||
'timeout': DynamicConfig.get('llm_timeout', LLM_CONFIG.get('timeout')),
|
||||
}
|
||||
# 如果数据库中没有数据,初始化默认配置
|
||||
if not llm_configs:
|
||||
init_default_backup_llm()
|
||||
llm_configs = BackupLLMConfig.query.order_by(BackupLLMConfig.sort_order).all()
|
||||
|
||||
# 获取备用大模型配置
|
||||
backup_configs = BackupLLMConfig.query.order_by(BackupLLMConfig.sort_order).all()
|
||||
|
||||
return render_template('admin/llm_config.html', config=config, backup_configs=[c.to_dict() for c in backup_configs])
|
||||
return render_template('admin/llm_config.html', llm_configs=[c.to_dict() for c in llm_configs])
|
||||
|
||||
|
||||
@admin_bp.route('/llm_config/save', methods=['POST'])
|
||||
@@ -765,17 +758,37 @@ def reset_llm_config():
|
||||
|
||||
# ==================== 获取当前LLM配置(供其他模块使用) ====================
|
||||
def get_llm_config():
|
||||
"""获取当前LLM配置"""
|
||||
from config import LLM_CONFIG
|
||||
"""获取当前默认LLM配置"""
|
||||
# 从数据库获取默认配置
|
||||
default_config = BackupLLMConfig.query.filter_by(is_default=True, is_active=True).first()
|
||||
|
||||
return {
|
||||
'api_base': DynamicConfig.get('llm_api_base', LLM_CONFIG.get('api_base')),
|
||||
'api_key': DynamicConfig.get('llm_api_key', LLM_CONFIG.get('api_key')),
|
||||
'model': DynamicConfig.get('llm_model', LLM_CONFIG.get('model')),
|
||||
'max_tokens': DynamicConfig.get('llm_max_tokens', LLM_CONFIG.get('max_tokens')),
|
||||
'chunk_size': DynamicConfig.get('llm_chunk_size', LLM_CONFIG.get('chunk_size')),
|
||||
'timeout': DynamicConfig.get('llm_timeout', LLM_CONFIG.get('timeout')),
|
||||
}
|
||||
if default_config:
|
||||
return {
|
||||
'api_base': default_config.api_base,
|
||||
'api_key': default_config.api_key or '',
|
||||
'model': default_config.model,
|
||||
'max_tokens': default_config.max_tokens,
|
||||
'chunk_size': default_config.chunk_size,
|
||||
'timeout': default_config.timeout,
|
||||
'provider_name': default_config.provider_name,
|
||||
}
|
||||
|
||||
# 如果没有默认配置,尝试获取第一个启用的
|
||||
first_config = BackupLLMConfig.query.filter_by(is_active=True).order_by(BackupLLMConfig.sort_order).first()
|
||||
if first_config:
|
||||
return {
|
||||
'api_base': first_config.api_base,
|
||||
'api_key': first_config.api_key or '',
|
||||
'model': first_config.model,
|
||||
'max_tokens': first_config.max_tokens,
|
||||
'chunk_size': first_config.chunk_size,
|
||||
'timeout': first_config.timeout,
|
||||
'provider_name': first_config.provider_name,
|
||||
}
|
||||
|
||||
# 如果都没有,使用配置文件默认值
|
||||
from config import LLM_CONFIG
|
||||
return LLM_CONFIG
|
||||
|
||||
|
||||
# ==================== 用户类型配置管理(动态增删) ====================
|
||||
@@ -1234,7 +1247,7 @@ def backup_llm_list():
|
||||
@admin_bp.route('/backup-llm/add', methods=['POST'])
|
||||
@admin_required
|
||||
def add_backup_llm():
|
||||
"""添加备用大模型接口"""
|
||||
"""添加大模型接口"""
|
||||
data = request.json
|
||||
|
||||
config = BackupLLMConfig(
|
||||
@@ -1242,7 +1255,11 @@ def add_backup_llm():
|
||||
api_base=data.get('api_base'),
|
||||
api_key=data.get('api_key'),
|
||||
model=data.get('model'),
|
||||
max_tokens=int(data.get('max_tokens', 8000)),
|
||||
chunk_size=int(data.get('chunk_size', 2000)),
|
||||
timeout=int(data.get('timeout', 180)),
|
||||
is_active=data.get('is_active', True),
|
||||
is_default=False,
|
||||
sort_order=int(data.get('sort_order', 0)),
|
||||
description=data.get('description'),
|
||||
)
|
||||
@@ -1254,7 +1271,7 @@ def add_backup_llm():
|
||||
log = OperationLog(
|
||||
user_id=session.get('user_id'),
|
||||
username='admin',
|
||||
action='add_backup_llm',
|
||||
action='add_llm_config',
|
||||
target=config.provider_name,
|
||||
detail=json.dumps(config.to_dict(), ensure_ascii=False)
|
||||
)
|
||||
@@ -1267,7 +1284,7 @@ def add_backup_llm():
|
||||
@admin_bp.route('/backup-llm/<int:config_id>/edit', methods=['POST'])
|
||||
@admin_required
|
||||
def edit_backup_llm(config_id):
|
||||
"""编辑备用大模型接口"""
|
||||
"""编辑大模型接口"""
|
||||
config = BackupLLMConfig.query.get_or_404(config_id)
|
||||
data = request.json
|
||||
|
||||
@@ -1275,6 +1292,9 @@ def edit_backup_llm(config_id):
|
||||
config.api_base = data.get('api_base', config.api_base)
|
||||
config.api_key = data.get('api_key', config.api_key)
|
||||
config.model = data.get('model', config.model)
|
||||
config.max_tokens = int(data.get('max_tokens', config.max_tokens))
|
||||
config.chunk_size = int(data.get('chunk_size', config.chunk_size))
|
||||
config.timeout = int(data.get('timeout', config.timeout))
|
||||
config.is_active = data.get('is_active', True)
|
||||
config.sort_order = int(data.get('sort_order', config.sort_order))
|
||||
config.description = data.get('description', config.description)
|
||||
@@ -1285,7 +1305,7 @@ def edit_backup_llm(config_id):
|
||||
log = OperationLog(
|
||||
user_id=session.get('user_id'),
|
||||
username='admin',
|
||||
action='edit_backup_llm',
|
||||
action='edit_llm_config',
|
||||
target=config.provider_name,
|
||||
detail=json.dumps(config.to_dict(), ensure_ascii=False)
|
||||
)
|
||||
@@ -1321,7 +1341,7 @@ def delete_backup_llm(config_id):
|
||||
@admin_bp.route('/backup-llm/<int:config_id>/toggle', methods=['POST'])
|
||||
@admin_required
|
||||
def toggle_backup_llm(config_id):
|
||||
"""切换备用大模型接口状态"""
|
||||
"""切换大模型接口状态"""
|
||||
config = BackupLLMConfig.query.get_or_404(config_id)
|
||||
config.is_active = not config.is_active
|
||||
db.session.commit()
|
||||
@@ -1329,6 +1349,34 @@ def toggle_backup_llm(config_id):
|
||||
return jsonify({'success': True, 'is_active': config.is_active})
|
||||
|
||||
|
||||
@admin_bp.route('/backup-llm/<int:config_id>/set-default', methods=['POST'])
|
||||
@admin_required
|
||||
def set_default_llm(config_id):
|
||||
"""设置默认大模型接口"""
|
||||
config = BackupLLMConfig.query.get_or_404(config_id)
|
||||
|
||||
# 先清除所有默认标记
|
||||
BackupLLMConfig.query.update({'is_default': False})
|
||||
|
||||
# 设置当前为默认
|
||||
config.is_default = True
|
||||
config.is_active = True # 默认的必须启用
|
||||
db.session.commit()
|
||||
|
||||
# 记录日志
|
||||
log = OperationLog(
|
||||
user_id=session.get('user_id'),
|
||||
username='admin',
|
||||
action='set_default_llm',
|
||||
target=config.provider_name,
|
||||
detail=f'设置 {config.provider_name} 为默认大模型'
|
||||
)
|
||||
db.session.add(log)
|
||||
db.session.commit()
|
||||
|
||||
return jsonify({'success': True, 'config': config.to_dict()})
|
||||
|
||||
|
||||
@admin_bp.route('/backup-llm/<int:config_id>/test', methods=['POST'])
|
||||
@admin_required
|
||||
def test_backup_llm(config_id):
|
||||
|
||||
Reference in New Issue
Block a user