From 536a8bbbe42c527ef9eaee9a8a8d2ee3615f5a11 Mon Sep 17 00:00:00 2001 From: zhaoqingang <zhaoqg0118@163.com> Date: 星期二, 14 一月 2025 16:19:04 +0800 Subject: [PATCH] 文档智能过程信息显示 --- app/api/chat.py | 30 ++++++++++++++++++------------ 1 files changed, 18 insertions(+), 12 deletions(-) diff --git a/app/api/chat.py b/app/api/chat.py index 15758a5..508f2f2 100644 --- a/app/api/chat.py +++ b/app/api/chat.py @@ -685,7 +685,7 @@ complete_response = "" async for rag_response in dify_service.chat(token, current_user.id, question, files, conversation_id, {}): - # print(rag_response) + print(rag_response) try: if rag_response[:5] == "data:": # 濡傛灉鏄紝鍒欐埅鍙栨帀鍓�5涓瓧绗︼紝骞跺幓闄ら灏剧┖鐧界 @@ -712,9 +712,11 @@ elif isinstance(answer, dict): message = answer.get("title", "") + if answer.get("status") == "failed": + message = answer.get("error") result = {"message": message, "type": "system"} - continue + # continue elif data.get("event") == "message": # "event": "message_end" # 姝e父杈撳嚭 answer = data.get("answer", "") @@ -724,7 +726,7 @@ if isinstance(answer, str): logger.error("----------------鏈煡鏁版嵁--------------------") logger.error(data) - result = {"message": "", "type": "close", "download_url": ""} + # result = {"message": "", "type": "close", "download_url": ""} elif isinstance(answer, dict): download_url = "" outputs = answer.get("outputs", {}) @@ -734,8 +736,8 @@ else: message = answer.get("error", "") - # result = {"message": message, "type": "message", - # "download_url": download_url} + result = {"message": message, "type": "system", + "download_url": download_url} try: SessionService(db).update_session(chat_id, message={"role": "assistant", @@ -748,7 +750,7 @@ logger.error("淇濆瓨dify鐨勪細璇濆紓甯革紒") logger.error(e) # await websocket.send_json(result) - continue + # continue elif data.get("event") == "message_end": result = {"message": "", "type": "close"} @@ -783,15 +785,15 @@ inputs["Question_Difficulty"] = receive_message["difficulty"] if "is_paper" in receive_message: inputs["Generate_test_paper"] = receive_message["is_paper"] - if "single_choices" in receive_message: + if "single_choice" in receive_message: inputs["Multiple_choice_questions"] = receive_message["single_choice"] if "gap_filling" in receive_message: inputs["Fill_in_blank"] = receive_message["gap_filling"] if "true_or_false" in receive_message: inputs["true_or_false"] = receive_message["true_or_false"] - if "multiple_choices" in receive_message: + if "multiple_choice" in receive_message: inputs["Multiple_Choice"] = receive_message["multiple_choice"] - if "easy_questions" in receive_message: + if "easy_question" in receive_message: inputs["Short_Answer_Questions"] = receive_message["easy_question"] if "case_questions" in receive_message: inputs["Case_Questions"] = receive_message["case_questions"] @@ -799,7 +801,10 @@ inputs["key_words"] = receive_message["key_words"] upload_files = receive_message.get('upload_files', []) question = receive_message.get('message', "") - + session_log = SessionService(db).get_session_by_id(chat_id) + if not session_log and not upload_files: + await websocket.send_json({"message": "闇�瑕佷笂浼犳枃妗o紒", "type": "error"}) + continue try: session = SessionService(db).create_session( chat_id, @@ -823,6 +828,7 @@ }) if files: inputs["upload_files"] = files + # print(inputs) if not question and not inputs: await websocket.send_json({"message": "Invalid request", "type": "error"}) continue @@ -844,7 +850,7 @@ complete_response += rag_response try: data = json.loads(complete_response) - print(data) + # print(data) if data.get("event") == "node_started" or data.get( "event") == "node_finished": # "event": "message_end" if "data" not in data or not data["data"]: # 淇℃伅杩囨护 @@ -878,7 +884,7 @@ outputs = answer.get("outputs", {}) if outputs: message = outputs.get("answer", "") - # download_url = outputs.get("download_url", "") + download_url = outputs.get("download_url", "") else: message = answer.get("error", "") -- Gitblit v1.8.0