优化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]:
|
async def chat_stream(self, messages: List[Dict]) -> AsyncIterator[bytes]:
|
||||||
"""流式处理聊天请求,直接yield SSE格式数据
|
"""流式处理聊天请求
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
messages: 消息列表,每个消息包含 role 和 content
|
messages: 消息列表,每个消息包含 role 和 content
|
||||||
|
|
||||||
Yields:
|
Yields:
|
||||||
SSE格式的字节数据
|
NDJSON格式的字节数据
|
||||||
"""
|
"""
|
||||||
model_config = self._get_model_config(self.model or default_model)
|
model_config = self._get_model_config(self.model or default_model)
|
||||||
model_type = model_config["type"]
|
model_type = model_config["type"]
|
||||||
@ -252,6 +252,13 @@ class ChatService:
|
|||||||
continue
|
continue
|
||||||
data = line[6:].strip()
|
data = line[6:].strip()
|
||||||
if data == "[DONE]":
|
if data == "[DONE]":
|
||||||
yield b"data: [DONE]\n\n"
|
yield b'{"status": "done"}\n'
|
||||||
break
|
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