zhangqian
2024-10-17 f5f6ab74044362e44262c39b1b6e2a88a36e0f91
app/api/chat.py
@@ -29,11 +29,13 @@
    agent = db.query(AgentModel).filter(AgentModel.id == agent_id).first()
    if not agent:
        ret = {"message": "Agent not found", "type": "close"}
        return websocket.send_json(ret)
        await websocket.send_json(ret)
        return
    agent_type = agent.agent_type
    if chat_id == "" or chat_id == "0":
        ret = {"message": "Chat ID not found", "type": "close"}
        return websocket.send_json(ret)
        await websocket.send_json(ret)
        return
    if agent_type == AgentType.RAGFLOW:
        ragflow_service = RagflowService(settings.ragflow_base_url)
@@ -45,12 +47,14 @@
                    print(f"Received from client {chat_id}: {message}")
                    chat_history = message.get('chatHistory', [])
                    if len(chat_history) == 0:
                        chat_history = await ragflow_service.set_session(token, agent_id, message["message"], chat_id, True)
                        chat_history = await ragflow_service.get_session_history(token, agent_id)
                        if len(chat_history) == 0:
                            result = {"message": "内部错误:创建会话失败", "type": "close"}
                            await websocket.send_json(result)
                            continue
                            chat_history = await ragflow_service.set_session(token, agent_id,
                                                                             message["message"], chat_id, True)
                            if len(chat_history) == 0:
                                result = {"message": "内部错误:创建会话失败", "type": "close"}
                                await websocket.send_json(result)
                                return
                    async for rag_response in ragflow_service.chat(token, chat_id, chat_history):
                        try:
                            print(f"Received from ragflow: {rag_response}")
@@ -143,5 +147,5 @@
                print(f"Client {chat_id} disconnected")
    else:
        ret = {"message": "Agent not found", "type": "close"}
        return websocket.send_json(ret)
        await websocket.send_json(ret)