zhangqian
2024-10-14 5d535b127cffb34affcb5d2a15fb3ddd4aa7e5ba
处理异常返回错误
1个文件已修改
12 ■■■■ 已修改文件
app/api/chat.py 12 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/api/chat.py
@@ -51,14 +51,20 @@
                            print(f"Received from ragflow: {rag_response}")
                            json_str = rag_response[5:].strip()
                            json_data = json.loads(json_str)
                            if json_data.get("data") is not True:
                            data = json_data.get("data")
                            if data is True:  # 完成输出
                                result = {"message": "", "type": "close"}
                            elif data is None:  # 发生错误
                                answer = json_data.get("retmsg", json_data.get("retcode"))
                                result = {"message": "内部错误:" + answer, "type": "stream"}
                            else:  # 正常输出
                                answer = json_data.get("data", {}).get("answer", "")
                                result = {"message": answer, "type": "stream"}
                            else:
                                result = {"message": "", "type": "close"}
                            await websocket.send_json(result)
                            print(f"Forwarded to client {chat_id}: {result}")
                        except Exception as e:
                            result = {"message": f"内部错误: {e}", "type": "close"}
                            await websocket.send_json(result)
                            print(f"Error forwarding message to ragflow: {e}")
            # 启动任务处理客户端消息
            tasks = [