fix: resolve streaming duplication and markdown formatting issues
- Fix text duplication bug with proper delta calculation - Preserve markdown formatting with chunk-based delivery (50 chars) - Handle GeneratorExit errors from async context managers - Update Chat service streaming to preserve formatting - Ensure proper word-by-word streaming without duplicates
This commit is contained in:
+7
-4
@@ -214,9 +214,12 @@ async def create_chat_completion_stream(
|
||||
in_reasoning = False
|
||||
|
||||
elif item.type == "message":
|
||||
# Stream message content word by word
|
||||
# Stream message content in chunks (preserves newlines, markdown, etc.)
|
||||
text = item.data["content"][0]["text"]
|
||||
for word in text.split():
|
||||
chunk_size = 50 # characters per chunk
|
||||
|
||||
for i in range(0, len(text), chunk_size):
|
||||
chunk = text[i:i+chunk_size]
|
||||
yield ChatCompletionChunk(
|
||||
id=completion_id,
|
||||
object=constants.CHAT_COMPLETION_CHUNK_OBJECT,
|
||||
@@ -225,12 +228,12 @@ async def create_chat_completion_stream(
|
||||
choices=[
|
||||
ChatCompletionChunkChoice(
|
||||
index=0,
|
||||
delta=ChatCompletionChunkDelta(content=f"{word} "),
|
||||
delta=ChatCompletionChunkDelta(content=chunk),
|
||||
finish_reason=None,
|
||||
)
|
||||
],
|
||||
)
|
||||
await asyncio.sleep(0.05) # Simulate typing
|
||||
await asyncio.sleep(0.02) # Faster since chunks are larger
|
||||
|
||||
# Final chunk with finish_reason
|
||||
yield ChatCompletionChunk(
|
||||
|
||||
Reference in New Issue
Block a user