优化jchat流式返回逻辑
This commit is contained in:
parent
22c64923ca
commit
cfe7c81ef3
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user