mirror of
https://github.com/snailyp/gemini-balance.git
synced 2026-05-30 20:59:45 +08:00
这次提交重构了整个应用的异常处理机制,保证了处理方式的一致性,还能提供更详细的错误信息。 主要改动包括: - 修改了 `ApiClient`,现在抛出的异常会同时包含状态码和消息。这样上游服务就能传递准确的 HTTP 错误响应啦。 - 更新了所有服务层(`gemini`、`openai`、`vertex`、`embedding`),现在会捕获这些结构化的异常,不再从字符串里解析错误消息了。 - 增强了路由级别的错误处理,特别是针对流式端点,能正确捕获初始化错误,并返回结构化的 JSON 错误响应,而不是格式错误的 SSE 事件。 - 在所有 API 路由中添加了 `allowed_token` 的日志记录,方便追踪和调试授权问题。 - 还有一些常规的代码清理,比如调整了 import 顺序和格式化代码,提高了可读性和可维护性。
83 lines
2.9 KiB
Python
83 lines
2.9 KiB
Python
import datetime
|
|
import time
|
|
from typing import List, Union
|
|
|
|
import openai
|
|
from openai import APIStatusError
|
|
from openai.types import CreateEmbeddingResponse
|
|
|
|
from app.config.config import settings
|
|
from app.database.services import add_error_log, add_request_log
|
|
from app.log.logger import get_embeddings_logger
|
|
|
|
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
|
|
status_code = 500
|
|
error_log_msg = f"Generic error: {e}"
|
|
logger.error(f"Error creating embedding (Exception): {error_log_msg}")
|
|
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,
|
|
request_datetime=request_datetime,
|
|
)
|
|
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,
|
|
)
|