Skip to content

Commit 73ace1a

Browse files
authored
Refactor response handling in websocket_wiki.py (#459)
1 parent 43ed8a2 commit 73ace1a

1 file changed

Lines changed: 22 additions & 4 deletions

File tree

api/websocket_wiki.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -578,10 +578,28 @@ async def handle_websocket_chat(websocket: WebSocket):
578578
response = await model.acall(api_kwargs=api_kwargs, model_type=ModelType.LLM)
579579
# Handle streaming response from Ollama
580580
async for chunk in response:
581-
text = getattr(chunk, 'response', None) or getattr(chunk, 'text', None) or str(chunk)
582-
if text and not text.startswith('model=') and not text.startswith('created_at='):
583-
text = text.replace('<think>', '').replace('</think>', '')
584-
await websocket.send_text(text)
581+
text = None
582+
if isinstance(chunk, dict):
583+
text = chunk.get("message", {}).get("content") if isinstance(chunk.get("message"), dict) else chunk.get("message")
584+
else:
585+
message = getattr(chunk, "message", None)
586+
if message is not None:
587+
if isinstance(message, dict):
588+
text = message.get("content")
589+
else:
590+
text = getattr(message, "content", None)
591+
592+
if not text:
593+
text = getattr(chunk, 'response', None) or getattr(chunk, 'text', None)
594+
595+
if not text and hasattr(chunk, "__dict__"):
596+
message = chunk.__dict__.get("message")
597+
if isinstance(message, dict):
598+
text = message.get("content")
599+
600+
if isinstance(text, str) and text and not text.startswith('model=') and not text.startswith('created_at='):
601+
clean_text = text.replace('<think>', '').replace('</think>', '')
602+
await websocket.send_text(clean_text)
585603
# Explicitly close the WebSocket connection after the response is complete
586604
await websocket.close()
587605
elif request.provider == "openrouter":

0 commit comments

Comments
 (0)