🌟 The Multi-Agent Framework: First AI Software Company, Towards Natural Language Programming
simplify code
better62950cdecf627e8
diff --git a/metagpt/provider/openai_api.py b/metagpt/provider/openai_api.pyindex 85d5ef50e..1e0ccd206 100644--- a/metagpt/provider/openai_api.py+++ b/metagpt/provider/openai_api.py@@ -99,12 +99,13 @@ class OpenAILLM(BaseLLM):)log_llm_stream(chunk_message)collected_messages.append(chunk_message)+ chunk_has_usage = hasattr(chunk, "usage") and chunk.usageif has_finished:# for oneapi, there has a usage chunk after finish_reason not none chunk- if hasattr(chunk, "usage"):+ if chunk_has_usage:usage = CompletionUsage(**chunk.usage)if finish_reason:- if hasattr(chunk, "usage") and chunk.usage is not None:+ if chunk_has_usage:# Some services have usage as an attribute of the chunk, such as Fireworksif isinstance(chunk.usage, CompletionUsage):usage = chunk.usage@@ -113,7 +114,7 @@ class OpenAILLM(BaseLLM):elif hasattr(chunk.choices[0], "usage"):# The usage of some services is an attribute of chunk.choices[0], such as Moonshotusage = CompletionUsage(**chunk.choices[0].usage)- elif "openrouter.ai" in self.config.base_url and hasattr(chunk, "usage") and chunk.usage is not None:+ elif "openrouter.ai" in self.config.base_url and chunk_has_usage:# due to it get token cost from apiusage = chunk.usagehas_finished = True