From f5f6ab74044362e44262c39b1b6e2a88a36e0f91 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期四, 17 十月 2024 11:11:10 +0800
Subject: [PATCH] ragflow发送消息前查找消息历史并放到消息里

---
 app/api/chat.py |   20 ++++++++++++--------
 1 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/app/api/chat.py b/app/api/chat.py
index 828b7e8..fa53467 100644
--- a/app/api/chat.py
+++ b/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)
 

--
Gitblit v1.8.0