m
zhaoqingang
2024-12-09 52ba4076f5ad55fdf3239a33a2a376eaa0e0dea5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
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 app.config.config import settings
from app.utils.rsa_crypto import RagflowCrypto
 
 
class DifyService:
    def __init__(self, base_url: str):
        self.base_url = base_url
 
    def _handle_response(self, response: httpx.Response) -> Union[Dict, List]:
        if response.status_code != 200:
            if response.status_code == 201:
                return response.json()
            return {}
 
        data = response.json()
        ret_code = data.get("retcode")
        if ret_code == 401:
            raise HTTPException(
                status_code=status.HTTP_401_UNAUTHORIZED,
                detail="登录过期",
            )
        # if ret_code != 0:
        #     return {}
 
        # 检查返回的数据类型
        if isinstance(data.get("data"), dict):
            return data.get("data", {})
        elif isinstance(data.get("data"), list):
            return data.get("data", [])
        else:
            return data
 
    async def register(self, username: str, password: str, token=None):
        if not token:
            token = await get_df_token()
        email = f"{username}@df.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(email, 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}/console/api/workspaces/current/members/invite-email",
                headers={'Content-Type': 'application/json',"Authorization": f'Bearer {token}'},
                json={"emails": emails, "language":  "zh-Hans", "role": role}
            )
            print(response.text)
            return self._handle_response(response)
 
    async def login(self, email: str, password: str, remember_me,invite_token:str="") -> str:
        # password = RagflowCrypto(settings.PUBLIC_KEY, settings.PRIVATE_KEY).encrypt(password)
        data = {"email": email, "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}/console/api/login",
                headers={'Content-Type': 'application/json'},
                json=data
            )
            if response.status_code != 200:
                raise Exception(f"df login failed: {response.text}")
            return self._handle_response(response)
 
    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, upload_file_id: str, conversation_id: str):
 
        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": {},
            "query": message,
            "response_mode": "streaming",
            "conversation_id": conversation_id,
            "user": str(user_id),
            "files": files
        }
 
        async with httpx.AsyncClient(timeout=300.0) 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}"
 
 
 
    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, params=params, headers=headers)
            # print(response.text)
            # print(response.status_code)
            # print(response.res)
            data = self._handle_response(response)
            # print("----------------data----------------------:", data)
 
            return data
 
    async def upload(self, token: str, filename: str, file: bytes, user_id) -> dict:
        url = f"{self.base_url}/v1/files/upload"
        headers = {
            # 'Content-Type': 'application/json',
            'Authorization': f'Bearer {token}'
        }
        data = {
            'user': str(user_id)
        }
        # 创建表单数据,包含文件
        files = {"file": (filename, file)}
        async with httpx.AsyncClient() as client:
            response = await client.post(url, headers=headers, files=files, data=data)
            data = self._handle_response(response)
 
            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.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(
        #     "test05@163.com",
        #     "zhaoqg123456",
        #     True, "ebd36739-0272-4b3f-95ab-0c6ac1639831")
 
 
 
        # 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
 
    asyncio.run(a())