@@ -79,7 +79,7 @@ def _make_client_kwargs(self) -> dict:
7979 def _get_proxy_params (self ) -> dict :
8080 params = {}
8181 if self .config .proxy :
82- params = {"proxies " : self .config .proxy }
82+ params = {"proxy " : self .config .proxy }
8383 if self .config .base_url :
8484 params ["base_url" ] = self .config .base_url
8585
@@ -94,13 +94,16 @@ async def _achat_completion_stream(self, messages: list[dict], timeout=USE_CONFI
9494 collected_reasoning_messages = []
9595 has_finished = False
9696 async for chunk in response :
97- if hasattr (chunk .choices [0 ].delta , "reasoning_content" ):
98- collected_reasoning_messages .append (chunk .choices [0 ].delta .reasoning_content ) # for deepseek
97+ if not chunk .choices :
9998 continue
100- chunk_message = chunk .choices [0 ].delta .content or "" if chunk .choices else "" # extract the message
101- finish_reason = (
102- chunk .choices [0 ].finish_reason if chunk .choices and hasattr (chunk .choices [0 ], "finish_reason" ) else None
103- )
99+
100+ choice0 = chunk .choices [0 ]
101+ choice_delta = choice0 .delta
102+ if hasattr (choice_delta , "reasoning_content" ) and choice_delta .reasoning_content :
103+ collected_reasoning_messages .append (choice_delta .reasoning_content ) # for deepseek
104+ continue
105+ chunk_message = choice_delta .content or "" # extract the message
106+ finish_reason = choice0 .finish_reason if hasattr (choice0 , "finish_reason" ) else None
104107 log_llm_stream (chunk_message )
105108 collected_messages .append (chunk_message )
106109 chunk_has_usage = hasattr (chunk , "usage" ) and chunk .usage
@@ -111,13 +114,10 @@ async def _achat_completion_stream(self, messages: list[dict], timeout=USE_CONFI
111114 if finish_reason :
112115 if chunk_has_usage :
113116 # Some services have usage as an attribute of the chunk, such as Fireworks
114- if isinstance (chunk .usage , CompletionUsage ):
115- usage = chunk .usage
116- else :
117- usage = CompletionUsage (** chunk .usage )
118- elif hasattr (chunk .choices [0 ], "usage" ):
117+ usage = CompletionUsage (** chunk .usage ) if isinstance (chunk .usage , dict ) else chunk .usage
118+ elif hasattr (choice0 , "usage" ):
119119 # The usage of some services is an attribute of chunk.choices[0], such as Moonshot
120- usage = CompletionUsage (** chunk . choices [ 0 ] .usage )
120+ usage = CompletionUsage (** choice0 .usage )
121121 has_finished = True
122122
123123 log_llm_stream ("\n " )
0 commit comments