ソースを参照

dify images 问题

zhaoqingang 1 年間 前
コミット
56d8dce480
3 ファイル変更26 行追加13 行削除
  1. 8 2
      app/api/chat.py
  2. 15 9
      app/service/difyService.py
  3. 3 2
      app/task/fetch_agent.py

+ 8 - 2
app/api/chat.py

@@ -348,14 +348,20 @@ async def handle_client(websocket: WebSocket,
 
                                         result = {"message": answer_str, "type": "message"}
                                 elif data.get("event") == "message_end":
-                                    result = {"message": answer_str, "type": "close"}
+                                    message_files = []
+                                    res_msg = await dify_service.get_session_history(token, data.get("conversation_id"), str(current_user.id))
+                                    if len(res_msg) > 0:
+                                        message_files = res_msg[0].get("message_files")
+
+                                    result = {"message": answer_str, "type": "close", "message_files": message_files}
                                     try:
                                         SessionService(db).update_session(chat_id,
-                                                                          message={"role": "assistant", "content": data, "conversation_id": data.get("conversation_id")})
+                                                                          message={"role": "assistant", "content": answer_str},conversation_id=data.get("conversation_id"))
                                     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:

+ 15 - 9
app/service/difyService.py

@@ -28,8 +28,8 @@ class DifyService:
                 status_code=status.HTTP_401_UNAUTHORIZED,
                 detail="登录过期",
             )
-        if ret_code != 0:
-            return {}
+        # if ret_code != 0:
+        #     return {}
 
         # 检查返回的数据类型
         if isinstance(data.get("data"), dict):
@@ -107,16 +107,22 @@ class DifyService:
 
 
 
-    async def get_session_history(self, token: str, chat_id: str, is_all: int=0):
-        url = f"{self.base_url}/v1/conversation/get?conversation_id={chat_id}"
-        headers = {"Authorization": token}
+    async def get_session_history(self, token: str, conversation_id: str, user: str):
+        url = f"{self.base_url}/v1/messages"
+        params = {
+            'user': user,
+            'conversation_id': conversation_id
+        }
+        headers = {"Authorization": f'Bearer {token}'}
         async with httpx.AsyncClient() as client:
-            response = await client.get(url, headers=headers)
+            response = await client.get(url, params=params, headers=headers)
+            # print(response.text)
+            # print(response.status_code)
+            # print(response.res)
             data = self._handle_response(response)
             # print("----------------data----------------------:", data)
-            if is_all:
-                return data
-            return data.get("message", [])
+
+            return data
 
     async def upload(self, token: str, filename: str, file: bytes, user_id) -> dict:
         url = f"{self.base_url}/v1/files/upload"

+ 3 - 2
app/task/fetch_agent.py

@@ -116,8 +116,9 @@ def initialize_agents():
             ('da3451da89d911efb9490242ac190006', 3, '知识问答', 'RAGFLOW', 'knowledgeQA'),
             ('e96eb7a589db11ef87d20242ac190006', 5, '智能问答', 'RAGFLOW', 'chat'),
             ('basic_excel_talk', 6, '智能数据', 'BASIC', 'excelTalk'),
-            ('basic_question_talk', 7, '文档出卷', 'BASIC', 'questionTalk'),
-            ('9d75142a-66eb-4e23-b7d4-03efe4584915', 8, '小数绘图', 'DIFY', 'imageTalk')
+            ('basic_question_talk', 7, '出题组卷', 'BASIC', 'questionTalk'),
+            ('9d75142a-66eb-4e23-b7d4-03efe4584915', 8, '小数绘图', 'DIFY', 'imageTalk'),
+            ('basic_paper_talk', 8, '文档出卷', 'BASIC', 'paperTalk')
         ]
 
         for agent in initial_agents: