@@ -186,81 +186,79 @@ async def stream_generate(self, prompt: str, options: Optional[Union[GenerateOpt
186
186
187
187
async with self .session .post ("/generate" , json = data ) as response :
188
188
if response .status != 200 :
189
- try :
190
- error_data = await response .json ()
191
- error_msg = error_data .get ("detail" , "Streaming failed" )
192
- logger .error (f"Streaming error: { error_msg } " )
193
- yield f"\n Error: { error_msg } "
194
- return
195
- except :
196
- yield "\n Error: Streaming failed"
197
- return
189
+ error_msg = await response .text ()
190
+ logger .error (f"Streaming error: { error_msg } " )
191
+ yield f"Error: { error_msg } "
192
+ return
198
193
199
194
buffer = ""
200
195
current_sentence = ""
201
196
last_token_was_space = False
202
197
203
- async for line in response .content :
204
- if line :
205
- try :
206
- line = line .decode ('utf-8' ).strip ()
207
- # Skip empty lines
208
- if not line :
209
- continue
210
-
211
- # Handle SSE format
212
- if line .startswith ("data: " ):
213
- line = line [6 :] # Remove "data: " prefix
214
-
215
- # Skip control messages
216
- if line in ["[DONE]" , "[ERROR]" ]:
217
- continue
218
-
198
+ try :
199
+ async for line in response .content :
200
+ if line :
219
201
try :
220
- # Try to parse as JSON
221
- data = json .loads (line )
222
- text = data .get ("text" , data .get ("response" , "" ))
223
- except json .JSONDecodeError :
224
- # If not JSON, use the line as is
225
- text = line
226
-
227
- if text :
228
- # Clean up any special tokens
229
- text = text .replace ("<|" , "" ).replace ("|>" , "" )
230
- text = text .replace ("<" , "" ).replace (">" , "" )
231
- text = text .replace ("[" , "" ).replace ("]" , "" )
232
- text = text .replace ("{" , "" ).replace ("}" , "" )
233
- text = text .replace ("data:" , "" )
234
- text = text .replace ("��" , "" )
235
- text = text .replace ("\\ n" , "\n " )
236
- text = text .replace ("|user|" , "" )
237
- text = text .replace ("|The" , "The" )
238
- text = text .replace ("/|assistant|" , "" ).replace ("/|user|" , "" )
239
-
240
- # Add space between words if needed
241
- if (not text .startswith (" " ) and
242
- not text .startswith ("\n " ) and
243
- not last_token_was_space and
244
- buffer and
245
- not buffer .endswith (" " ) and
246
- not buffer .endswith ("\n " )):
247
- text = " " + text
202
+ line = line .decode ('utf-8' ).strip ()
203
+ # Skip empty lines
204
+ if not line :
205
+ continue
248
206
249
- # Update tracking variables
250
- buffer += text
251
- current_sentence += text
252
- last_token_was_space = text .endswith (" " ) or text .endswith ("\n " )
253
-
254
- # Check for sentence completion
255
- if any (current_sentence .endswith (p ) for p in ["." , "!" , "?" , "\n " ]):
256
- current_sentence = ""
257
-
258
- yield text
207
+ # Handle SSE format
208
+ if line .startswith ("data: " ):
209
+ line = line [6 :] # Remove "data: " prefix
210
+
211
+ # Skip control messages
212
+ if line in ["[DONE]" , "[ERROR]" ]:
213
+ continue
214
+
215
+ try :
216
+ # Try to parse as JSON
217
+ data = json .loads (line )
218
+ text = data .get ("text" , data .get ("response" , "" ))
219
+ except json .JSONDecodeError :
220
+ # If not JSON, use the line as is
221
+ text = line
222
+
223
+ if text :
224
+ # Clean up any special tokens
225
+ text = text .replace ("<|" , "" ).replace ("|>" , "" )
226
+ text = text .replace ("<" , "" ).replace (">" , "" )
227
+ text = text .replace ("[" , "" ).replace ("]" , "" )
228
+ text = text .replace ("{" , "" ).replace ("}" , "" )
229
+ text = text .replace ("data:" , "" )
230
+ text = text .replace ("��" , "" )
231
+ text = text .replace ("\\ n" , "\n " )
232
+ text = text .replace ("|user|" , "" )
233
+ text = text .replace ("|The" , "The" )
234
+ text = text .replace ("/|assistant|" , "" ).replace ("/|user|" , "" )
235
+ text = text .replace ("assistant" , "" ).replace ("Error:" , "" )
236
+
237
+ # Add space between words if needed
238
+ if (not text .startswith (" " ) and
239
+ not text .startswith ("\n " ) and
240
+ not last_token_was_space and
241
+ buffer and
242
+ not buffer .endswith (" " ) and
243
+ not buffer .endswith ("\n " )):
244
+ text = " " + text
245
+
246
+ # Update tracking variables
247
+ buffer += text
248
+ current_sentence += text
249
+ last_token_was_space = text .endswith (" " ) or text .endswith ("\n " )
250
+
251
+ yield text
252
+
253
+ except Exception as e :
254
+ logger .error (f"Error processing stream chunk: { str (e )} " )
255
+ yield f"\n Error: { str (e )} "
256
+ return
259
257
260
- except Exception as e :
261
- logger .error (f"Error processing stream chunk : { str (e )} " )
262
- yield f"\n Error: { str (e )} "
263
- return
258
+ except Exception as e :
259
+ logger .error (f"Stream connection error : { str (e )} " )
260
+ yield f"\n Error: Connection error - { str (e )} "
261
+ return
264
262
265
263
async def generate (self , prompt : str , options : Optional [Union [GenerateOptions , Dict ]] = None ) -> GenerateResponse :
266
264
"""Generate text from prompt"""
0 commit comments