From 56d8dce48022692c5a7541114ef84f4fa395ff9d Mon Sep 17 00:00:00 2001
From: zhaoqingang <zhaoqg0118@163.com>
Date: 星期二, 26 十一月 2024 11:22:49 +0800
Subject: [PATCH] dify images 问题

---
 app/api/chat.py            |   10 ++++++++--
 app/task/fetch_agent.py    |    5 +++--
 app/service/difyService.py |   24 +++++++++++++++---------
 3 files changed, 26 insertions(+), 13 deletions(-)

diff --git a/app/api/chat.py b/app/api/chat.py
index 9f2ea44..97c3006 100644
--- a/app/api/chat.py
+++ b/app/api/chat.py
@@ -348,14 +348,20 @@
 
                                         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:
diff --git a/app/service/difyService.py b/app/service/difyService.py
index 43dff22..9aee918 100644
--- a/app/service/difyService.py
+++ b/app/service/difyService.py
@@ -28,8 +28,8 @@
                 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 @@
 
 
 
-    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"
diff --git a/app/task/fetch_agent.py b/app/task/fetch_agent.py
index 80ac763..f9aed22 100644
--- a/app/task/fetch_agent.py
+++ b/app/task/fetch_agent.py
@@ -116,8 +116,9 @@
             ('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:

--
Gitblit v1.8.0