From cfe7c81ef38d4081032926a24e61379f5c98c227 Mon Sep 17 00:00:00 2001 From: jingrow Date: Tue, 26 May 2026 21:13:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96jchat=E6=B5=81=E5=BC=8F?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/jchat/service.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/apps/jchat/service.py b/apps/jchat/service.py index ecba35b..23c550a 100644 --- a/apps/jchat/service.py +++ b/apps/jchat/service.py @@ -223,13 +223,13 @@ class ChatService: } async def chat_stream(self, messages: List[Dict]) -> AsyncIterator[bytes]: - """流式处理聊天请求,直接yield SSE格式数据 + """流式处理聊天请求 Args: messages: 消息列表,每个消息包含 role 和 content Yields: - SSE格式的字节数据 + NDJSON格式的字节数据 """ model_config = self._get_model_config(self.model or default_model) model_type = model_config["type"] @@ -252,6 +252,13 @@ class ChatService: continue data = line[6:].strip() if data == "[DONE]": - yield b"data: [DONE]\n\n" + yield b'{"status": "done"}\n' break - yield f"data: {data}\n\n".encode() + try: + chunk = json.loads(data) + delta = chunk.get("choices", [{}])[0].get("delta", {}) + content = delta.get("content", "") + if content: + yield f'{{"content": {json.dumps(content)}}}\n'.encode() + except json.JSONDecodeError: + pass