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:
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
|
||||
|
||||
Reference in New Issue
Block a user