From d25edcac4425eae9010a8b65859a841cfe82abc4 Mon Sep 17 00:00:00 2001 From: zhangfuqiang Date: Mon, 23 Sep 2024 20:41:35 -0700 Subject: [PATCH] Fix issue #1367: Ensure chunk.usage is not None before passing it to CompletionUsage --- metagpt/provider/openai_api.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/metagpt/provider/openai_api.py b/metagpt/provider/openai_api.py index 68dc156c2..a73977587 100644 --- a/metagpt/provider/openai_api.py +++ b/metagpt/provider/openai_api.py @@ -92,7 +92,16 @@ async def _achat_completion_stream(self, messages: list[dict], timeout=USE_CONFI ) usage = None collected_messages = [] + usage = None + collected_messages = [] async for chunk in response: + if chunk.usage is not None: + usage = CompletionUsage(**chunk.usage) + chunk_message = chunk.choices[0].delta.content or '' if chunk.choices else '' # extract the message + finish_reason = ( + chunk.choices[0].finish_reason if chunk.choices and hasattr(chunk.choices[0], 'finish_reason') else None + ) + log_llm_stream(chunk_message) chunk_message = chunk.choices[0].delta.content or "" if chunk.choices else "" # extract the message finish_reason = ( chunk.choices[0].finish_reason if chunk.choices and hasattr(chunk.choices[0], "finish_reason") else None