From e780433f6b3bc4ada45107ce2077e6620c10a4eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=9B=E5=B0=A7?= Date: Wed, 13 Dec 2023 09:30:37 +0800 Subject: [PATCH] Fix token_usage None issue in ChatOpenAI with local Chatglm2-6B (#14493) When using local Chatglm2-6B by changing OPENAI_BASE_URL to localhost, the token_usage in ChatOpenAI becomes None. This leads to an AttributeError when trying to access token_usage.items(). This commit adds a check to ensure token_usage is not None before accessing its items. This change prevents the AttributeError and allows ChatOpenAI to work seamlessly with a local Chatglm2-6B model, aligning with the way it operates with the OpenAI API. Co-authored-by: Harrison Chase --- .../langchain_community/chat_models/openai.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/libs/community/langchain_community/chat_models/openai.py b/libs/community/langchain_community/chat_models/openai.py index 1bb86bf085..7026624c1b 100644 --- a/libs/community/langchain_community/chat_models/openai.py +++ b/libs/community/langchain_community/chat_models/openai.py @@ -367,11 +367,12 @@ class ChatOpenAI(BaseChatModel): # Happens in streaming continue token_usage = output["token_usage"] - for k, v in token_usage.items(): - if k in overall_token_usage: - overall_token_usage[k] += v - else: - overall_token_usage[k] = v + if token_usage is not None: + for k, v in token_usage.items(): + if k in overall_token_usage: + overall_token_usage[k] += v + else: + overall_token_usage[k] = v if system_fingerprint is None: system_fingerprint = output.get("system_fingerprint") combined = {"token_usage": overall_token_usage, "model_name": self.model_name}