mirror of
https://github.com/snailyp/gemini-balance.git
synced 2026-05-22 00:39:32 +08:00
本次提交主要包含以下更新:
- **错误日志页面增强**:
- 重构了 [`app/static/js/error_logs.js`](app/static/js/error_logs.js) 中的分页逻辑,将样式控制移至 CSS,简化了 JavaScript 代码。
- 更新了 [`app/templates/error_logs.html`](app/templates/error_logs.html) 中的分页样式,使其与 `keys_status.html` 保持一致,提升了视觉统一性。
- 在错误日志页面新增了“清空全部”按钮,方便用户一键清除所有错误记录。
- 调整了错误日志表格头部的文本颜色为白色,以改善深色主题下的可读性。
- **应用初始化与配置优化**:
- 调整了 [`app/config/config.py`](app/config/config.py) 中日志记录器的获取方式,确保在配置加载早期即可用。
- 在 [`app/core/application.py`](app/core/application.py) 中引入了更明确的数据库连接管理(连接、断开、初始化)逻辑。
- 优化了 [`app/utils/helpers.py`](app/utils/helpers.py) 中项目路径和版本文件路径的定义方式,使其在模块级别初始化。
- **依赖清理**:
- 从 [`requirements.txt`](requirements.txt) 中移除了不必要的注释。
这些更改旨在提升错误日志模块的用户体验和功能性,并优化应用程序的启动和配置管理流程。
93 lines
3.5 KiB
Python
93 lines
3.5 KiB
Python
from datetime import datetime, timezone
|
|
from typing import Any, Dict, Optional
|
|
|
|
from app.config.config import settings
|
|
from app.log.logger import get_model_logger
|
|
from app.service.client.api_client import GeminiApiClient
|
|
|
|
logger = get_model_logger()
|
|
|
|
|
|
class ModelService:
|
|
async def get_gemini_models(self, api_key: str) -> Optional[Dict[str, Any]]:
|
|
api_client = GeminiApiClient(base_url=settings.BASE_URL)
|
|
gemini_models = await api_client.get_models(api_key)
|
|
|
|
if gemini_models is None:
|
|
logger.error("从 API 客户端获取模型列表失败。")
|
|
return None
|
|
|
|
try:
|
|
filtered_models_list = []
|
|
for model in gemini_models.get("models", []):
|
|
model_id = model["name"].split("/")[-1]
|
|
if model_id not in settings.FILTERED_MODELS:
|
|
filtered_models_list.append(model)
|
|
else:
|
|
logger.debug(f"Filtered out model: {model_id}")
|
|
|
|
gemini_models["models"] = filtered_models_list
|
|
return gemini_models
|
|
except Exception as e:
|
|
logger.error(f"处理模型列表时出错: {e}")
|
|
return None
|
|
|
|
async def get_gemini_openai_models(self, api_key: str) -> Optional[Dict[str, Any]]:
|
|
"""获取 Gemini 模型并转换为 OpenAI 格式"""
|
|
gemini_models = await self.get_gemini_models(api_key)
|
|
if gemini_models is None:
|
|
return None
|
|
|
|
return await self.convert_to_openai_models_format(gemini_models)
|
|
|
|
async def convert_to_openai_models_format(
|
|
self, gemini_models: Dict[str, Any]
|
|
) -> Dict[str, Any]:
|
|
openai_format = {"object": "list", "data": [], "success": True}
|
|
|
|
for model in gemini_models.get("models", []):
|
|
model_id = model["name"].split("/")[-1]
|
|
openai_model = {
|
|
"id": model_id,
|
|
"object": "model",
|
|
"created": int(datetime.now(timezone.utc).timestamp()),
|
|
"owned_by": "google",
|
|
"permission": [],
|
|
"root": model["name"],
|
|
"parent": None,
|
|
}
|
|
openai_format["data"].append(openai_model)
|
|
|
|
if model_id in settings.SEARCH_MODELS:
|
|
search_model = openai_model.copy()
|
|
search_model["id"] = f"{model_id}-search"
|
|
openai_format["data"].append(search_model)
|
|
if model_id in settings.IMAGE_MODELS:
|
|
image_model = openai_model.copy()
|
|
image_model["id"] = f"{model_id}-image"
|
|
openai_format["data"].append(image_model)
|
|
if model_id in settings.THINKING_MODELS:
|
|
non_thinking_model = openai_model.copy()
|
|
non_thinking_model["id"] = f"{model_id}-non-thinking"
|
|
openai_format["data"].append(non_thinking_model)
|
|
|
|
if settings.CREATE_IMAGE_MODEL:
|
|
image_model = openai_model.copy()
|
|
image_model["id"] = f"{settings.CREATE_IMAGE_MODEL}-chat"
|
|
openai_format["data"].append(image_model)
|
|
return openai_format
|
|
|
|
async def check_model_support(self, model: str) -> bool:
|
|
if not model or not isinstance(model, str):
|
|
return False
|
|
|
|
model = model.strip()
|
|
if model.endswith("-search"):
|
|
model = model[:-7]
|
|
return model in settings.SEARCH_MODELS
|
|
if model.endswith("-image"):
|
|
model = model[:-6]
|
|
return model in settings.IMAGE_MODELS
|
|
|
|
return model not in settings.FILTERED_MODELS
|