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:
5
.gitignore
vendored
5
.gitignore
vendored
@@ -8,4 +8,7 @@ __pycache__/
|
||||
|
||||
# 环境
|
||||
venv/
|
||||
.env
|
||||
.env
|
||||
|
||||
# 数据文件
|
||||
data/
|
||||
149
admin/app.py
149
admin/app.py
@@ -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中转系统 - 后台管理")
|
||||
|
||||
359
admin/templates/auto-profiles.html
Normal file
359
admin/templates/auto-profiles.html
Normal file
@@ -0,0 +1,359 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Auto配置管理 - LLM Proxy</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css" rel="stylesheet">
|
||||
<style>
|
||||
body { background-color: #f8f9fa; }
|
||||
.provider-item { cursor: move; transition: all 0.2s; }
|
||||
.provider-item:hover { background-color: #f0f0f0; }
|
||||
.provider-item.selected { border-color: #198754; background-color: #f0fff0; }
|
||||
.provider-item.disabled { opacity: 0.5; }
|
||||
.drag-handle { cursor: grab; }
|
||||
.profile-card { transition: all 0.2s; }
|
||||
.profile-card:hover { transform: translateY(-2px); box-shadow: 0 4px 12px rgba(0,0,0,0.1); }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
|
||||
<div class="container">
|
||||
<a class="navbar-brand" href="/">
|
||||
<i class="bi bi-cpu"></i> LLM Proxy Admin
|
||||
</a>
|
||||
<div class="navbar-nav ms-auto">
|
||||
<a class="nav-link" href="/">仪表盘</a>
|
||||
<a class="nav-link" href="/providers">提供商</a>
|
||||
<a class="nav-link" href="/models">模型</a>
|
||||
<a class="nav-link active" href="/auto-profiles">Auto配置</a>
|
||||
<a class="nav-link" href="/logs">日志</a>
|
||||
<a class="nav-link" href="/config">配置</a>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<div class="container py-4">
|
||||
<div class="d-flex justify-content-between align-items-center mb-4">
|
||||
<h4><i class="bi bi-shuffle"></i> Auto配置管理</h4>
|
||||
<button class="btn btn-primary" onclick="showCreateModal()">
|
||||
<i class="bi bi-plus-lg"></i> 创建Auto配置
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="alert alert-info">
|
||||
<i class="bi bi-info-circle"></i>
|
||||
<strong>Auto配置</strong>允许您创建自定义的自动选择模式,指定哪些提供商参与选择以及优先级顺序。
|
||||
例如:<code>auto-fast</code> 只选择响应快的模型,<code>auto-cheap</code> 只选择便宜的模型。
|
||||
</div>
|
||||
|
||||
<div id="profilesList" class="row">
|
||||
<div class="col-12 text-center py-4">
|
||||
<div class="spinner-border text-primary"></div>
|
||||
<p class="mt-2">加载中...</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 创建/编辑模态框 -->
|
||||
<div class="modal fade" id="profileModal" tabindex="-1">
|
||||
<div class="modal-dialog modal-lg">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title" id="modalTitle">创建Auto配置</h5>
|
||||
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<form id="profileForm">
|
||||
<input type="hidden" id="profileId">
|
||||
|
||||
<div class="row">
|
||||
<div class="col-md-6 mb-3">
|
||||
<label class="form-label">配置名称</label>
|
||||
<input type="text" class="form-control" id="profileName"
|
||||
placeholder="例如: auto-fast, auto-cheap" required>
|
||||
<small class="text-muted">模型调用时使用此名称,如 model="auto-fast"</small>
|
||||
</div>
|
||||
<div class="col-md-6 mb-3">
|
||||
<label class="form-label">显示名称</label>
|
||||
<input type="text" class="form-control" id="displayName"
|
||||
placeholder="例如: 快速模式, 经济模式">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label class="form-label">描述</label>
|
||||
<textarea class="form-control" id="profileDesc" rows="2"
|
||||
placeholder="描述这个Auto配置的用途"></textarea>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label class="form-label">选择策略</label>
|
||||
<select class="form-select" id="profileStrategy">
|
||||
<option value="priority">按优先级(推荐)</option>
|
||||
<option value="random">随机选择</option>
|
||||
</select>
|
||||
<small class="text-muted">按优先级会选择列表中第一个可用的提供商</small>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label class="form-label">候选提供商(拖拽调整优先级)</label>
|
||||
<div id="providerList" class="border rounded p-2" style="min-height: 100px;">
|
||||
<!-- 提供商列表将通过JS加载 -->
|
||||
</div>
|
||||
<small class="text-muted">勾选参与自动选择的提供商,拖拽调整优先级</small>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
|
||||
<button type="button" class="btn btn-primary" onclick="saveProfile()">保存</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/sortablejs@1.15.0/Sortable.min.js"></script>
|
||||
<script>
|
||||
let providers = [];
|
||||
let profiles = [];
|
||||
|
||||
// 加载提供商和配置
|
||||
async function loadData() {
|
||||
const [providersRes, profilesRes] = await Promise.all([
|
||||
fetch('/api/providers'),
|
||||
fetch('/api/auto-profiles')
|
||||
]);
|
||||
|
||||
providers = await providersRes.json();
|
||||
profiles = await profilesRes.json();
|
||||
|
||||
renderProfiles();
|
||||
}
|
||||
|
||||
// 渲染配置列表
|
||||
function renderProfiles() {
|
||||
const container = document.getElementById('profilesList');
|
||||
|
||||
if (profiles.length === 0) {
|
||||
container.innerHTML = `
|
||||
<div class="col-12 text-center py-4 text-muted">
|
||||
<i class="bi bi-inbox display-4"></i>
|
||||
<p class="mt-2">暂无Auto配置</p>
|
||||
</div>
|
||||
`;
|
||||
return;
|
||||
}
|
||||
|
||||
container.innerHTML = profiles.map(profile => `
|
||||
<div class="col-md-6 col-lg-4 mb-3">
|
||||
<div class="card profile-card h-100">
|
||||
<div class="card-header d-flex justify-content-between align-items-center">
|
||||
<h6 class="mb-0">
|
||||
<code>${profile.name}</code>
|
||||
${profile.name === 'auto' ? '<span class="badge bg-secondary ms-2">默认</span>' : ''}
|
||||
</h6>
|
||||
<div class="dropdown">
|
||||
<button class="btn btn-sm btn-outline-secondary dropdown-toggle" data-bs-toggle="dropdown">
|
||||
<i class="bi bi-three-dots"></i>
|
||||
</button>
|
||||
<ul class="dropdown-menu">
|
||||
<li><a class="dropdown-item" href="#" onclick="editProfile('${profile.name}')">
|
||||
<i class="bi bi-pencil"></i> 编辑
|
||||
</a></li>
|
||||
${profile.name !== 'auto' ? `
|
||||
<li><a class="dropdown-item text-danger" href="#" onclick="deleteProfile('${profile.name}')">
|
||||
<i class="bi bi-trash"></i> 删除
|
||||
</a></li>
|
||||
` : ''}
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<h6 class="card-title">${profile.display_name || profile.name}</h6>
|
||||
<p class="card-text small text-muted">${profile.description || '暂无描述'}</p>
|
||||
<div class="mt-2">
|
||||
<span class="badge bg-info">${profile.strategy === 'priority' ? '按优先级' : '随机'}</span>
|
||||
<span class="badge bg-secondary">${profile.provider_details?.length || 0} 个提供商</span>
|
||||
</div>
|
||||
<div class="mt-2">
|
||||
${profile.provider_details?.slice(0, 3).map(p =>
|
||||
`<span class="badge bg-light text-dark me-1">${p.name}</span>`
|
||||
).join('') || ''}
|
||||
${(profile.provider_details?.length || 0) > 3 ?
|
||||
`<span class="text-muted small">+${profile.provider_details.length - 3} 更多</span>` : ''}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
`).join('');
|
||||
}
|
||||
|
||||
// 显示创建模态框
|
||||
function showCreateModal() {
|
||||
document.getElementById('modalTitle').textContent = '创建Auto配置';
|
||||
document.getElementById('profileId').value = '';
|
||||
document.getElementById('profileForm').reset();
|
||||
|
||||
renderProviderList([]);
|
||||
|
||||
new bootstrap.Modal(document.getElementById('profileModal')).show();
|
||||
}
|
||||
|
||||
// 编辑配置
|
||||
async function editProfile(profileName) {
|
||||
const res = await fetch(`/api/auto-profiles/${profileName}`);
|
||||
const profile = await res.json();
|
||||
|
||||
document.getElementById('modalTitle').textContent = '编辑Auto配置';
|
||||
document.getElementById('profileId').value = profileName;
|
||||
document.getElementById('profileName').value = profileName;
|
||||
document.getElementById('profileName').disabled = true;
|
||||
document.getElementById('displayName').value = profile.display_name || '';
|
||||
document.getElementById('profileDesc').value = profile.description || '';
|
||||
document.getElementById('profileStrategy').value = profile.strategy;
|
||||
|
||||
renderProviderList(profile.provider_details || []);
|
||||
|
||||
new bootstrap.Modal(document.getElementById('profileModal')).show();
|
||||
}
|
||||
|
||||
// 渲染提供商列表
|
||||
function renderProviderList(selectedProviders) {
|
||||
const container = document.getElementById('providerList');
|
||||
const selectedIds = selectedProviders.map(p => p.id);
|
||||
|
||||
// 按 priority 排序
|
||||
const sortedProviders = [...providers].sort((a, b) => a.priority - b.priority);
|
||||
|
||||
container.innerHTML = sortedProviders.map(p => {
|
||||
const isSelected = selectedIds.includes(p.id) || (selectedIds.includes('*') && p.id !== '*');
|
||||
const isAll = p.id === '*';
|
||||
|
||||
return `
|
||||
<div class="provider-item border rounded p-2 mb-2 ${isSelected ? 'selected' : ''} ${isAll ? 'bg-light' : ''}"
|
||||
data-id="${p.id}" data-priority="${p.priority}">
|
||||
<div class="d-flex align-items-center">
|
||||
<i class="bi bi-grip-vertical drag-handle me-2 text-muted"></i>
|
||||
<div class="form-check me-3">
|
||||
<input type="checkbox" class="form-check-input provider-check"
|
||||
id="provider-${p.id}" ${isSelected ? 'checked' : ''}
|
||||
${isAll ? 'onchange="toggleAllProviders(this)"' : ''}>
|
||||
</div>
|
||||
<div class="flex-grow-1">
|
||||
<div class="fw-bold">${p.name}</div>
|
||||
<small class="text-muted">
|
||||
优先级: ${p.priority} |
|
||||
模型: ${p.models?.slice(0, 2).join(', ')}${p.models?.length > 2 ? '...' : ''}
|
||||
</small>
|
||||
</div>
|
||||
${p.available ?
|
||||
'<span class="badge bg-success">可用</span>' :
|
||||
'<span class="badge bg-danger">不可用</span>'}
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
}).join('');
|
||||
|
||||
// 初始化拖拽排序
|
||||
new Sortable(container, {
|
||||
animation: 150,
|
||||
handle: '.drag-handle',
|
||||
ghostClass: 'bg-info bg-opacity-10',
|
||||
});
|
||||
}
|
||||
|
||||
// 切换所有提供商
|
||||
function toggleAllProviders(checkbox) {
|
||||
const checks = document.querySelectorAll('.provider-check');
|
||||
checks.forEach(c => {
|
||||
if (c.id !== 'provider-*') {
|
||||
c.checked = checkbox.checked;
|
||||
c.closest('.provider-item').classList.toggle('selected', checkbox.checked);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 保存配置
|
||||
async function saveProfile() {
|
||||
const profileName = document.getElementById('profileName').value.trim();
|
||||
const displayName = document.getElementById('displayName').value.trim();
|
||||
const description = document.getElementById('profileDesc').value.trim();
|
||||
const strategy = document.getElementById('profileStrategy').value;
|
||||
|
||||
if (!profileName) {
|
||||
alert('请输入配置名称');
|
||||
return;
|
||||
}
|
||||
|
||||
// 收集选中的提供商
|
||||
const selectedProviders = [];
|
||||
const allCheckbox = document.getElementById('provider-*');
|
||||
|
||||
if (allCheckbox?.checked) {
|
||||
selectedProviders.push('*');
|
||||
} else {
|
||||
document.querySelectorAll('.provider-check:checked').forEach(c => {
|
||||
if (c.id !== 'provider-*') {
|
||||
selectedProviders.push(c.dataset?.id || c.id.replace('provider-', ''));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const data = {
|
||||
name: profileName,
|
||||
display_name: displayName || profileName,
|
||||
description: description,
|
||||
strategy: strategy,
|
||||
providers: selectedProviders.length > 0 ? selectedProviders : ['*']
|
||||
};
|
||||
|
||||
const profileId = document.getElementById('profileId').value;
|
||||
const url = profileId ? `/api/auto-profiles/${profileId}` : '/api/auto-profiles';
|
||||
const method = profileId ? 'PUT' : 'POST';
|
||||
|
||||
try {
|
||||
const res = await fetch(url, {
|
||||
method: method,
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: JSON.stringify(data)
|
||||
});
|
||||
|
||||
const result = await res.json();
|
||||
|
||||
if (result.success) {
|
||||
bootstrap.Modal.getInstance(document.getElementById('profileModal')).hide();
|
||||
loadData();
|
||||
} else {
|
||||
alert('保存失败: ' + (result.error || '未知错误'));
|
||||
}
|
||||
} catch (e) {
|
||||
alert('保存失败: ' + e.message);
|
||||
}
|
||||
}
|
||||
|
||||
// 删除配置
|
||||
async function deleteProfile(profileName) {
|
||||
if (!confirm(`确定删除配置 "${profileName}" 吗?`)) return;
|
||||
|
||||
try {
|
||||
const res = await fetch(`/api/auto-profiles/${profileName}`, {method: 'DELETE'});
|
||||
const result = await res.json();
|
||||
|
||||
if (result.success) {
|
||||
loadData();
|
||||
} else {
|
||||
alert('删除失败: ' + (result.error || '未知错误'));
|
||||
}
|
||||
} catch (e) {
|
||||
alert('删除失败: ' + e.message);
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化
|
||||
loadData();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -30,6 +30,9 @@
|
||||
<a href="/models" class="flex items-center gap-3 px-6 py-3 text-slate-300 hover:bg-slate-700 hover:text-white">
|
||||
<i class="ri-cpu-line"></i><span>模型管理</span>
|
||||
</a>
|
||||
<a href="/auto-profiles" class="flex items-center gap-3 px-6 py-3 text-slate-300 hover:bg-slate-700 hover:text-white">
|
||||
<i class="ri-shuffle-line"></i><span>Auto配置</span>
|
||||
</a>
|
||||
<a href="/logs" class="flex items-center gap-3 px-6 py-3 text-slate-300 hover:bg-slate-700 hover:text-white">
|
||||
<i class="ri-file-list-line"></i><span>日志查看</span>
|
||||
</a>
|
||||
|
||||
@@ -31,6 +31,9 @@
|
||||
<a href="/models" class="flex items-center gap-3 px-6 py-3 text-slate-300 hover:bg-slate-700 hover:text-white">
|
||||
<i class="ri-cpu-line"></i><span>模型管理</span>
|
||||
</a>
|
||||
<a href="/auto-profiles" class="flex items-center gap-3 px-6 py-3 text-slate-300 hover:bg-slate-700 hover:text-white">
|
||||
<i class="ri-shuffle-line"></i><span>Auto配置</span>
|
||||
</a>
|
||||
<a href="/logs" class="flex items-center gap-3 px-6 py-3 text-slate-300 hover:bg-slate-700 hover:text-white">
|
||||
<i class="ri-file-list-line"></i><span>日志查看</span>
|
||||
</a>
|
||||
|
||||
@@ -30,6 +30,9 @@
|
||||
<a href="/models" class="flex items-center gap-3 px-6 py-3 bg-slate-700 text-white">
|
||||
<i class="ri-cpu-line"></i><span>模型管理</span>
|
||||
</a>
|
||||
<a href="/auto-profiles" class="flex items-center gap-3 px-6 py-3 text-slate-300 hover:bg-slate-700 hover:text-white">
|
||||
<i class="ri-shuffle-line"></i><span>Auto配置</span>
|
||||
</a>
|
||||
<a href="/logs" class="flex items-center gap-3 px-6 py-3 text-slate-300 hover:bg-slate-700 hover:text-white">
|
||||
<i class="ri-file-list-line"></i><span>日志查看</span>
|
||||
</a>
|
||||
|
||||
@@ -32,6 +32,9 @@
|
||||
<a href="/models" class="flex items-center gap-3 px-6 py-3 text-slate-300 hover:bg-slate-700 hover:text-white">
|
||||
<i class="ri-cpu-line"></i><span>模型管理</span>
|
||||
</a>
|
||||
<a href="/auto-profiles" class="flex items-center gap-3 px-6 py-3 text-slate-300 hover:bg-slate-700 hover:text-white">
|
||||
<i class="ri-shuffle-line"></i><span>Auto配置</span>
|
||||
</a>
|
||||
<a href="/logs" class="flex items-center gap-3 px-6 py-3 text-slate-300 hover:bg-slate-700 hover:text-white">
|
||||
<i class="ri-file-list-line"></i><span>日志查看</span>
|
||||
</a>
|
||||
|
||||
83
app.py
83
app.py
@@ -16,7 +16,7 @@ import sys
|
||||
# 添加配置路径
|
||||
sys.path.insert(0, str(Path(__file__).parent))
|
||||
from config.settings import (
|
||||
get_providers, get_model_aliases, SERVER_CONFIG,
|
||||
get_providers, get_model_aliases, get_auto_profiles, SERVER_CONFIG,
|
||||
LOG_CONFIG, RETRY_CONFIG
|
||||
)
|
||||
|
||||
@@ -28,15 +28,17 @@ CONFIG_CACHE_TTL = 5
|
||||
_last_config_load = 0
|
||||
_cached_providers = []
|
||||
_cached_aliases = {}
|
||||
_cached_auto_profiles = {}
|
||||
|
||||
def refresh_config():
|
||||
"""动态刷新配置(支持后台管理修改)"""
|
||||
global _last_config_load, _cached_providers, _cached_aliases, provider_status
|
||||
global _last_config_load, _cached_providers, _cached_aliases, _cached_auto_profiles, provider_status
|
||||
|
||||
current_time = time.time()
|
||||
if current_time - _last_config_load > CONFIG_CACHE_TTL:
|
||||
_cached_providers = get_providers()
|
||||
_cached_aliases = get_model_aliases()
|
||||
_cached_auto_profiles = get_auto_profiles()
|
||||
_last_config_load = current_time
|
||||
|
||||
# 更新提供商状态缓存(新增的提供商)
|
||||
@@ -78,8 +80,8 @@ def get_provider_for_model(model_name):
|
||||
resolved_model = _cached_aliases.get(model_name, model_name)
|
||||
|
||||
# auto模式:按优先级选择可用提供商
|
||||
if resolved_model == 'auto':
|
||||
return get_available_provider()
|
||||
if resolved_model == 'auto' or resolved_model.startswith('auto-'):
|
||||
return get_available_provider_for_auto(resolved_model)
|
||||
|
||||
# 查找支持该模型的提供商
|
||||
sorted_providers = sorted(_cached_providers, key=lambda x: x['priority'])
|
||||
@@ -105,21 +107,52 @@ def get_provider_for_model(model_name):
|
||||
return None, None
|
||||
|
||||
|
||||
def get_available_provider():
|
||||
"""获取可用的提供商(按优先级)"""
|
||||
def get_available_provider_for_auto(auto_name='auto'):
|
||||
"""获取auto模式下的可用提供商(支持自定义auto配置)"""
|
||||
refresh_config()
|
||||
|
||||
# 获取auto配置
|
||||
profile = _cached_auto_profiles.get(auto_name, _cached_auto_profiles.get('auto', {}))
|
||||
|
||||
# 获取允许的提供商
|
||||
allowed_providers = profile.get('providers', ['*'])
|
||||
|
||||
# 筛选提供商
|
||||
sorted_providers = sorted(_cached_providers, key=lambda x: x['priority'])
|
||||
candidates = []
|
||||
|
||||
for provider in sorted_providers:
|
||||
if provider['enabled'] and provider_status.get(provider['name'], {}).get('available', True):
|
||||
return provider, provider['default_model']
|
||||
if not provider['enabled']:
|
||||
continue
|
||||
if not provider_status.get(provider['name'], {}).get('available', True):
|
||||
continue
|
||||
|
||||
# 检查是否在允许列表中
|
||||
if '*' in allowed_providers:
|
||||
candidates.append(provider)
|
||||
elif provider.get('id') in allowed_providers or provider['name'] in allowed_providers:
|
||||
candidates.append(provider)
|
||||
|
||||
# 如果都不可用,返回第一个尝试(让错误信息传递)
|
||||
if sorted_providers:
|
||||
return sorted_providers[0], sorted_providers[0]['default_model']
|
||||
# 根据策略选择
|
||||
strategy = profile.get('strategy', 'priority')
|
||||
|
||||
return None, None
|
||||
if not candidates:
|
||||
# 如果没有候选,返回第一个尝试(让错误信息传递)
|
||||
if sorted_providers:
|
||||
return sorted_providers[0], sorted_providers[0]['default_model']
|
||||
return None, None
|
||||
|
||||
if strategy == 'priority':
|
||||
# 按优先级选择第一个
|
||||
return candidates[0], candidates[0]['default_model']
|
||||
elif strategy == 'random':
|
||||
# 随机选择
|
||||
import random
|
||||
selected = random.choice(candidates)
|
||||
return selected, selected['default_model']
|
||||
else:
|
||||
# 默认按优先级
|
||||
return candidates[0], candidates[0]['default_model']
|
||||
|
||||
|
||||
def mark_provider_error(provider_name, error):
|
||||
@@ -225,6 +258,18 @@ def list_models():
|
||||
models_list = []
|
||||
added_models = set()
|
||||
|
||||
# 添加所有auto配置
|
||||
for profile_name, profile in _cached_auto_profiles.items():
|
||||
if profile_name not in added_models:
|
||||
models_list.append({
|
||||
"id": profile_name,
|
||||
"object": "model",
|
||||
"created": int(time.time()),
|
||||
"owned_by": "proxy",
|
||||
"description": profile.get('description', 'Auto-select available model')
|
||||
})
|
||||
added_models.add(profile_name)
|
||||
|
||||
for provider in _cached_providers:
|
||||
if not provider['enabled']:
|
||||
continue
|
||||
@@ -238,16 +283,6 @@ def list_models():
|
||||
})
|
||||
added_models.add(model)
|
||||
|
||||
# 添加auto模型
|
||||
if "auto" not in added_models:
|
||||
models_list.insert(0, {
|
||||
"id": "auto",
|
||||
"object": "model",
|
||||
"created": int(time.time()),
|
||||
"owned_by": "proxy",
|
||||
"description": "Auto-select available model by priority"
|
||||
})
|
||||
|
||||
return jsonify({
|
||||
"object": "list",
|
||||
"data": models_list
|
||||
@@ -310,7 +345,7 @@ def chat_completions():
|
||||
tried_providers.append(provider['name'])
|
||||
|
||||
# 尝试下一个提供商
|
||||
next_provider, next_model = get_available_provider()
|
||||
next_provider, next_model = get_available_provider_for_auto('auto')
|
||||
if next_provider and next_provider['name'] not in tried_providers:
|
||||
provider = next_provider
|
||||
resolved_model = next_model
|
||||
@@ -328,7 +363,7 @@ def chat_completions():
|
||||
tried_providers.append(provider['name'])
|
||||
|
||||
# 尝试下一个提供商
|
||||
next_provider, next_model = get_available_provider()
|
||||
next_provider, next_model = get_available_provider_for_auto('auto')
|
||||
if next_provider and next_provider['name'] not in tried_providers:
|
||||
provider = next_provider
|
||||
resolved_model = next_model
|
||||
|
||||
@@ -45,6 +45,16 @@ DEFAULT_MODEL_ALIASES = {
|
||||
"deepseek-v3.2": "Pro/deepseek-ai/DeepSeek-V3.2",
|
||||
}
|
||||
|
||||
# 默认Auto配置(自定义候选模型和优先级)
|
||||
DEFAULT_AUTO_PROFILES = {
|
||||
"auto": {
|
||||
"name": "默认Auto",
|
||||
"description": "所有启用的提供商按优先级自动选择",
|
||||
"providers": ["*"], # * 表示所有启用的提供商
|
||||
"strategy": "priority", # priority | random | round-robin
|
||||
}
|
||||
}
|
||||
|
||||
def load_config():
|
||||
"""加载配置"""
|
||||
if CONFIG_FILE.exists():
|
||||
@@ -141,6 +151,49 @@ def update_model_alias(alias, target):
|
||||
save_config(config)
|
||||
return aliases
|
||||
|
||||
def get_auto_profiles():
|
||||
"""获取Auto配置列表"""
|
||||
config = load_config()
|
||||
return config.get("auto_profiles", DEFAULT_AUTO_PROFILES)
|
||||
|
||||
def get_auto_profile(profile_name):
|
||||
"""获取单个Auto配置"""
|
||||
profiles = get_auto_profiles()
|
||||
return profiles.get(profile_name)
|
||||
|
||||
def add_auto_profile(profile_name, profile_data):
|
||||
"""添加Auto配置"""
|
||||
config = load_config()
|
||||
profiles = config.get("auto_profiles", {})
|
||||
profiles[profile_name] = profile_data
|
||||
config["auto_profiles"] = profiles
|
||||
save_config(config)
|
||||
return profiles
|
||||
|
||||
def update_auto_profile(profile_name, profile_data):
|
||||
"""更新Auto配置"""
|
||||
config = load_config()
|
||||
profiles = config.get("auto_profiles", {})
|
||||
if profile_name in profiles:
|
||||
profiles[profile_name] = {**profiles[profile_name], **profile_data}
|
||||
config["auto_profiles"] = profiles
|
||||
save_config(config)
|
||||
return profiles[profile_name]
|
||||
return None
|
||||
|
||||
def delete_auto_profile(profile_name):
|
||||
"""删除Auto配置"""
|
||||
if profile_name == "auto":
|
||||
return False # 不能删除默认的auto
|
||||
config = load_config()
|
||||
profiles = config.get("auto_profiles", {})
|
||||
if profile_name in profiles:
|
||||
del profiles[profile_name]
|
||||
config["auto_profiles"] = profiles
|
||||
save_config(config)
|
||||
return True
|
||||
return False
|
||||
|
||||
# 初始化配置
|
||||
config = load_config()
|
||||
UPSTREAM_PROVIDERS = config.get("providers", DEFAULT_PROVIDERS)
|
||||
|
||||
Reference in New Issue
Block a user