diff --git a/app.py b/app.py index 8ab879d..2ba116d 100644 --- a/app.py +++ b/app.py @@ -132,6 +132,31 @@ def create_model_instance(model_id, settings, is_reasoning=False): elif "gemini" in model_id.lower() or "google" in model_id.lower(): base_url = proxy_api_config.get('apis', {}).get('google', '') + # 从前端设置获取自定义API基础URL (apiBaseUrls) + api_base_urls = settings.get('apiBaseUrls', {}) + if api_base_urls: + # 根据模型类型选择对应的自定义API基础URL + if "claude" in model_id.lower() or "anthropic" in model_id.lower(): + custom_base_url = api_base_urls.get('anthropic') + if custom_base_url: + base_url = custom_base_url + elif any(keyword in model_id.lower() for keyword in ["gpt", "openai"]): + custom_base_url = api_base_urls.get('openai') + if custom_base_url: + base_url = custom_base_url + elif "deepseek" in model_id.lower(): + custom_base_url = api_base_urls.get('deepseek') + if custom_base_url: + base_url = custom_base_url + elif "qvq" in model_id.lower() or "alibaba" in model_id.lower() or "qwen" in model_id.lower(): + custom_base_url = api_base_urls.get('alibaba') + if custom_base_url: + base_url = custom_base_url + elif "gemini" in model_id.lower() or "google" in model_id.lower(): + custom_base_url = api_base_urls.get('google') + if custom_base_url: + base_url = custom_base_url + # 创建模型实例 model_instance = ModelFactory.create_model( model_name=model_id, @@ -139,7 +164,7 @@ def create_model_instance(model_id, settings, is_reasoning=False): temperature=None if is_reasoning else float(settings.get('temperature', 0.7)), system_prompt=settings.get('systemPrompt'), language=settings.get('language', '中文'), - base_url=base_url # 添加中转API URL + api_base_url=base_url # 现在BaseModel支持api_base_url参数 ) # 设置最大输出Token,但不为阿里巴巴模型设置(它们有自己内部的处理逻辑) diff --git a/models/base.py b/models/base.py index b61ad9f..6c9a953 100644 --- a/models/base.py +++ b/models/base.py @@ -2,11 +2,12 @@ from abc import ABC, abstractmethod from typing import Generator, Any class BaseModel(ABC): - def __init__(self, api_key: str, temperature: float = 0.7, system_prompt: str = None, language: str = None): + def __init__(self, api_key: str, temperature: float = 0.7, system_prompt: str = None, language: str = None, api_base_url: str = None): self.api_key = api_key self.temperature = temperature self.language = language self.system_prompt = system_prompt or self.get_default_system_prompt() + self.api_base_url = api_base_url @abstractmethod def analyze_image(self, image_data: str, proxies: dict = None) -> Generator[dict, None, None]: diff --git a/static/js/settings.js b/static/js/settings.js index fe3e236..87c1e2b 100644 --- a/static/js/settings.js +++ b/static/js/settings.js @@ -547,6 +547,13 @@ class SettingsManager { await this.refreshApiKeyStatus(); console.log('已自动刷新API密钥状态'); + // 加载 API 基础 URL 设置 + if (settings.apiBaseUrlValues) { + this.apiBaseUrlValues = settings.apiBaseUrlValues; + await this.refreshApiBaseUrlStatus(); + console.log('已加载 API 基础 URL 设置'); + } + // 加载其他设置 // Load model selection if (settings.model && this.modelExists(settings.model)) { @@ -764,6 +771,7 @@ class SettingsManager { // 保存UI设置到localStorage(不包含API密钥) const settings = { apiKeys: this.apiKeyValues, // 保存到localStorage(向后兼容) + apiBaseUrlValues: this.apiBaseUrlValues, // 添加API基础URL保存到localStorage model: this.modelSelect.value, maxTokens: this.maxTokens.value, reasoningDepth: this.reasoningDepthSelect?.value || 'standard', @@ -879,7 +887,8 @@ class SettingsManager { provider: modelInfo.provider || 'unknown' }, reasoningConfig: reasoningConfig, - apiBaseUrls: apiBaseUrls + apiBaseUrls: apiBaseUrls, + apiKeys: this.apiKeyValues // 确保传递API密钥 }; }