Files
llm-proxy/config/settings.py
hubian 3f463f2f98 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配置
2026-04-09 17:46:47 +08:00

226 lines
6.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
大模型API中转系统配置 - 支持动态修改
"""
import json
from pathlib import Path
# 配置文件路径
CONFIG_FILE = Path(__file__).parent.parent / 'data' / 'config.json'
# 默认上游模型配置
DEFAULT_PROVIDERS = [
{
"id": "local-qwen",
"name": "Local Qwen",
"priority": 1,
"base_url": "http://192.168.2.5:1234/v1",
"api_key": "sk-lm-fuP5tGU8:Hi7YU87jHyDP6Ay8Tl2j",
"models": ["qwen3.5-4b", "qwen3.5", "qwen"],
"default_model": "qwen3.5-4b",
"timeout": 120,
"enabled": True,
},
{
"id": "siliconflow-deepseek",
"name": "SiliconFlow DeepSeek",
"priority": 2,
"base_url": "https://api.siliconflow.cn/v1",
"api_key": "sk-fhpoexpptvjghpnphtaxbkhjwulzovoqfffbckcfscjmwhcg",
"models": ["Pro/deepseek-ai/DeepSeek-V3.2", "deepseek-v3", "deepseek"],
"default_model": "Pro/deepseek-ai/DeepSeek-V3.2",
"timeout": 120,
"enabled": True,
},
]
# 默认模型别名
DEFAULT_MODEL_ALIASES = {
"auto": "auto",
"qwen": "qwen3.5-4b",
"qwen3.5": "qwen3.5-4b",
"qwen3.5-4b": "qwen3.5-4b",
"deepseek": "Pro/deepseek-ai/DeepSeek-V3.2",
"deepseek-v3": "Pro/deepseek-ai/DeepSeek-V3.2",
"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():
try:
data = json.loads(CONFIG_FILE.read_text(encoding='utf-8'))
return data
except:
pass
return {
"providers": DEFAULT_PROVIDERS,
"model_aliases": DEFAULT_MODEL_ALIASES,
}
def save_config(config):
"""保存配置"""
CONFIG_FILE.parent.mkdir(parents=True, exist_ok=True)
CONFIG_FILE.write_text(json.dumps(config, ensure_ascii=False, indent=2), encoding='utf-8')
def get_providers():
"""获取提供商列表"""
config = load_config()
return config.get("providers", DEFAULT_PROVIDERS)
def get_provider(provider_id):
"""获取单个提供商"""
providers = get_providers()
for p in providers:
if p.get("id") == provider_id:
return p
return None
def add_provider(provider):
"""添加提供商"""
config = load_config()
providers = config.get("providers", [])
# 生成ID
if not provider.get("id"):
provider["id"] = provider["name"].lower().replace(" ", "-").replace(".", "-")
providers.append(provider)
config["providers"] = providers
save_config(config)
return provider
def update_provider(provider_id, data):
"""更新提供商"""
config = load_config()
providers = config.get("providers", [])
for i, p in enumerate(providers):
if p.get("id") == provider_id:
providers[i] = {**p, **data}
config["providers"] = providers
save_config(config)
return providers[i]
return None
def delete_provider(provider_id):
"""删除提供商"""
config = load_config()
providers = config.get("providers", [])
providers = [p for p in providers if p.get("id") != provider_id]
config["providers"] = providers
save_config(config)
return True
def update_priority(provider_ids):
"""更新优先级顺序"""
config = load_config()
providers = config.get("providers", [])
# 按新顺序设置优先级
for i, pid in enumerate(provider_ids):
for p in providers:
if p.get("id") == pid:
p["priority"] = i + 1
config["providers"] = providers
save_config(config)
return providers
def get_model_aliases():
"""获取模型别名"""
config = load_config()
return config.get("model_aliases", DEFAULT_MODEL_ALIASES)
def update_model_alias(alias, target):
"""更新模型别名"""
config = load_config()
aliases = config.get("model_aliases", {})
aliases[alias] = target
config["model_aliases"] = aliases
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)
MODEL_ALIASES = config.get("model_aliases", DEFAULT_MODEL_ALIASES)
# 服务配置
SERVER_CONFIG = {
"host": "0.0.0.0",
"port": 19007,
"debug": True,
}
# 日志配置
LOG_CONFIG = {
"log_dir": "logs",
"log_requests": True,
"log_errors": True,
}
# 重试配置
RETRY_CONFIG = {
"max_retries": 3,
"retry_delay": 1,
"retry_on_errors": [
"connection_error",
"timeout",
"rate_limit",
"server_error",
],
}