mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-08 20:42:30 +08:00
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:
@ -44,14 +44,17 @@ class Base(ABC):
|
|||||||
raise NotImplementedError("Please implement encode method!")
|
raise NotImplementedError("Please implement encode method!")
|
||||||
|
|
||||||
def total_token_count(self, resp):
|
def total_token_count(self, resp):
|
||||||
try:
|
if hasattr(resp, "usage") and hasattr(resp.usage, "total_tokens"):
|
||||||
return resp.usage.total_tokens
|
try:
|
||||||
except Exception:
|
return resp.usage.total_tokens
|
||||||
pass
|
except Exception:
|
||||||
try:
|
pass
|
||||||
return resp["usage"]["total_tokens"]
|
|
||||||
except Exception:
|
if 'usage' in resp and 'total_tokens' in resp['usage']:
|
||||||
pass
|
try:
|
||||||
|
return resp["usage"]["total_tokens"]
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user