zhangqian
2024-11-19 7305b7b9c88be497452e4dcf8b70decef0353bad
app/api/chat.py
@@ -14,6 +14,7 @@
from app.service.basic import BasicService
from app.service.ragflow import RagflowService
from app.service.service_token import get_bisheng_token, get_ragflow_token
from app.service.session import SessionService
router = APIRouter()
@@ -203,12 +204,17 @@
                # 接收前端消息
                message = await websocket.receive_json()
                question = message.get("message")
                SessionService(db).create_session(
                    session_id=chat_id,
                    name=question,
                    agent_id=agent_id,
                    agent_type=AgentType.BASIC
                )
                if not question:
                    await websocket.send_json({"message": "Invalid request", "type": "error"})
                    continue
                service = BasicService(base_url=settings.basic_base_url)
                complete_response = ""
                async for result in service.excel_talk(question, chat_id):
                    try:
                        if result[:5] == "data:":
@@ -217,13 +223,19 @@
                        else:
                            # 否则,保持原样
                            text = result
                        complete_response += text
                        try:
                            json_data = json.loads(complete_response)
                            output = json_data.get("output", "")
                            result = {"message": output, "type": "message"}
                            await websocket.send_json(result | json_data)
                            complete_response = ""
                            data = json.loads(text)
                            output = data.get("output", "")
                            excel_name = data.get("excel_name", "")
                            image_name = data.get("excel_name", "")
                            excel_url = None
                            image_url = None
                            if excel_name:
                                excel_url = f"/api/files/download/?agent_id=basic_excel_talk&file_id={excel_name}&file_type=excel"
                            if image_name:
                                image_url = f"/api/files/download/?agent_id=basic_excel_talk&file_id={image_name}&file_type=image"
                            result = {"message": output, "type": "message", "excel_url": excel_url, "image_url": image_url}
                            await websocket.send_json(result | data)
                        except json.JSONDecodeError as e:
                            print(f"Error decoding JSON: {e}")
                            print(f"Response text: {text}")