Refactor: for total_token_count method use if to check first. (#9707)
### What problem does this PR solve? for total_token_count method use if to check first, to improve the performance when we need to handle exception cases ### Type of change - [x] Refactoring
This commit is contained in:
parent
ae505e6165
commit
ca320a8c30
1 changed files with 11 additions and 8 deletions
|
|
@ -44,14 +44,17 @@ class Base(ABC):
|
|||
raise NotImplementedError("Please implement encode method!")
|
||||
|
||||
def total_token_count(self, resp):
|
||||
try:
|
||||
return resp.usage.total_tokens
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
return resp["usage"]["total_tokens"]
|
||||
except Exception:
|
||||
pass
|
||||
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
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue