Hansimov commited on
Commit
51e07f2
1 Parent(s): dc3b7a9

:gem: [Feature] Enable get models of chat.openai.com

Browse files
Files changed (3) hide show
  1. requirements.txt +2 -0
  2. tests/__init__.py +0 -0
  3. tests/openai.py +62 -0
requirements.txt CHANGED
@@ -1,4 +1,5 @@
1
  aiohttp
 
2
  fastapi
3
  httpx
4
  jinja2
@@ -8,6 +9,7 @@ pydantic
8
  requests
9
  sse_starlette
10
  termcolor
 
11
  tiktoken
12
  transformers
13
  uvicorn
 
1
  aiohttp
2
+ curl_cffi
3
  fastapi
4
  httpx
5
  jinja2
 
9
  requests
10
  sse_starlette
11
  termcolor
12
+ tclogger
13
  tiktoken
14
  transformers
15
  uvicorn
tests/__init__.py ADDED
File without changes
tests/openai.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import uuid
2
+
3
+ from pathlib import Path
4
+
5
+ from curl_cffi import requests
6
+ from tclogger import logger, OSEnver
7
+
8
+ secrets_path = Path(__file__).parents[1] / "secrets.json"
9
+ ENVER = OSEnver(secrets_path)
10
+
11
+
12
+ class OpenaiAPI:
13
+ def __init__(self):
14
+ self.api_me = "https://chat.openai.com/backend-anon/me"
15
+ self.api_models = "https://chat.openai.com/backend-anon/models"
16
+
17
+ def auth(self):
18
+ http_proxy = ENVER["http_proxy"]
19
+ if http_proxy:
20
+ logger.note(f"> Using Proxy: {http_proxy}")
21
+ requests_proxies = {
22
+ "http": http_proxy,
23
+ "https": http_proxy,
24
+ }
25
+ uuid_str = str(uuid.uuid4())
26
+ requests_headers = {
27
+ "Accept": "*/*",
28
+ "Accept-Encoding": "gzip, deflate, br, zstd",
29
+ "Accept-Language": "en-US,en;q=0.9",
30
+ "Cache-Control": "no-cache",
31
+ "Oai-Device-Id": uuid_str,
32
+ "Oai-Language": "en-US",
33
+ "Pragma": "no-cache",
34
+ "Referer": "https://chat.openai.com/",
35
+ "Sec-Ch-Ua": 'Google Chrome";v="123", "Not:A-Brand";v="8", "Chromium";v="123"',
36
+ "Sec-Ch-Ua-Mobile": "?0",
37
+ "Sec-Ch-Ua-Platform": '"Windows"',
38
+ "Sec-Fetch-Dest": "empty",
39
+ "Sec-Fetch-Mode": "cors",
40
+ "Sec-Fetch-Site": "same-origin",
41
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36",
42
+ }
43
+
44
+ logger.note(f"> Get: {self.api_models}")
45
+
46
+ res = requests.get(
47
+ self.api_models,
48
+ headers=requests_headers,
49
+ proxies=requests_proxies,
50
+ timeout=10,
51
+ impersonate="chrome120",
52
+ )
53
+
54
+ logger.warn(res.status_code)
55
+ logger.mesg(res.json())
56
+
57
+
58
+ if __name__ == "__main__":
59
+ api = OpenaiAPI()
60
+ api.auth()
61
+
62
+ # python -m tests.openai