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 |   47 ++++++++++++++++++++++++++++++++++-------------
 1 files changed, 34 insertions(+), 13 deletions(-)

diff --git a/app/service/difyService.py b/app/service/difyService.py
index 78daa44..3731b51 100644
--- a/app/service/difyService.py
+++ b/app/service/difyService.py
@@ -65,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,
             "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',
@@ -154,6 +148,33 @@
                 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__":

--
Gitblit v1.8.0