feat: 更新主流大模型供应商目录
- 按云服务、Coding Plan、API 网关和本地模型分组 - 新增兼容模型发现与供应商协议校验
This commit is contained in:
1 parent
01e4435f29
commit
d8b124fc7c
15 files changed
+838
-237
No files matched your search
+60
-17
@@ -34,10 +34,20 @@ func maskAPIKey(key string) string {
|
||||
return strings.Repeat("*", len(key)-4) + key[len(key)-4:]
|
||||
}
|
||||
|
||||
// canReuseMaskedAPIKey 仅允许原提供商与原 Endpoint 继续使用数据库中的密钥。 // Logan.Li
|
||||
func canReuseMaskedAPIKey(provider, endpoint string, saved model.LLMConfig) bool {
|
||||
if saved.ID == 0 || saved.Provider != provider {
|
||||
return false
|
||||
}
|
||||
requestedEndpoint := resolveEndpoint(provider, strings.TrimSpace(endpoint))
|
||||
savedEndpoint := resolveEndpoint(saved.Provider, strings.TrimSpace(saved.Endpoint))
|
||||
return requestedEndpoint != "" && requestedEndpoint == savedEndpoint
|
||||
}
|
||||
|
||||
// getActiveConfig 读取启用的配置(脱敏后) // Logan.Li
|
||||
func (h *LLMHandler) getActiveConfig(maskKey bool) (model.LLMConfig, error) {
|
||||
var cfg model.LLMConfig
|
||||
if err := h.db.First(&cfg).Error; err != nil {
|
||||
if err := h.db.Take(&cfg).Error; err != nil {
|
||||
return cfg, err
|
||||
}
|
||||
if maskKey {
|
||||
@@ -60,17 +70,17 @@ func (h *LLMHandler) GetLLMConfig(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, model.Response{Code: 200, Data: gin.H{
|
||||
"id": cfg.ID,
|
||||
"provider": cfg.Provider,
|
||||
"model": cfg.Model,
|
||||
"api_key": cfg.APIKey,
|
||||
"endpoint": cfg.Endpoint,
|
||||
"temperature": cfg.Temperature,
|
||||
"max_tokens": cfg.MaxTokens,
|
||||
"system_prompt": cfg.SystemPrompt,
|
||||
"enabled": cfg.Enabled,
|
||||
"id": cfg.ID,
|
||||
"provider": cfg.Provider,
|
||||
"model": cfg.Model,
|
||||
"api_key": cfg.APIKey,
|
||||
"endpoint": cfg.Endpoint,
|
||||
"temperature": cfg.Temperature,
|
||||
"max_tokens": cfg.MaxTokens,
|
||||
"system_prompt": cfg.SystemPrompt,
|
||||
"enabled": cfg.Enabled,
|
||||
"reasoning_enabled": cfg.ReasoningEnabled,
|
||||
"configured": true,
|
||||
"configured": true,
|
||||
}})
|
||||
}
|
||||
|
||||
@@ -83,9 +93,13 @@ func (h *LLMHandler) SaveLLMConfig(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
var cfg model.LLMConfig
|
||||
h.db.First(&cfg)
|
||||
// 若前端回传脱敏值(含 ****),保留原 Key
|
||||
if strings.Contains(req.APIKey, "****") && cfg.ID != 0 {
|
||||
h.db.Take(&cfg)
|
||||
// 脱敏密钥只能在提供商和 Endpoint 均未改变时复用,避免凭证被转发到其他地址。 // Logan.Li
|
||||
if strings.Contains(req.APIKey, "****") {
|
||||
if !canReuseMaskedAPIKey(req.Provider, req.Endpoint, cfg) {
|
||||
c.JSON(http.StatusBadRequest, model.Response{Code: 400, Message: "提供商或 Endpoint 已变化,请重新填写 API Key"})
|
||||
return
|
||||
}
|
||||
req.APIKey = cfg.APIKey
|
||||
}
|
||||
cfg.Provider = req.Provider
|
||||
@@ -137,12 +151,18 @@ func resolveEndpoint(provider, endpoint string) string {
|
||||
return "https://open.bigmodel.cn/api/paas/v4"
|
||||
case "qwen":
|
||||
return "https://dashscope.aliyuncs.com/compatible-mode/v1"
|
||||
case "hunyuan": // 腾讯混元 OpenAI 兼容端点 // Logan.Li
|
||||
return "https://tokenhub.tencentmaas.com/v1"
|
||||
case "volcengine": // 火山引擎方舟 OpenAI 兼容端点 // Logan.Li
|
||||
return "https://ark.cn-beijing.volces.com/api/v3"
|
||||
case "baidu": // 百度千帆 v2 OpenAI 兼容端点 // Logan.Li
|
||||
return "https://qianfan.baidubce.com/v2"
|
||||
case "moonshot":
|
||||
return "https://api.moonshot.cn/v1"
|
||||
case "kimi": // Kimi 编码套餐 OpenAI 兼容端点(注意:该端点有客户端白名单,通用调用可能被拒) // Logan.Li
|
||||
return "https://api.kimi.com/coding/v1"
|
||||
case "mimo": // 小米 MiMo OpenAI 兼容端点 // Logan.Li
|
||||
return "https://api.mimo.mi.com/v1"
|
||||
return "https://api.xiaomimimo.com/v1"
|
||||
case "anthropic": // Anthropic 官方 Messages 协议(anthropic.go 客户端拼 /v1/messages) // Logan.Li
|
||||
return "https://api.anthropic.com"
|
||||
case "glm-coding": // 智谱 GLM 编码套餐 Anthropic 协议端点(模型 GLM-5.2) // Logan.Li
|
||||
@@ -153,20 +173,43 @@ func resolveEndpoint(provider, endpoint string) string {
|
||||
return "https://api.minimaxi.com/anthropic"
|
||||
case "openai": // OpenAI 官方端点 // Logan.Li
|
||||
return "https://api.openai.com/v1"
|
||||
case "google": // Gemini OpenAI 兼容端点 // Logan.Li
|
||||
return "https://generativelanguage.googleapis.com/v1beta/openai"
|
||||
case "xai": // xAI OpenAI 兼容端点 // Logan.Li
|
||||
return "https://api.x.ai/v1"
|
||||
case "mistral": // Mistral OpenAI 兼容端点 // Logan.Li
|
||||
return "https://api.mistral.ai/v1"
|
||||
case "groq": // Groq OpenAI 兼容端点 // Logan.Li
|
||||
return "https://api.groq.com/openai/v1"
|
||||
case "openrouter": // OpenRouter OpenAI 兼容端点 // Logan.Li
|
||||
return "https://openrouter.ai/api/v1"
|
||||
case "together": // Together AI OpenAI 兼容端点 // Logan.Li
|
||||
return "https://api.together.ai/v1"
|
||||
case "cerebras": // Cerebras OpenAI 兼容端点 // Logan.Li
|
||||
return "https://api.cerebras.ai/v1"
|
||||
case "siliconflow": // 硅基流动 OpenAI 兼容端点 // Logan.Li
|
||||
return "https://api.siliconflow.cn/v1"
|
||||
case "ollama":
|
||||
return "http://localhost:11434/v1"
|
||||
case "vllm": // vLLM OpenAI 兼容端点 // Logan.Li
|
||||
return "http://localhost:8000/v1"
|
||||
default: // openai / custom
|
||||
case "lmstudio": // LM Studio OpenAI 兼容端点 // Logan.Li
|
||||
return "http://localhost:1234/v1"
|
||||
case "new-api", "custom": // 网关地址必须由用户明确填写 // Logan.Li
|
||||
return ""
|
||||
default: // 未知的历史供应商保持 OpenAI 默认兼容 // Logan.Li
|
||||
return "https://api.openai.com/v1"
|
||||
}
|
||||
}
|
||||
|
||||
// isLocalLLMProvider 本地/自建模型,API Key 可选 // Logan.Li
|
||||
func isLocalLLMProvider(provider string) bool {
|
||||
return provider == "ollama" || provider == "vllm" || provider == "custom"
|
||||
return provider == "ollama" || provider == "vllm" || provider == "lmstudio" || provider == "custom"
|
||||
}
|
||||
|
||||
// requiresExplicitEndpoint 判断是否必须手动填写网关地址。 // Logan.Li
|
||||
func requiresExplicitEndpoint(provider string) bool {
|
||||
return provider == "new-api" || provider == "custom"
|
||||
}
|
||||
|
||||
// resolveProtocol 根据提供商推断接入协议(决定走 OpenAI 还是 Anthropic 客户端) // Logan.Li
|
||||
|
||||
Reference in new issue
Block a user