fix: 暂时使用非流式输出确保稳定性
This commit is contained in:
77
main_v2.py
77
main_v2.py
@@ -659,58 +659,45 @@ 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
|
await websocket.send_json({
|
||||||
|
"type": "thinking_start",
|
||||||
|
"conversation_id": conversation_id
|
||||||
|
})
|
||||||
|
|
||||||
# 流式生成
|
# 调用LLM(非流式)
|
||||||
async for chunk in llm_service.chat_stream(
|
response, thinking_content = await llm_service.chat(
|
||||||
messages=history,
|
messages=history,
|
||||||
provider_config=agent_config['provider'],
|
provider_config=agent_config['provider'],
|
||||||
agent_config=agent_config['agent'],
|
agent_config=agent_config['agent'],
|
||||||
enable_thinking=enable_thinking
|
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({
|
|
||||||
"type": "thinking_start",
|
|
||||||
"conversation_id": conversation_id
|
|
||||||
})
|
|
||||||
thinking_buffer += chunk_text
|
|
||||||
await websocket.send_json({
|
|
||||||
"type": "thinking_stream",
|
|
||||||
"conversation_id": conversation_id,
|
|
||||||
"text": chunk_text
|
|
||||||
})
|
|
||||||
elif chunk_type == 'content':
|
|
||||||
if in_thinking:
|
|
||||||
in_thinking = False
|
|
||||||
await websocket.send_json({
|
|
||||||
"type": "thinking_end",
|
|
||||||
"conversation_id": conversation_id,
|
|
||||||
"content": thinking_buffer # 发送完整思考内容用于折叠
|
|
||||||
})
|
|
||||||
content_buffer += chunk_text
|
|
||||||
await websocket.send_json({
|
|
||||||
"type": "content_stream",
|
|
||||||
"conversation_id": conversation_id,
|
|
||||||
"text": chunk_text
|
|
||||||
})
|
|
||||||
|
|
||||||
# 如果思考没结束就结束了,发送思考结束
|
# 发送思考内容
|
||||||
if in_thinking:
|
if thinking_content:
|
||||||
await websocket.send_json({
|
await websocket.send_json({
|
||||||
"type": "thinking_end",
|
"type": "thinking_content",
|
||||||
"conversation_id": conversation_id,
|
"conversation_id": conversation_id,
|
||||||
"content": thinking_buffer
|
"content": thinking_content
|
||||||
})
|
})
|
||||||
|
|
||||||
|
# 发送思考结束
|
||||||
|
await websocket.send_json({
|
||||||
|
"type": "thinking_end",
|
||||||
|
"conversation_id": conversation_id,
|
||||||
|
"content": thinking_content
|
||||||
|
})
|
||||||
|
|
||||||
|
# 直接发送完整回复
|
||||||
|
await websocket.send_json({
|
||||||
|
"type": "content_stream",
|
||||||
|
"conversation_id": conversation_id,
|
||||||
|
"text": response
|
||||||
|
})
|
||||||
|
|
||||||
# 发送完成通知
|
# 发送完成通知
|
||||||
await websocket.send_json({
|
await websocket.send_json({
|
||||||
"type": "stream_end",
|
"type": "stream_end",
|
||||||
@@ -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'),
|
||||||
|
|||||||
Reference in New Issue
Block a user