From 8ed94d1f261ae4c1283167722dce0fb23e327916 Mon Sep 17 00:00:00 2001 From: SengokuCola <1026294844@qq.com> Date: Thu, 11 Sep 2025 00:25:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BB=98=E8=B4=B9emb?= =?UTF-8?q?=E6=A8=A1=E5=9E=8B=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/llm_models/model_client/openai_client.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/llm_models/model_client/openai_client.py b/src/llm_models/model_client/openai_client.py index 1287dbec..4901fd2e 100644 --- a/src/llm_models/model_client/openai_client.py +++ b/src/llm_models/model_client/openai_client.py @@ -551,12 +551,18 @@ class OpenaiClient(BaseClient): # 解析使用情况 if hasattr(raw_response, "usage"): + usage_obj = raw_response.usage + # 安全地获取usage属性,处理不同API版本的差异 + prompt_tokens = getattr(usage_obj, 'prompt_tokens', 0) or 0 + completion_tokens = getattr(usage_obj, 'completion_tokens', 0) or 0 + total_tokens = getattr(usage_obj, 'total_tokens', 0) or 0 + response.usage = UsageRecord( model_name=model_info.name, provider_name=model_info.api_provider, - prompt_tokens=raw_response.usage.prompt_tokens or 0, - completion_tokens=raw_response.usage.completion_tokens or 0, # type: ignore - total_tokens=raw_response.usage.total_tokens or 0, + prompt_tokens=prompt_tokens, + completion_tokens=completion_tokens, + total_tokens=total_tokens, ) return response