mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-23 23:16:58 +08:00
fix: guard Dashscope response attribute access in token/log utils (#12082)
### What problem does this PR solve? Guard Dashscope response attribute access in token/log utils, since `dashscope_response` returns dict like object. ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue)
This commit is contained in:
@ -75,9 +75,12 @@ def init_root_logger(logfile_basename: str, log_format: str = "%(asctime)-15s %(
|
|||||||
def log_exception(e, *args):
|
def log_exception(e, *args):
|
||||||
logging.exception(e)
|
logging.exception(e)
|
||||||
for a in args:
|
for a in args:
|
||||||
if hasattr(a, "text"):
|
try:
|
||||||
logging.error(a.text)
|
text = getattr(a, "text")
|
||||||
raise Exception(a.text)
|
except Exception:
|
||||||
else:
|
text = None
|
||||||
|
if text is not None:
|
||||||
|
logging.error(text)
|
||||||
|
raise Exception(text)
|
||||||
logging.error(str(a))
|
logging.error(str(a))
|
||||||
raise e
|
raise e
|
||||||
|
|||||||
@ -44,20 +44,20 @@ def total_token_count_from_response(resp):
|
|||||||
if resp is None:
|
if resp is None:
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
if hasattr(resp, "usage") and hasattr(resp.usage, "total_tokens"):
|
|
||||||
try:
|
try:
|
||||||
|
if hasattr(resp, "usage") and hasattr(resp.usage, "total_tokens"):
|
||||||
return resp.usage.total_tokens
|
return resp.usage.total_tokens
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if hasattr(resp, "usage_metadata") and hasattr(resp.usage_metadata, "total_tokens"):
|
|
||||||
try:
|
try:
|
||||||
|
if hasattr(resp, "usage_metadata") and hasattr(resp.usage_metadata, "total_tokens"):
|
||||||
return resp.usage_metadata.total_tokens
|
return resp.usage_metadata.total_tokens
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if hasattr(resp, "meta") and hasattr(resp.meta, "billed_units") and hasattr(resp.meta.billed_units, "input_tokens"):
|
|
||||||
try:
|
try:
|
||||||
|
if hasattr(resp, "meta") and hasattr(resp.meta, "billed_units") and hasattr(resp.meta.billed_units, "input_tokens"):
|
||||||
return resp.meta.billed_units.input_tokens
|
return resp.meta.billed_units.input_tokens
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
@ -85,4 +85,3 @@ def total_token_count_from_response(resp):
|
|||||||
def truncate(string: str, max_len: int) -> str:
|
def truncate(string: str, max_len: int) -> str:
|
||||||
"""Returns truncated text if the length of text exceed max_len."""
|
"""Returns truncated text if the length of text exceed max_len."""
|
||||||
return encoder.decode(encoder.encode(string)[:max_len])
|
return encoder.decode(encoder.encode(string)[:max_len])
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user