功能: 1. 当发生错误时,不再只显示错误信息,而是提供友好的兜底回复 2. 兜底回复包含: - 自我介绍(介绍AI助手的功能) - 红色突出显示的错误信息(使用 diff 语法) - 如果是超时/不可用错误,提示用户手动切换模型 3. 同时支持流式和非流式接口的兜底机制 4. 流式接口使用打字机效果显示兜底回复
This commit is contained in:
@@ -151,6 +151,8 @@ async def chat_endpoint(
|
|||||||
raise HTTPException(status_code=400, detail="message required")
|
raise HTTPException(status_code=400, detail="message required")
|
||||||
|
|
||||||
thread_id = request.thread_id or str(uuid.uuid4())
|
thread_id = request.thread_id or str(uuid.uuid4())
|
||||||
|
|
||||||
|
try:
|
||||||
result = await agent_service.process_message(
|
result = await agent_service.process_message(
|
||||||
request.message, thread_id, request.model, request.user_id
|
request.message, thread_id, request.model, request.user_id
|
||||||
)
|
)
|
||||||
@@ -172,6 +174,39 @@ async def chat_endpoint(
|
|||||||
total_tokens=input_tokens + output_tokens,
|
total_tokens=input_tokens + output_tokens,
|
||||||
elapsed_time=elapsed_time
|
elapsed_time=elapsed_time
|
||||||
)
|
)
|
||||||
|
except Exception as e:
|
||||||
|
error(f"同步响应异常: {e}")
|
||||||
|
|
||||||
|
# === 兜底输出机制 ===
|
||||||
|
error_message = str(e)
|
||||||
|
is_timeout_error = any(keyword in error_message.lower() for keyword in
|
||||||
|
["timeout", "timed out", "超时", "connection", "unavailable", "不可用"])
|
||||||
|
|
||||||
|
# 1. 自我介绍
|
||||||
|
intro_text = "你好!我是 AI 智能助手,我可以帮你处理各种问题,包括查询通讯录、词典翻译、新闻分析、知识库检索、联网搜索等。\n\n"
|
||||||
|
|
||||||
|
# 2. 错误信息(红色突出)
|
||||||
|
error_display = f"**⚠️ 当前遇到问题**\n\n```diff\n- {error_message}\n```\n\n"
|
||||||
|
|
||||||
|
# 3. 模型切换提示(如果是超时/不可用错误)
|
||||||
|
switch_hint = ""
|
||||||
|
if is_timeout_error:
|
||||||
|
switch_hint = "💡 **提示**:当前模型可能响应超时或不可用,请尝试手动切换到其他模型(如 DeepSeek、智谱AI等)。\n\n"
|
||||||
|
|
||||||
|
# 4. 组合完整兜底回复
|
||||||
|
fallback_text = intro_text + error_display + switch_hint
|
||||||
|
|
||||||
|
actual_model = request.model if request.model in agent_service.graphs else next(iter(agent_service.graphs.keys()))
|
||||||
|
|
||||||
|
return ChatResponse(
|
||||||
|
reply=fallback_text,
|
||||||
|
thread_id=thread_id,
|
||||||
|
model_used=actual_model,
|
||||||
|
input_tokens=0,
|
||||||
|
output_tokens=0,
|
||||||
|
total_tokens=0,
|
||||||
|
elapsed_time=0.0
|
||||||
|
)
|
||||||
|
|
||||||
# ========== 历史查询接口 ==========
|
# ========== 历史查询接口 ==========
|
||||||
@app.get("/threads")
|
@app.get("/threads")
|
||||||
@@ -225,7 +260,34 @@ async def chat_stream_endpoint(
|
|||||||
yield "data: [DONE]\n\n"
|
yield "data: [DONE]\n\n"
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
error(f"流式响应异常: {e}")
|
error(f"流式响应异常: {e}")
|
||||||
yield f"data: {json.dumps({'type': 'error', 'message': str(e)}, ensure_ascii=False)}\n\n"
|
|
||||||
|
# === 兜底输出机制 ===
|
||||||
|
error_message = str(e)
|
||||||
|
is_timeout_error = any(keyword in error_message.lower() for keyword in
|
||||||
|
["timeout", "timed out", "超时", "connection", "unavailable", "不可用"])
|
||||||
|
|
||||||
|
# 1. 自我介绍
|
||||||
|
intro_text = "你好!我是 AI 智能助手,我可以帮你处理各种问题,包括查询通讯录、词典翻译、新闻分析、知识库检索、联网搜索等。\n\n"
|
||||||
|
|
||||||
|
# 2. 错误信息(红色突出)
|
||||||
|
error_display = f"**⚠️ 当前遇到问题**\n\n```diff\n- {error_message}\n```\n\n"
|
||||||
|
|
||||||
|
# 3. 模型切换提示(如果是超时/不可用错误)
|
||||||
|
switch_hint = ""
|
||||||
|
if is_timeout_error:
|
||||||
|
switch_hint = "💡 **提示**:当前模型可能响应超时或不可用,请尝试手动切换到其他模型(如 DeepSeek、智谱AI等)。\n\n"
|
||||||
|
|
||||||
|
# 4. 组合完整兜底回复
|
||||||
|
fallback_text = intro_text + error_display + switch_hint
|
||||||
|
|
||||||
|
# 5. 以 llm_token 方式发送兜底回复,模拟打字机效果
|
||||||
|
for char in fallback_text:
|
||||||
|
yield f"data: {json.dumps({'type': 'llm_token', 'node': 'fallback', 'token': char}, ensure_ascii=False)}\n\n"
|
||||||
|
import asyncio
|
||||||
|
await asyncio.sleep(0.01)
|
||||||
|
|
||||||
|
# 6. 发送错误事件
|
||||||
|
yield f"data: {json.dumps({'type': 'error', 'message': error_message}, ensure_ascii=False)}\n\n"
|
||||||
yield "data: [DONE]\n\n"
|
yield "data: [DONE]\n\n"
|
||||||
|
|
||||||
return StreamingResponse(
|
return StreamingResponse(
|
||||||
|
|||||||
Reference in New Issue
Block a user