From a015e3b2b83e26f3245ae5acfde6cfe20a31b447 Mon Sep 17 00:00:00 2001
From: zhaoqingang <zhaoqg0118@163.com>
Date: 星期二, 31 十二月 2024 15:43:13 +0800
Subject: [PATCH] 文档报告增加小标题功能

---
 app/service/difyService.py |  176 +++++++++++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 143 insertions(+), 33 deletions(-)

diff --git a/app/service/difyService.py b/app/service/difyService.py
index 9aee918..075cfaa 100644
--- a/app/service/difyService.py
+++ b/app/service/difyService.py
@@ -1,13 +1,15 @@
 import json
 from datetime import datetime
+from urllib.parse import urlparse, parse_qs
 
 import httpx
 from typing import Union, Dict, List
 from fastapi import HTTPException
 from starlette import status
 
-from Log import logger
+# from Log import logger
 from app.config.config import settings
+# from app.service.service_token import get_admin_token
 from app.utils.rsa_crypto import RagflowCrypto
 
 
@@ -39,51 +41,100 @@
         else:
             return data
 
-    async def register(self, username: str, password: str):
-        password = RagflowCrypto(settings.PUBLIC_KEY, settings.PRIVATE_KEY).encrypt(password)
+    async def register(self, username: str, password: str, token=None):
+        # if not token:
+        #     token = await get_admin_token()
+        email = f"{username}@basic.com"
+        invite_res = await self.invite_workspaces_member(token, [email], "admin")
+        # print(invite_res)
+        if invite_res.get("result") != "success" or not invite_res.get("invitation_results"):
+            # logger.error(invite_res)
+            return {}
+        invite_token = \
+        parse_qs(urlparse(invite_res.get("invitation_results")[0].get("url", "")).query).get('token', [None])[0]
+        # print(invite_token)
+        if not invite_token:
+            return {}
+        await self.login(username, password, True, invite_token)
+        activate_res = await self.activate(email, username, invite_token, "", "")
+        activate_res["email"] = email
+        activate_res["id"] = invite_token
+        return activate_res
+
+
+    async def invite_workspaces_member(self, token, emails: list, role: str):
         async with httpx.AsyncClient() as client:
             response = await client.post(
-                f"{self.base_url}/v1/user/register",
-                headers={'Content-Type': 'application/json'},
-                json={"nickname": username, "email":  f"{username}@example.com", "password": password}
+                f"{self.base_url}/console/api/workspaces/current/members/invite-email",
+                headers={'Content-Type': 'application/json',"Authorization": f'Bearer {token}'},
+                json={"emails": emails, "language":  "zh-Hans", "role": role}
             )
-            if response.status_code != 200:
-                raise Exception(f"Ragflow registration failed: {response.text}")
+            # print(response.text)
             return self._handle_response(response)
 
-    async def login(self, username: str, password: str) -> str:
-        password = RagflowCrypto(settings.PUBLIC_KEY, settings.PRIVATE_KEY).encrypt(password)
+    async def login(self, username: str, password: str, remember_me=True, invite_token:str="") -> str:
+        # password = RagflowCrypto(settings.PUBLIC_KEY, settings.PRIVATE_KEY).encrypt(password)
+        data = {"email": f"{username}@basic.com", "password": password, "remember_me": remember_me, "invite_token": invite_token,
+         "language": "zh-Hans"}
+
         async with httpx.AsyncClient() as client:
             response = await client.post(
-                f"{self.base_url}/v1/user/login",
+                f"{self.base_url}/console/api/login",
                 headers={'Content-Type': 'application/json'},
-                json={"email": f"{username}@example.com", "password": password}
+                json=data
             )
             if response.status_code != 200:
-                raise Exception(f"Ragflow login failed: {response.text}")
-            authorization = response.headers.get('Authorization')
-            if not authorization:
-                raise Exception("Authorization header not found in response")
-            return authorization
+                raise Exception(f"df login failed: {response.text}")
+            data = self._handle_response(response)
+            return data.get('access_token')
 
-    async def chat(self, token: str, user_id: int,  message: str, upload_file_id: str, conversation_id: str):
+    async def email_check(self, token, email: str):
+        async with httpx.AsyncClient() as client:
+            response = await client.get(
+                f"{self.base_url}/console/api/activate/check?email={email}&token={token}",
+                headers={'Content-Type': 'application/json'}
+            )
+            return self._handle_response(response)
+
+    async def activate(self, email: str, name: str, token, workspace_id:str, access_token) -> str:
+        # password = RagflowCrypto(settings.PUBLIC_KEY, settings.PRIVATE_KEY).encrypt(password)
+        data = {"email": email, "name": name, "token": token, #  "workspace_id": workspace_id,
+         "interface_language": "en-US", "timezone": "Asia/Shanghai"}
+        # print(data)
+        async with httpx.AsyncClient() as client:
+            response = await client.post(
+                f"{self.base_url}/console/api/activate",
+                headers={'Content-Type': 'application/json'},  # , 'Authorization': f'Bearer {access_token}'
+                json=data
+            )
+            if response.status_code != 200:
+                raise Exception(f"df login failed: {response.text}")
+            return self._handle_response(response)
+
+    async def invite_member_activate(self, token:str, email: str, name: str, password:str) -> str:
+        invite_res  =  await self.invite_workspaces_member(token, [email], "admin")
+        # print(invite_res)
+        if invite_res.get("result") != "success" or not invite_res.get("invitation_results"):
+            # logger.error(invite_res)
+            return {}
+        invite_token = parse_qs(urlparse(invite_res.get("invitation_results")[0].get("url", "")).query).get('token', [None])[0]
+        # print(invite_token)
+        if not invite_token:
+            return {}
+        await self.login(email, password, True, invite_token)
+        activate_res = await self.activate(email, name,invite_token, "", "")
+        return activate_res
+
+
+
+    async def chat(self, token: str, user_id: int,  message: str, files: [], 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
         }
@@ -141,14 +192,73 @@
 
             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.invite_workspaces_member("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiMjE0Y2I2ODctZTZlMC00ZTE2LWExNzUtYzcyNDNlMGRhMWEwIiwiZXhwIjoxNzMzNzI2NDA5LCJpc3MiOiJTRUxGX0hPU1RFRCIsInN1YiI6IkNvbnNvbGUgQVBJIFBhc3Nwb3J0In0.jLe1ODbcqCe79CDt6fFwnuuQL4I2FB9YTs9ynk4FeoQ", ["test05@163.com"],
+        #                          "admin")
+
+        # b = await a.email_check(
+        #     "ebd36739-0272-4b3f-95ab-0c6ac1639831",
+        #     "test05@163.com")
+        b = await a.login(
+            "zhao1234567",
+            "zhaoqg123456")
+
+
+
+        # b = await a.activate(
+        #     "test05@163.com",
+        #     "test05", "ebd36739-0272-4b3f-95ab-0c6ac1639831", "", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiNzg5OWUzOGQtNzczOS00NGNmLTgyODItZmFlMGZhNDJlZDYwIiwiZXhwIjoxNzMzNzI5NjQyLCJpc3MiOiJTRUxGX0hPU1RFRCIsInN1YiI6IkNvbnNvbGUgQVBJIFBhc3Nwb3J0In0.YMvypPnrvvUIfqzcESj820nP46IsFdTpF_YPz8_Exso")
+
+        # b = await a.invite_member_activate(
+        #     "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiMDE2NTcxNjAtZTllYi00NzVhLWIzMzYtZjlmZWJlY2I5YjczIiwiZXhwIjoxNzMzNzM0ODE0LCJpc3MiOiJTRUxGX0hPU1RFRCIsInN1YiI6IkNvbnNvbGUgQVBJIFBhc3Nwb3J0In0.khaXX3ndDe_pccEHcyTUcO2sgBCEfXCR74ZniP_b54Y",
+        #         "zhao1@df.com",
+        #          "zhao1Q",
+        # "ZHAOQG123456")
         print(b)
 
     import asyncio

--
Gitblit v1.8.0