Преглед изворни кода

ragflow和bisheng的会话时间统一

zhaoqingang пре 1 година
родитељ
комит
c795d4b39c
6 измењених фајлова са 17 додато и 19 уклоњено
  1. 8 5
      app/api/chat.py
  2. 2 2
      app/api/files.py
  3. 1 1
      app/models/dialog_model.py
  4. 1 1
      app/service/bisheng.py
  5. 4 9
      app/service/difyService.py
  6. 1 1
      app/service/ragflow.py

+ 8 - 5
app/api/chat.py

@@ -297,7 +297,7 @@ async def handle_client(websocket: WebSocket,
                     conversation_id = ""
                     receive_message = await websocket.receive_json()
                     print(f"Received from client {chat_id}: {receive_message}")
-                    upload_file_id = receive_message.get('upload_file_id', [])
+                    upload_file_id = receive_message.get('upload_file_id', "")
                     question = receive_message.get('message', "")
                     if not question and not image_url:
                         await websocket.send_json({"message": "Invalid request", "type": "error"})
@@ -316,8 +316,8 @@ async def handle_client(websocket: WebSocket,
                     # complete_response = ""
                     answer_str = ""
                     async for rag_response in dify_service.chat(token, current_user.id, question, upload_file_id, conversation_id):
-                        print("=============================================")
-                        print(rag_response)
+                        # print("=============================================")
+                        # print(rag_response)
                         try:
                             if rag_response[:5] == "data:":
                                 # 如果是,则截取掉前5个字符,并去除首尾空白符
@@ -333,7 +333,7 @@ async def handle_client(websocket: WebSocket,
                                 if data.get("event") == "agent_message":# "event": "message_end"
                                     if "answer" not in  data or not data["answer"]:  # 信息过滤
                                         logger.error("非法数据--------------------")
-                                        logger.error(data)
+                                        # logger.error(data)
 
                                         continue
                                     else:  # 正常输出
@@ -345,8 +345,8 @@ async def handle_client(websocket: WebSocket,
                                             logger.error("未知数据体:0---------------------------------")
                                             logger.error(answer)
                                             answer_str += answer.get("action_input", "")
-                                        continue
 
+                                        result = {"message": answer_str, "type": "message"}
                                 elif data.get("event") == "message_end":
                                     result = {"message": answer_str, "type": "close"}
                                     try:
@@ -355,6 +355,9 @@ async def handle_client(websocket: WebSocket,
                                     except Exception as e:
                                         logger.error("保存dify的会话异常!")
                                         logger.error(e)
+                                elif data.get("event") == "message_file":
+                                    url = data.get("url", "")
+                                    result = {"message": url, "type": "image"}
                                 else:
                                     continue
                                 await websocket.send_json(result)

+ 2 - 2
app/api/files.py

@@ -103,11 +103,11 @@ async def upload_files(
             return Response(code=400, msg=str(e))
         dify_service = DifyService(base_url=settings.dify_base_url)
         try:
-            token = get_bisheng_token(db, current_user.id)
+            token = settings.dify_api_token
             result = await dify_service.upload(token, file.filename, file_content, current_user.id)
         except Exception as e:
             raise HTTPException(status_code=500, detail=str(e))
-        result["file_name"] = file.filename
+        # result["file_name"] = file.filename
         return Response(code=200, msg="", data=result)
 
         return Response(code=200, msg="", data=result)

+ 1 - 1
app/models/dialog_model.py

@@ -57,6 +57,6 @@ class ConversationModel(Base):
         return {
             'id': self.id,
             'updated_time': self.update_time,
-            'update_date': self.update_date.strftime('%Y-%m-%d %H:%M:%S'),
+            'update_date': datetime.fromtimestamp(self.update_time / 1000.0).strftime('%Y-%m-%d %H:%M:%S'),
             'name': self.name,
         }

+ 1 - 1
app/service/bisheng.py

@@ -94,7 +94,7 @@ class BishengService:
                 {
                     "id": item["chat_id"],
                     "name": process_name(item),
-                    "update_date": item["update_time"],
+                    "update_date": item["update_time"].replace("T", " "),
                     "updated_time": int(datetime.strptime(item["update_time"], "%Y-%m-%dT%H:%M:%S").timestamp() * 1000)
                 }
                 for item in data

+ 4 - 9
app/service/difyService.py

@@ -17,6 +17,8 @@ class DifyService:
 
     def _handle_response(self, response: httpx.Response) -> Union[Dict, List]:
         if response.status_code != 200:
+            if response.status_code == 201:
+                return response.json()
             return {}
 
         data = response.json()
@@ -35,7 +37,7 @@ class DifyService:
         elif isinstance(data.get("data"), list):
             return data.get("data", [])
         else:
-            return {}
+            return data
 
     async def register(self, username: str, password: str):
         password = RagflowCrypto(settings.PUBLIC_KEY, settings.PRIVATE_KEY).encrypt(password)
@@ -130,15 +132,8 @@ class DifyService:
         async with httpx.AsyncClient() as client:
             response = await client.post(url, headers=headers, files=files, data=data)
             data = self._handle_response(response)
-            logger.error("----------------------------ffff-------------------------------")
-            logger.error(response.status_code)
-            logger.error(response.text)
-            # file_path = data.get("file_path", "")
-            result = {
-                "file_path": data
-            }
 
-            return result
+            return data
 
 
 

+ 1 - 1
app/service/ragflow.py

@@ -100,7 +100,7 @@ class RagflowService:
                     "id": item["id"],
                     "name": item["name"],
                     "updated_time": item["update_time"],
-                    "update_date": datetime.utcfromtimestamp(item["update_time"] / 1000.0).strftime('%Y-%m-%d %H:%M:%S')
+                    "update_date": datetime.fromtimestamp(item["update_time"] / 1000.0).strftime('%Y-%m-%d %H:%M:%S')
                 }
                 for item in data
                 if "id" in item and "name" in item and item["name"]