zhaoqingang
2025-02-10 3eabd0961bc63f08f10b4c582ed1c471df43a8c7
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
import json
 
from app.config.config import settings
# from Log import logger
from app.service.v2.app_driver.chat_base import ChatBase
from app.utils.rsa_crypto import RagflowCrypto
 
 
class ChatBaseApply(ChatBase):
 
    async def chat_get(self, url, params, headers):
 
        res = await self.http_get(url, params, headers)
        if res.status_code == 200:
            return res.json()
        else:
            return {}
 
    async def chat_ping(self, url, params, headers):
        res = await self.http_get(url, params, headers)
        if res.status_code != 200:
            return 0
        if res.json().get("code") == "unauthorized" or res.json().get("code") == 401:
            return 0
        return 200
 
 
    async def chat_post(self, url, data, headers):
 
        res = await self.http_post(url, data, headers)
        if res.status_code == 200 or res.status_code == 201:
            return res.json()
        else:
            return {}
 
    async def chat_login(self, url, data, headers):
 
        res = await self.http_post(url, data, headers)
        if res.status_code == 200:
            res_json = res.json()
            authorization = res.headers.get('Authorization')
            if authorization:
                res_json["data"]["access_token"] = authorization
            return res_json
        else:
            return {}
 
 
    @staticmethod
    async def password_encrypt(password):
        password = RagflowCrypto(settings.PUBLIC_KEY, settings.PRIVATE_KEY).encrypt(password)
        return password
 
    @staticmethod
    async def get_chat_headers(token):
        return {
            'Content-Type': 'application/json',
            'Authorization': token
        }
 
 
 
if __name__ == "__main__":
    async def aa():
        chat_id = "bcb56e4b-8f21-41f1-b22a-80335fe58345"
        token = "app-9sbGzhtFuGIducdepzQgX06v"
        base_url = "http://192.168.20.116"
        url = f"{base_url}/v1/parameters"
        chat = ChatBaseApply()
        data = {
            "question": "电网技术总结300字",
            "stream": True,
            "session_id": "9969c152cce411ef8a140242ac1b0002"
        }
        params = {
            "user": "1"
        }
        headers = {
            'Content-Type': 'application/json',
            'Authorization': f"Bearer {token}"
        }
        # ans = await chat.chat_parameters(url, params, headers)
        # print(ans)
 
        ping_url = "http://192.168.20.116:11080/v1/system/version"
        # ping_url = "http://192.168.20.119:13002/console/api/workspaces"
        user_token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiNjEzNzdiYzctZTViYy00YjhiLTgxYTYtNWZkOTVhODVlMmE4IiwiZXhwIjoxNzM5MjU3Njk1LCJpc3MiOiJTRUxGX0hPU1RFRCIsInN1YiI6IkNvbnNvbGUgQVBJIFBhc3Nwb3J0In0.w7xQrepd1dYR4iPXcbuthIZjdm45bTJFbolOM_SE9aQ"
        # token = "Bearer {}"
        token = "{}"
        res = await chat.chat_ping(ping_url, {}, await chat.get_chat_headers(token.format(user_token)))
        print(res)
 
    import asyncio
 
    asyncio.run(aa())