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