Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

Refactor: for total_token_count method use if to check first. (#9707)

Stephen Huca320a8c3028
diff --git a/rag/llm/rerank_model.py b/rag/llm/rerank_model.py
index 22da0110f6..a69efa7ebf 100644
--- a/rag/llm/rerank_model.py
+++ b/rag/llm/rerank_model.py
@@ -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