feat: 添加自定义Auto配置功能

新增功能:
- Auto配置管理页面 (/auto-profiles)
- 创建自定义auto模式,如auto-fast, auto-cheap
- 指定候选提供商和优先级
- 支持按优先级/随机选择策略

API:
- GET/POST /api/auto-profiles
- GET/PUT/DELETE /api/auto-profiles/<name>

改进:
- 主服务支持自定义auto模式路由
- 模型列表显示所有auto配置
This commit is contained in:
2026-04-09 17:46:47 +08:00
parent 82100cdf00
commit 3f463f2f98
9 changed files with 634 additions and 27 deletions

View File

@@ -16,12 +16,13 @@ import requests
# 添加父目录到路径
sys.path.insert(0, str(Path(__file__).parent.parent))
from config.settings import (
DEFAULT_PROVIDERS, DEFAULT_MODEL_ALIASES,
DEFAULT_PROVIDERS, DEFAULT_MODEL_ALIASES, DEFAULT_AUTO_PROFILES,
SERVER_CONFIG, LOG_CONFIG, RETRY_CONFIG,
load_config, save_config, get_providers,
get_provider, add_provider, update_provider,
delete_provider, update_priority, get_model_aliases,
update_model_alias
update_model_alias, get_auto_profiles, get_auto_profile,
add_auto_profile, update_auto_profile, delete_auto_profile
)
app = Flask(__name__)
@@ -93,6 +94,10 @@ def logs_page():
def config_page():
return render_template('config.html')
@app.route('/auto-profiles')
def auto_profiles_page():
return render_template('auto-profiles.html')
# ============ API路由 ============
@app.route('/api/stats')
@@ -432,6 +437,146 @@ def api_recent_requests():
# 模拟数据
return jsonify([])
# ============ Auto配置管理 ============
@app.route('/api/auto-profiles')
def api_auto_profiles():
"""获取所有Auto配置"""
profiles = get_auto_profiles()
providers = get_providers()
result = []
for name, profile in profiles.items():
# 解析允许的提供商信息
allowed_providers = profile.get('providers', ['*'])
provider_details = []
if '*' in allowed_providers:
provider_details = [{'id': '*', 'name': '所有启用的提供商'}]
else:
for p in providers:
if p.get('id') in allowed_providers or p['name'] in allowed_providers:
provider_details.append({
'id': p.get('id'),
'name': p['name'],
'priority': p['priority']
})
result.append({
'name': name,
'display_name': profile.get('name', name),
'description': profile.get('description', ''),
'strategy': profile.get('strategy', 'priority'),
'providers': allowed_providers,
'provider_details': provider_details,
})
return jsonify(result)
@app.route('/api/auto-profiles/<profile_name>', methods=['GET'])
def api_auto_profile_detail(profile_name):
"""获取单个Auto配置详情"""
profile = get_auto_profile(profile_name)
if not profile:
return jsonify({'error': 'Profile not found'}), 404
providers = get_providers()
allowed_providers = profile.get('providers', ['*'])
provider_details = []
if '*' in allowed_providers:
provider_details = [{'id': '*', 'name': '所有启用的提供商', 'selected': True}]
for p in providers:
provider_details.append({
'id': p.get('id'),
'name': p['name'],
'priority': p['priority'],
'selected': True
})
else:
for p in providers:
selected = p.get('id') in allowed_providers or p['name'] in allowed_providers
provider_details.append({
'id': p.get('id'),
'name': p['name'],
'priority': p['priority'],
'selected': selected
})
return jsonify({
'name': profile_name,
'display_name': profile.get('name', profile_name),
'description': profile.get('description', ''),
'strategy': profile.get('strategy', 'priority'),
'providers': allowed_providers,
'provider_details': provider_details,
})
@app.route('/api/auto-profiles', methods=['POST'])
def api_add_auto_profile():
"""添加新的Auto配置"""
data = request.get_json()
if not data or not data.get('name'):
return jsonify({'error': 'Missing profile name'}), 400
profile_name = data['name'].lower().replace(' ', '-').replace('.', '-')
if profile_name in get_auto_profiles():
return jsonify({'error': 'Profile already exists'}), 400
profile_data = {
'name': data.get('display_name', data['name']),
'description': data.get('description', ''),
'providers': data.get('providers', ['*']),
'strategy': data.get('strategy', 'priority'),
}
result = add_auto_profile(profile_name, profile_data)
return jsonify({'success': True, 'profile': {profile_name: profile_data}})
@app.route('/api/auto-profiles/<profile_name>', methods=['PUT'])
def api_update_auto_profile(profile_name):
"""更新Auto配置"""
data = request.get_json()
if not data:
return jsonify({'error': 'Invalid request body'}), 400
profile_data = {}
if 'display_name' in data:
profile_data['name'] = data['display_name']
if 'description' in data:
profile_data['description'] = data['description']
if 'providers' in data:
profile_data['providers'] = data['providers']
if 'strategy' in data:
profile_data['strategy'] = data['strategy']
result = update_auto_profile(profile_name, profile_data)
if not result:
return jsonify({'error': 'Profile not found'}), 404
return jsonify({'success': True, 'profile': result})
@app.route('/api/auto-profiles/<profile_name>', methods=['DELETE'])
def api_delete_auto_profile(profile_name):
"""删除Auto配置"""
result = delete_auto_profile(profile_name)
if not result:
return jsonify({'error': 'Cannot delete default auto profile or profile not found'}), 400
return jsonify({'success': True})
if __name__ == '__main__':
print("=" * 50)
print("大模型API中转系统 - 后台管理")