Fix miscalculated token count (#9776)

### What problem does this PR solve?

The total token was incorrectly accumulated when using the
OpenAI-API-Compatible api.

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)
This commit is contained in:
Yuhao Bi
2025-09-05 19:17:21 +08:00
committed by GitHub
parent 45f52e85d7
commit 91d6fb8061

View File

@ -374,7 +374,7 @@ class Base(ABC):
if not tol: if not tol:
total_tokens += num_tokens_from_string(resp.choices[0].delta.content) total_tokens += num_tokens_from_string(resp.choices[0].delta.content)
else: else:
total_tokens += tol total_tokens = tol
finish_reason = resp.choices[0].finish_reason if hasattr(resp.choices[0], "finish_reason") else "" finish_reason = resp.choices[0].finish_reason if hasattr(resp.choices[0], "finish_reason") else ""
if finish_reason == "length": if finish_reason == "length":
@ -410,7 +410,7 @@ class Base(ABC):
if not tol: if not tol:
total_tokens += num_tokens_from_string(resp.choices[0].delta.content) total_tokens += num_tokens_from_string(resp.choices[0].delta.content)
else: else:
total_tokens += tol total_tokens = tol
answer += resp.choices[0].delta.content answer += resp.choices[0].delta.content
yield resp.choices[0].delta.content yield resp.choices[0].delta.content