refactor(llm_models): 将用量记录函数改为异步以支持数据库写入

将 `LLMUsageRecorder` 中的 `record_usage_to_database` 和 `LLMRequest` 中的 `_record_usage` 方法从同步转换为异步。

此项重构是为了支持异步数据库操作,避免在记录模型用量时阻塞事件循环,从而提升应用的整体性能和响应能力。
This commit is contained in:
tt-P607
2025-09-26 21:59:38 +08:00
parent 50835498f0
commit 9608a040a6
2 changed files with 4 additions and 4 deletions

View File

@@ -145,7 +145,7 @@ class LLMUsageRecorder:
LLM使用情况记录器SQLAlchemy版本
"""
def record_usage_to_database(
async def record_usage_to_database(
self,
model_info: ModelInfo,
model_usage: UsageRecord,

View File

@@ -891,7 +891,7 @@ class LLMRequest:
max_tokens=self.model_for_task.max_tokens if max_tokens is None else max_tokens,
)
self._record_usage(model_info, response.usage, time.time() - start_time, "/chat/completions")
await self._record_usage(model_info, response.usage, time.time() - start_time, "/chat/completions")
if not response.content and not response.tool_calls:
if raise_when_empty:
@@ -916,14 +916,14 @@ class LLMRequest:
embedding_input=embedding_input
)
self._record_usage(model_info, response.usage, time.time() - start_time, "/embeddings")
await self._record_usage(model_info, response.usage, time.time() - start_time, "/embeddings")
if not response.embedding:
raise RuntimeError("获取embedding失败")
return response.embedding, model_info.name
def _record_usage(self, model_info: ModelInfo, usage: Optional[UsageRecord], time_cost: float, endpoint: str):
async def _record_usage(self, model_info: ModelInfo, usage: Optional[UsageRecord], time_cost: float, endpoint: str):
"""
记录模型使用情况。