zhaoqingang
2025-01-07 46e223745cebfedb78563ea3b671ec0f2cadbf0e
多余输出过滤
1个文件已修改
28 ■■■■ 已修改文件
app/api/chat.py 28 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/api/chat.py
@@ -1,6 +1,7 @@
import json
import re
import uuid
from copy import deepcopy
from fastapi import WebSocket, WebSocketDisconnect, APIRouter, Depends
import asyncio
@@ -531,7 +532,7 @@
                            workflow_dict = {}
                            if input["workflow_type"] == 1 or input["workflow_type"] == 2:
                                async for rag_response in dify_service.workflow(input["token"], current_user.id, i):
                                    # print(rag_response)
                                    print(rag_response)
                                    try:
                                        if rag_response[:5] == "data:":
                                            # 如果是,则截取掉前5个字符,并去除首尾空白符
@@ -544,7 +545,15 @@
                                        try:
                                            data = json.loads(complete_response)
                                            # print(data)
                                            node_list.append(data)
                                            node_data = deepcopy(data)
                                            if "data" in node_data:
                                                if "outputs" in node_data["data"]:
                                                    node_data["data"]["outputs"] = {}
                                                if "inputs" in node_data["data"]:
                                                    node_data["data"]["inputs"] = {}
                                            print(node_data)
                                            node_list.append(node_data)
                                            complete_response = ""
                                            if data.get("event") == "node_started":  # "event": "message_end"
@@ -615,7 +624,7 @@
                                                                                              "conversation_id"))
                                                        node_list = []
                                                    except Exception as e:
                                                        logger.error("保存dify的会话异常!")
                                                        logger.error("保存dcd ify的会话异常!")
                                                        logger.error(e)
                                                    try:
                                                        await websocket.send_json(result)
@@ -623,7 +632,7 @@
                                                        logger.error(e)
                                                        logger.error("返回客户端消息异常!")
                                                    result = {"message": "", "type": "close", "workflow": workflow_dict, "is_next": is_next}
                                                    result = {"message": "", "type": "close", "workflow": workflow_dict, "is_next": is_next, "download_url": download_url}
                                            else:
@@ -634,6 +643,7 @@
                                                logger.error(e)
                                                logger.error("返回客户端消息异常!")
                                            complete_response = ""
                                        except json.JSONDecodeError as e:
                                            print(f"Error decoding JSON: {e}")
                                            # print(f"Response text: {text}")
@@ -660,8 +670,14 @@
                                            complete_response += rag_response
                                        try:
                                            data = json.loads(complete_response)
                                            node_list.append(data)
                                            # print(data)
                                            node_data = deepcopy(data)
                                            if "data" in node_data:
                                                if "outputs" in node_data["data"]:
                                                    node_data["data"]["outputs"] = {}
                                                if "inputs" in node_data["data"]:
                                                    node_data["data"]["inputs"] = {}
                                            print(node_data)
                                            node_list.append(node_data)
                                            complete_response = ""
                                            if data.get("event") == "node_started":  # "event": "message_end"
                                                if "data" not in data or not data["data"]:  # 信息过滤