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 |   23 ++++++++++++++++-------
 1 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/app/api/chat.py b/app/api/chat.py
index 6279b64..97c3006 100644
--- a/app/api/chat.py
+++ b/app/api/chat.py
@@ -297,7 +297,7 @@
                     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 @@
                     # 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 @@
                                 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:  # 姝e父杈撳嚭
@@ -345,16 +345,25 @@
                                             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"}
+                                    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:
                                     continue
                                 await websocket.send_json(result)

--
Gitblit v1.8.0