From a77073fec80037072df84fc6a58e0f3be7e43b14 Mon Sep 17 00:00:00 2001
From: zhangxiao <898441624@qq.com>
Date: 星期四, 17 十月 2024 17:19:31 +0800
Subject: [PATCH] 改为websocket 脚本运行。(功能有待优化)

---
 app/service/ragflow.py |   47 +++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 47 insertions(+), 0 deletions(-)

diff --git a/app/service/ragflow.py b/app/service/ragflow.py
index df131f1..e4d5657 100644
--- a/app/service/ragflow.py
+++ b/app/service/ragflow.py
@@ -59,3 +59,50 @@
                         return
                 else:
                     yield f"Error: {response.status_code}"
+
+    async def get_chat_sessions(self, token: str, dialog_id: str) -> list:
+        url = f"{self.base_url}/v1/conversation/list?dialog_id={dialog_id}"
+        headers = {
+            "Authorization": token
+        }
+        async with httpx.AsyncClient() as client:
+            response = await client.get(url, headers=headers)
+            if response.status_code != 200:
+                raise Exception(f"Failed to fetch data from Ragflow API: {response.text}")
+
+            data = response.json().get("data", [])
+            result = [
+                {
+                    "id": item["id"],
+                    "name": item["name"],
+                    "updated_time": item["update_time"]
+                }
+                for item in data
+            ]
+            return result
+
+    async def set_session(self, token: str, dialog_id: str, name: str, chat_id: str, is_new: bool) -> list:
+        url = f"{self.base_url}/v1/conversation/set?dialog_id={dialog_id}"
+        headers = {
+            "Authorization": token
+        }
+
+        data = {"dialog_id": dialog_id,
+                "name": name,
+                "is_new": is_new,
+                "conversation_id": chat_id,
+                }
+
+        async with httpx.AsyncClient() as client:
+            response = await client.post(url, headers=headers, json=data)
+            if response.status_code != 200:
+                return []
+            return [{
+                "content": "浣犲ソ锛� 鎴戞槸浣犵殑鍔╃悊锛屾湁浠�涔堝彲浠ュ府鍒颁綘鐨勫悧锛�",
+                "role": "assistant"
+            },
+                {
+                    "content": name,
+                    "doc_ids": [],
+                    "role": "user"
+                }]

--
Gitblit v1.8.0