| | |
| | | import json |
| | | import re |
| | | import uuid |
| | | from copy import deepcopy |
| | | |
| | | from fastapi import WebSocket, WebSocketDisconnect, APIRouter, Depends |
| | | import asyncio |
| | |
| | | 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个字符,并去除首尾空白符 |
| | |
| | | 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" |
| | | |
| | |
| | | "conversation_id")) |
| | | node_list = [] |
| | | except Exception as e: |
| | | logger.error("保存dify的会话异常!") |
| | | logger.error("保存dcd ify的会话异常!") |
| | | logger.error(e) |
| | | try: |
| | | await websocket.send_json(result) |
| | |
| | | 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: |
| | |
| | | logger.error(e) |
| | | logger.error("返回客户端消息异常!") |
| | | complete_response = "" |
| | | |
| | | except json.JSONDecodeError as e: |
| | | print(f"Error decoding JSON: {e}") |
| | | # print(f"Response text: {text}") |
| | |
| | | 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"]: # 信息过滤 |