mirror of
https://github.com/snailyp/gemini-balance.git
synced 2026-05-16 20:27:36 +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) 中移除了不必要的注释。
这些更改旨在提升错误日志模块的用户体验和功能性,并优化应用程序的启动和配置管理流程。
79 lines
2.8 KiB
Python
79 lines
2.8 KiB
Python
import datetime
|
|
import time
|
|
import re
|
|
from typing import List, Union
|
|
|
|
import openai
|
|
from openai import APIStatusError
|
|
from openai.types import CreateEmbeddingResponse
|
|
|
|
from app.config.config import settings
|
|
from app.log.logger import get_embeddings_logger
|
|
from app.database.services import add_error_log, add_request_log
|
|
|
|
logger = get_embeddings_logger()
|
|
|
|
|
|
class EmbeddingService:
|
|
|
|
async def create_embedding(
|
|
self, input_text: Union[str, List[str]], model: str, api_key: str
|
|
) -> CreateEmbeddingResponse:
|
|
"""Create embeddings using OpenAI API with database logging"""
|
|
start_time = time.perf_counter()
|
|
request_datetime = datetime.datetime.now()
|
|
is_success = False
|
|
status_code = None
|
|
response = None
|
|
error_log_msg = ""
|
|
if isinstance(input_text, list):
|
|
request_msg_log = {"input_truncated": [str(item)[:100] + "..." if len(str(item)) > 100 else str(item) for item in input_text[:5]]}
|
|
if len(input_text) > 5:
|
|
request_msg_log["input_truncated"].append("...")
|
|
else:
|
|
request_msg_log = {"input_truncated": input_text[:1000] + "..." if len(input_text) > 1000 else input_text}
|
|
|
|
|
|
try:
|
|
client = openai.OpenAI(api_key=api_key, base_url=settings.BASE_URL)
|
|
response = client.embeddings.create(input=input_text, model=model)
|
|
is_success = True
|
|
status_code = 200
|
|
return response
|
|
except APIStatusError as e:
|
|
is_success = False
|
|
status_code = e.status_code
|
|
error_log_msg = f"OpenAI API error: {e}"
|
|
logger.error(f"Error creating embedding (APIStatusError): {error_log_msg}")
|
|
raise e
|
|
except Exception as e:
|
|
is_success = False
|
|
error_log_msg = f"Generic error: {e}"
|
|
logger.error(f"Error creating embedding (Exception): {error_log_msg}")
|
|
match = re.search(r"status code (\d+)", str(e))
|
|
if match:
|
|
status_code = int(match.group(1))
|
|
else:
|
|
status_code = 500
|
|
raise e
|
|
finally:
|
|
end_time = time.perf_counter()
|
|
latency_ms = int((end_time - start_time) * 1000)
|
|
if not is_success:
|
|
await add_error_log(
|
|
gemini_key=api_key,
|
|
model_name=model,
|
|
error_type="openai-embedding",
|
|
error_log=error_log_msg,
|
|
error_code=status_code,
|
|
request_msg=request_msg_log
|
|
)
|
|
await add_request_log(
|
|
model_name=model,
|
|
api_key=api_key,
|
|
is_success=is_success,
|
|
status_code=status_code,
|
|
latency_ms=latency_ms,
|
|
request_time=request_datetime
|
|
)
|