fix: 暂时使用非流式输出确保稳定性

This commit is contained in:
2026-04-12 18:32:35 +08:00
parent 186f69c87a
commit 08c4f79313

View File

@@ -659,56 +659,43 @@ async def websocket_endpoint(websocket: WebSocket, user_id: str):
# 获取对话历史 # 获取对话历史
history = conv_service.get_conversation_history(conversation_id, limit=agent_config['agent'].get('max_history', 20)) history = conv_service.get_conversation_history(conversation_id, limit=agent_config['agent'].get('max_history', 20))
# 使用流式调用LLM # 使用流式调用LLM(简化版本,确保稳定)
try: try:
thinking_buffer = "" # 先发送思考开始
content_buffer = "" if agent_config['agent'].get('enable_thinking') and enable_thinking:
in_thinking = False
# 流式生成
async for chunk in llm_service.chat_stream(
messages=history,
provider_config=agent_config['provider'],
agent_config=agent_config['agent'],
enable_thinking=enable_thinking
):
chunk_type = chunk.get('type', 'content')
chunk_text = chunk.get('text', '')
if chunk_type == 'thinking':
if not in_thinking:
in_thinking = True
await websocket.send_json({ await websocket.send_json({
"type": "thinking_start", "type": "thinking_start",
"conversation_id": conversation_id "conversation_id": conversation_id
}) })
thinking_buffer += chunk_text
# 调用LLM非流式
response, thinking_content = await llm_service.chat(
messages=history,
provider_config=agent_config['provider'],
agent_config=agent_config['agent'],
enable_thinking=enable_thinking
)
# 发送思考内容
if thinking_content:
await websocket.send_json({ await websocket.send_json({
"type": "thinking_stream", "type": "thinking_content",
"conversation_id": conversation_id, "conversation_id": conversation_id,
"text": chunk_text "content": thinking_content
}) })
elif chunk_type == 'content':
if in_thinking: # 发送思考结束
in_thinking = False
await websocket.send_json({ await websocket.send_json({
"type": "thinking_end", "type": "thinking_end",
"conversation_id": conversation_id, "conversation_id": conversation_id,
"content": thinking_buffer # 发送完整思考内容用于折叠 "content": thinking_content
}) })
content_buffer += chunk_text
# 直接发送完整回复
await websocket.send_json({ await websocket.send_json({
"type": "content_stream", "type": "content_stream",
"conversation_id": conversation_id, "conversation_id": conversation_id,
"text": chunk_text "text": response
})
# 如果思考没结束就结束了,发送思考结束
if in_thinking:
await websocket.send_json({
"type": "thinking_end",
"conversation_id": conversation_id,
"content": thinking_buffer
}) })
# 发送完成通知 # 发送完成通知
@@ -721,9 +708,9 @@ async def websocket_endpoint(websocket: WebSocket, user_id: str):
assistant_msg = conv_service.add_message( assistant_msg = conv_service.add_message(
conversation_id=conversation.id, conversation_id=conversation.id,
role='assistant', role='assistant',
content=content_buffer, content=response,
source='web', source='web',
thinking_content=thinking_buffer if thinking_buffer else None, thinking_content=thinking_content if thinking_content else None,
agent_id=current_agent_id, agent_id=current_agent_id,
model_used=agent_config['provider'].get('default_model') model_used=agent_config['provider'].get('default_model')
) )
@@ -734,8 +721,8 @@ async def websocket_endpoint(websocket: WebSocket, user_id: str):
"message": { "message": {
"id": assistant_msg.id, "id": assistant_msg.id,
"role": "assistant", "role": "assistant",
"content": content_buffer, "content": response,
"thinking_content": thinking_buffer if thinking_buffer else None, "thinking_content": thinking_content if thinking_content else None,
"source": "web", "source": "web",
"agent_id": current_agent_id, "agent_id": current_agent_id,
"agent_name": agent_config['agent'].get('display_name'), "agent_name": agent_config['agent'].get('display_name'),