zhaoqingang
2025-01-15 31f43e12d107aa0858aafeba58d3fc44cf2cf131
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,19 +712,24 @@
                                            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"
                                         # 正常输出
                                        answer = data.get("answer", "")
                                        result = {"message": answer, "type": "stream"}
                                    elif data.get("event") == "error":
                                        answer = data.get("message", "")
                                        result = {"message": answer, "type": "system"}
                                    elif data.get("event") == "workflow_finished":
                                        answer = data.get("data", "")
                                        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 +739,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 +753,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 +788,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 +804,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": "需要上传文档!", "type": "error"})
                            continue
                        try:
                            session = SessionService(db).create_session(
                                chat_id,
@@ -823,6 +831,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 +853,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"]:  # 信息过滤
@@ -867,6 +876,9 @@
                                         # 正常输出
                                        answer = data.get("answer", "")
                                        result = {"message": answer, "type": "stream"}
                                    elif data.get("event") == "error":
                                        answer = data.get("message", "")
                                        result = {"message": answer, "type": "system"}
                                    elif data.get("event") == "workflow_finished":
                                        answer = data.get("data", "")
                                        if isinstance(answer, str):
@@ -878,12 +890,12 @@
                                            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", "")
                                            # 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",
@@ -896,7 +908,7 @@
                                                logger.error("保存dify的会话异常!")
                                                logger.error(e)
                                            # await websocket.send_json(result)
                                        continue
                                        # continue
                                    elif data.get("event") == "message_end":
                                        result = {"message": "", "type": "close"}