From b7ad7ae90cd832281afe6d743776ad87f1e56e07 Mon Sep 17 00:00:00 2001
From: zhaoqingang <zhaoqg0118@163.com>
Date: 星期五, 03 一月 2025 16:55:47 +0800
Subject: [PATCH] 工作流日志

---
 app/service/difyService.py |   93 +++++++++++++++++++++++++++++++++-------------
 1 files changed, 66 insertions(+), 27 deletions(-)

diff --git a/app/service/difyService.py b/app/service/difyService.py
index 43dff22..3731b51 100644
--- a/app/service/difyService.py
+++ b/app/service/difyService.py
@@ -6,7 +6,6 @@
 from fastapi import HTTPException
 from starlette import status
 
-from Log import logger
 from app.config.config import settings
 from app.utils.rsa_crypto import RagflowCrypto
 
@@ -28,8 +27,8 @@
                 status_code=status.HTTP_401_UNAUTHORIZED,
                 detail="鐧诲綍杩囨湡",
             )
-        if ret_code != 0:
-            return {}
+        # if ret_code != 0:
+        #     return {}
 
         # 妫�鏌ヨ繑鍥炵殑鏁版嵁绫诲瀷
         if isinstance(data.get("data"), dict):
@@ -66,28 +65,22 @@
                 raise Exception("Authorization header not found in response")
             return authorization
 
-    async def chat(self, token: str, user_id: int,  message: str, upload_file_id: str, conversation_id: str):
+    async def chat(self, token: str, user_id: int,  message: str, files: list, conversation_id: str, inputs:dict):
 
         target_url = f"{self.base_url}/v1/chat-messages"
-        files = []
-        if upload_file_id:
-            files = [
-                {
-                    "type": "image",
-                    "transfer_method": "local_file",
-                    "url": "",
-                    "upload_file_id": upload_file_id
-                }
-            ]
         data = {
-            "inputs": {},
+            "inputs": inputs,
             "query": message,
             "response_mode": "streaming",
-            "conversation_id": "",
+            "conversation_id": conversation_id,
             "user": str(user_id),
             "files": files
         }
-
+        # if parent_message_id:
+        # data["parent_message_id"] = parent_message_id
+        # print("----------------------------------------------")
+        # print(data)
+        # print("-----------------------------------------------")
         async with httpx.AsyncClient(timeout=300.0) as client:
             headers = {
                 'Content-Type': 'application/json',
@@ -107,16 +100,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"
@@ -135,14 +134,54 @@
 
             return data
 
+    async def save_images(self, url: str, filename: str):
+        url = f"{self.base_url}{url}"
+
+        async with httpx.AsyncClient() as client:
+            response = await client.get(url)
+
+            response.raise_for_status()
+
+            # 鎵撳紑涓�涓枃浠剁敤浜庡啓鍏�
+            with open(f"app/images/{filename}", 'wb') as f:
+                # 鍐欏叆璇锋眰鐨勫唴瀹�
+                f.write(response.content)
+
+
+    async def workflow(self, token: str, user_id: int,  inputs: dict):
+
+        target_url = f"{self.base_url}/v1/workflows/run"
+        data = {
+            "inputs": inputs,
+            "response_mode": "streaming",
+            "user": str(user_id),
+            "files":[]
+        }
+
+        async with httpx.AsyncClient(timeout=1800) as client:
+            headers = {
+                'Content-Type': 'application/json',
+                'Authorization': f'Bearer {token}'
+            }
+            async with client.stream("POST", target_url, data=json.dumps(data), headers=headers) as response:
+                if response.status_code == 200:
+                    try:
+                        async for answer in response.aiter_text():
+                            # print(f"response of ragflow chat: {answer}")
+                            yield answer
+                    except GeneratorExit as e:
+                        print(e)
+                        return
+                else:
+                    yield f"Error: {response.status_code}"
 
 
 
 if __name__ == "__main__":
     async def a():
-        a = DifyService("http://192.168.20.119:11080")
-        b = await a.get_knowledge_list("ImY3ZTZlZWQwYTY2NTExZWY5ZmFiMDI0MmFjMTMwMDA2Ig.Zzxwmw.uI_HAWzOkipQuga1aeQtoeIc3IM", 1,
-                                 10)
+        a = DifyService("http://192.168.20.116")
+        b = await a.get_session_history("app-YmOAMDsPpDDlqryMHnc9TzTO", "f94c6328-8ff0-4713-af3f-e823d547682d",
+                                 "63")
         print(b)
 
     import asyncio

--
Gitblit v1.8.0