Refactor: use the same implement for total token count from res (#10197)

### What problem does this PR solve?
use the same implement for total token count from res

### Type of change

- [x] Refactoring
This commit is contained in:
Stephen Hu
2025-09-22 17:17:06 +08:00
committed by GitHub
parent ca9f30e1a1
commit 94dbd4aac9
4 changed files with 20 additions and 33 deletions

View File

@ -88,6 +88,20 @@ def num_tokens_from_string(string: str) -> int:
except Exception:
return 0
def total_token_count_from_response(resp):
if hasattr(resp, "usage") and hasattr(resp.usage, "total_tokens"):
try:
return resp.usage.total_tokens
except Exception:
pass
if 'usage' in resp and 'total_tokens' in resp['usage']:
try:
return resp["usage"]["total_tokens"]
except Exception:
pass
return 0
def truncate(string: str, max_len: int) -> str:
"""Returns truncated text if the length of text exceed max_len."""