deeme commited on
Commit
1271bf4
·
verified ·
1 Parent(s): b226181

Upload 39 files

Browse files
.env ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ UPLOAD_BY_URL=true
2
+ TZ=Asia/Shanghai
3
+ ENABLE_GATEWAY=true
4
+ AUTO_SEED=false
Dockerfile CHANGED
@@ -1,8 +1,11 @@
1
- FROM lanqian528/chat2api:latest
2
 
3
- # 创建应用程序所需的目录,并赋予必要权限
4
- RUN mkdir -p /app/data && chmod -R 777 /app/data
 
 
5
 
6
- ENV TZ=Asia/Shanghai
7
- ENV UPLOAD_BY_URL=true
8
- ENV ENABLE_GATEWAY=false
 
 
1
+ FROM python:3.11-slim
2
 
3
+ WORKDIR /app
4
+ COPY . /app
5
+ RUN pip install --no-cache-dir -r requirements.txt
6
+ EXPOSE 5005
7
 
8
+ RUN mkdir /app/data
9
+ RUN chmod -R 777 /app/data
10
+
11
+ CMD ["python", "app.py"]
api/chat2api.py ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import asyncio
2
+ import types
3
+
4
+ from apscheduler.schedulers.asyncio import AsyncIOScheduler
5
+ from fastapi import Request, HTTPException, Form, Security
6
+ from fastapi.responses import HTMLResponse, StreamingResponse, JSONResponse
7
+ from fastapi.security import HTTPAuthorizationCredentials
8
+ from starlette.background import BackgroundTask
9
+
10
+ import utils.globals as globals
11
+ from app import app, templates, security_scheme
12
+ from chatgpt.ChatService import ChatService
13
+ from chatgpt.authorization import refresh_all_tokens
14
+ from utils.Logger import logger
15
+ from utils.configs import api_prefix, scheduled_refresh
16
+ from utils.retry import async_retry
17
+
18
+ scheduler = AsyncIOScheduler()
19
+
20
+
21
+ @app.on_event("startup")
22
+ async def app_start():
23
+ if scheduled_refresh:
24
+ scheduler.add_job(id='refresh', func=refresh_all_tokens, trigger='cron', hour=3, minute=0, day='*/2',
25
+ kwargs={'force_refresh': True})
26
+ scheduler.start()
27
+ asyncio.get_event_loop().call_later(0, lambda: asyncio.create_task(refresh_all_tokens(force_refresh=False)))
28
+
29
+
30
+ async def to_send_conversation(request_data, req_token):
31
+ chat_service = ChatService(req_token)
32
+ try:
33
+ await chat_service.set_dynamic_data(request_data)
34
+ await chat_service.get_chat_requirements()
35
+ return chat_service
36
+ except HTTPException as e:
37
+ await chat_service.close_client()
38
+ raise HTTPException(status_code=e.status_code, detail=e.detail)
39
+ except Exception as e:
40
+ await chat_service.close_client()
41
+ logger.error(f"Server error, {str(e)}")
42
+ raise HTTPException(status_code=500, detail="Server error")
43
+
44
+
45
+ async def process(request_data, req_token):
46
+ chat_service = await to_send_conversation(request_data, req_token)
47
+ await chat_service.prepare_send_conversation()
48
+ res = await chat_service.send_conversation()
49
+ return chat_service, res
50
+
51
+
52
+ @app.post(f"/{api_prefix}/v1/chat/completions" if api_prefix else "/v1/chat/completions")
53
+ async def send_conversation(request: Request, credentials: HTTPAuthorizationCredentials = Security(security_scheme)):
54
+ req_token = credentials.credentials
55
+ try:
56
+ request_data = await request.json()
57
+ except Exception:
58
+ raise HTTPException(status_code=400, detail={"error": "Invalid JSON body"})
59
+ chat_service, res = await async_retry(process, request_data, req_token)
60
+ try:
61
+ if isinstance(res, types.AsyncGeneratorType):
62
+ background = BackgroundTask(chat_service.close_client)
63
+ return StreamingResponse(res, media_type="text/event-stream", background=background)
64
+ else:
65
+ background = BackgroundTask(chat_service.close_client)
66
+ return JSONResponse(res, media_type="application/json", background=background)
67
+ except HTTPException as e:
68
+ await chat_service.close_client()
69
+ if e.status_code == 500:
70
+ logger.error(f"Server error, {str(e)}")
71
+ raise HTTPException(status_code=500, detail="Server error")
72
+ raise HTTPException(status_code=e.status_code, detail=e.detail)
73
+ except Exception as e:
74
+ await chat_service.close_client()
75
+ logger.error(f"Server error, {str(e)}")
76
+ raise HTTPException(status_code=500, detail="Server error")
77
+
78
+
79
+ @app.get(f"/{api_prefix}/tokens" if api_prefix else "/tokens", response_class=HTMLResponse)
80
+ async def upload_html(request: Request):
81
+ tokens_count = len(set(globals.token_list) - set(globals.error_token_list))
82
+ return templates.TemplateResponse("tokens.html",
83
+ {"request": request, "api_prefix": api_prefix, "tokens_count": tokens_count})
84
+
85
+
86
+ @app.post(f"/{api_prefix}/tokens/upload" if api_prefix else "/tokens/upload")
87
+ async def upload_post(text: str = Form(...)):
88
+ lines = text.split("\n")
89
+ for line in lines:
90
+ if line.strip() and not line.startswith("#"):
91
+ globals.token_list.append(line.strip())
92
+ with open(globals.TOKENS_FILE, "a", encoding="utf-8") as f:
93
+ f.write(line.strip() + "\n")
94
+ logger.info(f"Token count: {len(globals.token_list)}, Error token count: {len(globals.error_token_list)}")
95
+ tokens_count = len(set(globals.token_list) - set(globals.error_token_list))
96
+ return {"status": "success", "tokens_count": tokens_count}
97
+
98
+
99
+ @app.post(f"/{api_prefix}/tokens/clear" if api_prefix else "/tokens/clear")
100
+ async def clear_tokens():
101
+ globals.token_list.clear()
102
+ globals.error_token_list.clear()
103
+ with open(globals.TOKENS_FILE, "w", encoding="utf-8") as f:
104
+ pass
105
+ logger.info(f"Token count: {len(globals.token_list)}, Error token count: {len(globals.error_token_list)}")
106
+ tokens_count = len(set(globals.token_list) - set(globals.error_token_list))
107
+ return {"status": "success", "tokens_count": tokens_count}
108
+
109
+
110
+ @app.post(f"/{api_prefix}/tokens/error" if api_prefix else "/tokens/error")
111
+ async def error_tokens():
112
+ error_tokens_list = list(set(globals.error_token_list))
113
+ return {"status": "success", "error_tokens": error_tokens_list}
114
+
115
+
116
+ @app.get(f"/{api_prefix}/tokens/add/{{token}}" if api_prefix else "/tokens/add/{token}")
117
+ async def add_token(token: str):
118
+ if token.strip() and not token.startswith("#"):
119
+ globals.token_list.append(token.strip())
120
+ with open(globals.TOKENS_FILE, "a", encoding="utf-8") as f:
121
+ f.write(token.strip() + "\n")
122
+ logger.info(f"Token count: {len(globals.token_list)}, Error token count: {len(globals.error_token_list)}")
123
+ tokens_count = len(set(globals.token_list) - set(globals.error_token_list))
124
+ return {"status": "success", "tokens_count": tokens_count}
125
+
126
+
127
+ @app.post(f"/{api_prefix}/seed_tokens/clear" if api_prefix else "/seed_tokens/clear")
128
+ async def clear_seed_tokens():
129
+ globals.seed_map.clear()
130
+ globals.conversation_map.clear()
131
+ with open(globals.SEED_MAP_FILE, "w", encoding="utf-8") as f:
132
+ f.write("{}")
133
+ with open(globals.CONVERSATION_MAP_FILE, "w", encoding="utf-8") as f:
134
+ f.write("{}")
135
+ logger.info(f"Seed token count: {len(globals.seed_map)}")
136
+ return {"status": "success", "seed_tokens_count": len(globals.seed_map)}
api/files.py ADDED
@@ -0,0 +1,144 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import io
2
+
3
+ import pybase64
4
+ from PIL import Image
5
+
6
+ from utils.Client import Client
7
+ from utils.configs import export_proxy_url, cf_file_url
8
+
9
+
10
+ async def get_file_content(url):
11
+ if url.startswith("data:"):
12
+ mime_type, base64_data = url.split(';')[0].split(':')[1], url.split(',')[1]
13
+ file_content = pybase64.b64decode(base64_data)
14
+ return file_content, mime_type
15
+ else:
16
+ client = Client()
17
+ try:
18
+ if cf_file_url:
19
+ body = {"file_url": url}
20
+ r = await client.post(cf_file_url, timeout=60, json=body)
21
+ else:
22
+ r = await client.get(url, proxy=export_proxy_url, timeout=60)
23
+ if r.status_code != 200:
24
+ return None, None
25
+ file_content = r.content
26
+ mime_type = r.headers.get('Content-Type', '').split(';')[0].strip()
27
+ return file_content, mime_type
28
+ finally:
29
+ await client.close()
30
+ del client
31
+
32
+
33
+ async def determine_file_use_case(mime_type):
34
+ multimodal_types = ["image/jpeg", "image/webp", "image/png", "image/gif"]
35
+ my_files_types = ["text/x-php", "application/msword", "text/x-c", "text/html",
36
+ "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
37
+ "application/json", "text/javascript", "application/pdf",
38
+ "text/x-java", "text/x-tex", "text/x-typescript", "text/x-sh",
39
+ "text/x-csharp", "application/vnd.openxmlformats-officedocument.presentationml.presentation",
40
+ "text/x-c++", "application/x-latext", "text/markdown", "text/plain",
41
+ "text/x-ruby", "text/x-script.python"]
42
+
43
+ if mime_type in multimodal_types:
44
+ return "multimodal"
45
+ elif mime_type in my_files_types:
46
+ return "my_files"
47
+ else:
48
+ return "ace_upload"
49
+
50
+
51
+ async def get_image_size(file_content):
52
+ with Image.open(io.BytesIO(file_content)) as img:
53
+ return img.width, img.height
54
+
55
+
56
+ async def get_file_extension(mime_type):
57
+ extension_mapping = {
58
+ "image/jpeg": ".jpg",
59
+ "image/png": ".png",
60
+ "image/gif": ".gif",
61
+ "image/webp": ".webp",
62
+ "text/x-php": ".php",
63
+ "application/msword": ".doc",
64
+ "text/x-c": ".c",
65
+ "text/html": ".html",
66
+ "application/vnd.openxmlformats-officedocument.wordprocessingml.document": ".docx",
67
+ "application/json": ".json",
68
+ "text/javascript": ".js",
69
+ "application/pdf": ".pdf",
70
+ "text/x-java": ".java",
71
+ "text/x-tex": ".tex",
72
+ "text/x-typescript": ".ts",
73
+ "text/x-sh": ".sh",
74
+ "text/x-csharp": ".cs",
75
+ "application/vnd.openxmlformats-officedocument.presentationml.presentation": ".pptx",
76
+ "text/x-c++": ".cpp",
77
+ "application/x-latex": ".latex",
78
+ "text/markdown": ".md",
79
+ "text/plain": ".txt",
80
+ "text/x-ruby": ".rb",
81
+ "text/x-script.python": ".py",
82
+ "application/zip": ".zip",
83
+ "application/x-zip-compressed": ".zip",
84
+ "application/x-tar": ".tar",
85
+ "application/x-compressed-tar": ".tar.gz",
86
+ "application/vnd.rar": ".rar",
87
+ "application/x-rar-compressed": ".rar",
88
+ "application/x-7z-compressed": ".7z",
89
+ "application/octet-stream": ".bin",
90
+ "audio/mpeg": ".mp3",
91
+ "audio/wav": ".wav",
92
+ "audio/ogg": ".ogg",
93
+ "audio/aac": ".aac",
94
+ "video/mp4": ".mp4",
95
+ "video/x-msvideo": ".avi",
96
+ "video/x-matroska": ".mkv",
97
+ "video/webm": ".webm",
98
+ "application/rtf": ".rtf",
99
+ "application/vnd.ms-excel": ".xls",
100
+ "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": ".xlsx",
101
+ "text/css": ".css",
102
+ "text/xml": ".xml",
103
+ "application/xml": ".xml",
104
+ "application/vnd.android.package-archive": ".apk",
105
+ "application/vnd.apple.installer+xml": ".mpkg",
106
+ "application/x-bzip": ".bz",
107
+ "application/x-bzip2": ".bz2",
108
+ "application/x-csh": ".csh",
109
+ "application/x-debian-package": ".deb",
110
+ "application/x-dvi": ".dvi",
111
+ "application/java-archive": ".jar",
112
+ "application/x-java-jnlp-file": ".jnlp",
113
+ "application/vnd.mozilla.xul+xml": ".xul",
114
+ "application/vnd.ms-fontobject": ".eot",
115
+ "application/ogg": ".ogx",
116
+ "application/x-font-ttf": ".ttf",
117
+ "application/font-woff": ".woff",
118
+ "application/x-shockwave-flash": ".swf",
119
+ "application/vnd.visio": ".vsd",
120
+ "application/xhtml+xml": ".xhtml",
121
+ "application/vnd.ms-powerpoint": ".ppt",
122
+ "application/vnd.oasis.opendocument.text": ".odt",
123
+ "application/vnd.oasis.opendocument.spreadsheet": ".ods",
124
+ "application/x-xpinstall": ".xpi",
125
+ "application/vnd.google-earth.kml+xml": ".kml",
126
+ "application/vnd.google-earth.kmz": ".kmz",
127
+ "application/x-font-otf": ".otf",
128
+ "application/vnd.ms-excel.addin.macroEnabled.12": ".xlam",
129
+ "application/vnd.ms-excel.sheet.binary.macroEnabled.12": ".xlsb",
130
+ "application/vnd.ms-excel.template.macroEnabled.12": ".xltm",
131
+ "application/vnd.ms-powerpoint.addin.macroEnabled.12": ".ppam",
132
+ "application/vnd.ms-powerpoint.presentation.macroEnabled.12": ".pptm",
133
+ "application/vnd.ms-powerpoint.slideshow.macroEnabled.12": ".ppsm",
134
+ "application/vnd.ms-powerpoint.template.macroEnabled.12": ".potm",
135
+ "application/vnd.ms-word.document.macroEnabled.12": ".docm",
136
+ "application/vnd.ms-word.template.macroEnabled.12": ".dotm",
137
+ "application/x-ms-application": ".application",
138
+ "application/x-ms-wmd": ".wmd",
139
+ "application/x-ms-wmz": ".wmz",
140
+ "application/x-ms-xbap": ".xbap",
141
+ "application/vnd.ms-xpsdocument": ".xps",
142
+ "application/x-silverlight-app": ".xap"
143
+ }
144
+ return extension_mapping.get(mime_type, "")
api/models.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ model_proxy = {
2
+ "gpt-3.5-turbo": "gpt-3.5-turbo-0125",
3
+ "gpt-3.5-turbo-16k": "gpt-3.5-turbo-16k-0613",
4
+ "gpt-4": "gpt-4-0613",
5
+ "gpt-4-32k": "gpt-4-32k-0613",
6
+ "gpt-4-turbo-preview": "gpt-4-0125-preview",
7
+ "gpt-4-vision-preview": "gpt-4-1106-vision-preview",
8
+ "gpt-4-turbo": "gpt-4-turbo-2024-04-09",
9
+ "gpt-4o": "gpt-4o-2024-08-06",
10
+ "gpt-4o-mini": "gpt-4o-mini-2024-07-18",
11
+ "o1-preview": "o1-preview-2024-09-12",
12
+ "o1-mini": "o1-mini-2024-09-12",
13
+ "o1": "o1-2024-12-18",
14
+ "o3-mini": "o3-mini-2025-01-31",
15
+ "o3-mini-high": "o3-mini-high-2025-01-31",
16
+ "claude-3-opus": "claude-3-opus-20240229",
17
+ "claude-3-sonnet": "claude-3-sonnet-20240229",
18
+ "claude-3-haiku": "claude-3-haiku-20240307",
19
+ }
20
+
21
+ model_system_fingerprint = {
22
+ "gpt-3.5-turbo-0125": ["fp_b28b39ffa8"],
23
+ "gpt-3.5-turbo-1106": ["fp_592ef5907d"],
24
+ "gpt-4-0125-preview": ["fp_f38f4d6482", "fp_2f57f81c11", "fp_a7daf7c51e", "fp_a865e8ede4", "fp_13c70b9f70",
25
+ "fp_b77cb481ed"],
26
+ "gpt-4-1106-preview": ["fp_e467c31c3d", "fp_d986a8d1ba", "fp_99a5a401bb", "fp_123d5a9f90", "fp_0d1affc7a6",
27
+ "fp_5c95a4634e"],
28
+ "gpt-4-turbo-2024-04-09": ["fp_d1bac968b4"],
29
+ "gpt-4o-2024-05-13": ["fp_3aa7262c27"],
30
+ "gpt-4o-mini-2024-07-18": ["fp_c9aa9c0491"]
31
+ }
api/tokens.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+
3
+ import tiktoken
4
+
5
+
6
+ async def calculate_image_tokens(width, height, detail):
7
+ if detail == "low":
8
+ return 85
9
+ else:
10
+ max_dimension = max(width, height)
11
+ if max_dimension > 2048:
12
+ scale_factor = 2048 / max_dimension
13
+ new_width = int(width * scale_factor)
14
+ new_height = int(height * scale_factor)
15
+ else:
16
+ new_width = width
17
+ new_height = height
18
+
19
+ width, height = new_width, new_height
20
+ min_dimension = min(width, height)
21
+ if min_dimension > 768:
22
+ scale_factor = 768 / min_dimension
23
+ new_width = int(width * scale_factor)
24
+ new_height = int(height * scale_factor)
25
+ else:
26
+ new_width = width
27
+ new_height = height
28
+
29
+ width, height = new_width, new_height
30
+ num_masks_w = math.ceil(width / 512)
31
+ num_masks_h = math.ceil(height / 512)
32
+ total_masks = num_masks_w * num_masks_h
33
+
34
+ tokens_per_mask = 170
35
+ total_tokens = total_masks * tokens_per_mask + 85
36
+
37
+ return total_tokens
38
+
39
+
40
+ async def num_tokens_from_messages(messages, model=''):
41
+ try:
42
+ encoding = tiktoken.encoding_for_model(model)
43
+ except KeyError:
44
+ encoding = tiktoken.get_encoding("cl100k_base")
45
+ if model == "gpt-3.5-turbo-0301":
46
+ tokens_per_message = 4
47
+ else:
48
+ tokens_per_message = 3
49
+ num_tokens = 0
50
+ for message in messages:
51
+ num_tokens += tokens_per_message
52
+ for key, value in message.items():
53
+ if isinstance(value, list):
54
+ for item in value:
55
+ if item.get("type") == "text":
56
+ num_tokens += len(encoding.encode(item.get("text")))
57
+ if item.get("type") == "image_url":
58
+ pass
59
+ else:
60
+ num_tokens += len(encoding.encode(value))
61
+ num_tokens += 3
62
+ return num_tokens
63
+
64
+
65
+ async def num_tokens_from_content(content, model=None):
66
+ try:
67
+ encoding = tiktoken.encoding_for_model(model)
68
+ except KeyError:
69
+ encoding = tiktoken.get_encoding("cl100k_base")
70
+ encoded_content = encoding.encode(content)
71
+ len_encoded_content = len(encoded_content)
72
+ return len_encoded_content
73
+
74
+
75
+ async def split_tokens_from_content(content, max_tokens, model=None):
76
+ try:
77
+ encoding = tiktoken.encoding_for_model(model)
78
+ except KeyError:
79
+ encoding = tiktoken.get_encoding("cl100k_base")
80
+ encoded_content = encoding.encode(content)
81
+ len_encoded_content = len(encoded_content)
82
+ if len_encoded_content >= max_tokens:
83
+ content = encoding.decode(encoded_content[:max_tokens])
84
+ return content, max_tokens, "length"
85
+ else:
86
+ return content, len_encoded_content, "stop"
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import warnings
2
+
3
+ import uvicorn
4
+ from fastapi import FastAPI, HTTPException
5
+ from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
6
+ from fastapi.middleware.cors import CORSMiddleware
7
+ from fastapi.templating import Jinja2Templates
8
+
9
+ from utils.configs import enable_gateway, api_prefix
10
+
11
+ warnings.filterwarnings("ignore")
12
+
13
+
14
+ log_config = uvicorn.config.LOGGING_CONFIG
15
+ default_format = "%(asctime)s | %(levelname)s | %(message)s"
16
+ access_format = r'%(asctime)s | %(levelname)s | %(client_addr)s: %(request_line)s %(status_code)s'
17
+ log_config["formatters"]["default"]["fmt"] = default_format
18
+ log_config["formatters"]["access"]["fmt"] = access_format
19
+
20
+ app = FastAPI(
21
+ docs_url=f"/{api_prefix}/docs", # 设置 Swagger UI 文档路径
22
+ redoc_url=f"/{api_prefix}/redoc", # 设置 Redoc 文档路径
23
+ openapi_url=f"/{api_prefix}/openapi.json" # 设置 OpenAPI JSON 路径
24
+ )
25
+
26
+ app.add_middleware(
27
+ CORSMiddleware,
28
+ allow_origins=["*"],
29
+ allow_credentials=True,
30
+ allow_methods=["*"],
31
+ allow_headers=["*"],
32
+ )
33
+
34
+ templates = Jinja2Templates(directory="templates")
35
+ security_scheme = HTTPBearer()
36
+
37
+ from app import app
38
+
39
+ import api.chat2api
40
+
41
+ if enable_gateway:
42
+ import gateway.share
43
+ import gateway.login
44
+ import gateway.chatgpt
45
+ import gateway.gpts
46
+ import gateway.admin
47
+ import gateway.v1
48
+ import gateway.backend
49
+ else:
50
+ @app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD", "PATCH", "TRACE"])
51
+ async def reverse_proxy():
52
+ raise HTTPException(status_code=404, detail="Gateway is disabled")
53
+
54
+
55
+ if __name__ == "__main__":
56
+ uvicorn.run("app:app", host="0.0.0.0", port=5005)
57
+ # uvicorn.run("app:app", host="0.0.0.0", port=5005, ssl_keyfile="key.pem", ssl_certfile="cert.pem")
chatgpt/ChatService.py ADDED
@@ -0,0 +1,540 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import asyncio
2
+ import json
3
+ import random
4
+ import uuid
5
+
6
+ from fastapi import HTTPException
7
+ from starlette.concurrency import run_in_threadpool
8
+
9
+ from api.files import get_image_size, get_file_extension, determine_file_use_case
10
+ from api.models import model_proxy
11
+ from chatgpt.authorization import get_req_token, verify_token
12
+ from chatgpt.chatFormat import api_messages_to_chat, stream_response, format_not_stream_response, head_process_response
13
+ from chatgpt.chatLimit import check_is_limit, handle_request_limit
14
+ from chatgpt.fp import get_fp
15
+ from chatgpt.proofofWork import get_config, get_dpl, get_answer_token, get_requirements_token
16
+
17
+ from utils.Client import Client
18
+ from utils.Logger import logger
19
+ from utils.configs import (
20
+ chatgpt_base_url_list,
21
+ ark0se_token_url_list,
22
+ sentinel_proxy_url_list,
23
+ history_disabled,
24
+ pow_difficulty,
25
+ conversation_only,
26
+ enable_limit,
27
+ upload_by_url,
28
+ auth_key,
29
+ turnstile_solver_url,
30
+ oai_language,
31
+ )
32
+
33
+
34
+ class ChatService:
35
+ def __init__(self, origin_token=None):
36
+ # self.user_agent = random.choice(user_agents_list) if user_agents_list else "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
37
+ self.req_token = get_req_token(origin_token)
38
+ self.chat_token = "gAAAAAB"
39
+ self.s = None
40
+ self.ss = None
41
+ self.ws = None
42
+
43
+ async def set_dynamic_data(self, data):
44
+ if self.req_token:
45
+ req_len = len(self.req_token.split(","))
46
+ if req_len == 1:
47
+ self.access_token = await verify_token(self.req_token)
48
+ self.account_id = None
49
+ else:
50
+ self.access_token = await verify_token(self.req_token.split(",")[0])
51
+ self.account_id = self.req_token.split(",")[1]
52
+ else:
53
+ logger.info("Request token is empty, use no-auth 3.5")
54
+ self.access_token = None
55
+ self.account_id = None
56
+
57
+ self.fp = get_fp(self.req_token).copy()
58
+ self.proxy_url = self.fp.pop("proxy_url", None)
59
+ self.impersonate = self.fp.pop("impersonate", "safari15_3")
60
+ self.user_agent = self.fp.get("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0")
61
+ logger.info(f"Request token: {self.req_token}")
62
+ logger.info(f"Request proxy: {self.proxy_url}")
63
+ logger.info(f"Request UA: {self.user_agent}")
64
+ logger.info(f"Request impersonate: {self.impersonate}")
65
+
66
+ self.data = data
67
+ await self.set_model()
68
+ if enable_limit and self.req_token:
69
+ limit_response = await handle_request_limit(self.req_token, self.req_model)
70
+ if limit_response:
71
+ raise HTTPException(status_code=429, detail=limit_response)
72
+
73
+ self.account_id = self.data.get('Chatgpt-Account-Id', self.account_id)
74
+ self.parent_message_id = self.data.get('parent_message_id')
75
+ self.conversation_id = self.data.get('conversation_id')
76
+ self.history_disabled = self.data.get('history_disabled', history_disabled)
77
+
78
+ self.api_messages = self.data.get("messages", [])
79
+ self.prompt_tokens = 0
80
+ self.max_tokens = self.data.get("max_tokens", 2147483647)
81
+ if not isinstance(self.max_tokens, int):
82
+ self.max_tokens = 2147483647
83
+
84
+ # self.proxy_url = random.choice(proxy_url_list) if proxy_url_list else None
85
+
86
+ self.host_url = random.choice(chatgpt_base_url_list) if chatgpt_base_url_list else "https://chatgpt.com"
87
+ self.ark0se_token_url = random.choice(ark0se_token_url_list) if ark0se_token_url_list else None
88
+
89
+ self.s = Client(proxy=self.proxy_url, impersonate=self.impersonate)
90
+ if sentinel_proxy_url_list:
91
+ self.ss = Client(proxy=random.choice(sentinel_proxy_url_list), impersonate=self.impersonate)
92
+ else:
93
+ self.ss = self.s
94
+
95
+ self.persona = None
96
+ self.ark0se_token = None
97
+ self.proof_token = None
98
+ self.turnstile_token = None
99
+
100
+ self.chat_headers = None
101
+ self.chat_request = None
102
+
103
+ self.base_headers = {
104
+ 'accept': '*/*',
105
+ 'accept-encoding': 'gzip, deflate, br, zstd',
106
+ 'accept-language': 'en-US,en;q=0.9',
107
+ 'content-type': 'application/json',
108
+ 'oai-language': oai_language,
109
+ 'origin': self.host_url,
110
+ 'priority': 'u=1, i',
111
+ 'referer': f'{self.host_url}/',
112
+ 'sec-fetch-dest': 'empty',
113
+ 'sec-fetch-mode': 'cors',
114
+ 'sec-fetch-site': 'same-origin'
115
+ }
116
+ self.base_headers.update(self.fp)
117
+
118
+ if self.access_token:
119
+ self.base_url = self.host_url + "/backend-api"
120
+ self.base_headers['authorization'] = f'Bearer {self.access_token}'
121
+ if self.account_id:
122
+ self.base_headers['chatgpt-account-id'] = self.account_id
123
+ else:
124
+ self.base_url = self.host_url + "/backend-anon"
125
+
126
+ if auth_key:
127
+ self.base_headers['authkey'] = auth_key
128
+
129
+ await get_dpl(self)
130
+
131
+ async def set_model(self):
132
+ self.origin_model = self.data.get("model", "gpt-3.5-turbo-0125")
133
+ self.resp_model = model_proxy.get(self.origin_model, self.origin_model)
134
+ if "gizmo" in self.origin_model or "g-" in self.origin_model:
135
+ self.gizmo_id = "g-" + self.origin_model.split("g-")[-1]
136
+ else:
137
+ self.gizmo_id = None
138
+
139
+ if "o3-mini-high" in self.origin_model:
140
+ self.req_model = "o3-mini-high"
141
+ elif "o3-mini-medium" in self.origin_model:
142
+ self.req_model = "o3-mini-medium"
143
+ elif "o3-mini-low" in self.origin_model:
144
+ self.req_model = "o3-mini-low"
145
+ elif "o3-mini" in self.origin_model:
146
+ self.req_model = "o3-mini"
147
+ elif "o3" in self.origin_model:
148
+ self.req_model = "o3"
149
+ elif "o1-preview" in self.origin_model:
150
+ self.req_model = "o1-preview"
151
+ elif "o1-pro" in self.origin_model:
152
+ self.req_model = "o1-pro"
153
+ elif "o1-mini" in self.origin_model:
154
+ self.req_model = "o1-mini"
155
+ elif "o1" in self.origin_model:
156
+ self.req_model = "o1"
157
+ elif "gpt-4.5o" in self.origin_model:
158
+ self.req_model = "gpt-4.5o"
159
+ elif "gpt-4o-canmore" in self.origin_model:
160
+ self.req_model = "gpt-4o-canmore"
161
+ elif "gpt-4o-mini" in self.origin_model:
162
+ self.req_model = "gpt-4o-mini"
163
+ elif "gpt-4o" in self.origin_model:
164
+ self.req_model = "gpt-4o"
165
+ elif "gpt-4-mobile" in self.origin_model:
166
+ self.req_model = "gpt-4-mobile"
167
+ elif "gpt-4" in self.origin_model:
168
+ self.req_model = "gpt-4"
169
+ elif "gpt-3.5" in self.origin_model:
170
+ self.req_model = "text-davinci-002-render-sha"
171
+ elif "auto" in self.origin_model:
172
+ self.req_model = "auto"
173
+ else:
174
+ self.req_model = "gpt-4o"
175
+
176
+ async def get_chat_requirements(self):
177
+ if conversation_only:
178
+ return None
179
+ url = f'{self.base_url}/sentinel/chat-requirements'
180
+ headers = self.base_headers.copy()
181
+ try:
182
+ config = get_config(self.user_agent)
183
+ p = get_requirements_token(config)
184
+ data = {'p': p}
185
+ r = await self.ss.post(url, headers=headers, json=data, timeout=5)
186
+ if r.status_code == 200:
187
+ resp = r.json()
188
+
189
+ self.persona = resp.get("persona")
190
+ if self.persona != "chatgpt-paid":
191
+ if self.req_model == "gpt-4" or self.req_model == "o1-preview":
192
+ logger.error(f"Model {self.resp_model} not support for {self.persona}")
193
+ raise HTTPException(
194
+ status_code=404,
195
+ detail={
196
+ "message": f"The model `{self.origin_model}` does not exist or you do not have access to it.",
197
+ "type": "invalid_request_error",
198
+ "param": None,
199
+ "code": "model_not_found",
200
+ },
201
+ )
202
+
203
+ turnstile = resp.get('turnstile', {})
204
+ turnstile_required = turnstile.get('required')
205
+ if turnstile_required:
206
+ turnstile_dx = turnstile.get("dx")
207
+ try:
208
+ if turnstile_solver_url:
209
+ res = await self.s.post(
210
+ turnstile_solver_url, json={"url": "https://chatgpt.com", "p": p, "dx": turnstile_dx}
211
+ )
212
+ self.turnstile_token = res.json().get("t")
213
+ except Exception as e:
214
+ logger.info(f"Turnstile ignored: {e}")
215
+ # raise HTTPException(status_code=403, detail="Turnstile required")
216
+
217
+ ark0se = resp.get('ark' + 'ose', {})
218
+ ark0se_required = ark0se.get('required')
219
+ if ark0se_required:
220
+ if self.persona == "chatgpt-freeaccount":
221
+ ark0se_method = "chat35"
222
+ else:
223
+ ark0se_method = "chat4"
224
+ if not self.ark0se_token_url:
225
+ raise HTTPException(status_code=403, detail="Ark0se service required")
226
+ ark0se_dx = ark0se.get("dx")
227
+ ark0se_client = Client(impersonate=self.impersonate)
228
+ try:
229
+ r2 = await ark0se_client.post(
230
+ url=self.ark0se_token_url, json={"blob": ark0se_dx, "method": ark0se_method}, timeout=15
231
+ )
232
+ r2esp = r2.json()
233
+ logger.info(f"ark0se_token: {r2esp}")
234
+ if r2esp.get('solved', True):
235
+ self.ark0se_token = r2esp.get('token')
236
+ else:
237
+ raise HTTPException(status_code=403, detail="Failed to get Ark0se token")
238
+ except Exception:
239
+ raise HTTPException(status_code=403, detail="Failed to get Ark0se token")
240
+ finally:
241
+ await ark0se_client.close()
242
+
243
+ proofofwork = resp.get('proofofwork', {})
244
+ proofofwork_required = proofofwork.get('required')
245
+ if proofofwork_required:
246
+ proofofwork_diff = proofofwork.get("difficulty")
247
+ if proofofwork_diff <= pow_difficulty:
248
+ raise HTTPException(status_code=403, detail=f"Proof of work difficulty too high: {proofofwork_diff}")
249
+ proofofwork_seed = proofofwork.get("seed")
250
+ self.proof_token, solved = await run_in_threadpool(
251
+ get_answer_token, proofofwork_seed, proofofwork_diff, config
252
+ )
253
+ if not solved:
254
+ raise HTTPException(status_code=403, detail="Failed to solve proof of work")
255
+
256
+ self.chat_token = resp.get('token')
257
+ if not self.chat_token:
258
+ raise HTTPException(status_code=403, detail=f"Failed to get chat token: {r.text}")
259
+ return self.chat_token
260
+ else:
261
+ if "application/json" == r.headers.get("Content-Type", ""):
262
+ detail = r.json().get("detail", r.json())
263
+ else:
264
+ detail = r.text
265
+ if "cf_chl_opt" in detail:
266
+ raise HTTPException(status_code=r.status_code, detail="cf_chl_opt")
267
+ if r.status_code == 429:
268
+ raise HTTPException(status_code=r.status_code, detail="rate-limit")
269
+ raise HTTPException(status_code=r.status_code, detail=detail)
270
+ except HTTPException as e:
271
+ raise HTTPException(status_code=e.status_code, detail=e.detail)
272
+ except Exception as e:
273
+ raise HTTPException(status_code=500, detail=str(e))
274
+
275
+ async def prepare_send_conversation(self):
276
+ try:
277
+ chat_messages, self.prompt_tokens = await api_messages_to_chat(self, self.api_messages, upload_by_url)
278
+ except Exception as e:
279
+ logger.error(f"Failed to format messages: {str(e)}")
280
+ raise HTTPException(status_code=400, detail="Failed to format messages.")
281
+ self.chat_headers = self.base_headers.copy()
282
+ self.chat_headers.update(
283
+ {
284
+ 'accept': 'text/event-stream',
285
+ 'openai-sentinel-chat-requirements-token': self.chat_token,
286
+ 'openai-sentinel-proof-token': self.proof_token,
287
+ }
288
+ )
289
+ if self.ark0se_token:
290
+ self.chat_headers['openai-sentinel-ark' + 'ose-token'] = self.ark0se_token
291
+
292
+ if self.turnstile_token:
293
+ self.chat_headers['openai-sentinel-turnstile-token'] = self.turnstile_token
294
+
295
+ if conversation_only:
296
+ self.chat_headers.pop('openai-sentinel-chat-requirements-token', None)
297
+ self.chat_headers.pop('openai-sentinel-proof-token', None)
298
+ self.chat_headers.pop('openai-sentinel-ark' + 'ose-token', None)
299
+ self.chat_headers.pop('openai-sentinel-turnstile-token', None)
300
+
301
+ if self.gizmo_id:
302
+ conversation_mode = {"kind": "gizmo_interaction", "gizmo_id": self.gizmo_id}
303
+ logger.info(f"Gizmo id: {self.gizmo_id}")
304
+ else:
305
+ conversation_mode = {"kind": "primary_assistant"}
306
+
307
+ logger.info(f"Model mapping: {self.origin_model} -> {self.req_model}")
308
+ self.chat_request = {
309
+ "action": "next",
310
+ "client_contextual_info": {
311
+ "is_dark_mode": False,
312
+ "time_since_loaded": random.randint(50, 500),
313
+ "page_height": random.randint(500, 1000),
314
+ "page_width": random.randint(1000, 2000),
315
+ "pixel_ratio": 1.5,
316
+ "screen_height": random.randint(800, 1200),
317
+ "screen_width": random.randint(1200, 2200),
318
+ },
319
+ "conversation_mode": conversation_mode,
320
+ "conversation_origin": None,
321
+ "force_paragen": False,
322
+ "force_paragen_model_slug": "",
323
+ "force_rate_limit": False,
324
+ "force_use_sse": True,
325
+ "history_and_training_disabled": self.history_disabled,
326
+ "messages": chat_messages,
327
+ "model": self.req_model,
328
+ "paragen_cot_summary_display_override": "allow",
329
+ "paragen_stream_type_override": None,
330
+ "parent_message_id": self.parent_message_id if self.parent_message_id else f"{uuid.uuid4()}",
331
+ "reset_rate_limits": False,
332
+ "suggestions": [],
333
+ "supported_encodings": [],
334
+ "system_hints": [],
335
+ "timezone": "America/Los_Angeles",
336
+ "timezone_offset_min": -480,
337
+ "variant_purpose": "comparison_implicit",
338
+ "websocket_request_id": f"{uuid.uuid4()}",
339
+ }
340
+ if self.conversation_id:
341
+ self.chat_request['conversation_id'] = self.conversation_id
342
+ return self.chat_request
343
+
344
+ async def send_conversation(self):
345
+ try:
346
+ url = f'{self.base_url}/conversation'
347
+ stream = self.data.get("stream", False)
348
+ r = await self.s.post_stream(url, headers=self.chat_headers, json=self.chat_request, timeout=10, stream=True)
349
+ if r.status_code != 200:
350
+ rtext = await r.atext()
351
+ if "application/json" == r.headers.get("Content-Type", ""):
352
+ detail = json.loads(rtext).get("detail", json.loads(rtext))
353
+ if r.status_code == 429:
354
+ check_is_limit(detail, token=self.req_token, model=self.req_model)
355
+ else:
356
+ if "cf_chl_opt" in rtext:
357
+ # logger.error(f"Failed to send conversation: cf_chl_opt")
358
+ raise HTTPException(status_code=r.status_code, detail="cf_chl_opt")
359
+ if r.status_code == 429:
360
+ # logger.error(f"Failed to send conversation: rate-limit")
361
+ raise HTTPException(status_code=r.status_code, detail="rate-limit")
362
+ detail = r.text[:100]
363
+ # logger.error(f"Failed to send conversation: {detail}")
364
+ raise HTTPException(status_code=r.status_code, detail=detail)
365
+
366
+ content_type = r.headers.get("Content-Type", "")
367
+ if "text/event-stream" in content_type:
368
+ res, start = await head_process_response(r.aiter_lines())
369
+ if not start:
370
+ raise HTTPException(
371
+ status_code=403,
372
+ detail="Our systems have detected unusual activity coming from your system. Please try again later.",
373
+ )
374
+ if stream:
375
+ return stream_response(self, res, self.resp_model, self.max_tokens)
376
+ else:
377
+ return await format_not_stream_response(
378
+ stream_response(self, res, self.resp_model, self.max_tokens),
379
+ self.prompt_tokens,
380
+ self.max_tokens,
381
+ self.resp_model,
382
+ )
383
+ elif "application/json" in content_type:
384
+ rtext = await r.atext()
385
+ resp = json.loads(rtext)
386
+ raise HTTPException(status_code=r.status_code, detail=resp)
387
+ else:
388
+ rtext = await r.atext()
389
+ raise HTTPException(status_code=r.status_code, detail=rtext)
390
+ except HTTPException as e:
391
+ raise HTTPException(status_code=e.status_code, detail=e.detail)
392
+ except Exception as e:
393
+ raise HTTPException(status_code=500, detail=str(e))
394
+
395
+ async def get_download_url(self, file_id):
396
+ url = f"{self.base_url}/files/{file_id}/download"
397
+ headers = self.base_headers.copy()
398
+ try:
399
+ r = await self.s.get(url, headers=headers, timeout=10)
400
+ if r.status_code == 200:
401
+ download_url = r.json().get('download_url')
402
+ return download_url
403
+ else:
404
+ raise HTTPException(status_code=r.status_code, detail=r.text)
405
+ except Exception as e:
406
+ logger.error(f"Failed to get download url: {e}")
407
+ return ""
408
+
409
+ async def get_download_url_from_upload(self, file_id):
410
+ url = f"{self.base_url}/files/{file_id}/uploaded"
411
+ headers = self.base_headers.copy()
412
+ try:
413
+ r = await self.s.post(url, headers=headers, json={}, timeout=10)
414
+ if r.status_code == 200:
415
+ download_url = r.json().get('download_url')
416
+ return download_url
417
+ else:
418
+ raise HTTPException(status_code=r.status_code, detail=r.text)
419
+ except Exception as e:
420
+ logger.error(f"Failed to get download url from upload: {e}")
421
+ return ""
422
+
423
+ async def get_upload_url(self, file_name, file_size, use_case="multimodal"):
424
+ url = f'{self.base_url}/files'
425
+ headers = self.base_headers.copy()
426
+ try:
427
+ r = await self.s.post(
428
+ url,
429
+ headers=headers,
430
+ json={"file_name": file_name, "file_size": file_size, "reset_rate_limits": False, "timezone_offset_min": -480, "use_case": use_case},
431
+ timeout=5,
432
+ )
433
+ if r.status_code == 200:
434
+ res = r.json()
435
+ file_id = res.get('file_id')
436
+ upload_url = res.get('upload_url')
437
+ logger.info(f"file_id: {file_id}, upload_url: {upload_url}")
438
+ return file_id, upload_url
439
+ else:
440
+ raise HTTPException(status_code=r.status_code, detail=r.text)
441
+ except Exception as e:
442
+ logger.error(f"Failed to get upload url: {e}")
443
+ return "", ""
444
+
445
+ async def upload(self, upload_url, file_content, mime_type):
446
+ headers = self.base_headers.copy()
447
+ headers.update(
448
+ {
449
+ 'accept': 'application/json, text/plain, */*',
450
+ 'content-type': mime_type,
451
+ 'x-ms-blob-type': 'BlockBlob',
452
+ 'x-ms-version': '2020-04-08',
453
+ }
454
+ )
455
+ headers.pop('authorization', None)
456
+ headers.pop('oai-device-id', None)
457
+ headers.pop('oai-language', None)
458
+ try:
459
+ r = await self.s.put(upload_url, headers=headers, data=file_content, timeout=60)
460
+ if r.status_code == 201:
461
+ return True
462
+ else:
463
+ raise HTTPException(status_code=r.status_code, detail=r.text)
464
+ except Exception as e:
465
+ logger.error(f"Failed to upload file: {e}")
466
+ return False
467
+
468
+ async def upload_file(self, file_content, mime_type):
469
+ if not file_content or not mime_type:
470
+ return None
471
+
472
+ width, height = None, None
473
+ if mime_type.startswith("image/"):
474
+ try:
475
+ width, height = await get_image_size(file_content)
476
+ except Exception as e:
477
+ logger.error(f"Error image mime_type, change to text/plain: {e}")
478
+ mime_type = 'text/plain'
479
+ file_size = len(file_content)
480
+ file_extension = await get_file_extension(mime_type)
481
+ file_name = f"{uuid.uuid4()}{file_extension}"
482
+ use_case = await determine_file_use_case(mime_type)
483
+
484
+ file_id, upload_url = await self.get_upload_url(file_name, file_size, use_case)
485
+ if file_id and upload_url:
486
+ if await self.upload(upload_url, file_content, mime_type):
487
+ download_url = await self.get_download_url_from_upload(file_id)
488
+ if download_url:
489
+ file_meta = {
490
+ "file_id": file_id,
491
+ "file_name": file_name,
492
+ "size_bytes": file_size,
493
+ "mime_type": mime_type,
494
+ "width": width,
495
+ "height": height,
496
+ "use_case": use_case,
497
+ }
498
+ logger.info(f"File_meta: {file_meta}")
499
+ return file_meta
500
+
501
+ async def check_upload(self, file_id):
502
+ url = f'{self.base_url}/files/{file_id}'
503
+ headers = self.base_headers.copy()
504
+ try:
505
+ for i in range(30):
506
+ r = await self.s.get(url, headers=headers, timeout=5)
507
+ if r.status_code == 200:
508
+ res = r.json()
509
+ retrieval_index_status = res.get('retrieval_index_status', '')
510
+ if retrieval_index_status == "success":
511
+ break
512
+ await asyncio.sleep(1)
513
+ return True
514
+ except HTTPException:
515
+ return False
516
+
517
+ async def get_response_file_url(self, conversation_id, message_id, sandbox_path):
518
+ try:
519
+ url = f"{self.base_url}/conversation/{conversation_id}/interpreter/download"
520
+ params = {"message_id": message_id, "sandbox_path": sandbox_path}
521
+ headers = self.base_headers.copy()
522
+ r = await self.s.get(url, headers=headers, params=params, timeout=10)
523
+ if r.status_code == 200:
524
+ return r.json().get("download_url")
525
+ else:
526
+ return None
527
+ except Exception:
528
+ logger.info("Failed to get response file url")
529
+ return None
530
+
531
+ async def close_client(self):
532
+ if self.s:
533
+ await self.s.close()
534
+ del self.s
535
+ if self.ss:
536
+ await self.ss.close()
537
+ del self.ss
538
+ if self.ws:
539
+ await self.ws.close()
540
+ del self.ws
chatgpt/authorization.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import asyncio
2
+ import json
3
+ import random
4
+
5
+ from fastapi import HTTPException
6
+
7
+ import utils.configs as configs
8
+ import utils.globals as globals
9
+ from chatgpt.refreshToken import rt2ac
10
+ from utils.Logger import logger
11
+
12
+
13
+ def get_req_token(req_token, seed=None):
14
+ if configs.auto_seed:
15
+ available_token_list = list(set(globals.token_list) - set(globals.error_token_list))
16
+ length = len(available_token_list)
17
+ if seed and length > 0:
18
+ if seed not in globals.seed_map.keys():
19
+ globals.seed_map[seed] = {"token": random.choice(available_token_list), "conversations": []}
20
+ with open(globals.SEED_MAP_FILE, "w") as f:
21
+ json.dump(globals.seed_map, f, indent=4)
22
+ else:
23
+ req_token = globals.seed_map[seed]["token"]
24
+ return req_token
25
+
26
+ if req_token in configs.authorization_list:
27
+ if len(available_token_list) > 0:
28
+ if configs.random_token:
29
+ req_token = random.choice(available_token_list)
30
+ return req_token
31
+ else:
32
+ globals.count += 1
33
+ globals.count %= length
34
+ return available_token_list[globals.count]
35
+ else:
36
+ return ""
37
+ else:
38
+ return req_token
39
+ else:
40
+ seed = req_token
41
+ if seed not in globals.seed_map.keys():
42
+ raise HTTPException(status_code=401, detail={"error": "Invalid Seed"})
43
+ return globals.seed_map[seed]["token"]
44
+
45
+
46
+ async def verify_token(req_token):
47
+ if not req_token:
48
+ if configs.authorization_list:
49
+ logger.error("Unauthorized with empty token.")
50
+ raise HTTPException(status_code=401)
51
+ else:
52
+ return None
53
+ else:
54
+ if req_token.startswith("eyJhbGciOi") or req_token.startswith("fk-"):
55
+ access_token = req_token
56
+ return access_token
57
+ elif len(req_token) == 45:
58
+ try:
59
+ if req_token in globals.error_token_list:
60
+ raise HTTPException(status_code=401, detail="Error RefreshToken")
61
+
62
+ access_token = await rt2ac(req_token, force_refresh=False)
63
+ return access_token
64
+ except HTTPException as e:
65
+ raise HTTPException(status_code=e.status_code, detail=e.detail)
66
+ else:
67
+ return req_token
68
+
69
+
70
+ async def refresh_all_tokens(force_refresh=False):
71
+ for token in list(set(globals.token_list) - set(globals.error_token_list)):
72
+ if len(token) == 45:
73
+ try:
74
+ await asyncio.sleep(0.5)
75
+ await rt2ac(token, force_refresh=force_refresh)
76
+ except HTTPException:
77
+ pass
78
+ logger.info("All tokens refreshed.")
chatgpt/chatFormat.py ADDED
@@ -0,0 +1,436 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import asyncio
2
+ import json
3
+ import random
4
+ import re
5
+ import string
6
+ import time
7
+ import uuid
8
+
9
+ import pybase64
10
+ import websockets
11
+ from fastapi import HTTPException
12
+
13
+ from api.files import get_file_content
14
+ from api.models import model_system_fingerprint
15
+ from api.tokens import split_tokens_from_content, calculate_image_tokens, num_tokens_from_messages
16
+ from utils.Logger import logger
17
+
18
+ moderation_message = "I'm sorry, I cannot provide or engage in any content related to pornography, violence, or any unethical material. If you have any other questions or need assistance, please feel free to let me know. I'll do my best to provide support and assistance."
19
+
20
+
21
+ async def format_not_stream_response(response, prompt_tokens, max_tokens, model):
22
+ chat_id = f"chatcmpl-{''.join(random.choice(string.ascii_letters + string.digits) for _ in range(29))}"
23
+ system_fingerprint_list = model_system_fingerprint.get(model, None)
24
+ system_fingerprint = random.choice(system_fingerprint_list) if system_fingerprint_list else None
25
+ created_time = int(time.time())
26
+ all_text = ""
27
+ async for chunk in response:
28
+ try:
29
+ if chunk.startswith("data: [DONE]"):
30
+ break
31
+ elif not chunk.startswith("data: "):
32
+ continue
33
+ else:
34
+ chunk = json.loads(chunk[6:])
35
+ if not chunk["choices"][0].get("delta"):
36
+ continue
37
+ all_text += chunk["choices"][0]["delta"]["content"]
38
+ except Exception as e:
39
+ logger.error(f"Error: {chunk}, error: {str(e)}")
40
+ continue
41
+ content, completion_tokens, finish_reason = await split_tokens_from_content(all_text, max_tokens, model)
42
+ message = {
43
+ "role": "assistant",
44
+ "content": content,
45
+ }
46
+ usage = {
47
+ "prompt_tokens": prompt_tokens,
48
+ "completion_tokens": completion_tokens,
49
+ "total_tokens": prompt_tokens + completion_tokens
50
+ }
51
+ if not message.get("content"):
52
+ raise HTTPException(status_code=403, detail="No content in the message.")
53
+
54
+ data = {
55
+ "id": chat_id,
56
+ "object": "chat.completion",
57
+ "created": created_time,
58
+ "model": model,
59
+ "choices": [
60
+ {
61
+ "index": 0,
62
+ "message": message,
63
+ "logprobs": None,
64
+ "finish_reason": finish_reason
65
+ }
66
+ ],
67
+ "usage": usage
68
+ }
69
+ if system_fingerprint:
70
+ data["system_fingerprint"] = system_fingerprint
71
+ return data
72
+
73
+
74
+ async def wss_stream_response(websocket, conversation_id):
75
+ while not websocket.closed:
76
+ try:
77
+ message = await asyncio.wait_for(websocket.recv(), timeout=10)
78
+ if message:
79
+ resultObj = json.loads(message)
80
+ sequenceId = resultObj.get("sequenceId", None)
81
+ if not sequenceId:
82
+ continue
83
+ data = resultObj.get("data", {})
84
+ if conversation_id != data.get("conversation_id", ""):
85
+ continue
86
+ sequenceId = resultObj.get('sequenceId')
87
+ if sequenceId and sequenceId % 80 == 0:
88
+ await websocket.send(
89
+ json.dumps(
90
+ {"type": "sequenceAck", "sequenceId": sequenceId}
91
+ )
92
+ )
93
+ decoded_bytes = pybase64.b64decode(data.get("body", None))
94
+ yield decoded_bytes
95
+ else:
96
+ print("No message received within the specified time.")
97
+ except asyncio.TimeoutError:
98
+ logger.error("Timeout! No message received within the specified time.")
99
+ break
100
+ except websockets.ConnectionClosed as e:
101
+ if e.code == 1000:
102
+ logger.error("WebSocket closed normally with code 1000 (OK)")
103
+ yield b"data: [DONE]\n\n"
104
+ else:
105
+ logger.error(f"WebSocket closed with error code {e.code}")
106
+ except Exception as e:
107
+ logger.error(f"Error: {str(e)}")
108
+ continue
109
+
110
+
111
+ async def head_process_response(response):
112
+ async for chunk in response:
113
+ chunk = chunk.decode("utf-8")
114
+ if chunk.startswith("data: {"):
115
+ chunk_old_data = json.loads(chunk[6:])
116
+ message = chunk_old_data.get("message", {})
117
+ if not message and "error" in chunk_old_data:
118
+ return response, False
119
+ role = message.get('author', {}).get('role')
120
+ if role == 'user' or role == 'system':
121
+ continue
122
+
123
+ status = message.get("status")
124
+ if status == "in_progress":
125
+ return response, True
126
+ return response, False
127
+
128
+
129
+ async def stream_response(service, response, model, max_tokens):
130
+ chat_id = f"chatcmpl-{''.join(random.choice(string.ascii_letters + string.digits) for _ in range(29))}"
131
+ system_fingerprint_list = model_system_fingerprint.get(model, None)
132
+ system_fingerprint = random.choice(system_fingerprint_list) if system_fingerprint_list else None
133
+ created_time = int(time.time())
134
+ completion_tokens = 0
135
+ len_last_content = 0
136
+ len_last_citation = 0
137
+ last_message_id = None
138
+ last_role = None
139
+ last_content_type = None
140
+ model_slug = None
141
+ end = False
142
+
143
+ chunk_new_data = {
144
+ "id": chat_id,
145
+ "object": "chat.completion.chunk",
146
+ "created": created_time,
147
+ "model": model,
148
+ "choices": [
149
+ {
150
+ "index": 0,
151
+ "delta": {"role": "assistant", "content": ""},
152
+ "logprobs": None,
153
+ "finish_reason": None
154
+ }
155
+ ]
156
+ }
157
+ if system_fingerprint:
158
+ chunk_new_data["system_fingerprint"] = system_fingerprint
159
+ yield f"data: {json.dumps(chunk_new_data)}\n\n"
160
+
161
+ async for chunk in response:
162
+ chunk = chunk.decode("utf-8")
163
+ if end:
164
+ logger.info(f"Response Model: {model_slug}")
165
+ yield "data: [DONE]\n\n"
166
+ break
167
+ try:
168
+ if chunk.startswith("data: {"):
169
+ chunk_old_data = json.loads(chunk[6:])
170
+ finish_reason = None
171
+ message = chunk_old_data.get("message", {})
172
+ conversation_id = chunk_old_data.get("conversation_id")
173
+ role = message.get('author', {}).get('role')
174
+ if role == 'user' or role == 'system':
175
+ continue
176
+
177
+ status = message.get("status")
178
+ message_id = message.get("id")
179
+ content = message.get("content", {})
180
+ recipient = message.get("recipient", "")
181
+ meta_data = message.get("metadata", {})
182
+ initial_text = meta_data.get("initial_text", "")
183
+ model_slug = meta_data.get("model_slug", model_slug)
184
+
185
+ if not message and chunk_old_data.get("type") == "moderation":
186
+ delta = {"role": "assistant", "content": moderation_message}
187
+ finish_reason = "stop"
188
+ end = True
189
+ elif status == "in_progress":
190
+ outer_content_type = content.get("content_type")
191
+ if outer_content_type == "text":
192
+ part = content.get("parts", [])[0]
193
+ if not part:
194
+ if role == 'assistant' and last_role != 'assistant':
195
+ if last_role == None:
196
+ new_text = ""
197
+ else:
198
+ new_text = f"\n"
199
+ elif role == 'tool' and last_role != 'tool':
200
+ new_text = f">{initial_text}\n"
201
+ else:
202
+ new_text = ""
203
+ else:
204
+ if last_message_id and last_message_id != message_id:
205
+ continue
206
+ citation = message.get("metadata", {}).get("citations", [])
207
+ if len(citation) > len_last_citation:
208
+ inside_metadata = citation[-1].get("metadata", {})
209
+ citation_title = inside_metadata.get("title", "")
210
+ citation_url = inside_metadata.get("url", "")
211
+ new_text = f' **[[""]]({citation_url} "{citation_title}")** '
212
+ len_last_citation = len(citation)
213
+ else:
214
+ if role == 'assistant' and last_role != 'assistant':
215
+ if recipient == 'dalle.text2im':
216
+ new_text = f"\n```{recipient}\n{part[len_last_content:]}"
217
+ elif last_role == None:
218
+ new_text = part[len_last_content:]
219
+ else:
220
+ new_text = f"\n\n{part[len_last_content:]}"
221
+ elif role == 'tool' and last_role != 'tool':
222
+ new_text = f">{initial_text}\n{part[len_last_content:]}"
223
+ elif role == 'tool':
224
+ new_text = part[len_last_content:].replace("\n\n", "\n")
225
+ else:
226
+ new_text = part[len_last_content:]
227
+ len_last_content = len(part)
228
+ else:
229
+ text = content.get("text", "")
230
+ if outer_content_type == "code" and last_content_type != "code":
231
+ language = content.get("language", "")
232
+ if not language or language == "unknown":
233
+ language = recipient
234
+ new_text = "\n```" + language + "\n" + text[len_last_content:]
235
+ elif outer_content_type == "execution_output" and last_content_type != "execution_output":
236
+ new_text = "\n```" + "Output" + "\n" + text[len_last_content:]
237
+ else:
238
+ new_text = text[len_last_content:]
239
+ len_last_content = len(text)
240
+ if last_content_type == "code" and outer_content_type != "code":
241
+ new_text = "\n```\n" + new_text
242
+ elif last_content_type == "execution_output" and outer_content_type != "execution_output":
243
+ new_text = "\n```\n" + new_text
244
+
245
+ delta = {"content": new_text}
246
+ last_content_type = outer_content_type
247
+ if completion_tokens >= max_tokens:
248
+ delta = {}
249
+ finish_reason = "length"
250
+ end = True
251
+ elif status == "finished_successfully":
252
+ if content.get("content_type") == "multimodal_text":
253
+ parts = content.get("parts", [])
254
+ delta = {}
255
+ for part in parts:
256
+ if isinstance(part, str):
257
+ continue
258
+ inner_content_type = part.get('content_type')
259
+ if inner_content_type == "image_asset_pointer":
260
+ last_content_type = "image_asset_pointer"
261
+ file_id = part.get('asset_pointer').replace('file-service://', '')
262
+ logger.debug(f"file_id: {file_id}")
263
+ image_download_url = await service.get_download_url(file_id)
264
+ logger.debug(f"image_download_url: {image_download_url}")
265
+ if image_download_url:
266
+ delta = {"content": f"\n```\n![image]({image_download_url})\n"}
267
+ else:
268
+ delta = {"content": f"\n```\nFailed to load the image.\n"}
269
+ elif message.get("end_turn"):
270
+ part = content.get("parts", [])[0]
271
+ new_text = part[len_last_content:]
272
+ if not new_text:
273
+ matches = re.findall(r'\(sandbox:(.*?)\)', part)
274
+ if matches:
275
+ file_url_content = ""
276
+ for i, sandbox_path in enumerate(matches):
277
+ file_download_url = await service.get_response_file_url(conversation_id, message_id, sandbox_path)
278
+ if file_download_url:
279
+ file_url_content += f"\n```\n\n![File {i+1}]({file_download_url})\n"
280
+ delta = {"content": file_url_content}
281
+ else:
282
+ delta = {}
283
+ else:
284
+ delta = {"content": new_text}
285
+ finish_reason = "stop"
286
+ end = True
287
+ else:
288
+ len_last_content = 0
289
+ if meta_data.get("finished_text"):
290
+ delta = {"content": f"\n{meta_data.get('finished_text')}\n"}
291
+ else:
292
+ continue
293
+ else:
294
+ continue
295
+ last_message_id = message_id
296
+ last_role = role
297
+ if not end and not delta.get("content"):
298
+ delta = {"role": "assistant", "content": ""}
299
+ chunk_new_data["choices"][0]["delta"] = delta
300
+ chunk_new_data["choices"][0]["finish_reason"] = finish_reason
301
+ if not service.history_disabled:
302
+ chunk_new_data.update({
303
+ "message_id": message_id,
304
+ "conversation_id": conversation_id,
305
+ })
306
+ completion_tokens += 1
307
+ yield f"data: {json.dumps(chunk_new_data)}\n\n"
308
+ elif chunk.startswith("data: [DONE]"):
309
+ logger.info(f"Response Model: {model_slug}")
310
+ yield "data: [DONE]\n\n"
311
+ else:
312
+ continue
313
+ except Exception as e:
314
+ if chunk.startswith("data: "):
315
+ chunk_data = json.loads(chunk[6:])
316
+ if chunk_data.get("error"):
317
+ logger.error(f"Error: {chunk_data.get('error')}")
318
+ yield "data: [DONE]\n\n"
319
+ break
320
+ logger.error(f"Error: {chunk}, details: {str(e)}")
321
+ continue
322
+
323
+
324
+ def get_url_from_content(content):
325
+ if isinstance(content, str) and content.startswith('http'):
326
+ try:
327
+ url = re.match(
328
+ r'(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))',
329
+ content.split(' ')[0])[0]
330
+ content = content.replace(url, '').strip()
331
+ return url, content
332
+ except Exception:
333
+ return None, content
334
+ return None, content
335
+
336
+
337
+ def format_messages_with_url(content):
338
+ url_list = []
339
+ while True:
340
+ url, content = get_url_from_content(content)
341
+ if url:
342
+ url_list.append(url)
343
+ logger.info(f"Found a file_url from messages: {url}")
344
+ else:
345
+ break
346
+ if not url_list:
347
+ return content
348
+ new_content = [
349
+ {
350
+ "type": "text",
351
+ "text": content
352
+ }
353
+ ]
354
+ for url in url_list:
355
+ new_content.append({
356
+ "type": "image_url",
357
+ "image_url": {
358
+ "url": url
359
+ }
360
+ })
361
+ return new_content
362
+
363
+
364
+ async def api_messages_to_chat(service, api_messages, upload_by_url=False):
365
+ file_tokens = 0
366
+ chat_messages = []
367
+ for api_message in api_messages:
368
+ role = api_message.get('role')
369
+ content = api_message.get('content')
370
+ if upload_by_url:
371
+ if isinstance(content, str):
372
+ content = format_messages_with_url(content)
373
+ if isinstance(content, list):
374
+ parts = []
375
+ attachments = []
376
+ content_type = "multimodal_text"
377
+ for i in content:
378
+ if i.get("type") == "text":
379
+ parts.append(i.get("text"))
380
+ elif i.get("type") == "image_url":
381
+ image_url = i.get("image_url")
382
+ url = image_url.get("url")
383
+ detail = image_url.get("detail", "auto")
384
+ file_content, mime_type = await get_file_content(url)
385
+ file_meta = await service.upload_file(file_content, mime_type)
386
+ if file_meta:
387
+ file_id = file_meta["file_id"]
388
+ file_size = file_meta["size_bytes"]
389
+ file_name = file_meta["file_name"]
390
+ mime_type = file_meta["mime_type"]
391
+ use_case = file_meta["use_case"]
392
+ if mime_type.startswith("image/"):
393
+ width, height = file_meta["width"], file_meta["height"]
394
+ file_tokens += await calculate_image_tokens(width, height, detail)
395
+ parts.append({
396
+ "content_type": "image_asset_pointer",
397
+ "asset_pointer": f"file-service://{file_id}",
398
+ "size_bytes": file_size,
399
+ "width": width,
400
+ "height": height
401
+ })
402
+ attachments.append({
403
+ "id": file_id,
404
+ "size": file_size,
405
+ "name": file_name,
406
+ "mime_type": mime_type,
407
+ "width": width,
408
+ "height": height
409
+ })
410
+ else:
411
+ if not use_case == "ace_upload":
412
+ await service.check_upload(file_id)
413
+ file_tokens += file_size // 1000
414
+ attachments.append({
415
+ "id": file_id,
416
+ "size": file_size,
417
+ "name": file_name,
418
+ "mime_type": mime_type,
419
+ })
420
+ metadata = {
421
+ "attachments": attachments
422
+ }
423
+ else:
424
+ content_type = "text"
425
+ parts = [content]
426
+ metadata = {}
427
+ chat_message = {
428
+ "id": f"{uuid.uuid4()}",
429
+ "author": {"role": role},
430
+ "content": {"content_type": content_type, "parts": parts},
431
+ "metadata": metadata
432
+ }
433
+ chat_messages.append(chat_message)
434
+ text_tokens = await num_tokens_from_messages(api_messages, service.resp_model)
435
+ prompt_tokens = text_tokens + file_tokens
436
+ return chat_messages, prompt_tokens
chatgpt/chatLimit.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ from datetime import datetime
3
+
4
+ from utils.Logger import logger
5
+
6
+ limit_details = {}
7
+
8
+
9
+ def check_is_limit(detail, token, model):
10
+ if token and isinstance(detail, dict) and detail.get('clears_in'):
11
+ clear_time = int(time.time()) + detail.get('clears_in')
12
+ limit_details.setdefault(token, {})[model] = clear_time
13
+ logger.info(f"{token[:40]}: Reached {model} limit, will be cleared at {datetime.fromtimestamp(clear_time).replace(microsecond=0)}")
14
+
15
+
16
+ async def handle_request_limit(token, model):
17
+ try:
18
+ if limit_details.get(token) and model in limit_details[token]:
19
+ limit_time = limit_details[token][model]
20
+ is_limit = limit_time > int(time.time())
21
+ if is_limit:
22
+ clear_date = datetime.fromtimestamp(limit_time).replace(microsecond=0)
23
+ result = f"Request limit exceeded. You can continue with the default model now, or try again after {clear_date}"
24
+ logger.info(result)
25
+ return result
26
+ else:
27
+ del limit_details[token][model]
28
+ return None
29
+ except KeyError as e:
30
+ logger.error(f"Key error: {e}")
31
+ return None
32
+ except Exception as e:
33
+ logger.error(f"An unexpected error occurred: {e}")
34
+ return None
chatgpt/fp.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import random
3
+ import uuid
4
+
5
+ import ua_generator
6
+ from ua_generator.data.version import VersionRange
7
+ from ua_generator.options import Options
8
+
9
+ import utils.globals as globals
10
+ from utils import configs
11
+
12
+
13
+ def get_fp(req_token):
14
+ fp = globals.fp_map.get(req_token, {})
15
+ if fp and fp.get("user-agent") and fp.get("impersonate"):
16
+ if "proxy_url" in fp.keys() and (fp["proxy_url"] is None or fp["proxy_url"] not in configs.proxy_url_list):
17
+ fp["proxy_url"] = random.choice(configs.proxy_url_list) if configs.proxy_url_list else None
18
+ globals.fp_map[req_token] = fp
19
+ with open(globals.FP_FILE, "w", encoding="utf-8") as f:
20
+ json.dump(globals.fp_map, f, indent=4)
21
+ if globals.impersonate_list and "impersonate" in fp.keys() and fp["impersonate"] not in globals.impersonate_list:
22
+ fp["impersonate"] = random.choice(globals.impersonate_list)
23
+ globals.fp_map[req_token] = fp
24
+ with open(globals.FP_FILE, "w", encoding="utf-8") as f:
25
+ json.dump(globals.fp_map, f, indent=4)
26
+ if configs.user_agents_list and "user-agent" in fp.keys() and fp["user-agent"] not in configs.user_agents_list:
27
+ fp["user-agent"] = random.choice(configs.user_agents_list)
28
+ globals.fp_map[req_token] = fp
29
+ with open(globals.FP_FILE, "w", encoding="utf-8") as f:
30
+ json.dump(globals.fp_map, f, indent=4)
31
+ fp = {k.lower(): v for k, v in fp.items()}
32
+ return fp
33
+ else:
34
+ options = Options(version_ranges={
35
+ 'chrome': VersionRange(min_version=124),
36
+ 'edge': VersionRange(min_version=124),
37
+ })
38
+ ua = ua_generator.generate(
39
+ device=configs.device_tuple if configs.device_tuple else ('desktop'),
40
+ browser=configs.browser_tuple if configs.browser_tuple else ('chrome', 'edge', 'firefox', 'safari'),
41
+ platform=configs.platform_tuple if configs.platform_tuple else ('windows', 'macos'),
42
+ options=options
43
+ )
44
+ fp = {
45
+ "user-agent": ua.text if not configs.user_agents_list else random.choice(configs.user_agents_list),
46
+ "impersonate": random.choice(globals.impersonate_list),
47
+ "proxy_url": random.choice(configs.proxy_url_list) if configs.proxy_url_list else None,
48
+ "oai-device-id": str(uuid.uuid4())
49
+ }
50
+ if ua.device == "desktop" and ua.browser in ("chrome", "edge"):
51
+ fp["sec-ch-ua-platform"] = ua.ch.platform
52
+ fp["sec-ch-ua"] = ua.ch.brands
53
+ fp["sec-ch-ua-mobile"] = ua.ch.mobile
54
+
55
+ if not req_token:
56
+ return fp
57
+ else:
58
+ globals.fp_map[req_token] = fp
59
+ with open(globals.FP_FILE, "w", encoding="utf-8") as f:
60
+ json.dump(globals.fp_map, f, indent=4)
61
+ return fp
chatgpt/proofofWork.py ADDED
@@ -0,0 +1,505 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import hashlib
2
+ import json
3
+ import random
4
+ import re
5
+ import time
6
+ import uuid
7
+ from datetime import datetime, timedelta, timezone
8
+ from html.parser import HTMLParser
9
+
10
+ import pybase64
11
+
12
+ from utils.Logger import logger
13
+ from utils.configs import conversation_only
14
+
15
+ cores = [8, 16, 24, 32]
16
+ timeLayout = "%a %b %d %Y %H:%M:%S"
17
+
18
+ cached_scripts = []
19
+ cached_dpl = ""
20
+ cached_time = 0
21
+ cached_require_proof = ""
22
+
23
+ navigator_key = [
24
+ "registerProtocolHandler−function registerProtocolHandler() { [native code] }",
25
+ "storage−[object StorageManager]",
26
+ "locks−[object LockManager]",
27
+ "appCodeName−Mozilla",
28
+ "permissions−[object Permissions]",
29
+ "share−function share() { [native code] }",
30
+ "webdriver−false",
31
+ "managed−[object NavigatorManagedData]",
32
+ "canShare−function canShare() { [native code] }",
33
+ "vendor−Google Inc.",
34
+ "vendor−Google Inc.",
35
+ "mediaDevices−[object MediaDevices]",
36
+ "vibrate−function vibrate() { [native code] }",
37
+ "storageBuckets−[object StorageBucketManager]",
38
+ "mediaCapabilities−[object MediaCapabilities]",
39
+ "getGamepads−function getGamepads() { [native code] }",
40
+ "bluetooth−[object Bluetooth]",
41
+ "share−function share() { [native code] }",
42
+ "cookieEnabled−true",
43
+ "virtualKeyboard−[object VirtualKeyboard]",
44
+ "product−Gecko",
45
+ "mediaDevices−[object MediaDevices]",
46
+ "canShare−function canShare() { [native code] }",
47
+ "getGamepads−function getGamepads() { [native code] }",
48
+ "product−Gecko",
49
+ "xr−[object XRSystem]",
50
+ "clipboard−[object Clipboard]",
51
+ "storageBuckets−[object StorageBucketManager]",
52
+ "unregisterProtocolHandler−function unregisterProtocolHandler() { [native code] }",
53
+ "productSub−20030107",
54
+ "login−[object NavigatorLogin]",
55
+ "vendorSub−",
56
+ "login−[object NavigatorLogin]",
57
+ "getInstalledRelatedApps−function getInstalledRelatedApps() { [native code] }",
58
+ "mediaDevices−[object MediaDevices]",
59
+ "locks−[object LockManager]",
60
+ "webkitGetUserMedia−function webkitGetUserMedia() { [native code] }",
61
+ "vendor−Google Inc.",
62
+ "xr−[object XRSystem]",
63
+ "mediaDevices−[object MediaDevices]",
64
+ "virtualKeyboard−[object VirtualKeyboard]",
65
+ "virtualKeyboard−[object VirtualKeyboard]",
66
+ "appName−Netscape",
67
+ "storageBuckets−[object StorageBucketManager]",
68
+ "presentation−[object Presentation]",
69
+ "onLine−true",
70
+ "mimeTypes−[object MimeTypeArray]",
71
+ "credentials−[object CredentialsContainer]",
72
+ "presentation−[object Presentation]",
73
+ "getGamepads−function getGamepads() { [native code] }",
74
+ "vendorSub−",
75
+ "virtualKeyboard−[object VirtualKeyboard]",
76
+ "serviceWorker−[object ServiceWorkerContainer]",
77
+ "xr−[object XRSystem]",
78
+ "product−Gecko",
79
+ "keyboard−[object Keyboard]",
80
+ "gpu−[object GPU]",
81
+ "getInstalledRelatedApps−function getInstalledRelatedApps() { [native code] }",
82
+ "webkitPersistentStorage−[object DeprecatedStorageQuota]",
83
+ "doNotTrack",
84
+ "clearAppBadge−function clearAppBadge() { [native code] }",
85
+ "presentation−[object Presentation]",
86
+ "serial−[object Serial]",
87
+ "locks−[object LockManager]",
88
+ "requestMIDIAccess−function requestMIDIAccess() { [native code] }",
89
+ "locks−[object LockManager]",
90
+ "requestMediaKeySystemAccess−function requestMediaKeySystemAccess() { [native code] }",
91
+ "vendor−Google Inc.",
92
+ "pdfViewerEnabled−true",
93
+ "language−zh-CN",
94
+ "setAppBadge−function setAppBadge() { [native code] }",
95
+ "geolocation−[object Geolocation]",
96
+ "userAgentData−[object NavigatorUAData]",
97
+ "mediaCapabilities−[object MediaCapabilities]",
98
+ "requestMIDIAccess−function requestMIDIAccess() { [native code] }",
99
+ "getUserMedia−function getUserMedia() { [native code] }",
100
+ "mediaDevices−[object MediaDevices]",
101
+ "webkitPersistentStorage−[object DeprecatedStorageQuota]",
102
+ "sendBeacon−function sendBeacon() { [native code] }",
103
+ "hardwareConcurrency−32",
104
+ "credentials−[object CredentialsContainer]",
105
+ "storage−[object StorageManager]",
106
+ "cookieEnabled−true",
107
+ "pdfViewerEnabled−true",
108
+ "windowControlsOverlay−[object WindowControlsOverlay]",
109
+ "scheduling−[object Scheduling]",
110
+ "pdfViewerEnabled−true",
111
+ "hardwareConcurrency−32",
112
+ "xr−[object XRSystem]",
113
+ "webdriver−false",
114
+ "getInstalledRelatedApps−function getInstalledRelatedApps() { [native code] }",
115
+ "getInstalledRelatedApps−function getInstalledRelatedApps() { [native code] }",
116
+ "bluetooth−[object Bluetooth]"
117
+ ]
118
+ document_key = ['_reactListeningo743lnnpvdg', 'location']
119
+ window_key = [
120
+ "0",
121
+ "window",
122
+ "self",
123
+ "document",
124
+ "name",
125
+ "location",
126
+ "customElements",
127
+ "history",
128
+ "navigation",
129
+ "locationbar",
130
+ "menubar",
131
+ "personalbar",
132
+ "scrollbars",
133
+ "statusbar",
134
+ "toolbar",
135
+ "status",
136
+ "closed",
137
+ "frames",
138
+ "length",
139
+ "top",
140
+ "opener",
141
+ "parent",
142
+ "frameElement",
143
+ "navigator",
144
+ "origin",
145
+ "external",
146
+ "screen",
147
+ "innerWidth",
148
+ "innerHeight",
149
+ "scrollX",
150
+ "pageXOffset",
151
+ "scrollY",
152
+ "pageYOffset",
153
+ "visualViewport",
154
+ "screenX",
155
+ "screenY",
156
+ "outerWidth",
157
+ "outerHeight",
158
+ "devicePixelRatio",
159
+ "clientInformation",
160
+ "screenLeft",
161
+ "screenTop",
162
+ "styleMedia",
163
+ "onsearch",
164
+ "isSecureContext",
165
+ "trustedTypes",
166
+ "performance",
167
+ "onappinstalled",
168
+ "onbeforeinstallprompt",
169
+ "crypto",
170
+ "indexedDB",
171
+ "sessionStorage",
172
+ "localStorage",
173
+ "onbeforexrselect",
174
+ "onabort",
175
+ "onbeforeinput",
176
+ "onbeforematch",
177
+ "onbeforetoggle",
178
+ "onblur",
179
+ "oncancel",
180
+ "oncanplay",
181
+ "oncanplaythrough",
182
+ "onchange",
183
+ "onclick",
184
+ "onclose",
185
+ "oncontentvisibilityautostatechange",
186
+ "oncontextlost",
187
+ "oncontextmenu",
188
+ "oncontextrestored",
189
+ "oncuechange",
190
+ "ondblclick",
191
+ "ondrag",
192
+ "ondragend",
193
+ "ondragenter",
194
+ "ondragleave",
195
+ "ondragover",
196
+ "ondragstart",
197
+ "ondrop",
198
+ "ondurationchange",
199
+ "onemptied",
200
+ "onended",
201
+ "onerror",
202
+ "onfocus",
203
+ "onformdata",
204
+ "oninput",
205
+ "oninvalid",
206
+ "onkeydown",
207
+ "onkeypress",
208
+ "onkeyup",
209
+ "onload",
210
+ "onloadeddata",
211
+ "onloadedmetadata",
212
+ "onloadstart",
213
+ "onmousedown",
214
+ "onmouseenter",
215
+ "onmouseleave",
216
+ "onmousemove",
217
+ "onmouseout",
218
+ "onmouseover",
219
+ "onmouseup",
220
+ "onmousewheel",
221
+ "onpause",
222
+ "onplay",
223
+ "onplaying",
224
+ "onprogress",
225
+ "onratechange",
226
+ "onreset",
227
+ "onresize",
228
+ "onscroll",
229
+ "onsecuritypolicyviolation",
230
+ "onseeked",
231
+ "onseeking",
232
+ "onselect",
233
+ "onslotchange",
234
+ "onstalled",
235
+ "onsubmit",
236
+ "onsuspend",
237
+ "ontimeupdate",
238
+ "ontoggle",
239
+ "onvolumechange",
240
+ "onwaiting",
241
+ "onwebkitanimationend",
242
+ "onwebkitanimationiteration",
243
+ "onwebkitanimationstart",
244
+ "onwebkittransitionend",
245
+ "onwheel",
246
+ "onauxclick",
247
+ "ongotpointercapture",
248
+ "onlostpointercapture",
249
+ "onpointerdown",
250
+ "onpointermove",
251
+ "onpointerrawupdate",
252
+ "onpointerup",
253
+ "onpointercancel",
254
+ "onpointerover",
255
+ "onpointerout",
256
+ "onpointerenter",
257
+ "onpointerleave",
258
+ "onselectstart",
259
+ "onselectionchange",
260
+ "onanimationend",
261
+ "onanimationiteration",
262
+ "onanimationstart",
263
+ "ontransitionrun",
264
+ "ontransitionstart",
265
+ "ontransitionend",
266
+ "ontransitioncancel",
267
+ "onafterprint",
268
+ "onbeforeprint",
269
+ "onbeforeunload",
270
+ "onhashchange",
271
+ "onlanguagechange",
272
+ "onmessage",
273
+ "onmessageerror",
274
+ "onoffline",
275
+ "ononline",
276
+ "onpagehide",
277
+ "onpageshow",
278
+ "onpopstate",
279
+ "onrejectionhandled",
280
+ "onstorage",
281
+ "onunhandledrejection",
282
+ "onunload",
283
+ "crossOriginIsolated",
284
+ "scheduler",
285
+ "alert",
286
+ "atob",
287
+ "blur",
288
+ "btoa",
289
+ "cancelAnimationFrame",
290
+ "cancelIdleCallback",
291
+ "captureEvents",
292
+ "clearInterval",
293
+ "clearTimeout",
294
+ "close",
295
+ "confirm",
296
+ "createImageBitmap",
297
+ "fetch",
298
+ "find",
299
+ "focus",
300
+ "getComputedStyle",
301
+ "getSelection",
302
+ "matchMedia",
303
+ "moveBy",
304
+ "moveTo",
305
+ "open",
306
+ "postMessage",
307
+ "print",
308
+ "prompt",
309
+ "queueMicrotask",
310
+ "releaseEvents",
311
+ "reportError",
312
+ "requestAnimationFrame",
313
+ "requestIdleCallback",
314
+ "resizeBy",
315
+ "resizeTo",
316
+ "scroll",
317
+ "scrollBy",
318
+ "scrollTo",
319
+ "setInterval",
320
+ "setTimeout",
321
+ "stop",
322
+ "structuredClone",
323
+ "webkitCancelAnimationFrame",
324
+ "webkitRequestAnimationFrame",
325
+ "chrome",
326
+ "caches",
327
+ "cookieStore",
328
+ "ondevicemotion",
329
+ "ondeviceorientation",
330
+ "ondeviceorientationabsolute",
331
+ "launchQueue",
332
+ "documentPictureInPicture",
333
+ "getScreenDetails",
334
+ "queryLocalFonts",
335
+ "showDirectoryPicker",
336
+ "showOpenFilePicker",
337
+ "showSaveFilePicker",
338
+ "originAgentCluster",
339
+ "onpageswap",
340
+ "onpagereveal",
341
+ "credentialless",
342
+ "speechSynthesis",
343
+ "onscrollend",
344
+ "webkitRequestFileSystem",
345
+ "webkitResolveLocalFileSystemURL",
346
+ "sendMsgToSolverCS",
347
+ "webpackChunk_N_E",
348
+ "__next_set_public_path__",
349
+ "next",
350
+ "__NEXT_DATA__",
351
+ "__SSG_MANIFEST_CB",
352
+ "__NEXT_P",
353
+ "_N_E",
354
+ "regeneratorRuntime",
355
+ "__REACT_INTL_CONTEXT__",
356
+ "DD_RUM",
357
+ "_",
358
+ "filterCSS",
359
+ "filterXSS",
360
+ "__SEGMENT_INSPECTOR__",
361
+ "__NEXT_PRELOADREADY",
362
+ "Intercom",
363
+ "__MIDDLEWARE_MATCHERS",
364
+ "__STATSIG_SDK__",
365
+ "__STATSIG_JS_SDK__",
366
+ "__STATSIG_RERENDER_OVERRIDE__",
367
+ "_oaiHandleSessionExpired",
368
+ "__BUILD_MANIFEST",
369
+ "__SSG_MANIFEST",
370
+ "__intercomAssignLocation",
371
+ "__intercomReloadLocation"
372
+ ]
373
+
374
+
375
+ class ScriptSrcParser(HTMLParser):
376
+ def handle_starttag(self, tag, attrs):
377
+ global cached_scripts, cached_dpl, cached_time
378
+ if tag == "script":
379
+ attrs_dict = dict(attrs)
380
+ if "src" in attrs_dict:
381
+ src = attrs_dict["src"]
382
+ cached_scripts.append(src)
383
+ match = re.search(r"c/[^/]*/_", src)
384
+ if match:
385
+ cached_dpl = match.group(0)
386
+ cached_time = int(time.time())
387
+
388
+
389
+ def get_data_build_from_html(html_content):
390
+ global cached_scripts, cached_dpl, cached_time
391
+ parser = ScriptSrcParser()
392
+ parser.feed(html_content)
393
+ if not cached_scripts:
394
+ cached_scripts.append("https://chatgpt.com/backend-api/sentinel/sdk.js")
395
+ if not cached_dpl:
396
+ match = re.search(r'<html[^>]*data-build="([^"]*)"', html_content)
397
+ if match:
398
+ data_build = match.group(1)
399
+ cached_dpl = data_build
400
+ cached_time = int(time.time())
401
+ logger.info(f"Found dpl: {cached_dpl}")
402
+
403
+
404
+ async def get_dpl(service):
405
+ global cached_scripts, cached_dpl, cached_time
406
+ if int(time.time()) - cached_time < 15 * 60:
407
+ return True
408
+ headers = service.base_headers.copy()
409
+ cached_scripts = []
410
+ cached_dpl = ""
411
+ try:
412
+ if conversation_only:
413
+ return True
414
+ r = await service.s.get(f"{service.host_url}/", headers=headers, timeout=5)
415
+ r.raise_for_status()
416
+ get_data_build_from_html(r.text)
417
+ if not cached_dpl:
418
+ raise Exception("No Cached DPL")
419
+ else:
420
+ return True
421
+ except Exception as e:
422
+ logger.info(f"Failed to get dpl: {e}")
423
+ cached_dpl = None
424
+ cached_time = int(time.time())
425
+ return False
426
+
427
+
428
+ def get_parse_time():
429
+ now = datetime.now(timezone(timedelta(hours=-5)))
430
+ return now.strftime(timeLayout) + " GMT-0500 (Eastern Standard Time)"
431
+
432
+
433
+ def get_config(user_agent):
434
+ config = [
435
+ random.randint(1080, 1440+1080),
436
+ get_parse_time(),
437
+ 4294705152,
438
+ 0,
439
+ user_agent,
440
+ random.choice(cached_scripts) if cached_scripts else "",
441
+ cached_dpl,
442
+ "en-US",
443
+ "en-US,es-US,en,es",
444
+ 0,
445
+ random.choice(navigator_key),
446
+ random.choice(document_key),
447
+ random.choice(window_key),
448
+ time.perf_counter() * 1000,
449
+ str(uuid.uuid4()),
450
+ "",
451
+ random.choice(cores),
452
+ time.time() * 1000 - (time.perf_counter() * 1000),
453
+ ]
454
+ return config
455
+
456
+
457
+ def get_answer_token(seed, diff, config):
458
+ start = time.time()
459
+ answer, solved = generate_answer(seed, diff, config)
460
+ end = time.time()
461
+ logger.info(f'diff: {diff}, time: {int((end - start) * 1e6) / 1e3}ms, solved: {solved}')
462
+ return "gAAAAAB" + answer, solved
463
+
464
+
465
+ def generate_answer(seed, diff, config):
466
+ diff_len = len(diff)
467
+ seed_encoded = seed.encode()
468
+ static_config_part1 = (json.dumps(config[:3], separators=(',', ':'), ensure_ascii=False)[:-1] + ',').encode()
469
+ static_config_part2 = (',' + json.dumps(config[4:9], separators=(',', ':'), ensure_ascii=False)[1:-1] + ',').encode()
470
+ static_config_part3 = (',' + json.dumps(config[10:], separators=(',', ':'), ensure_ascii=False)[1:]).encode()
471
+
472
+ target_diff = bytes.fromhex(diff)
473
+
474
+ for i in range(500000):
475
+ dynamic_json_i = str(i).encode()
476
+ dynamic_json_j = str(i >> 1).encode()
477
+ final_json_bytes = static_config_part1 + dynamic_json_i + static_config_part2 + dynamic_json_j + static_config_part3
478
+ base_encode = pybase64.b64encode(final_json_bytes)
479
+ hash_value = hashlib.sha3_512(seed_encoded + base_encode).digest()
480
+ if hash_value[:diff_len] <= target_diff:
481
+ return base_encode.decode(), True
482
+
483
+ return "wQ8Lk5FbGpA2NcR9dShT6gYjU7VxZ4D" + pybase64.b64encode(f'"{seed}"'.encode()).decode(), False
484
+
485
+
486
+ def get_requirements_token(config):
487
+ require, solved = generate_answer(format(random.random()), "0fffff", config)
488
+ return 'gAAAAAC' + require
489
+
490
+
491
+ if __name__ == "__main__":
492
+ # cached_scripts.append(
493
+ # "https://cdn.oaistatic.com/_next/static/cXh69klOLzS0Gy2joLDRS/_ssgManifest.js?dpl=453ebaec0d44c2decab71692e1bfe39be35a24b3")
494
+ # cached_dpl = "453ebaec0d44c2decab71692e1bfe39be35a24b3"
495
+ # cached_time = int(time.time())
496
+ # for i in range(10):
497
+ # seed = format(random.random())
498
+ # diff = "000032"
499
+ # config = get_config("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome")
500
+ # answer = get_answer_token(seed, diff, config)
501
+ cached_scripts.append(
502
+ "https://cdn.oaistatic.com/_next/static/cXh69klOLzS0Gy2joLDRS/_ssgManifest.js?dpl=453ebaec0d44c2decab71692e1bfe39be35a24b3")
503
+ cached_dpl = "prod-f501fe933b3edf57aea882da888e1a544df99840"
504
+ config = get_config("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36")
505
+ get_requirements_token(config)
chatgpt/refreshToken.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import random
3
+ import time
4
+
5
+ from fastapi import HTTPException
6
+
7
+ from utils.Client import Client
8
+ from utils.Logger import logger
9
+ from utils.configs import proxy_url_list
10
+ import utils.globals as globals
11
+
12
+
13
+ async def rt2ac(refresh_token, force_refresh=False):
14
+ if not force_refresh and (refresh_token in globals.refresh_map and int(time.time()) - globals.refresh_map.get(refresh_token, {}).get("timestamp", 0) < 5 * 24 * 60 * 60):
15
+ access_token = globals.refresh_map[refresh_token]["token"]
16
+ # logger.info(f"refresh_token -> access_token from cache")
17
+ return access_token
18
+ else:
19
+ try:
20
+ access_token = await chat_refresh(refresh_token)
21
+ globals.refresh_map[refresh_token] = {"token": access_token, "timestamp": int(time.time())}
22
+ with open(globals.REFRESH_MAP_FILE, "w") as f:
23
+ json.dump(globals.refresh_map, f, indent=4)
24
+ logger.info(f"refresh_token -> access_token with openai: {access_token}")
25
+ return access_token
26
+ except HTTPException as e:
27
+ raise HTTPException(status_code=e.status_code, detail=e.detail)
28
+
29
+
30
+ async def chat_refresh(refresh_token):
31
+ data = {
32
+ "client_id": "pdlLIX2Y72MIl2rhLhTE9VV9bN905kBh",
33
+ "grant_type": "refresh_token",
34
+ "redirect_uri": "com.openai.chat://auth0.openai.com/ios/com.openai.chat/callback",
35
+ "refresh_token": refresh_token
36
+ }
37
+ client = Client(proxy=random.choice(proxy_url_list) if proxy_url_list else None)
38
+ try:
39
+ r = await client.post("https://auth0.openai.com/oauth/token", json=data, timeout=5)
40
+ if r.status_code == 200:
41
+ access_token = r.json()['access_token']
42
+ return access_token
43
+ else:
44
+ if "invalid_grant" in r.text or "access_denied" in r.text:
45
+ if refresh_token not in globals.error_token_list:
46
+ globals.error_token_list.append(refresh_token)
47
+ with open(globals.ERROR_TOKENS_FILE, "a", encoding="utf-8") as f:
48
+ f.write(refresh_token + "\n")
49
+ raise Exception(r.text)
50
+ else:
51
+ raise Exception(r.text[:300])
52
+ except Exception as e:
53
+ logger.error(f"Failed to refresh access_token `{refresh_token}`: {str(e)}")
54
+ raise HTTPException(status_code=500, detail=f"Failed to refresh access_token.")
55
+ finally:
56
+ await client.close()
57
+ del client
chatgpt/turnstile.py ADDED
@@ -0,0 +1,268 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pybase64
2
+ import json
3
+ import random
4
+ import time
5
+ from typing import Any, Callable, Dict, List, Union
6
+
7
+
8
+ class OrderedMap:
9
+ def __init__(self):
10
+ self.keys = []
11
+ self.values = {}
12
+
13
+ def add(self, key: str, value: Any):
14
+ if key not in self.values:
15
+ self.keys.append(key)
16
+ self.values[key] = value
17
+
18
+ def to_json(self):
19
+ return json.dumps({k: self.values[k] for k in self.keys})
20
+
21
+
22
+ TurnTokenList = List[List[Any]]
23
+ FloatMap = Dict[float, Any]
24
+ StringMap = Dict[str, Any]
25
+ FuncType = Callable[..., Any]
26
+
27
+
28
+ def get_turnstile_token(dx: str, p: str) -> Union[str, None]:
29
+ try:
30
+ decoded_bytes = pybase64.b64decode(dx)
31
+ return process_turnstile_token(decoded_bytes.decode(), p)
32
+ except Exception as e:
33
+ print(f"Error in get_turnstile_token: {e}")
34
+ return None
35
+
36
+
37
+ def process_turnstile_token(dx: str, p: str) -> str:
38
+ result = []
39
+ p_length = len(p)
40
+ if p_length != 0:
41
+ for i, r in enumerate(dx):
42
+ result.append(chr(ord(r) ^ ord(p[i % p_length])))
43
+ else:
44
+ result = list(dx)
45
+ return ''.join(result)
46
+
47
+
48
+ def is_slice(input_val: Any) -> bool:
49
+ return isinstance(input_val, (list, tuple))
50
+
51
+
52
+ def is_float(input_val: Any) -> bool:
53
+ return isinstance(input_val, float)
54
+
55
+
56
+ def is_string(input_val: Any) -> bool:
57
+ return isinstance(input_val, str)
58
+
59
+
60
+ def to_str(input_val: Any) -> str:
61
+ if input_val is None:
62
+ return "undefined"
63
+ elif is_float(input_val):
64
+ return str(input_val)
65
+ elif is_string(input_val):
66
+ special_cases = {
67
+ "window.Math": "[object Math]",
68
+ "window.Reflect": "[object Reflect]",
69
+ "window.performance": "[object Performance]",
70
+ "window.localStorage": "[object Storage]",
71
+ "window.Object": "function Object() { [native code] }",
72
+ "window.Reflect.set": "function set() { [native code] }",
73
+ "window.performance.now": "function () { [native code] }",
74
+ "window.Object.create": "function create() { [native code] }",
75
+ "window.Object.keys": "function keys() { [native code] }",
76
+ "window.Math.random": "function random() { [native code] }"
77
+ }
78
+ return special_cases.get(input_val, input_val)
79
+ elif isinstance(input_val, list) and all(isinstance(item, str) for item in input_val):
80
+ return ','.join(input_val)
81
+ else:
82
+ return str(input_val)
83
+
84
+
85
+ def get_func_map() -> FloatMap:
86
+ process_map: FloatMap = {}
87
+
88
+ def func_1(e: float, t: float):
89
+ e_str = to_str(process_map[e])
90
+ t_str = to_str(process_map[t])
91
+ res = process_turnstile_token(e_str, t_str)
92
+ process_map[e] = res
93
+
94
+ def func_2(e: float, t: Any):
95
+ process_map[e] = t
96
+
97
+ def func_5(e: float, t: float):
98
+ n = process_map[e]
99
+ tres = process_map[t]
100
+ if is_slice(n):
101
+ nt = n + [tres]
102
+ process_map[e] = nt
103
+ else:
104
+ if is_string(n) or is_string(tres):
105
+ res = to_str(n) + to_str(tres)
106
+ elif is_float(n) and is_float(tres):
107
+ res = n + tres
108
+ else:
109
+ res = "NaN"
110
+ process_map[e] = res
111
+
112
+ def func_6(e: float, t: float, n: float):
113
+ tv = process_map[t]
114
+ nv = process_map[n]
115
+ if is_string(tv) and is_string(nv):
116
+ res = f"{tv}.{nv}"
117
+ if res == "window.document.location":
118
+ process_map[e] = "https://chatgpt.com/"
119
+ else:
120
+ process_map[e] = res
121
+ else:
122
+ print("func type 6 error")
123
+
124
+ def func_24(e: float, t: float, n: float):
125
+ tv = process_map[t]
126
+ nv = process_map[n]
127
+ if is_string(tv) and is_string(nv):
128
+ process_map[e] = f"{tv}.{nv}"
129
+ else:
130
+ print("func type 24 error")
131
+
132
+ def func_7(e: float, *args):
133
+ n = [process_map[arg] for arg in args]
134
+ ev = process_map[e]
135
+ if isinstance(ev, str):
136
+ if ev == "window.Reflect.set":
137
+ obj = n[0]
138
+ key_str = str(n[1])
139
+ val = n[2]
140
+ obj.add(key_str, val)
141
+ elif callable(ev):
142
+ ev(*n)
143
+
144
+ def func_17(e: float, t: float, *args):
145
+ i = [process_map[arg] for arg in args]
146
+ tv = process_map[t]
147
+ res = None
148
+ if isinstance(tv, str):
149
+ if tv == "window.performance.now":
150
+ current_time = time.time_ns()
151
+ elapsed_ns = current_time - int(start_time * 1e9)
152
+ res = (elapsed_ns + random.random()) / 1e6
153
+ elif tv == "window.Object.create":
154
+ res = OrderedMap()
155
+ elif tv == "window.Object.keys":
156
+ if isinstance(i[0], str) and i[0] == "window.localStorage":
157
+ res = ["STATSIG_LOCAL_STORAGE_INTERNAL_STORE_V4", "STATSIG_LOCAL_STORAGE_STABLE_ID",
158
+ "client-correlated-secret", "oai/apps/capExpiresAt", "oai-did",
159
+ "STATSIG_LOCAL_STORAGE_LOGGING_REQUEST", "UiState.isNavigationCollapsed.1"]
160
+ elif tv == "window.Math.random":
161
+ res = random.random()
162
+ elif callable(tv):
163
+ res = tv(*i)
164
+ process_map[e] = res
165
+
166
+ def func_8(e: float, t: float):
167
+ process_map[e] = process_map[t]
168
+
169
+ def func_14(e: float, t: float):
170
+ tv = process_map[t]
171
+ if is_string(tv):
172
+ token_list = json.loads(tv)
173
+ process_map[e] = token_list
174
+ else:
175
+ print("func type 14 error")
176
+
177
+ def func_15(e: float, t: float):
178
+ tv = process_map[t]
179
+ process_map[e] = json.dumps(tv)
180
+
181
+ def func_18(e: float):
182
+ ev = process_map[e]
183
+ e_str = to_str(ev)
184
+ decoded = pybase64.b64decode(e_str).decode()
185
+ process_map[e] = decoded
186
+
187
+ def func_19(e: float):
188
+ ev = process_map[e]
189
+ e_str = to_str(ev)
190
+ encoded = pybase64.b64encode(e_str.encode()).decode()
191
+ process_map[e] = encoded
192
+
193
+ def func_20(e: float, t: float, n: float, *args):
194
+ o = [process_map[arg] for arg in args]
195
+ ev = process_map[e]
196
+ tv = process_map[t]
197
+ if ev == tv:
198
+ nv = process_map[n]
199
+ if callable(nv):
200
+ nv(*o)
201
+ else:
202
+ print("func type 20 error")
203
+
204
+ def func_21(*args):
205
+ pass
206
+
207
+ def func_23(e: float, t: float, *args):
208
+ i = list(args)
209
+ ev = process_map[e]
210
+ tv = process_map[t]
211
+ if ev is not None:
212
+ if callable(tv):
213
+ tv(*i)
214
+
215
+ process_map.update({
216
+ 1: func_1, 2: func_2, 5: func_5, 6: func_6, 24: func_24, 7: func_7,
217
+ 17: func_17, 8: func_8, 10: "window", 14: func_14, 15: func_15,
218
+ 18: func_18, 19: func_19, 20: func_20, 21: func_21, 23: func_23
219
+ })
220
+
221
+ return process_map
222
+
223
+ start_time = 0
224
+
225
+
226
+ def process_turnstile(dx: str, p: str) -> str:
227
+ global start_time
228
+ start_time = time.time()
229
+ tokens = get_turnstile_token(dx, p)
230
+ if tokens is None:
231
+ return ""
232
+
233
+ token_list = json.loads(tokens)
234
+ # print(token_list)
235
+ res = ""
236
+ process_map = get_func_map()
237
+
238
+ def func_3(e: str):
239
+ nonlocal res
240
+ res = pybase64.b64encode(e.encode()).decode()
241
+
242
+ process_map[3] = func_3
243
+ process_map[9] = token_list
244
+ process_map[16] = p
245
+
246
+ for token in token_list:
247
+ try:
248
+ e = token[0]
249
+ t = token[1:]
250
+ f = process_map.get(e)
251
+ if callable(f):
252
+ f(*t)
253
+ else:
254
+ pass
255
+ # print(f"Warning: No function found for key {e}")
256
+ except Exception as exc:
257
+ pass
258
+ # print(f"Error processing token {token}: {exc}")
259
+
260
+ return res
261
+
262
+
263
+ if __name__ == "__main__":
264
+ result = process_turnstile(
265
+ "PBp5bWF1cHlLe1ttQhRfaTdmXEpidGdEYU5JdGJpR3xfHFVuGHVEY0tZVG18Vh54RWJ5CXpxKXl3SUZ7b2FZAWJaTBl6RGQZURh8BndUcRlQVgoYalAca2QUX24ffQZgdVVbbmBrAH9FV08Rb2oVVgBeQVRrWFp5VGZMYWNyMnoSN0FpaQgFT1l1f3h7c1RtcQUqY1kZbFJ5BQRiZEJXS3RvHGtieh9PaBlHaXhVWnVLRUlKdwsdbUtbKGFaAlN4a0V/emUJe2J2dl9BZkAxZWU/WGocRUBnc3VyT3F4WkJmYSthdBIGf0RwQ2FjAUBnd3ZEelgbVUEIDAJjS1VZbU9sSWFjfk55J2lZFV0HWX1cbVV5dWdAfkFIAVQVbloUXQtYaAR+VXhUF1BZdG4CBHRyK21AG1JaHhBFaBwCWUlocyQGVT4NBzNON2ASFVtXeQRET1kARndjUEBDT2RKeQN7RmJjeVtvZGpDeWJ1EHxafVd+Wk1AbzdLVTpafkd9dWZKeARecGJrS0xcenZIEEJQOmcFa01menFOeVRiSGFZC1JnWUA0SU08QGgeDFFgY34YWXAdZHYaHRhANFRMOV0CZmBfVExTWh9lZlVpSnx6eQURb2poa2RkQVJ0cmF0bwJbQgB6RlRbQHRQaQFKBHtENwVDSWpgHAlbTU1hXEpwdBh2eBlNY3l2UEhnblx7AmpaQ08JDDAzJUVAbn5IA2d8XX5ZFVlrYWhSXWlYQlEdZlQ/QUwuYwJgTG5GZghSRHdCYk1CWWBjclp0aWo3TWMSQmFaaAdge05FbmFhH3hxCFZuIX1BY01WVW5ABx5jfG1ZbjcZEiwwPFYQVm0sdHV8Xnl7alRuemgKZUwICklweW1heHR5Q3UqYVoSR3BCaldIc3Z8SmJOS212CAY5AmMkYmMaRn5UXEthZFsHYFx7ZHRnYV5tcFBZeHocQxUXXU0bYk0VFUZ0ZgFrSWcMRksCAwdJEBBncF12fGUVdnFNQnl4ZQB9WUclYGMRe04TQUZMf0FEbEthW357HEN2aVhAdHAMH0NPdWFicm1YbzNRBSkWMDUAOVdXbBlfRz51ah54YG5iVX9sR2t6RF1pR1RGU20MABBWQy55T3dQfmlUfmFrA35gY2AdDiBWMWVlP1hqHEVAZ3NzfE9/c1pCZWErYXQSB2BKcENjew1baXB9Rm1aG1VBCAkJY01aWW1NbklgZH5Oek1rTX9FFEB7RHNGEG9pKH1eRgFSZGJJdkcMQHUSY0IRQRkzUmFgBG90cklvVwNZThIHQXYABjFJaApCWh1qUEhnWVpiBHxDRDlAHg8kFVcCY1dCUk8VRm9obEN9e21EdnluWxN7eWt8RnFOekRTRXZKXkNPWH40YGMRXHwfRHZ7Z1JKS2R9XG1XR09qCGlaZmZ/QXwnfloWTQxIflxbSVNdSUZgHBRLKCwpQwwmXzB2NFRMOVxUTFNfH3BoRVhfWkcBYghVaSh0ZWMFeG9qBWp5eENNeGNldncHR0wBezVPTjdlSGcOTndjVkAUVl99YQFkRUE2YlNKe3ppeml2V2lvYkhGHjtbNHIALywsMScPEjEFO3Q1MQ0UGDYvK148ETYxIzEcD0gzchNcLSs+LAJxJiEQKBd5MCsXCRclFA0gBRg3axk1HTkBGyoUPRhwCwI2OAIRB2gUBRcjATt6ORQ9JDANOHFlEQITIC8VOS4GAC49GDscBBQMNQ4hDQtQZHYMHmk3BRFHeHZvcXNvd01+WXxPFF9pN2ZaSmR3Z0RkQkl7YmlHbzMsSS8HEy4PPggxGAAYBBcuJREBEQA7LAMANgEiNiZgFR5Mchs0eH83ERFsGCceZTESe2MeEgQSGwgXIgIbb38FFBAWEC1GFC42OQ0CCwcudSIpOwY6MRw7IjwYAgAYD3UbOA8AaHoHPiUkBgQmTA4FUxgAOCoJKxNmVSoANDIzAjdlDxA6ISIOKhQDEhwLPS82IT4CUFIsOyIwLD4+BBsDAww1AnMqHAIlMiMTGT0oAQlUE3QDQhIUACMxDwhGLxEXHQsSIV0FLgMaAgJ2LgsEHyEPLBcKOBtfUhg9MiAXPT5fHhA1Wg8+BxoPLgYcGS0WRSsELjIZKg8EJw4lFQAoUCcTcxASLS9BOTsZD3ERGRUhOD1YUjJxWBEBdnc9PwkQNytyED0zAQtaG3Y2ACsWXSsoPV4+DBQ2DyQ+bg0MHxVHKhAqNh8QPVkNET5fAis5Jh0uGxACKA8kOyo6IBkHIgkKdx0sAgA8SAQVHCkCLwcoBnQHGRAeAxAXOQAdKxhrNxMLJQYrKwAxHnFcOA4HIlEEAVkVDigqAwMoORQQKFkaOy0pISMoRmYDPyFLCRIqVhwCImITET04Gx8QPTMWWRQDcgstAioLGSkBTjw7ECYLeSgraxFoazw2CQcrJgU1cQ0fAB4YEykpIQMEPgJ0NUY0Lhc8IBEEWQtyNSkeECEmHitRFhsULgUrASkfO3E6XDsqLTAVcg8pFCwUaT8rPiMALzskFQQNJBkfKgUxBwscAj4YWhYHDxoXEBRwHgUUMx4gCxsCGBRJAz5yABsCAxIPFSo2AQILLSs7NS4EAGEnFBANJBgTOV0FLWJSKAUQeRkDKyAjCjYqIwEUBwAUPT5iBgohDzYmBAEBJS4pCSspGgUQBDsuD3wvKFd7HwE/EQ8ZFQgRICYEAgUuRhovHFYdM15eNwIgZBgmBVIoJGBnACRXChIKQR8lDVh2CicfKTIBcxwzNionIg4PEVI0FyMQOTkaABI3JSoAByVTKAItJn1ULjcEOG4gBjoqDnAQDjsGHzA2cF92CTIlAhMdchoJABA6KQEyajcgBAM+IhwyE292OTQ0IzUsAVY8EBcxMRxoKgEhBRQSGTMLfQsgFDp1PDQsCgEFKAkIASA8EhF4IgpjIzMJJC4WcyYcEQkPPSMBHlUSfFkuPCQnKiMaAGYWEC80EQIeex9wJjszCSQMFg4iDDcvVxMEBR17Knw0OnMVRyc4fj9ROQpiABoWFxAscR0Na3gBHWdyPjcOBCMleBQgKR4rLQViBhcLGnEgDDZ4ACoPJhQQIH4nHBoDNhkWCyUWDRgVFx4YAwAzFjAELCUPNScjDQ4hDB54Gwg4K2g3BmMBKjkwGggiFAo0Iwp6BBQeDxYwBz4VKCIzeDQmJjYeXTUmHCZpcygrAQt3NAFrBjsmGhtWJz8uUiR3CjorPy4NJXUuOjYIBDoMDGM4MwxxNiMNGg4SES01GHA1O3EIOSo7LQUXHnEeOgIjPXENLjQSfn4OVSkSAgcFBQIxDQUuajUPOj0MFwwcZhMnVzQOCQMDAWBWZBUPPx4oBAA5YA5qBwcrEwQ+IjppEz47Ji4CE2YNKTEzAUcjBgAoFFwyKHwbCz8pARUrDgIIMgg1H2MXGTUBFx0XAgMdEj0HOQ4MIionOyE2cUcxHAA7Iw0sNTkBDUU9GRsbPgkzOBwNKD9hHBdVJipxVTYRAgMmGAIVKxc2JREoNxgtMysDHggNExYWBh8FHwUfBQ8/KQYONiUrLjkfIwpxHDgYCTw1MDEMMBU2JRErK2crDzZdCy94UjAOC00MMgFCKTJxZw8mdgoSCzQMcAtzDC8hMBw7CHJ/GjQ+Cw4aDAVyMTMwEi8gHhUfNB8sDi4hWTQ0GDdJdSEVNggXAhY7Knd3MQ4KGhoZDm11DysqLxI8NXYZCXMDMngaMQg5PSsYKjYxJRJzdx8jOzQlIwklEwgtDhEMdwskLAs3Izg7LQscJi4IeyE3GiAbDAYrHzEzEjcxKicAdSteCTMqJHsUMSEXMT0kJD4Ga3V2Kk4rMSUZHS8qMAsqHTsEPR8RXzArXzc2OgYQOy4oPXc1AQM+DhpuMDFRFTMrBn8pCQkCdCE/MDILKG8uGllRNRlGRy0NGjsyFGoTKSUsOiwkAi8sNRJUNgQ0czEuFgUNMShjBAsBDDErbywzKBoKKzkeOncPDR42HCskNGg7BjEMVgAvOyApLQ5WPgAVHiM+Jz8eOA8BOSI7Xwo4JGIJNjYdCz0MFmAuPhEbLzc3VjUQAGwoHjATcSAGdwUVCjIqMDA1OyQNUB5gGRw6UwpkNS0eECoqbCt2KzQEdD1jBzEZOxQdIjBoMxVqCyoEBToSDB5xPz44LA9MCDAKMAZhLgZZACwMKAYDPWgHODIGHiwMIDUpZ2YEMA04By8INQl3ClQLLC8wCDIIXG8/PSARMDYQLxQyeh8qFTg7MhhUDzkLKwNzDT8RPQ84JC0dDTAqGDA7KxkoKDAcPzh1KQo9LzkeN3YMIxc4HzsBNxorAj0jQX90CCMlPQ4FMTYPfDgwDA0sMyoJHyw6EigMCwULUBsDcnsAdQUAKRAMFBIqLQwCGCkLLmoOJQIEOSU/JQ0JFQgmDx02LwgrIjMLHQQ9DCw+cgoRJREWZAQkCyoyNgskJip0JDg5cy1BXXIzJAl3GCQCdggwZXEbBmcPNAwwCAV9fAkGDDUUBhBmKTgyKAo0KRklcRc/IxY5KQ8SACIKEgg4FVUuDx0FUVoiK3IuEiQEGQkkYToJDhcPJhVTfA8zMiMhFgxnAystCycgLTweB1A0GAMuACIBVEUKHSYiCR0UJA0ENQsRBwUPCgEpMCcvGyUKdxcvH3U5OAwRegMnCiE1IxYiOgsGEGoOAhg/DxJ9IggHCzESCgMsJgJ9awodFDksDRAyCyA1NwodDCwJOFcWCw0yNwokfTUKLwt3IwolIwwocTcbRRAeCwoMHiUZOWkeCRclHihWMyVVcTcfVQEkJjAyMyReOT0jEFwMC1UPPyMwATQnO1oxHz8DNSIoAScYMBMtDi8iFgwgHwwKMAxnDjsXDQooCx4YHSY4JQYYPgQ0Cz0PVkQEEQYqKCIWPTELLBsxElgUMBcENhMKPQQRbyQVRhJdREdUW0tUYB4MX2BjeAU8bxEfZUVYW1VHTF5OSQV/f1xBMU5Jamd7QX9fbWd4H3p1ZhNuYmRFVHRyZHRnBltCCnxGV1YxeEQcDUp3ZlJAFFhafWEKFUlQQ25cOW9iHm90Yk5teXpaSGdhXHsBYStPTR1fdG5wHUIAZ0ZuZWVTeFQVWWliaFxSGFRQOARhQlRVQFVpBmBObEZmAUlKdU9gW0VFbHJkXW0Ffko6cmVTfEx3CXdvV1x+eWMDE2h1IXlJZ0J1VkNKe1cGBnZkcE1gdFJbbXdsWntMECo=",
266
+ "gAAAAACWzMwMzIsIlRodSBKdWwgMTEgMjAyNCAwMzoxMDo0NiBHTVQrMDgwMCAo5Lit5Zu95qCH5YeG5pe26Ze0KSIsNDI5NDcwNTE1MiwxLCJNb3ppbGxhLzUuMCAoV2luZG93cyBOVCAxMC4wOyBXaW42NDsgeDY0KSBBcHBsZVdlYktpdC81MzcuMzYgKEtIVE1MLCBsaWtlIEdlY2tvKSBDaHJvbWUvMTI2LjAuMC4wIFNhZmFyaS81MzcuMzYgRWRnLzEyNi4wLjAuMCIsImh0dHBzOi8vY2RuLm9haXN0YXRpYy5jb20vX25leHQvc3RhdGljL2NodW5rcy9wYWdlcy9fYXBwLWMwOWZmNWY0MjQwMjcwZjguanMiLCJjL1pGWGkxeTNpMnpaS0EzSVQwNzRzMy9fIiwiemgtQ04iLCJ6aC1DTixlbixlbi1HQixlbi1VUyIsMTM1LCJ3ZWJraXRUZW1wb3JhcnlTdG9yYWdl4oiSW29iamVjdCBEZXByZWNhdGVkU3RvcmFnZVF1b3RhXSIsIl9yZWFjdExpc3RlbmluZ3NxZjF0ejFzNmsiLCJmZXRjaCIsMzY1NCwiNWU1NDUzNzItMzcyNy00ZDAyLTkwMDYtMzMwMDRjMWJmYTQ2Il0="
267
+ )
268
+ print(result)
chatgpt/wssClient.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import time
3
+
4
+ from utils.Logger import logger
5
+ import utils.globals as globals
6
+
7
+
8
+ def save_wss_map(wss_map):
9
+ with open(globals.WSS_MAP_FILE, "w") as f:
10
+ json.dump(wss_map, f, indent=4)
11
+
12
+
13
+ async def token2wss(token):
14
+ if not token:
15
+ return False, None
16
+ if token in globals.wss_map:
17
+ wss_mode = globals.wss_map[token]["wss_mode"]
18
+ if wss_mode:
19
+ if int(time.time()) - globals.wss_map.get(token, {}).get("timestamp", 0) < 60 * 60:
20
+ wss_url = globals.wss_map[token]["wss_url"]
21
+ logger.info(f"token -> wss_url from cache")
22
+ return wss_mode, wss_url
23
+ else:
24
+ logger.info(f"token -> wss_url expired")
25
+ return wss_mode, None
26
+ else:
27
+ return False, None
28
+ return False, None
29
+
30
+
31
+ async def set_wss(token, wss_mode, wss_url=None):
32
+ if not token:
33
+ return True
34
+ globals.wss_map[token] = {"timestamp": int(time.time()), "wss_url": wss_url, "wss_mode": wss_mode}
35
+ save_wss_map(globals.wss_map)
36
+ return True
gateway/admin.py ADDED
File without changes
gateway/backend.py ADDED
@@ -0,0 +1,491 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import random
3
+ import re
4
+ import time
5
+ import uuid
6
+
7
+ from fastapi import Request, HTTPException
8
+ from fastapi.responses import RedirectResponse, StreamingResponse, Response
9
+ from starlette.background import BackgroundTask
10
+ from starlette.concurrency import run_in_threadpool
11
+
12
+ import utils.globals as globals
13
+ from app import app
14
+ from chatgpt.authorization import verify_token
15
+ from chatgpt.fp import get_fp
16
+ from chatgpt.proofofWork import get_answer_token, get_config, get_requirements_token
17
+ from gateway.chatgpt import chatgpt_html
18
+ from gateway.reverseProxy import chatgpt_reverse_proxy, content_generator, get_real_req_token, headers_reject_list, \
19
+ headers_accept_list
20
+ from utils.Client import Client
21
+ from utils.Logger import logger
22
+ from utils.configs import x_sign, turnstile_solver_url, chatgpt_base_url_list, no_sentinel, sentinel_proxy_url_list, \
23
+ force_no_history
24
+
25
+ banned_paths = [
26
+ "backend-api/accounts/logout_all",
27
+ "backend-api/accounts/deactivate",
28
+ "backend-api/payments",
29
+ "backend-api/subscriptions",
30
+ "backend-api/user_system_messages",
31
+ "backend-api/memories",
32
+ "backend-api/settings/clear_account_user_memory",
33
+ "backend-api/conversations/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}",
34
+ "backend-api/accounts/mfa_info",
35
+ "backend-api/accounts/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}/invites",
36
+ "admin",
37
+ ]
38
+ redirect_paths = ["auth/logout"]
39
+ chatgpt_paths = ["c/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"]
40
+
41
+
42
+ @app.get("/backend-api/accounts/check/v4-2023-04-27")
43
+ async def check_account(request: Request):
44
+ token = request.headers.get("Authorization").replace("Bearer ", "")
45
+ check_account_response = await chatgpt_reverse_proxy(request, "backend-api/accounts/check/v4-2023-04-27")
46
+ if len(token) == 45 or token.startswith("eyJhbGciOi"):
47
+ return check_account_response
48
+ else:
49
+ check_account_str = check_account_response.body.decode('utf-8')
50
+ check_account_info = json.loads(check_account_str)
51
+ for key in check_account_info.get("accounts", {}).keys():
52
+ account_id = check_account_info["accounts"][key]["account"]["account_id"]
53
+ globals.seed_map[token]["user_id"] = \
54
+ check_account_info["accounts"][key]["account"]["account_user_id"].split("__")[0]
55
+ check_account_info["accounts"][key]["account"]["account_user_id"] = f"user-chatgpt__{account_id}"
56
+ with open(globals.SEED_MAP_FILE, "w", encoding="utf-8") as f:
57
+ json.dump(globals.seed_map, f, indent=4)
58
+ return check_account_info
59
+
60
+
61
+ @app.get("/backend-api/gizmos/bootstrap")
62
+ async def get_gizmos_bootstrap(request: Request):
63
+ token = request.headers.get("Authorization", "").replace("Bearer ", "")
64
+ if len(token) == 45 or token.startswith("eyJhbGciOi"):
65
+ return await chatgpt_reverse_proxy(request, "backend-api/gizmos/bootstrap")
66
+ else:
67
+ return {"gizmos": []}
68
+
69
+
70
+ @app.get("/backend-api/gizmos/pinned")
71
+ async def get_gizmos_pinned(request: Request):
72
+ token = request.headers.get("Authorization", "").replace("Bearer ", "")
73
+ if len(token) == 45 or token.startswith("eyJhbGciOi"):
74
+ return await chatgpt_reverse_proxy(request, "backend-api/gizmos/pinned")
75
+ else:
76
+ return {"items": [], "cursor": None}
77
+
78
+
79
+ @app.get("/public-api/gizmos/discovery/recent")
80
+ async def get_gizmos_discovery_recent(request: Request):
81
+ token = request.headers.get("Authorization", "").replace("Bearer ", "")
82
+ if len(token) == 45 or token.startswith("eyJhbGciOi"):
83
+ return await chatgpt_reverse_proxy(request, "public-api/gizmos/discovery/recent")
84
+ else:
85
+ return {
86
+ "info": {
87
+ "id": "recent",
88
+ "title": "Recently Used",
89
+ },
90
+ "list": {
91
+ "items": [],
92
+ "cursor": None
93
+ }
94
+ }
95
+
96
+
97
+ @app.get("/backend-api/gizmos/snorlax/sidebar")
98
+ async def get_gizmos_snorlax_sidebar(request: Request):
99
+ token = request.headers.get("Authorization", "").replace("Bearer ", "")
100
+ if len(token) == 45 or token.startswith:
101
+ return await chatgpt_reverse_proxy(request, "backend-api/gizmos/snorlax/sidebar")
102
+ else:
103
+ return {"items": [], "cursor": None}
104
+
105
+
106
+ @app.post("/backend-api/gizmos/snorlax/upsert")
107
+ async def get_gizmos_snorlax_upsert(request: Request):
108
+ token = request.headers.get("Authorization", "").replace("Bearer ", "")
109
+ if len(token) == 45 or token.startswith:
110
+ return await chatgpt_reverse_proxy(request, "backend-api/gizmos/snorlax/upsert")
111
+ else:
112
+ raise HTTPException(status_code=403, detail="Forbidden")
113
+
114
+
115
+ @app.api_route("/backend-api/conversations", methods=["GET", "PATCH"])
116
+ async def get_conversations(request: Request):
117
+ token = request.headers.get("Authorization", "").replace("Bearer ", "")
118
+ if len(token) == 45 or token.startswith("eyJhbGciOi"):
119
+ return await chatgpt_reverse_proxy(request, "backend-api/conversations")
120
+ if request.method == "GET":
121
+ limit = int(request.query_params.get("limit", 28))
122
+ offset = int(request.query_params.get("offset", 0))
123
+ is_archived = request.query_params.get("is_archived", None)
124
+ items = []
125
+ for conversation_id in globals.seed_map.get(token, {}).get("conversations", []):
126
+ conversation = globals.conversation_map.get(conversation_id, None)
127
+ if conversation:
128
+ if is_archived == "true":
129
+ if conversation.get("is_archived", False):
130
+ items.append(conversation)
131
+ else:
132
+ if not conversation.get("is_archived", False):
133
+ items.append(conversation)
134
+ items = items[int(offset):int(offset) + int(limit)]
135
+ conversations = {
136
+ "items": items,
137
+ "total": len(items),
138
+ "limit": limit,
139
+ "offset": offset,
140
+ "has_missing_conversations": False
141
+ }
142
+ return Response(content=json.dumps(conversations, indent=4), media_type="application/json")
143
+ else:
144
+ raise HTTPException(status_code=403, detail="Forbidden")
145
+
146
+
147
+ @app.get("/backend-api/conversation/{conversation_id}")
148
+ async def update_conversation(request: Request, conversation_id: str):
149
+ token = request.headers.get("Authorization", "").replace("Bearer ", "")
150
+ conversation_details_response = await chatgpt_reverse_proxy(request,
151
+ f"backend-api/conversation/{conversation_id}")
152
+ if len(token) == 45 or token.startswith("eyJhbGciOi"):
153
+ return conversation_details_response
154
+ else:
155
+ conversation_details_str = conversation_details_response.body.decode('utf-8')
156
+ conversation_details = json.loads(conversation_details_str)
157
+ if conversation_id in globals.seed_map[token][
158
+ "conversations"] and conversation_id in globals.conversation_map:
159
+ globals.conversation_map[conversation_id]["title"] = conversation_details.get("title", None)
160
+ globals.conversation_map[conversation_id]["is_archived"] = conversation_details.get("is_archived",
161
+ False)
162
+ globals.conversation_map[conversation_id]["conversation_template_id"] = conversation_details.get(
163
+ "conversation_template_id", None)
164
+ globals.conversation_map[conversation_id]["gizmo_id"] = conversation_details.get("gizmo_id", None)
165
+ globals.conversation_map[conversation_id]["async_status"] = conversation_details.get("async_status",
166
+ None)
167
+ with open(globals.CONVERSATION_MAP_FILE, "w", encoding="utf-8") as f:
168
+ json.dump(globals.conversation_map, f, indent=4)
169
+ return conversation_details_response
170
+
171
+
172
+ @app.patch("/backend-api/conversation/{conversation_id}")
173
+ async def patch_conversation(request: Request, conversation_id: str):
174
+ token = request.headers.get("Authorization", "").replace("Bearer ", "")
175
+ patch_response = (await chatgpt_reverse_proxy(request, f"backend-api/conversation/{conversation_id}"))
176
+ if len(token) == 45 or token.startswith("eyJhbGciOi"):
177
+ return patch_response
178
+ else:
179
+ data = await request.json()
180
+ if conversation_id in globals.seed_map[token][
181
+ "conversations"] and conversation_id in globals.conversation_map:
182
+ if not data.get("is_visible", True):
183
+ globals.conversation_map.pop(conversation_id)
184
+ globals.seed_map[token]["conversations"].remove(conversation_id)
185
+ with open(globals.SEED_MAP_FILE, "w", encoding="utf-8") as f:
186
+ json.dump(globals.seed_map, f, indent=4)
187
+ else:
188
+ globals.conversation_map[conversation_id].update(data)
189
+ with open(globals.CONVERSATION_MAP_FILE, "w", encoding="utf-8") as f:
190
+ json.dump(globals.conversation_map, f, indent=4)
191
+ return patch_response
192
+
193
+
194
+ @app.get("/backend-api/me")
195
+ async def get_me(request: Request):
196
+ token = request.headers.get("Authorization", "").replace("Bearer ", "")
197
+ if len(token) == 45 or token.startswith("eyJhbGciOi"):
198
+ return await chatgpt_reverse_proxy(request, "backend-api/me")
199
+ else:
200
+ me = {
201
+ "object": "user",
202
+ "id": "org-chatgpt",
203
+ "email": "chatgpt@openai.com",
204
+ "name": "ChatGPT",
205
+ "picture": "https://cdn.auth0.com/avatars/ai.png",
206
+ "created": int(time.time()),
207
+ "phone_number": None,
208
+ "mfa_flag_enabled": False,
209
+ "amr": [],
210
+ "groups": [],
211
+ "orgs": {
212
+ "object": "list",
213
+ "data": [
214
+ {
215
+ "object": "organization",
216
+ "id": "org-chatgpt",
217
+ "created": 1715641300,
218
+ "title": "Personal",
219
+ "name": "user-chatgpt",
220
+ "description": "Personal org for chatgpt@openai.com",
221
+ "personal": True,
222
+ "settings": {
223
+ "threads_ui_visibility": "NONE",
224
+ "usage_dashboard_visibility": "ANY_ROLE",
225
+ "disable_user_api_keys": False
226
+ },
227
+ "parent_org_id": None,
228
+ "is_default": True,
229
+ "role": "owner",
230
+ "is_scale_tier_authorized_purchaser": None,
231
+ "is_scim_managed": False,
232
+ "projects": {
233
+ "object": "list",
234
+ "data": []
235
+ },
236
+ "groups": [],
237
+ "geography": None
238
+ }
239
+ ]
240
+ },
241
+ "has_payg_project_spend_limit": True
242
+ }
243
+ return Response(content=json.dumps(me, indent=4), media_type="application/json")
244
+
245
+
246
+ @app.post("/backend-api/edge")
247
+ async def edge():
248
+ return Response(status_code=204)
249
+
250
+
251
+ if no_sentinel:
252
+ openai_sentinel_tokens_cache = {}
253
+
254
+ @app.post("/backend-api/sentinel/chat-requirements")
255
+ async def sentinel_chat_conversations(request: Request):
256
+ token = request.headers.get("Authorization", "").replace("Bearer ", "")
257
+ req_token = await get_real_req_token(token)
258
+ access_token = await verify_token(req_token)
259
+ fp = get_fp(req_token).copy()
260
+ proxy_url = fp.pop("proxy_url", None)
261
+ impersonate = fp.pop("impersonate", "safari15_3")
262
+ user_agent = fp.get("user-agent",
263
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0")
264
+
265
+ host_url = random.choice(chatgpt_base_url_list) if chatgpt_base_url_list else "https://chatgpt.com"
266
+ proof_token = None
267
+ turnstile_token = None
268
+
269
+ # headers = {
270
+ # key: value for key, value in request.headers.items()
271
+ # if (key.lower() not in ["host", "origin", "referer", "priority", "sec-ch-ua-platform", "sec-ch-ua",
272
+ # "sec-ch-ua-mobile", "oai-device-id"] and key.lower() not in headers_reject_list)
273
+ # }
274
+ headers = {
275
+ key: value for key, value in request.headers.items()
276
+ if (key.lower() in headers_accept_list)
277
+ }
278
+ headers.update(fp)
279
+ headers.update({"authorization": f"Bearer {access_token}"})
280
+ client = Client(proxy=proxy_url, impersonate=impersonate)
281
+ if sentinel_proxy_url_list:
282
+ clients = Client(proxy=random.choice(sentinel_proxy_url_list), impersonate=impersonate)
283
+ else:
284
+ clients = client
285
+
286
+ try:
287
+ config = get_config(user_agent)
288
+ p = get_requirements_token(config)
289
+ data = {'p': p}
290
+ r = await clients.post(f'{host_url}/backend-api/sentinel/chat-requirements', headers=headers, json=data,
291
+ timeout=10)
292
+ if r.status_code != 200:
293
+ raise HTTPException(status_code=r.status_code, detail="Failed to get chat requirements")
294
+ resp = r.json()
295
+ turnstile = resp.get('turnstile', {})
296
+ turnstile_required = turnstile.get('required')
297
+ if turnstile_required:
298
+ turnstile_dx = turnstile.get("dx")
299
+ try:
300
+ if turnstile_solver_url:
301
+ res = await client.post(turnstile_solver_url,
302
+ json={"url": "https://chatgpt.com", "p": p, "dx": turnstile_dx})
303
+ turnstile_token = res.json().get("t")
304
+ except Exception as e:
305
+ logger.info(f"Turnstile ignored: {e}")
306
+
307
+ proofofwork = resp.get('proofofwork', {})
308
+ proofofwork_required = proofofwork.get('required')
309
+ if proofofwork_required:
310
+ proofofwork_diff = proofofwork.get("difficulty")
311
+ proofofwork_seed = proofofwork.get("seed")
312
+ proof_token, solved = await run_in_threadpool(
313
+ get_answer_token, proofofwork_seed, proofofwork_diff, config
314
+ )
315
+ if not solved:
316
+ raise HTTPException(status_code=403, detail="Failed to solve proof of work")
317
+ chat_token = resp.get('token')
318
+
319
+ openai_sentinel_tokens_cache[req_token] = {
320
+ "chat_token": chat_token,
321
+ "proof_token": proof_token,
322
+ "turnstile_token": turnstile_token
323
+ }
324
+ except Exception as e:
325
+ logger.error(f"Sentinel failed: {e}")
326
+
327
+ return {
328
+ "arkose": {
329
+ "dx": None,
330
+ "required": False
331
+ },
332
+ "persona": "chatgpt-paid",
333
+ "proofofwork": {
334
+ "difficulty": None,
335
+ "required": False,
336
+ "seed": None
337
+ },
338
+ "token": str(uuid.uuid4()),
339
+ "turnstile": {
340
+ "dx": None,
341
+ "required": False
342
+ }
343
+ }
344
+
345
+
346
+ @app.post("/backend-alt/conversation")
347
+ @app.post("/backend-api/conversation")
348
+ async def chat_conversations(request: Request):
349
+ token = request.headers.get("Authorization", "").replace("Bearer ", "")
350
+ req_token = await get_real_req_token(token)
351
+ access_token = await verify_token(req_token)
352
+ fp = get_fp(req_token).copy()
353
+ proxy_url = fp.pop("proxy_url", None)
354
+ impersonate = fp.pop("impersonate", "safari15_3")
355
+ user_agent = fp.get("user-agent",
356
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0")
357
+
358
+ host_url = random.choice(chatgpt_base_url_list) if chatgpt_base_url_list else "https://chatgpt.com"
359
+ proof_token = None
360
+ turnstile_token = None
361
+
362
+ # headers = {
363
+ # key: value for key, value in request.headers.items()
364
+ # if (key.lower() not in ["host", "origin", "referer", "priority", "sec-ch-ua-platform", "sec-ch-ua",
365
+ # "sec-ch-ua-mobile", "oai-device-id"] and key.lower() not in headers_reject_list)
366
+ # }
367
+ headers = {
368
+ key: value for key, value in request.headers.items()
369
+ if (key.lower() in headers_accept_list)
370
+ }
371
+ headers.update(fp)
372
+ headers.update({"authorization": f"Bearer {access_token}"})
373
+
374
+ try:
375
+ client = Client(proxy=proxy_url, impersonate=impersonate)
376
+ if sentinel_proxy_url_list:
377
+ clients = Client(proxy=random.choice(sentinel_proxy_url_list), impersonate=impersonate)
378
+ else:
379
+ clients = client
380
+
381
+ sentinel_tokens = openai_sentinel_tokens_cache.get(req_token, {})
382
+ openai_sentinel_tokens_cache.pop(req_token, None)
383
+ if not sentinel_tokens:
384
+ config = get_config(user_agent)
385
+ p = get_requirements_token(config)
386
+ data = {'p': p}
387
+ r = await clients.post(f'{host_url}/backend-api/sentinel/chat-requirements', headers=headers, json=data,
388
+ timeout=10)
389
+ resp = r.json()
390
+ turnstile = resp.get('turnstile', {})
391
+ turnstile_required = turnstile.get('required')
392
+ if turnstile_required:
393
+ turnstile_dx = turnstile.get("dx")
394
+ try:
395
+ if turnstile_solver_url:
396
+ res = await client.post(turnstile_solver_url,
397
+ json={"url": "https://chatgpt.com", "p": p, "dx": turnstile_dx})
398
+ turnstile_token = res.json().get("t")
399
+ except Exception as e:
400
+ logger.info(f"Turnstile ignored: {e}")
401
+
402
+ proofofwork = resp.get('proofofwork', {})
403
+ proofofwork_required = proofofwork.get('required')
404
+ if proofofwork_required:
405
+ proofofwork_diff = proofofwork.get("difficulty")
406
+ proofofwork_seed = proofofwork.get("seed")
407
+ proof_token, solved = await run_in_threadpool(
408
+ get_answer_token, proofofwork_seed, proofofwork_diff, config
409
+ )
410
+ if not solved:
411
+ raise HTTPException(status_code=403, detail="Failed to solve proof of work")
412
+ chat_token = resp.get('token')
413
+ headers.update({
414
+ "openai-sentinel-chat-requirements-token": chat_token,
415
+ "openai-sentinel-proof-token": proof_token,
416
+ "openai-sentinel-turnstile-token": turnstile_token,
417
+ })
418
+ else:
419
+ headers.update({
420
+ "openai-sentinel-chat-requirements-token": sentinel_tokens.get("chat_token", ""),
421
+ "openai-sentinel-proof-token": sentinel_tokens.get("proof_token", ""),
422
+ "openai-sentinel-turnstile-token": sentinel_tokens.get("turnstile_token", "")
423
+ })
424
+ except Exception as e:
425
+ logger.error(f"Sentinel failed: {e}")
426
+ return Response(status_code=403, content="Sentinel failed")
427
+
428
+ params = dict(request.query_params)
429
+ data = await request.body()
430
+ request_cookies = dict(request.cookies)
431
+
432
+ async def c_close(client, clients):
433
+ if client:
434
+ await client.close()
435
+ del client
436
+ if clients:
437
+ await clients.close()
438
+ del clients
439
+
440
+ history = True
441
+ try:
442
+ req_json = json.loads(data)
443
+ history = not req_json.get("history_and_training_disabled", False)
444
+ except Exception:
445
+ pass
446
+ if force_no_history:
447
+ history = False
448
+ req_json = json.loads(data)
449
+ req_json["history_and_training_disabled"] = True
450
+ data = json.dumps(req_json).encode("utf-8")
451
+
452
+ background = BackgroundTask(c_close, client, clients)
453
+ r = await client.post_stream(f"{host_url}{request.url.path}", params=params, headers=headers,
454
+ cookies=request_cookies, data=data, stream=True, allow_redirects=False)
455
+ rheaders = r.headers
456
+ logger.info(f"Request token: {req_token}")
457
+ logger.info(f"Request proxy: {proxy_url}")
458
+ logger.info(f"Request UA: {user_agent}")
459
+ logger.info(f"Request impersonate: {impersonate}")
460
+ if x_sign:
461
+ rheaders.update({"x-sign": x_sign})
462
+ if 'stream' in rheaders.get("content-type", ""):
463
+ conv_key = r.cookies.get("conv_key", "")
464
+ response = StreamingResponse(content_generator(r, token, history), headers=rheaders,
465
+ media_type=r.headers.get("content-type", ""), background=background)
466
+ response.set_cookie("conv_key", value=conv_key)
467
+ return response
468
+ else:
469
+ return Response(content=(await r.atext()), headers=rheaders, media_type=rheaders.get("content-type"),
470
+ status_code=r.status_code, background=background)
471
+
472
+
473
+ @app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD", "PATCH", "TRACE"])
474
+ async def reverse_proxy(request: Request, path: str):
475
+ token = request.headers.get("Authorization", "").replace("Bearer ", "")
476
+ if len(token) != 45 and not token.startswith("eyJhbGciOi"):
477
+ for banned_path in banned_paths:
478
+ if re.match(banned_path, path):
479
+ raise HTTPException(status_code=403, detail="Forbidden")
480
+
481
+ for chatgpt_path in chatgpt_paths:
482
+ if re.match(chatgpt_path, path):
483
+ return await chatgpt_html(request)
484
+
485
+ for redirect_path in redirect_paths:
486
+ if re.match(redirect_path, path):
487
+ redirect_url = str(request.base_url)
488
+ response = RedirectResponse(url=f"{redirect_url}login", status_code=302)
489
+ return response
490
+
491
+ return await chatgpt_reverse_proxy(request, path)
gateway/chatgpt.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ from urllib.parse import quote
3
+
4
+ from fastapi import Request
5
+ from fastapi.responses import HTMLResponse
6
+
7
+ from app import app, templates
8
+ from gateway.login import login_html
9
+ from utils.kv_utils import set_value_for_key
10
+
11
+ with open("templates/chatgpt_context.json", "r", encoding="utf-8") as f:
12
+ chatgpt_context = json.load(f)
13
+
14
+
15
+ @app.get("/", response_class=HTMLResponse)
16
+ async def chatgpt_html(request: Request):
17
+ token = request.query_params.get("token")
18
+ if not token:
19
+ token = request.cookies.get("token")
20
+ if not token:
21
+ return await login_html(request)
22
+
23
+ if len(token) != 45 and not token.startswith("eyJhbGciOi"):
24
+ token = quote(token)
25
+
26
+ user_remix_context = chatgpt_context.copy()
27
+ set_value_for_key(user_remix_context, "user", {"id": "user-chatgpt"})
28
+ set_value_for_key(user_remix_context, "accessToken", token)
29
+
30
+ response = templates.TemplateResponse("chatgpt.html", {"request": request, "remix_context": user_remix_context})
31
+ response.set_cookie("token", value=token, expires="Thu, 01 Jan 2099 00:00:00 GMT")
32
+ return response
gateway/gpts.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+
3
+ from fastapi import Request
4
+ from fastapi.responses import Response
5
+
6
+ from app import app
7
+ from gateway.chatgpt import chatgpt_html
8
+
9
+ with open("templates/gpts_context.json", "r", encoding="utf-8") as f:
10
+ gpts_context = json.load(f)
11
+
12
+
13
+ @app.get("/gpts")
14
+ async def get_gpts():
15
+ return {"kind": "store"}
16
+
17
+
18
+ @app.get("/g/g-{gizmo_id}")
19
+ async def get_gizmo_json(request: Request, gizmo_id: str):
20
+ params = request.query_params
21
+ if params.get("_data") == "routes/g.$gizmoId._index":
22
+ return Response(content=json.dumps(gpts_context, indent=4), media_type="application/json")
23
+ else:
24
+ return await chatgpt_html(request)
gateway/login.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import Request
2
+ from fastapi.responses import HTMLResponse
3
+
4
+ from app import app, templates
5
+
6
+
7
+ @app.get("/login", response_class=HTMLResponse)
8
+ async def login_html(request: Request):
9
+ response = templates.TemplateResponse("login.html", {"request": request})
10
+ return response
gateway/reverseProxy.py ADDED
@@ -0,0 +1,326 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import random
3
+ import time
4
+ from datetime import datetime, timezone
5
+
6
+ from fastapi import Request, HTTPException
7
+ from fastapi.responses import StreamingResponse, Response
8
+ from starlette.background import BackgroundTask
9
+
10
+ import utils.globals as globals
11
+ from chatgpt.authorization import verify_token, get_req_token
12
+ from chatgpt.fp import get_fp
13
+ from utils.Client import Client
14
+ from utils.Logger import logger
15
+ from utils.configs import chatgpt_base_url_list, sentinel_proxy_url_list, force_no_history, file_host, voice_host
16
+
17
+
18
+ def generate_current_time():
19
+ current_time = datetime.now(timezone.utc)
20
+ formatted_time = current_time.isoformat(timespec='microseconds').replace('+00:00', 'Z')
21
+ return formatted_time
22
+
23
+
24
+ headers_reject_list = [
25
+ "x-real-ip",
26
+ "x-forwarded-for",
27
+ "x-forwarded-proto",
28
+ "x-forwarded-port",
29
+ "x-forwarded-host",
30
+ "x-forwarded-server",
31
+ "cf-warp-tag-id",
32
+ "cf-visitor",
33
+ "cf-ray",
34
+ "cf-connecting-ip",
35
+ "cf-ipcountry",
36
+ "cdn-loop",
37
+ "remote-host",
38
+ "x-frame-options",
39
+ "x-xss-protection",
40
+ "x-content-type-options",
41
+ "content-security-policy",
42
+ "host",
43
+ "cookie",
44
+ "connection",
45
+ "content-length",
46
+ "content-encoding",
47
+ "x-middleware-prefetch",
48
+ "x-nextjs-data",
49
+ "purpose",
50
+ "x-forwarded-uri",
51
+ "x-forwarded-path",
52
+ "x-forwarded-method",
53
+ "x-forwarded-protocol",
54
+ "x-forwarded-scheme",
55
+ "cf-request-id",
56
+ "cf-worker",
57
+ "cf-access-client-id",
58
+ "cf-access-client-device-type",
59
+ "cf-access-client-device-model",
60
+ "cf-access-client-device-name",
61
+ "cf-access-client-device-brand",
62
+ "x-middleware-prefetch",
63
+ "x-forwarded-for",
64
+ "x-forwarded-host",
65
+ "x-forwarded-proto",
66
+ "x-forwarded-server",
67
+ "x-real-ip",
68
+ "x-forwarded-port",
69
+ "cf-connecting-ip",
70
+ "cf-ipcountry",
71
+ "cf-ray",
72
+ "cf-visitor",
73
+ ]
74
+
75
+ headers_accept_list = [
76
+ "openai-sentinel-chat-requirements-token",
77
+ "openai-sentinel-proof-token",
78
+ "openai-sentinel-turnstile-token",
79
+ "accept",
80
+ "authorization",
81
+ "accept-encoding",
82
+ "accept-language",
83
+ "content-type",
84
+ "oai-device-id",
85
+ "oai-echo-logs",
86
+ "oai-language",
87
+ "sec-fetch-dest",
88
+ "sec-fetch-mode",
89
+ "sec-fetch-site",
90
+ ]
91
+
92
+
93
+ async def get_real_req_token(token):
94
+ req_token = get_req_token(token)
95
+ if len(req_token) == 45 or req_token.startswith("eyJhbGciOi"):
96
+ return req_token
97
+ else:
98
+ req_token = get_req_token("", token)
99
+ return req_token
100
+
101
+
102
+ def save_conversation(token, conversation_id, title=None):
103
+ if conversation_id not in globals.conversation_map:
104
+ conversation_detail = {
105
+ "id": conversation_id,
106
+ "title": title,
107
+ "create_time": generate_current_time(),
108
+ "update_time": generate_current_time()
109
+ }
110
+ globals.conversation_map[conversation_id] = conversation_detail
111
+ else:
112
+ globals.conversation_map[conversation_id]["update_time"] = generate_current_time()
113
+ if title:
114
+ globals.conversation_map[conversation_id]["title"] = title
115
+ if conversation_id not in globals.seed_map[token]["conversations"]:
116
+ globals.seed_map[token]["conversations"].insert(0, conversation_id)
117
+ else:
118
+ globals.seed_map[token]["conversations"].remove(conversation_id)
119
+ globals.seed_map[token]["conversations"].insert(0, conversation_id)
120
+ with open(globals.CONVERSATION_MAP_FILE, "w", encoding="utf-8") as f:
121
+ json.dump(globals.conversation_map, f, indent=4)
122
+ with open(globals.SEED_MAP_FILE, "w", encoding="utf-8") as f:
123
+ json.dump(globals.seed_map, f, indent=4)
124
+ if title:
125
+ logger.info(f"Conversation ID: {conversation_id}, Title: {title}")
126
+
127
+
128
+ async def content_generator(r, token, history=True):
129
+ conversation_id = None
130
+ title = None
131
+ async for chunk in r.aiter_content():
132
+ try:
133
+ if history and (len(token) != 45 and not token.startswith("eyJhbGciOi")) and (not conversation_id or not title):
134
+ chat_chunk = chunk.decode('utf-8')
135
+ if not conversation_id or not title and chat_chunk.startswith("event: delta\n\ndata: {"):
136
+ chunk_data = chat_chunk[19:]
137
+ conversation_id = json.loads(chunk_data).get("v").get("conversation_id")
138
+ if conversation_id:
139
+ save_conversation(token, conversation_id)
140
+ title = globals.conversation_map[conversation_id].get("title")
141
+ if chat_chunk.startswith("data: {"):
142
+ if "\n\nevent: delta" in chat_chunk:
143
+ index = chat_chunk.find("\n\nevent: delta")
144
+ chunk_data = chat_chunk[6:index]
145
+ elif "\n\ndata: {" in chat_chunk:
146
+ index = chat_chunk.find("\n\ndata: {")
147
+ chunk_data = chat_chunk[6:index]
148
+ else:
149
+ chunk_data = chat_chunk[6:]
150
+ chunk_data = chunk_data.strip()
151
+ if conversation_id is None:
152
+ conversation_id = json.loads(chunk_data).get("conversation_id")
153
+ if conversation_id:
154
+ save_conversation(token, conversation_id)
155
+ title = globals.conversation_map[conversation_id].get("title")
156
+ if title is None:
157
+ title = json.loads(chunk_data).get("title")
158
+ if title:
159
+ save_conversation(token, conversation_id, title)
160
+ except Exception as e:
161
+ # logger.error(e)
162
+ # logger.error(chunk.decode('utf-8'))
163
+ pass
164
+ yield chunk
165
+
166
+
167
+ async def chatgpt_reverse_proxy(request: Request, path: str):
168
+ try:
169
+ origin_host = request.url.netloc
170
+ if request.url.is_secure:
171
+ petrol = "https"
172
+ else:
173
+ petrol = "http"
174
+ if "x-forwarded-proto" in request.headers:
175
+ petrol = request.headers["x-forwarded-proto"]
176
+ if "cf-visitor" in request.headers:
177
+ cf_visitor = json.loads(request.headers["cf-visitor"])
178
+ petrol = cf_visitor.get("scheme", petrol)
179
+
180
+ params = dict(request.query_params)
181
+ request_cookies = dict(request.cookies)
182
+
183
+ # headers = {
184
+ # key: value for key, value in request.headers.items()
185
+ # if (key.lower() not in ["host", "origin", "referer", "priority",
186
+ # "oai-device-id"] and key.lower() not in headers_reject_list)
187
+ # }
188
+ headers = {
189
+ key: value for key, value in request.headers.items()
190
+ if (key.lower() in headers_accept_list)
191
+ }
192
+
193
+ base_url = random.choice(chatgpt_base_url_list) if chatgpt_base_url_list else "https://chatgpt.com"
194
+ if "assets/" in path:
195
+ base_url = "https://cdn.oaistatic.com"
196
+ if "file-" in path and "backend-api" not in path:
197
+ base_url = "https://files.oaiusercontent.com"
198
+ if "v1/" in path:
199
+ base_url = "https://ab.chatgpt.com"
200
+ if "sandbox" in path:
201
+ base_url = "https://web-sandbox.oaiusercontent.com"
202
+ path = path.replace("sandbox/", "")
203
+
204
+ token = headers.get("authorization", "").replace("Bearer ", "").strip()
205
+ if token:
206
+ req_token = await get_real_req_token(token)
207
+ access_token = await verify_token(req_token)
208
+ headers.update({"authorization": f"Bearer {access_token}"})
209
+
210
+ cookie_token = request.cookies.get("token", "")
211
+ req_token = await get_real_req_token(cookie_token)
212
+ fp = get_fp(req_token).copy()
213
+
214
+ proxy_url = fp.pop("proxy_url", None)
215
+ impersonate = fp.pop("impersonate", "safari15_3")
216
+ user_agent = fp.get("user-agent")
217
+ headers.update(fp)
218
+
219
+ headers.update({
220
+ "accept-language": "en-US,en;q=0.9",
221
+ "host": base_url.replace("https://", "").replace("http://", ""),
222
+ "origin": base_url,
223
+ "referer": f"{base_url}/"
224
+ })
225
+ if "v1/initialize" in path:
226
+ headers.update({"user-agent": request.headers.get("user-agent")})
227
+ if "statsig-api-key" not in headers:
228
+ headers.update({
229
+ "statsig-sdk-type": "js-client",
230
+ "statsig-api-key": "client-tnE5GCU2F2cTxRiMbvTczMDT1jpwIigZHsZSdqiy4u",
231
+ "statsig-sdk-version": "5.1.0",
232
+ "statsig-client-time": int(time.time() * 1000),
233
+ })
234
+
235
+ data = await request.body()
236
+
237
+ history = True
238
+ if path.endswith("backend-api/conversation") or path.endswith("backend-alt/conversation"):
239
+ try:
240
+ req_json = json.loads(data)
241
+ history = not req_json.get("history_and_training_disabled", False)
242
+ except Exception:
243
+ pass
244
+ if force_no_history:
245
+ history = False
246
+ req_json = json.loads(data)
247
+ req_json["history_and_training_disabled"] = True
248
+ data = json.dumps(req_json).encode("utf-8")
249
+
250
+
251
+ if sentinel_proxy_url_list and "backend-api/sentinel/chat-requirements" in path:
252
+ client = Client(proxy=random.choice(sentinel_proxy_url_list))
253
+ else:
254
+ client = Client(proxy=proxy_url, impersonate=impersonate)
255
+ try:
256
+ background = BackgroundTask(client.close)
257
+ r = await client.request(request.method, f"{base_url}/{path}", params=params, headers=headers,
258
+ cookies=request_cookies, data=data, stream=True, allow_redirects=False)
259
+ if r.status_code == 307 or r.status_code == 302 or r.status_code == 301:
260
+ return Response(status_code=307,
261
+ headers={"Location": r.headers.get("Location")
262
+ .replace("ab.chatgpt.com", origin_host)
263
+ .replace("chatgpt.com", origin_host)
264
+ .replace("cdn.oaistatic.com", origin_host)
265
+ .replace("https", petrol)}, background=background)
266
+ elif 'stream' in r.headers.get("content-type", ""):
267
+ logger.info(f"Request token: {req_token}")
268
+ logger.info(f"Request proxy: {proxy_url}")
269
+ logger.info(f"Request UA: {user_agent}")
270
+ logger.info(f"Request impersonate: {impersonate}")
271
+ conv_key = r.cookies.get("conv_key", "")
272
+ response = StreamingResponse(content_generator(r, token, history), media_type=r.headers.get("content-type", ""),
273
+ background=background)
274
+ response.set_cookie("conv_key", value=conv_key)
275
+ return response
276
+ elif 'image' in r.headers.get("content-type", "") or "audio" in r.headers.get("content-type", "") or "video" in r.headers.get("content-type", ""):
277
+ rheaders = dict(r.headers)
278
+ response = Response(content=await r.acontent(), headers=rheaders,
279
+ status_code=r.status_code, background=background)
280
+ return response
281
+ else:
282
+ if path.endswith("backend-api/conversation") or path.endswith("backend-alt/conversation") or "/register-websocket" in path:
283
+ response = Response(content=(await r.acontent()), media_type=r.headers.get("content-type"),
284
+ status_code=r.status_code, background=background)
285
+ else:
286
+ content = await r.atext()
287
+ if "public-api/" in path:
288
+ content = (content
289
+ .replace("https://ab.chatgpt.com", f"{petrol}://{origin_host}")
290
+ .replace("https://cdn.oaistatic.com", f"{petrol}://{origin_host}")
291
+ .replace("webrtc.chatgpt.com", voice_host if voice_host else "webrtc.chatgpt.com")
292
+ .replace("files.oaiusercontent.com", file_host if file_host else "files.oaiusercontent.com")
293
+ .replace("chatgpt.com/ces", f"{origin_host}/ces")
294
+ )
295
+ else:
296
+ content = (content
297
+ .replace("https://ab.chatgpt.com", f"{petrol}://{origin_host}")
298
+ .replace("https://cdn.oaistatic.com", f"{petrol}://{origin_host}")
299
+ .replace("webrtc.chatgpt.com", voice_host if voice_host else "webrtc.chatgpt.com")
300
+ .replace("files.oaiusercontent.com", file_host if file_host else "files.oaiusercontent.com")
301
+ .replace("web-sandbox.oaiusercontent.com", f"{origin_host}/sandbox")
302
+ .replace("https://chatgpt.com", f"{petrol}://{origin_host}")
303
+ .replace("chatgpt.com/ces", f"{origin_host}/ces")
304
+ )
305
+ if base_url == "https://web-sandbox.oaiusercontent.com":
306
+ content = content.replace("/assets", "/sandbox/assets")
307
+ rheaders = dict(r.headers)
308
+ content_type = rheaders.get("content-type", "")
309
+ cache_control = rheaders.get("cache-control", "")
310
+ expires = rheaders.get("expires", "")
311
+ content_disposition = rheaders.get("content-disposition", "")
312
+ rheaders = {
313
+ "cache-control": cache_control,
314
+ "content-type": content_type,
315
+ "expires": expires,
316
+ "content-disposition": content_disposition
317
+ }
318
+ response = Response(content=content, headers=rheaders,
319
+ status_code=r.status_code, background=background)
320
+ return response
321
+ except Exception:
322
+ await client.close()
323
+ except HTTPException as e:
324
+ raise e
325
+ except Exception as e:
326
+ raise HTTPException(status_code=500, detail=str(e))
gateway/route.py ADDED
File without changes
gateway/share.py ADDED
@@ -0,0 +1,251 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import random
3
+ import time
4
+
5
+ import jwt
6
+ from fastapi import Request, HTTPException, Security
7
+ from fastapi.responses import Response
8
+ from fastapi.security import HTTPAuthorizationCredentials
9
+
10
+ import utils.globals as globals
11
+ from app import app, security_scheme
12
+ from chatgpt.authorization import verify_token
13
+ from chatgpt.fp import get_fp
14
+ from gateway.reverseProxy import get_real_req_token
15
+ from utils.Client import Client
16
+ from utils.Logger import logger
17
+ from utils.configs import proxy_url_list, chatgpt_base_url_list, authorization_list
18
+
19
+ base_headers = {
20
+ 'accept': '*/*',
21
+ 'accept-encoding': 'gzip, deflate, br, zstd',
22
+ 'accept-language': 'en-US,en;q=0.9',
23
+ 'content-type': 'application/json',
24
+ 'oai-language': 'en-US',
25
+ 'priority': 'u=1, i',
26
+ 'sec-fetch-dest': 'empty',
27
+ 'sec-fetch-mode': 'cors',
28
+ 'sec-fetch-site': 'same-origin',
29
+ }
30
+
31
+
32
+ def verify_authorization(bearer_token):
33
+ if not bearer_token:
34
+ raise HTTPException(status_code=401, detail="Authorization header is missing")
35
+ if bearer_token not in authorization_list:
36
+ raise HTTPException(status_code=401, detail="Invalid authorization")
37
+
38
+
39
+ @app.get("/seedtoken")
40
+ async def get_seedtoken(request: Request, credentials: HTTPAuthorizationCredentials = Security(security_scheme)):
41
+ verify_authorization(credentials.credentials)
42
+ try:
43
+ params = request.query_params
44
+ seed = params.get("seed")
45
+
46
+ if seed:
47
+ if seed not in globals.seed_map:
48
+ raise HTTPException(status_code=404, detail=f"Seed '{seed}' not found")
49
+ return {
50
+ "status": "success",
51
+ "data": {
52
+ "seed": seed,
53
+ "token": globals.seed_map[seed]["token"]
54
+ }
55
+ }
56
+
57
+ token_map = {
58
+ seed: data["token"]
59
+ for seed, data in globals.seed_map.items()
60
+ }
61
+ return {"status": "success", "data": token_map}
62
+
63
+ except Exception as e:
64
+ raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
65
+
66
+
67
+ @app.post("/seedtoken")
68
+ async def set_seedtoken(request: Request, credentials: HTTPAuthorizationCredentials = Security(security_scheme)):
69
+ verify_authorization(credentials.credentials)
70
+ data = await request.json()
71
+
72
+ seed = data.get("seed")
73
+ token = data.get("token")
74
+
75
+ if seed not in globals.seed_map:
76
+ globals.seed_map[seed] = {
77
+ "token": token,
78
+ "conversations": []
79
+ }
80
+ else:
81
+ globals.seed_map[seed]["token"] = token
82
+
83
+ with open(globals.SEED_MAP_FILE, "w", encoding="utf-8") as f:
84
+ json.dump(globals.seed_map, f, indent=4)
85
+
86
+ return {"status": "success", "message": "Token updated successfully"}
87
+
88
+
89
+ @app.delete("/seedtoken")
90
+ async def delete_seedtoken(request: Request, credentials: HTTPAuthorizationCredentials = Security(security_scheme)):
91
+ verify_authorization(credentials.credentials)
92
+
93
+ try:
94
+ data = await request.json()
95
+ seed = data.get("seed")
96
+
97
+ if seed == "clear":
98
+ globals.seed_map.clear()
99
+ with open(globals.SEED_MAP_FILE, "w", encoding="utf-8") as f:
100
+ json.dump(globals.seed_map, f, indent=4)
101
+ return {"status": "success", "message": "All seeds deleted successfully"}
102
+
103
+ if not seed:
104
+ raise HTTPException(status_code=400, detail="Missing required field: seed")
105
+
106
+ if seed not in globals.seed_map:
107
+ raise HTTPException(status_code=404, detail=f"Seed '{seed}' not found")
108
+ del globals.seed_map[seed]
109
+
110
+ with open(globals.SEED_MAP_FILE, "w", encoding="utf-8") as f:
111
+ json.dump(globals.seed_map, f, indent=4)
112
+
113
+ return {
114
+ "status": "success",
115
+ "message": f"Seed '{seed}' deleted successfully"
116
+ }
117
+
118
+ except json.JSONDecodeError:
119
+ raise HTTPException(status_code=400, detail="Invalid JSON data")
120
+ except Exception as e:
121
+ raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
122
+
123
+
124
+ async def chatgpt_account_check(access_token):
125
+ auth_info = {}
126
+ client = Client(proxy=random.choice(proxy_url_list) if proxy_url_list else None)
127
+ try:
128
+ host_url = random.choice(chatgpt_base_url_list) if chatgpt_base_url_list else "https://chatgpt.com"
129
+ req_token = await get_real_req_token(access_token)
130
+ access_token = await verify_token(req_token)
131
+ fp = get_fp(req_token).copy()
132
+ proxy_url = fp.pop("proxy_url", None)
133
+ impersonate = fp.pop("impersonate", "safari15_3")
134
+
135
+ headers = base_headers.copy()
136
+ headers.update(fp)
137
+ headers.update({"authorization": f"Bearer {access_token}"})
138
+
139
+ client = Client(proxy=proxy_url, impersonate=impersonate)
140
+ r = await client.get(f"{host_url}/backend-api/models?history_and_training_disabled=false", headers=headers,
141
+ timeout=10)
142
+ if r.status_code != 200:
143
+ raise HTTPException(status_code=r.status_code, detail=r.text)
144
+ models = r.json()
145
+ r = await client.get(f"{host_url}/backend-api/accounts/check/v4-2023-04-27", headers=headers, timeout=10)
146
+ if r.status_code != 200:
147
+ raise HTTPException(status_code=r.status_code, detail=r.text)
148
+ accounts_info = r.json()
149
+
150
+ auth_info.update({"models": models["models"]})
151
+ auth_info.update({"accounts_info": accounts_info})
152
+
153
+ account_ordering = accounts_info.get("account_ordering", [])
154
+ is_deactivated = True
155
+ plan_type = None
156
+ team_ids = []
157
+ for account in account_ordering:
158
+ this_is_deactivated = accounts_info['accounts'].get(account, {}).get("account", {}).get("is_deactivated", False)
159
+ this_plan_type = accounts_info['accounts'].get(account, {}).get("account", {}).get("plan_type", "free")
160
+
161
+ if not this_is_deactivated:
162
+ is_deactivated = False
163
+
164
+ if "team" in this_plan_type and not this_is_deactivated:
165
+ plan_type = this_plan_type
166
+ team_ids.append(account)
167
+ elif plan_type is None:
168
+ plan_type = this_plan_type
169
+
170
+ auth_info.update({"accountCheckInfo": {
171
+ "is_deactivated": is_deactivated,
172
+ "plan_type": plan_type,
173
+ "team_ids": team_ids
174
+ }})
175
+
176
+ return auth_info
177
+ except Exception as e:
178
+ logger.error(f"chatgpt_account_check: {e}")
179
+ return {}
180
+ finally:
181
+ await client.close()
182
+
183
+
184
+ async def chatgpt_refresh(refresh_token):
185
+ client = Client(proxy=random.choice(proxy_url_list) if proxy_url_list else None)
186
+ try:
187
+ data = {
188
+ "client_id": "pdlLIX2Y72MIl2rhLhTE9VV9bN905kBh",
189
+ "grant_type": "refresh_token",
190
+ "redirect_uri": "com.openai.chat://auth0.openai.com/ios/com.openai.chat/callback",
191
+ "refresh_token": refresh_token
192
+ }
193
+ r = await client.post("https://auth0.openai.com/oauth/token", json=data, timeout=10)
194
+ if r.status_code != 200:
195
+ raise HTTPException(status_code=r.status_code, detail=r.text)
196
+ res = r.json()
197
+ auth_info = {}
198
+ auth_info.update(res)
199
+ auth_info.update({"refresh_token": refresh_token})
200
+ auth_info.update({"accessToken": res.get("access_token", "")})
201
+ return auth_info
202
+ except Exception as e:
203
+ logger.error(f"chatgpt_refresh: {e}")
204
+ return {}
205
+ finally:
206
+ await client.close()
207
+
208
+
209
+ @app.post("/auth/refresh")
210
+ async def refresh(request: Request):
211
+ auth_info = {}
212
+ form_data = await request.form()
213
+
214
+ auth_info.update(form_data)
215
+
216
+ access_token = auth_info.get("access_token", auth_info.get("accessToken", ""))
217
+ refresh_token = auth_info.get("refresh_token", "")
218
+
219
+ if not refresh_token and not access_token:
220
+ raise HTTPException(status_code=401, detail="refresh_token or access_token is required")
221
+
222
+ need_refresh = True
223
+ if access_token:
224
+ try:
225
+ access_token_info = jwt.decode(access_token, options={"verify_signature": False})
226
+ exp = access_token_info.get("exp", 0)
227
+ if exp > int(time.time()) + 60 * 60 * 24 * 5:
228
+ need_refresh = False
229
+ except Exception as e:
230
+ logger.error(f"access_token: {e}")
231
+
232
+ if refresh_token and need_refresh:
233
+ chatgpt_refresh_info = await chatgpt_refresh(refresh_token)
234
+ if chatgpt_refresh_info:
235
+ auth_info.update(chatgpt_refresh_info)
236
+ access_token = auth_info.get("accessToken", "")
237
+ account_check_info = await chatgpt_account_check(access_token)
238
+ if account_check_info:
239
+ auth_info.update(account_check_info)
240
+ auth_info.update({"accessToken": access_token})
241
+ return Response(content=json.dumps(auth_info), media_type="application/json")
242
+ elif access_token:
243
+ account_check_info = await chatgpt_account_check(access_token)
244
+ if account_check_info:
245
+ auth_info.update(account_check_info)
246
+ auth_info.update({"accessToken": access_token})
247
+ return Response(content=json.dumps(auth_info), media_type="application/json")
248
+
249
+ raise HTTPException(status_code=401, detail="Unauthorized")
250
+
251
+
gateway/v1.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+
3
+ from fastapi import Request
4
+ from fastapi.responses import Response
5
+
6
+ from app import app
7
+ from gateway.reverseProxy import chatgpt_reverse_proxy
8
+ from utils.kv_utils import set_value_for_key
9
+
10
+
11
+ @app.post("/v1/initialize")
12
+ async def initialize(request: Request):
13
+ initialize_response = (await chatgpt_reverse_proxy(request, f"v1/initialize"))
14
+ initialize_str = initialize_response.body.decode('utf-8')
15
+ initialize_json = json.loads(initialize_str)
16
+ set_value_for_key(initialize_json, "ip", "8.8.8.8")
17
+ set_value_for_key(initialize_json, "country", "US")
18
+ return Response(content=json.dumps(initialize_json, indent=4), media_type="application/json")
19
+
20
+
21
+ @app.post("/v1/rgstr")
22
+ async def rgstr():
23
+ return Response(status_code=202, content=json.dumps({"success": True}, indent=4), media_type="application/json")
24
+
25
+
26
+ @app.get("/ces/v1/projects/oai/settings")
27
+ async def ces_v1_projects_oai_settings():
28
+ return Response(status_code=200, content=json.dumps({"integrations":{"Segment.io":{"apiHost":"chatgpt.com/ces/v1","apiKey":"oai"}}}, indent=4), media_type="application/json")
29
+
30
+
31
+ @app.post("/ces/v1/{path:path}")
32
+ async def ces_v1():
33
+ return Response(status_code=202, content=json.dumps({"success": True}, indent=4), media_type="application/json")
requirements.txt ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ fastapi==0.115.3
2
+ python-multipart==0.0.13
3
+ curl_cffi==0.7.3
4
+ uvicorn
5
+ tiktoken
6
+ python-dotenv
7
+ websockets
8
+ pillow
9
+ pybase64
10
+ jinja2
11
+ APScheduler
12
+ ua-generator
13
+ pyjwt
templates/chatgpt.html ADDED
@@ -0,0 +1,427 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html data-build="prod-0749372c57dc2f5a1c6aa5f4f9802a682aebf139" dir="ltr" class="">
3
+ <head>
4
+ <meta charSet="UTF-8"/>
5
+ <meta name="viewport" content="width=device-width, initial-scale=1"/>
6
+ <link rel="preconnect" href=""/>
7
+ <meta name="robots" content="index, follow"/>
8
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
9
+ <meta name="apple-itunes-app" content="app-id=6448311069"/>
10
+ <link rel="apple-touch-icon" sizes="180x180" href="/assets/apple-touch-icon-mz9nytnj.webp"/>
11
+ <link rel="icon" type="image/png" sizes="32x32" href="/assets/favicon-32x32-p4ktpm1x.webp" media="(prefers-color-scheme: light)"/>
12
+ <link rel="icon" type="image/png" sizes="32x32" href="/assets/favicon-dark-32x32-gt5kfzyp.webp" media="(prefers-color-scheme: dark)"/>
13
+ <link rel="icon" type="image/svg+xml" sizes="32x32" href="/assets/favicon-o20kmmos.svg"/>
14
+ <title>ChatGPT</title>
15
+ <meta name="description" content="ChatGPT helps you get answers, find inspiration and be more productive. It is free to use and easy to try. Just ask and ChatGPT can help with writing, learning, brainstorming and more."/>
16
+ <meta name="keyword" content="ai chat,ai,chap gpt,chat gbt,chat gpt 3,chat gpt login,chat gpt website,chat gpt,chat gtp,chat openai,chat,chatai,chatbot gpt,chatg,chatgpt login,chatgpt,gpt chat,open ai,openai chat,openai chatgpt,openai"/>
17
+ <meta property="og:description" content="A conversational AI system that listens, learns, and challenges"/>
18
+ <meta property="og:title" content="ChatGPT"/>
19
+ <meta property="og:image" content="/assets/chatgpt-share-og-u7j5uyao.webp"/>
20
+ <meta property="og:url" content="https://chatgpt.com"/>
21
+ <link rel="modulepreload" href="/assets/manifest-de1f64fe.js"/>
22
+ <link rel="modulepreload" href="/assets/dpwaaxblirg4r8ba.js"/>
23
+ <link rel="modulepreload" href="/assets/lklglvrzltpv3f05.js"/>
24
+ <link rel="modulepreload" href="/assets/cvnnwrmw3zqz8rlo.js"/>
25
+ <link rel="modulepreload" href="/assets/ltrr4mhf6s4m7a72.js"/>
26
+ <link rel="modulepreload" href="/assets/olbhxi7cvw9yat6w.js"/>
27
+ <link rel="modulepreload" href="/assets/myjrgpm0gs628bnj.js"/>
28
+ <link rel="modulepreload" href="/assets/ntw4l0fkgqdiln85.js"/>
29
+ <link rel="modulepreload" href="/assets/kqs0d9q6go77jiiq.js"/>
30
+ <link rel="modulepreload" href="/assets/gruuolgwd1up46vp.js"/>
31
+ <link rel="modulepreload" href="/assets/hzzlkn5ny7a6v61q.js"/>
32
+ <link rel="modulepreload" href="/assets/jebvbwlsp750x4d4.js"/>
33
+ <link rel="modulepreload" href="/assets/m9aklkujr89s52e5.js"/>
34
+ <link rel="modulepreload" href="/assets/p8eg3euad9gvin3k.js"/>
35
+ <link rel="modulepreload" href="/assets/emuaj61pmpol8zzg.js"/>
36
+ <link rel="modulepreload" href="/assets/m5cd4p680vhusvsg.js"/>
37
+ <link rel="modulepreload" href="/assets/dz00r7qlzgek59uu.js"/>
38
+ <link rel="modulepreload" href="/assets/bwsmtgh34vk8igjb.js"/>
39
+ <link rel="modulepreload" href="/assets/vyarha3ckzwu9yf1.js"/>
40
+ <link rel="modulepreload" href="/assets/lnovrxcizvfadwta.js"/>
41
+ <link rel="modulepreload" href="/assets/n7xaphlc0h5m9xjk.js"/>
42
+ <link rel="modulepreload" href="/assets/lweu58rz02vpanvm.js"/>
43
+ <link rel="modulepreload" href="/assets/hags0lzrfk4kk50a.js"/>
44
+ <link rel="modulepreload" href="/assets/i9ntb1zjwbcuz2uj.js"/>
45
+ <link rel="modulepreload" href="/assets/c84k0nbcah46qpzd.js"/>
46
+ <link rel="modulepreload" href="/assets/jor8b61nis2ex9lk.js"/>
47
+ <link rel="modulepreload" href="/assets/jjr9on9cxlrbskjq.js"/>
48
+ <link rel="stylesheet" href="/assets/root-bh5spu2n.css"/>
49
+ <link rel="stylesheet" href="/assets/conversation-small-iybhw0ps.css"/>
50
+ </head>
51
+ <body class="">
52
+ <script>
53
+ !function() {
54
+ try {
55
+ var d = document.documentElement
56
+ , c = d.classList;
57
+ c.remove('light', 'dark');
58
+ var e = localStorage.getItem('theme');
59
+ if ('system' === e || (!e && true)) {
60
+ var t = '(prefers-color-scheme: dark)'
61
+ , m = window.matchMedia(t);
62
+ if (m.media !== t || m.matches) {
63
+ d.style.colorScheme = 'dark';
64
+ c.add('dark')
65
+ } else {
66
+ d.style.colorScheme = 'light';
67
+ c.add('light')
68
+ }
69
+ } else if (e) {
70
+ c.add(e || '')
71
+ }
72
+ if (e === 'light' || e === 'dark')
73
+ d.style.colorScheme = e
74
+ } catch (e) {}
75
+ }()
76
+ </script>
77
+ <div class="relative flex h-full w-full overflow-hidden transition-colors z-0">
78
+ <div class="z-[21] flex-shrink-0 overflow-x-hidden bg-token-sidebar-surface-primary max-md:!w-0" style="width:260px">
79
+ <div class="h-full w-[260px]">
80
+ <div class="flex h-full min-h-0 flex-col">
81
+ <div class="draggable relative h-full w-full flex-1 items-start border-white/20">
82
+ <h2 style="position:absolute;border:0;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0, 0, 0, 0);white-space:nowrap;word-wrap:normal">Chat history</h2>
83
+ <nav class="flex h-full w-full flex-col px-3" aria-label="Chat history">
84
+ <div class="flex justify-between flex h-[60px] items-center md:h-header-height">
85
+ <span class="hidden"></span>
86
+ <span class="flex" data-state="closed">
87
+ <button aria-label="Close sidebar" data-testid="close-sidebar-button" class="h-10 rounded-lg px-2 text-token-text-secondary focus-visible:outline-0 disabled:text-token-text-quaternary focus-visible:bg-token-sidebar-surface-secondary enabled:hover:bg-token-sidebar-surface-secondary no-draggable">
88
+ <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" class="icon-xl-heavy max-md:hidden">
89
+ <path fill-rule="evenodd" clip-rule="evenodd" d="M8.85719 3H15.1428C16.2266 2.99999 17.1007 2.99998 17.8086 3.05782C18.5375 3.11737 19.1777 3.24318 19.77 3.54497C20.7108 4.02433 21.4757 4.78924 21.955 5.73005C22.2568 6.32234 22.3826 6.96253 22.4422 7.69138C22.5 8.39925 22.5 9.27339 22.5 10.3572V13.6428C22.5 14.7266 22.5 15.6008 22.4422 16.3086C22.3826 17.0375 22.2568 17.6777 21.955 18.27C21.4757 19.2108 20.7108 19.9757 19.77 20.455C19.1777 20.7568 18.5375 20.8826 17.8086 20.9422C17.1008 21 16.2266 21 15.1428 21H8.85717C7.77339 21 6.89925 21 6.19138 20.9422C5.46253 20.8826 4.82234 20.7568 4.23005 20.455C3.28924 19.9757 2.52433 19.2108 2.04497 18.27C1.74318 17.6777 1.61737 17.0375 1.55782 16.3086C1.49998 15.6007 1.49999 14.7266 1.5 13.6428V10.3572C1.49999 9.27341 1.49998 8.39926 1.55782 7.69138C1.61737 6.96253 1.74318 6.32234 2.04497 5.73005C2.52433 4.78924 3.28924 4.02433 4.23005 3.54497C4.82234 3.24318 5.46253 3.11737 6.19138 3.05782C6.89926 2.99998 7.77341 2.99999 8.85719 3ZM6.35424 5.05118C5.74907 5.10062 5.40138 5.19279 5.13803 5.32698C4.57354 5.6146 4.1146 6.07354 3.82698 6.63803C3.69279 6.90138 3.60062 7.24907 3.55118 7.85424C3.50078 8.47108 3.5 9.26339 3.5 10.4V13.6C3.5 14.7366 3.50078 15.5289 3.55118 16.1458C3.60062 16.7509 3.69279 17.0986 3.82698 17.362C4.1146 17.9265 4.57354 18.3854 5.13803 18.673C5.40138 18.8072 5.74907 18.8994 6.35424 18.9488C6.97108 18.9992 7.76339 19 8.9 19H9.5V5H8.9C7.76339 5 6.97108 5.00078 6.35424 5.05118ZM11.5 5V19H15.1C16.2366 19 17.0289 18.9992 17.6458 18.9488C18.2509 18.8994 18.5986 18.8072 18.862 18.673C19.4265 18.3854 19.8854 17.9265 20.173 17.362C20.3072 17.0986 20.3994 16.7509 20.4488 16.1458C20.4992 15.5289 20.5 14.7366 20.5 13.6V10.4C20.5 9.26339 20.4992 8.47108 20.4488 7.85424C20.3994 7.24907 20.3072 6.90138 20.173 6.63803C19.8854 6.07354 19.4265 5.6146 18.862 5.32698C18.5986 5.19279 18.2509 5.10062 17.6458 5.05118C17.0289 5.00078 16.2366 5 15.1 5H11.5ZM5 8.5C5 7.94772 5.44772 7.5 6 7.5H7C7.55229 7.5 8 7.94772 8 8.5C8 9.05229 7.55229 9.5 7 9.5H6C5.44772 9.5 5 9.05229 5 8.5ZM5 12C5 11.4477 5.44772 11 6 11H7C7.55229 11 8 11.4477 8 12C8 12.5523 7.55229 13 7 13H6C5.44772 13 5 12.5523 5 12Z" fill="currentColor"></path>
90
+ </svg>
91
+ <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" class="icon-xl-heavy md:hidden">
92
+ <path fill-rule="evenodd" clip-rule="evenodd" d="M3 8C3 7.44772 3.44772 7 4 7H20C20.5523 7 21 7.44772 21 8C21 8.55228 20.5523 9 20 9H4C3.44772 9 3 8.55228 3 8ZM3 16C3 15.4477 3.44772 15 4 15H14C14.5523 15 15 15.4477 15 16C15 16.5523 14.5523 17 14 17H4C3.44772 17 3 16.5523 3 16Z" fill="currentColor"></path>
93
+ </svg>
94
+ </button>
95
+ </span>
96
+ <div class="flex">
97
+ <span class="hidden"></span>
98
+ <span class="flex" data-state="closed">
99
+ <button aria-label="New chat" data-testid="create-new-chat-button" class="h-10 rounded-lg px-2 text-token-text-secondary focus-visible:outline-0 disabled:text-token-text-quaternary focus-visible:bg-token-sidebar-surface-secondary enabled:hover:bg-token-sidebar-surface-secondary">
100
+ <svg width="24" height="24" viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg" class="icon-xl-heavy">
101
+ <path d="M15.6729 3.91287C16.8918 2.69392 18.8682 2.69392 20.0871 3.91287C21.3061 5.13182 21.3061 7.10813 20.0871 8.32708L14.1499 14.2643C13.3849 15.0293 12.3925 15.5255 11.3215 15.6785L9.14142 15.9899C8.82983 16.0344 8.51546 15.9297 8.29289 15.7071C8.07033 15.4845 7.96554 15.1701 8.01005 14.8586L8.32149 12.6785C8.47449 11.6075 8.97072 10.615 9.7357 9.85006L15.6729 3.91287ZM18.6729 5.32708C18.235 4.88918 17.525 4.88918 17.0871 5.32708L11.1499 11.2643C10.6909 11.7233 10.3932 12.3187 10.3014 12.9613L10.1785 13.8215L11.0386 13.6986C11.6812 13.6068 12.2767 13.3091 12.7357 12.8501L18.6729 6.91287C19.1108 6.47497 19.1108 5.76499 18.6729 5.32708ZM11 3.99929C11.0004 4.55157 10.5531 4.99963 10.0008 5.00007C9.00227 5.00084 8.29769 5.00827 7.74651 5.06064C7.20685 5.11191 6.88488 5.20117 6.63803 5.32695C6.07354 5.61457 5.6146 6.07351 5.32698 6.63799C5.19279 6.90135 5.10062 7.24904 5.05118 7.8542C5.00078 8.47105 5 9.26336 5 10.4V13.6C5 14.7366 5.00078 15.5289 5.05118 16.1457C5.10062 16.7509 5.19279 17.0986 5.32698 17.3619C5.6146 17.9264 6.07354 18.3854 6.63803 18.673C6.90138 18.8072 7.24907 18.8993 7.85424 18.9488C8.47108 18.9992 9.26339 19 10.4 19H13.6C14.7366 19 15.5289 18.9992 16.1458 18.9488C16.7509 18.8993 17.0986 18.8072 17.362 18.673C17.9265 18.3854 18.3854 17.9264 18.673 17.3619C18.7988 17.1151 18.8881 16.7931 18.9393 16.2535C18.9917 15.7023 18.9991 14.9977 18.9999 13.9992C19.0003 13.4469 19.4484 12.9995 20.0007 13C20.553 13.0004 21.0003 13.4485 20.9999 14.0007C20.9991 14.9789 20.9932 15.7808 20.9304 16.4426C20.8664 17.116 20.7385 17.7136 20.455 18.2699C19.9757 19.2107 19.2108 19.9756 18.27 20.455C17.6777 20.7568 17.0375 20.8826 16.3086 20.9421C15.6008 21 14.7266 21 13.6428 21H10.3572C9.27339 21 8.39925 21 7.69138 20.9421C6.96253 20.8826 6.32234 20.7568 5.73005 20.455C4.78924 19.9756 4.02433 19.2107 3.54497 18.2699C3.24318 17.6776 3.11737 17.0374 3.05782 16.3086C2.99998 15.6007 2.99999 14.7266 3 13.6428V10.3572C2.99999 9.27337 2.99998 8.39922 3.05782 7.69134C3.11737 6.96249 3.24318 6.3223 3.54497 5.73001C4.02433 4.7892 4.78924 4.0243 5.73005 3.54493C6.28633 3.26149 6.88399 3.13358 7.55735 3.06961C8.21919 3.00673 9.02103 3.00083 9.99922 3.00007C10.5515 2.99964 10.9996 3.447 11 3.99929Z" fill="currentColor"></path>
102
+ </svg>
103
+ </button>
104
+ </span>
105
+ </div>
106
+ </div>
107
+ <div class="flex-col flex-1 transition-opacity duration-500 relative -mr-2 pr-2 overflow-y-auto">
108
+ <div class="group/sidebar">
109
+ <div class="flex flex-col gap-2 text-token-text-primary text-sm h-full justify-center items-center empty:hidden mt-5 pb-2"></div>
110
+ </div>
111
+ </div>
112
+ </nav>
113
+ </div>
114
+ </div>
115
+ </div>
116
+ </div>
117
+ <div class="relative flex h-full max-w-full flex-1 flex-col overflow-hidden">
118
+ <main class="relative h-full w-full flex-1 overflow-auto transition-width">
119
+ <div role="presentation" class="composer-parent flex h-full flex-col focus-visible:outline-0">
120
+ <div class="flex-1 overflow-hidden @container/thread">
121
+ <div class="relative h-full">
122
+ <div class="absolute left-0 right-0">
123
+ <div class="draggable no-draggable-children sticky top-0 p-3 mb-1.5 flex items-center justify-between z-10 h-header-height font-semibold bg-token-main-surface-primary max-md:hidden">
124
+ <div class="absolute start-1/2 ltr:-translate-x-1/2 rtl:translate-x-1/2"></div>
125
+ <div class="flex items-center gap-0 overflow-hidden"></div>
126
+ <div class="gap-2 flex items-center pr-1 leading-[0]"></div>
127
+ </div>
128
+ </div>
129
+ <div class="flex h-full flex-col items-center justify-center text-token-text-primary">
130
+ <div class="h-full w-full lg:py-[18px]">
131
+ <div class="m-auto text-base px-3 md:px-4 w-full md:px-5 lg:px-4 xl:px-5 h-full">
132
+ <div class="mx-auto flex h-full w-full flex-col text-base lg:justify-center md:max-w-3xl lg:max-w-[40rem] xl:max-w-[48rem]">
133
+ <div class="mb-7 hidden text-center lg:block">
134
+ <div class="relative inline-flex justify-center text-center text-2xl font-semibold leading-9">
135
+ <h1 style="view-transition-name:var(--vt-splash-screen-headline)">What can I help with?</h1>
136
+ </div>
137
+ </div>
138
+ <div class="justify-center mt-[var(--screen-optical-compact-offset-amount)] flex h-full flex-shrink flex-col items-center overflow-hidden text-token-text-primary lg:hidden" style="opacity:0;will-change:opacity">
139
+ <div class="relative inline-flex justify-center text-center text-2xl font-semibold leading-9">
140
+ <h1 style="view-transition-name:var(--vt-splash-screen-headline)">What can I help with?</h1>
141
+ </div>
142
+ <div class="h-[116px]" style="opacity:0;will-change:opacity"></div>
143
+ </div>
144
+ <div class="lg:absolute lg:bottom-8 lg:left-0 lg:w-full">
145
+ <div class="mx-auto flex h-full w-full flex-col text-base lg:justify-center md:max-w-3xl lg:max-w-[40rem] xl:max-w-[48rem]">
146
+ <div class="block z-20"></div>
147
+ </div>
148
+ </div>
149
+ <div class="w-full">
150
+ <div class="flex justify-center"></div>
151
+ <form class="w-full" type="button" aria-haspopup="dialog" aria-expanded="false" aria-controls="radix-:R7nkkuiiklj5:" data-state="closed">
152
+ <div class="relative flex h-full max-w-full flex-1 flex-col">
153
+ <div class="group relative flex w-full items-center">
154
+ <div class="w-full">
155
+ <div id="composer-background" class="flex w-full cursor-text flex-col rounded-3xl px-2.5 py-1 transition-colors contain-inline-size bg-[#f4f4f4] dark:bg-token-main-surface-secondary" style="view-transition-name:var(--vt-composer)">
156
+ <div class="flex min-h-[44px] items-start pl-2">
157
+ <div class="min-w-0 max-w-full flex-1">
158
+ <div class="_prosemirror-parent_cy42l_1 text-token-text-primary max-h-[25dvh] max-h-52 overflow-auto default-browser">
159
+ <textarea class="block h-10 w-full resize-none border-0 bg-transparent px-0 py-2 text-token-text-primary placeholder:text-token-text-secondary" autofocus="" placeholder="Message ChatGPT" data-virtualkeyboard="true"></textarea>
160
+ <script nonce="c506c601-cf42-4631-ad36-4d3389e68112">
161
+ window.__oai_logHTML ? window.__oai_logHTML() : window.__oai_SSR_HTML = window.__oai_SSR_HTML || Date.now();
162
+ requestAnimationFrame((function() {
163
+ window.__oai_logTTI ? window.__oai_logTTI() : window.__oai_SSR_TTI = window.__oai_SSR_TTI || Date.now()
164
+ }
165
+ ))
166
+ </script>
167
+ </div>
168
+ </div>
169
+ <div class="w-[32px] pt-1"></div>
170
+ </div>
171
+ <div class="flex h-[44px] items-center justify-between">
172
+ <div class="flex gap-x-1">
173
+ <div style="view-transition-name:var(--vt-composer-attach-file-action)">
174
+ <div class="relative">
175
+ <div class="relative">
176
+ <span class="hidden"></span>
177
+ <span class="flex" data-state="closed">
178
+ <div class="flex">
179
+ <button disabled="" aria-label="Attach files is unavailable" class="flex items-center justify-center h-8 w-8 rounded-lg rounded-bl-xl text-token-text-primary dark:text-white focus-visible:outline-black dark:focus-visible:outline-white opacity-30">
180
+ <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
181
+ <path fill-rule="evenodd" clip-rule="evenodd" d="M9 7C9 4.23858 11.2386 2 14 2C16.7614 2 19 4.23858 19 7V15C19 18.866 15.866 22 12 22C8.13401 22 5 18.866 5 15V9C5 8.44772 5.44772 8 6 8C6.55228 8 7 8.44772 7 9V15C7 17.7614 9.23858 20 12 20C14.7614 20 17 17.7614 17 15V7C17 5.34315 15.6569 4 14 4C12.3431 4 11 5.34315 11 7V15C11 15.5523 11.4477 16 12 16C12.5523 16 13 15.5523 13 15V9C13 8.44772 13.4477 8 14 8C14.5523 8 15 8.44772 15 9V15C15 16.6569 13.6569 18 12 18C10.3431 18 9 16.6569 9 15V7Z" fill="currentColor"></path>
182
+ </svg>
183
+ </button>
184
+ <input disabled="" multiple="" type="file" style="display:none" tabindex="-1" class="hidden"/>
185
+ </div>
186
+ </span>
187
+ </div>
188
+ </div>
189
+ </div>
190
+ </div>
191
+ <div class="min-w-8">
192
+ <span class="hidden"></span>
193
+ <span class="" data-state="closed">
194
+ <button disabled="" aria-label="Send prompt" data-testid="send-button" class="flex h-8 w-8 items-center justify-center rounded-full transition-colors hover:opacity-70 focus-visible:outline-none focus-visible:outline-black disabled:text-[#f4f4f4] disabled:hover:opacity-100 dark:focus-visible:outline-white disabled:dark:bg-token-text-quaternary dark:disabled:text-token-main-surface-secondary bg-black text-white dark:bg-white dark:text-black disabled:bg-[#D7D7D7]">
195
+ <svg width="32" height="32" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg" class="icon-2xl">
196
+ <path fill-rule="evenodd" clip-rule="evenodd" d="M15.1918 8.90615C15.6381 8.45983 16.3618 8.45983 16.8081 8.90615L21.9509 14.049C22.3972 14.4953 22.3972 15.2189 21.9509 15.6652C21.5046 16.1116 20.781 16.1116 20.3347 15.6652L17.1428 12.4734V22.2857C17.1428 22.9169 16.6311 23.4286 15.9999 23.4286C15.3688 23.4286 14.8571 22.9169 14.8571 22.2857V12.4734L11.6652 15.6652C11.2189 16.1116 10.4953 16.1116 10.049 15.6652C9.60265 15.2189 9.60265 14.4953 10.049 14.049L15.1918 8.90615Z" fill="currentColor"></path>
197
+ </svg>
198
+ </button>
199
+ </span>
200
+ </div>
201
+ </div>
202
+ </div>
203
+ </div>
204
+ </div>
205
+ </div>
206
+ </form>
207
+ </div>
208
+ <div class="hidden lg:block h-[70px]" style="opacity:0;will-change:opacity"></div>
209
+ <div class="w-full px-2 py-2 text-center text-xs text-token-text-secondary empty:hidden md:px-[60px] lg:absolute lg:bottom-0 lg:left-0">
210
+ <div class="min-h-4">
211
+ <div>ChatGPT can make mistakes. Check important info.</div>
212
+ </div>
213
+ </div>
214
+ </div>
215
+ </div>
216
+ </div>
217
+ </div>
218
+ </div>
219
+ </div>
220
+ <div class="md:pt-0 dark:border-white/20 md:border-transparent md:dark:border-transparent w-full">
221
+ <div>
222
+ <div class="relative w-full px-2 py-2 text-center text-xs text-token-text-secondary empty:hidden md:px-[60px]"></div>
223
+ </div>
224
+ </div>
225
+ </div>
226
+ <div class="group absolute bottom-2 end-2 z-20 flex flex-col gap-1 md:flex lg:bottom-3 lg:end-3">
227
+ <button class="flex h-6 w-6 items-center justify-center rounded-full border border-token-border-light text-xs text-token-text-secondary" type="button" id="radix-:R9eiiklj5:" aria-haspopup="menu" aria-expanded="false" data-state="closed" data-testid="undefined-button">?</button>
228
+ </div>
229
+ </main>
230
+ </div>
231
+ </div>
232
+ <div aria-live="assertive" aria-atomic="true" class="sr-only"></div>
233
+ <div aria-live="polite" aria-atomic="true" class="sr-only"></div>
234
+ <audio class="fixed bottom-0 left-0 hidden h-0 w-0" autoPlay="" crossorigin="anonymous"></audio>
235
+ <script nonce="c506c601-cf42-4631-ad36-4d3389e68112">
236
+ window.__remixContext = {{ remix_context|tojson }};
237
+ __remixContext.p = function(v, e, p, x) {
238
+ if (typeof e !== 'undefined') {
239
+ x = new Error("Unexpected Server Error");
240
+ x.stack = undefined;
241
+ p = Promise.reject(x);
242
+ } else {
243
+ p = Promise.resolve(v);
244
+ }
245
+ return p;
246
+ }
247
+ ;
248
+ __remixContext.n = function(i, k) {
249
+ __remixContext.t = __remixContext.t || {};
250
+ __remixContext.t[i] = __remixContext.t[i] || {};
251
+ let p = new Promise( (r, e) => {
252
+ __remixContext.t[i][k] = {
253
+ r: (v) => {
254
+ r(v);
255
+ }
256
+ ,
257
+ e: (v) => {
258
+ e(v);
259
+ }
260
+ };
261
+ }
262
+ );
263
+ setTimeout( () => {
264
+ if (typeof p._error !== "undefined" || typeof p._data !== "undefined") {
265
+ return;
266
+ }
267
+ __remixContext.t[i][k].e(new Error("Server timeout."))
268
+ }
269
+ , 5000);
270
+ return p;
271
+ }
272
+ ;
273
+ __remixContext.r = function(i, k, v, e, p, x) {
274
+ p = __remixContext.t[i][k];
275
+ if (typeof e !== 'undefined') {
276
+ x = new Error("Unexpected Server Error");
277
+ x.stack = undefined;
278
+ p.e(x);
279
+ } else {
280
+ p.r(v);
281
+ }
282
+ }
283
+ ;
284
+ Object.assign(__remixContext.state.loaderData["root"], {
285
+ "rq:[\"account-status\"]": __remixContext.n("root", "rq:[\"account-status\"]")
286
+ });
287
+ Object.assign(__remixContext.state.loaderData["routes/_conversation"], {});
288
+ __remixContext.a = 1;
289
+ </script>
290
+ <script nonce="c506c601-cf42-4631-ad36-4d3389e68112" type="module" async="">
291
+ import "/assets/manifest-de1f64fe.js";
292
+ import*as route0 from "/assets/olbhxi7cvw9yat6w.js";
293
+ import*as route1 from "/assets/jor8b61nis2ex9lk.js";
294
+ import*as route2 from "/assets/jjr9on9cxlrbskjq.js";
295
+
296
+ window.__remixRouteModules = {
297
+ "root": route0,
298
+ "routes/_conversation": route1,
299
+ "routes/_conversation._index": route2
300
+ };
301
+
302
+ import("/assets/dpwaaxblirg4r8ba.js");
303
+ </script>
304
+ <!--$?-->
305
+ <template id="B:0"></template>
306
+ <!--/$-->
307
+ <div hidden id="S:0">
308
+ <script nonce="c506c601-cf42-4631-ad36-4d3389e68112" async="">
309
+ __remixContext.r("root", "rq:[\"account-status\"]", {
310
+ "__type": "AccountState",
311
+ "accountItems": [{
312
+ "data": {
313
+ "id": "chatgpt",
314
+ "residencyRegion": "no_constraint",
315
+ "accountUserId": "user-chatgpt__chatgpt",
316
+ "name": null,
317
+ "profilePictureId": null,
318
+ "profilePictureUrl": null,
319
+ "structure": "personal",
320
+ "role": "account-owner",
321
+ "organizationId": null,
322
+ "promoData": {},
323
+ "deactivated": false,
324
+ "subscriptionStatus": {
325
+ "billingPeriod": "yearly",
326
+ "hasPaidSubscription": true,
327
+ "isActiveSubscriptionGratis": false,
328
+ "subscriptionPlan": "chatgptpro",
329
+ "planType": "pro",
330
+ "subscriptionExpiresAt": 2524579200,
331
+ "wasPaidCustomer": true,
332
+ "hasCustomerObject": true,
333
+ "lastActiveSubscription": {
334
+ "subscription_id": "chatgpt",
335
+ "purchase_origin_platform": "chatgpt_web",
336
+ "will_renew": true
337
+ },
338
+ "isResellerHosted": false
339
+ },
340
+ "features": ["beta_features", "bizmo_settings", "breeze_available", "browsing_available", "canvas", "canvas_code_execution", "canvas_opt_in", "chart_serialization", "chat_preferences_available", "chatgpt_ios_attest", "code_interpreter_available", "d3_controls", "d3_editor", "d3_editor_gpts", "dalle_3", "gizmo_canvas_toggle", "gizmo_reviews", "gizmo_support_emails", "graphite", "mfa", "model_ab_use_v2", "model_switcher", "new_plugin_oauth_endpoint", "no_auth_training_enabled_by_default", "o1_launch", "o1_pro_launch", "paragen_mainline_alternative", "plugins_available", "privacy_policy_nov_2023", "search_tool", "sentinel_enabled_for_subscription", "share_multimodal_links", "shareable_links", "starter_prompts", "sunshine_available", "user_settings_announcements", "voice_advanced_ga"],
341
+ "canAccessWithCurrentSession": true,
342
+ "ssoConnectionName": null
343
+ }
344
+ }],
345
+ "currentAccountId": "chatgpt"
346
+ })
347
+ </script>
348
+ </div>
349
+ <script nonce="c506c601-cf42-4631-ad36-4d3389e68112">
350
+ $RC = function(b, c, e) {
351
+ c = document.getElementById(c);
352
+ c.parentNode.removeChild(c);
353
+ var a = document.getElementById(b);
354
+ if (a) {
355
+ b = a.previousSibling;
356
+ if (e)
357
+ b.data = "$!",
358
+ a.setAttribute("data-dgst", e);
359
+ else {
360
+ e = b.parentNode;
361
+ a = b.nextSibling;
362
+ var f = 0;
363
+ do {
364
+ if (a && 8 === a.nodeType) {
365
+ var d = a.data;
366
+ if ("/$" === d)
367
+ if (0 === f)
368
+ break;
369
+ else
370
+ f--;
371
+ else
372
+ "$" !== d && "$?" !== d && "$!" !== d || f++
373
+ }
374
+ d = a.nextSibling;
375
+ e.removeChild(a);
376
+ a = d
377
+ } while (a);
378
+ for (; c.firstChild; )
379
+ e.insertBefore(c.firstChild, a);
380
+ b.data = "$"
381
+ }
382
+ b._reactRetry && b._reactRetry()
383
+ }
384
+ }
385
+ ;
386
+ $RC("B:0", "S:0")
387
+ </script>
388
+ <script nonce="c506c601-cf42-4631-ad36-4d3389e68112">
389
+ (function() {
390
+ function c() {
391
+ var b = a.contentDocument || a.contentWindow.document;
392
+ if (b) {
393
+ var d = b.createElement('script');
394
+ d.nonce = 'c506c601-cf42-4631-ad36-4d3389e68112';
395
+ d.innerHTML = "window.__CF$cv$params={r:'8f95012aa8b767e2',t:'MTczNTQyNTEyOC4wMDAwMDA='};var a=document.createElement('script');a.nonce='c506c601-cf42-4631-ad36-4d3389e68112';a.src='/cdn-cgi/challenge-platform/scripts/jsd/main.js';document.getElementsByTagName('head')[0].appendChild(a);";
396
+ b.getElementsByTagName('head')[0].appendChild(d)
397
+ }
398
+ }
399
+ if (document.body) {
400
+ var a = document.createElement('iframe');
401
+ a.height = 1;
402
+ a.width = 1;
403
+ a.style.position = 'absolute';
404
+ a.style.top = 0;
405
+ a.style.left = 0;
406
+ a.style.border = 'none';
407
+ a.style.visibility = 'hidden';
408
+ document.body.appendChild(a);
409
+ if ('loading' !== document.readyState)
410
+ c();
411
+ else if (window.addEventListener)
412
+ document.addEventListener('DOMContentLoaded', c);
413
+ else {
414
+ var e = document.onreadystatechange || function() {}
415
+ ;
416
+ document.onreadystatechange = function(b) {
417
+ e(b);
418
+ 'loading' !== document.readyState && (document.onreadystatechange = e,
419
+ c())
420
+ }
421
+ }
422
+ }
423
+ }
424
+ )();
425
+ </script>
426
+ </body>
427
+ </html>
templates/chatgpt_context.json ADDED
The diff for this file is too large to render. See raw diff
 
templates/gpts_context.json ADDED
The diff for this file is too large to render. See raw diff
 
templates/login.html ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="zh-CN">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>登录</title>
7
+ <script src="https://cdn.tailwindcss.com"></script>
8
+ <style>
9
+ #popup {
10
+ display: none;
11
+ }
12
+ </style>
13
+ </head>
14
+ <body class="bg-gradient-to-br from-blue-300 via-indigo-300 to-purple-400 min-h-screen flex items-center justify-center">
15
+ <div class="bg-white p-8 rounded-xl shadow-2xl w-96 max-w-md">
16
+ <h2 class="text-3xl font-bold text-center text-gray-800 mb-4">登录</h2>
17
+ <button
18
+ type="button"
19
+ onclick="openPopup()"
20
+ class="w-full bg-gradient-to-r from-indigo-500 to-blue-500 text-white font-bold py-3 px-4 rounded-lg hover:opacity-90 transition duration-200 ease-in-out"
21
+ >
22
+ RefreshToken / AccessToken
23
+ </button>
24
+ <p class="text-xs text-gray-500 text-center mt-4">
25
+ <span class="font-semibold text-gray-800">非
26
+ <span class="font-bold text-indigo-600">RT</span>
27
+
28
+ <span class="font-bold text-indigo-600">AT</span>
29
+ 的输入,将作为</span>
30
+ <span class="font-bold text-indigo-600">Seed</span>
31
+ <span class="font-semibold text-gray-800">随机抽取后台账号</span>
32
+ </p>
33
+ </div>
34
+
35
+ <div id="popup" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center">
36
+ <div class="bg-white p-8 rounded-xl shadow-lg max-w-lg w-full h-auto">
37
+ <!-- <h3 class="text-xl font-bold text-gray-800 mb-4 text-center">请输入您的 Token</h3>-->
38
+ <p class="font-semibold text-gray-800 text-center mb-1">直接点击
39
+ <span class="font-bold text-indigo-600">开始</span>
40
+ 进入最近用过的账号</p>
41
+ <label for="popup-input"></label>
42
+ <textarea
43
+ id="popup-input"
44
+ name="token"
45
+ placeholder="RefreshToken / AccessToken / SeedToken"
46
+ class="w-full h-56 px-4 py-4 text-md rounded-md bg-gray-100 border-gray-300 focus:border-indigo-500 focus:bg-white focus:ring-2 focus:ring-indigo-200 text-gray-800 transition duration-200 ease-in-out mb-4"
47
+ style="resize: none;"
48
+ ></textarea>
49
+ <div class="flex justify-center space-x-4">
50
+ <button
51
+ onclick="submitToken()"
52
+ class="bg-indigo-500 hover:bg-indigo-600 text-white font-bold py-2 px-4 rounded-lg transition duration-200 ease-in-out"
53
+ >
54
+ 开 始
55
+ </button>
56
+ <button
57
+ onclick="closePopup()"
58
+ class="bg-gray-300 hover:bg-gray-400 text-gray-800 font-bold py-2 px-4 rounded-lg transition duration-200 ease-in-out"
59
+ >
60
+ 取 消
61
+ </button>
62
+ </div>
63
+ </div>
64
+ </div>
65
+
66
+ <script>
67
+ function openPopup() {
68
+ document.getElementById('popup').style.display = 'flex';
69
+ }
70
+
71
+ function closePopup() {
72
+ document.getElementById('popup').style.display = 'none';
73
+ }
74
+
75
+ function submitToken() {
76
+ var inputValue = document.getElementById('popup-input').value;
77
+ window.location.href = '/?token=' + inputValue;
78
+ closePopup();
79
+ }
80
+ </script>
81
+ </body>
82
+ </html>
templates/tokens.html ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="zh-CN">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta content="width=device-width, initial-scale=1.0" name="viewport">
6
+ <title>Tokens 管理</title>
7
+ <script src="https://cdn.tailwindcss.com"></script>
8
+ <script>
9
+ document.addEventListener('DOMContentLoaded', () => {
10
+ const apiPrefix = "{{ api_prefix }}";
11
+ const uploadForm = document.getElementById('uploadForm');
12
+ const clearForm = document.getElementById('clearForm');
13
+ const errorButton = document.getElementById('errorButton');
14
+
15
+ if (apiPrefix === "None") {
16
+ uploadForm.action = "/tokens/upload";
17
+ clearForm.action = "/tokens/clear";
18
+ errorButton.dataset.api = "/tokens/error";
19
+ } else {
20
+ uploadForm.action = `/${apiPrefix}/tokens/upload`;
21
+ clearForm.action = `/${apiPrefix}/tokens/clear`;
22
+ errorButton.dataset.api = `/${apiPrefix}/tokens/error`;
23
+ }
24
+
25
+ errorButton.addEventListener('click', async () => {
26
+ const response = await fetch(errorButton.dataset.api, {
27
+ method: 'POST',
28
+ });
29
+ const result = await response.json();
30
+ const errorTokens = result.error_tokens;
31
+
32
+ const errorModal = document.getElementById('errorModal');
33
+ const errorModalContent = document.getElementById('errorModalContent');
34
+
35
+ errorModalContent.innerHTML = errorTokens.map(token => `<p>${token}</p>`).join('');
36
+ errorModal.classList.remove('hidden');
37
+ });
38
+
39
+ document.getElementById('errorModalClose').addEventListener('click', () => {
40
+ document.getElementById('errorModal').classList.add('hidden');
41
+ });
42
+
43
+ document.getElementById('errorModalCopy').addEventListener('click', () => {
44
+ const errorModalContent = document.getElementById('errorModalContent');
45
+ const textToCopy = errorModalContent.innerText.replace(/\n\n/g, '\n');
46
+ navigator.clipboard.writeText(textToCopy).then(() => {
47
+ alert('错误 Tokens 已复制到剪贴板');
48
+ }).catch(err => {
49
+ alert('复制失败,请手动复制');
50
+ });
51
+ });
52
+ });
53
+ </script>
54
+ </head>
55
+ <body class="bg-gradient-to-r from-blue-200 via-purple-200 to-pink-200 flex justify-center items-center min-h-screen">
56
+ <div class="bg-white p-10 rounded-lg shadow-2xl w-128 text-center">
57
+ <h1 class="text-4xl font-extrabold text-gray-900 mb-6">Tokens 管理</h1>
58
+ <p class="text-gray-600 mb-4">当前可用 Tokens 数量:<span class="text-blue-600">{{ tokens_count }}</span></p>
59
+ <form class="mb-2" id="uploadForm" method="post">
60
+ <textarea class="w-full p-4 mb-4 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-400 resize-none" name="text" placeholder="一行一个Token,可以是 AccessToken 或 RefreshToken" rows="10"></textarea>
61
+ <p class="text-gray-600 mb-2">注:使用docker时如果挂载了data文件夹则重启后不需要再次上传</p>
62
+ <button class="w-full bg-blue-600 text-white py-3 rounded-md hover:bg-blue-700 transition duration-300 mb-2" type="submit">上传</button>
63
+ </form>
64
+ <button id="errorButton" class="w-full bg-yellow-600 text-white py-3 rounded-md hover:bg-yellow-700 transition duration-200 mt-2">查看错误Tokens</button>
65
+ <p class="text-gray-600 mt-2">点击清空,将会清空上传和错误的 Tokens</p>
66
+ <form id="clearForm" method="post">
67
+ <button class="w-full bg-red-600 text-white py-3 rounded-md hover:bg-red-700 transition duration-300" type="submit">清空Tokens</button>
68
+ </form>
69
+ </div>
70
+
71
+ <div id="errorModal" class="fixed inset-0 bg-gray-800 bg-opacity-75 flex justify-center items-center hidden">
72
+ <div class="bg-white p-6 rounded-lg shadow-lg w-150">
73
+ <h2 class="text-2xl font-bold mb-4">错误 Tokens</h2>
74
+ <div id="errorModalContent" class="list-disc list-inside text-left mb-4"></div>
75
+ <div class="flex justify-end space-x-4">
76
+ <button id="errorModalCopy" class="bg-green-600 text-white py-2 px-4 rounded-md hover:bg-green-700 transition duration-300">复制</button>
77
+ <button id="errorModalClose" class="bg-red-600 text-white py-2 px-4 rounded-md hover:bg-red-700 transition duration-300">关闭</button>
78
+ </div>
79
+ </div>
80
+ </div>
81
+ </body>
82
+ </html>
utils/Client.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import random
2
+
3
+ from curl_cffi.requests import AsyncSession
4
+
5
+
6
+ class Client:
7
+ def __init__(self, proxy=None, timeout=15, verify=True, impersonate='safari15_3'):
8
+ self.proxies = {"http": proxy, "https": proxy}
9
+ self.timeout = timeout
10
+ self.verify = verify
11
+
12
+ self.impersonate = impersonate
13
+ # impersonate=self.impersonate
14
+
15
+ # self.ja3 = ""
16
+ # self.akamai = ""
17
+ # ja3=self.ja3, akamai=self.akamai
18
+ self.session = AsyncSession(proxies=self.proxies, timeout=self.timeout, impersonate=self.impersonate, verify=self.verify)
19
+ self.session2 = AsyncSession(proxies=self.proxies, timeout=self.timeout, impersonate=self.impersonate, verify=self.verify)
20
+
21
+ async def post(self, *args, **kwargs):
22
+ r = await self.session.post(*args, **kwargs)
23
+ return r
24
+
25
+ async def post_stream(self, *args, headers=None, cookies=None, **kwargs):
26
+ if self.session:
27
+ headers = headers or self.session.headers
28
+ cookies = cookies or self.session.cookies
29
+ r = await self.session2.post(*args, headers=headers, cookies=cookies, **kwargs)
30
+ return r
31
+
32
+ async def get(self, *args, **kwargs):
33
+ r = await self.session.get(*args, **kwargs)
34
+ return r
35
+
36
+ async def request(self, *args, **kwargs):
37
+ r = await self.session.request(*args, **kwargs)
38
+ return r
39
+
40
+ async def put(self, *args, **kwargs):
41
+ r = await self.session.put(*args, **kwargs)
42
+ return r
43
+
44
+ async def close(self):
45
+ if hasattr(self, 'session'):
46
+ try:
47
+ await self.session.close()
48
+ del self.session
49
+ except Exception:
50
+ pass
51
+ if hasattr(self, 'session2'):
52
+ try:
53
+ await self.session2.close()
54
+ del self.session2
55
+ except Exception:
56
+ pass
utils/Logger.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+
3
+ logging.basicConfig(level=logging.INFO, format='%(asctime)s | %(levelname)s | %(message)s')
4
+
5
+
6
+ class Logger:
7
+ @staticmethod
8
+ def info(message):
9
+ logging.info(str(message))
10
+
11
+ @staticmethod
12
+ def warning(message):
13
+ logging.warning("\033[0;33m" + str(message) + "\033[0m")
14
+
15
+ @staticmethod
16
+ def error(message):
17
+ logging.error("\033[0;31m" + "-" * 50 + '\n| ' + str(message) + "\033[0m" + "\n" + "└" + "-" * 80)
18
+
19
+ @staticmethod
20
+ def debug(message):
21
+ logging.debug("\033[0;37m" + str(message) + "\033[0m")
22
+
23
+
24
+ logger = Logger()
utils/configs.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import ast
2
+ import os
3
+
4
+ from dotenv import load_dotenv
5
+
6
+ from utils.Logger import logger
7
+
8
+ load_dotenv(encoding="ascii")
9
+
10
+
11
+ def is_true(x):
12
+ if isinstance(x, bool):
13
+ return x
14
+ if isinstance(x, str):
15
+ return x.lower() in ['true', '1', 't', 'y', 'yes']
16
+ elif isinstance(x, int):
17
+ return x == 1
18
+ else:
19
+ return False
20
+
21
+
22
+ api_prefix = os.getenv('API_PREFIX', None)
23
+ authorization = os.getenv('AUTHORIZATION', '').replace(' ', '')
24
+ chatgpt_base_url = os.getenv('CHATGPT_BASE_URL', 'https://chatgpt.com').replace(' ', '')
25
+ auth_key = os.getenv('AUTH_KEY', None)
26
+ x_sign = os.getenv('X_SIGN', None)
27
+
28
+ ark0se_token_url = os.getenv('ARK' + 'OSE_TOKEN_URL', '').replace(' ', '')
29
+ if not ark0se_token_url:
30
+ ark0se_token_url = os.getenv('ARK0SE_TOKEN_URL', None)
31
+ proxy_url = os.getenv('PROXY_URL', '').replace(' ', '')
32
+ sentinel_proxy_url = os.getenv('SENTINEL_PROXY_URL', None)
33
+ export_proxy_url = os.getenv('EXPORT_PROXY_URL', None)
34
+ file_host = os.getenv('FILE_HOST', None)
35
+ voice_host = os.getenv('VOICE_HOST', None)
36
+ impersonate_list_str = os.getenv('IMPERSONATE', '[]')
37
+ user_agents_list_str = os.getenv('USER_AGENTS', '[]')
38
+ device_tuple_str = os.getenv('DEVICE_TUPLE', '()')
39
+ browser_tuple_str = os.getenv('BROWSER_TUPLE', '()')
40
+ platform_tuple_str = os.getenv('PLATFORM_TUPLE', '()')
41
+
42
+ cf_file_url = os.getenv('CF_FILE_URL', None)
43
+ turnstile_solver_url = os.getenv('TURNSTILE_SOLVER_URL', None)
44
+
45
+ history_disabled = is_true(os.getenv('HISTORY_DISABLED', True))
46
+ pow_difficulty = os.getenv('POW_DIFFICULTY', '000032')
47
+ retry_times = int(os.getenv('RETRY_TIMES', 3))
48
+ conversation_only = is_true(os.getenv('CONVERSATION_ONLY', False))
49
+ enable_limit = is_true(os.getenv('ENABLE_LIMIT', True))
50
+ upload_by_url = is_true(os.getenv('UPLOAD_BY_URL', False))
51
+ check_model = is_true(os.getenv('CHECK_MODEL', False))
52
+ scheduled_refresh = is_true(os.getenv('SCHEDULED_REFRESH', False))
53
+ random_token = is_true(os.getenv('RANDOM_TOKEN', True))
54
+ oai_language = os.getenv('OAI_LANGUAGE', 'zh-CN')
55
+
56
+ authorization_list = authorization.split(',') if authorization else []
57
+ chatgpt_base_url_list = chatgpt_base_url.split(',') if chatgpt_base_url else []
58
+ ark0se_token_url_list = ark0se_token_url.split(',') if ark0se_token_url else []
59
+ proxy_url_list = proxy_url.split(',') if proxy_url else []
60
+ sentinel_proxy_url_list = sentinel_proxy_url.split(',') if sentinel_proxy_url else []
61
+ impersonate_list = ast.literal_eval(impersonate_list_str)
62
+ user_agents_list = ast.literal_eval(user_agents_list_str)
63
+ device_tuple = ast.literal_eval(device_tuple_str)
64
+ browser_tuple = ast.literal_eval(browser_tuple_str)
65
+ platform_tuple = ast.literal_eval(platform_tuple_str)
66
+
67
+ enable_gateway = is_true(os.getenv('ENABLE_GATEWAY', False))
68
+ auto_seed = is_true(os.getenv('AUTO_SEED', True))
69
+ force_no_history = is_true(os.getenv('FORCE_NO_HISTORY', False))
70
+ no_sentinel = is_true(os.getenv('NO_SENTINEL', False))
71
+
72
+ with open('version.txt') as f:
73
+ version = f.read().strip()
74
+
75
+ logger.info("-" * 60)
76
+ logger.info(f"Chat2Api {version} | https://github.com/lanqian528/chat2api")
77
+ logger.info("-" * 60)
78
+ logger.info("Environment variables:")
79
+ logger.info("------------------------- Security -------------------------")
80
+ logger.info("API_PREFIX: " + str(api_prefix))
81
+ logger.info("AUTHORIZATION: " + str(authorization_list))
82
+ logger.info("AUTH_KEY: " + str(auth_key))
83
+ logger.info("------------------------- Request --------------------------")
84
+ logger.info("CHATGPT_BASE_URL: " + str(chatgpt_base_url_list))
85
+ logger.info("PROXY_URL: " + str(proxy_url_list))
86
+ logger.info("EXPORT_PROXY_URL: " + str(export_proxy_url))
87
+ logger.info("FILE_HOST: " + str(file_host))
88
+ logger.info("VOICE_HOST: " + str(voice_host))
89
+ logger.info("IMPERSONATE: " + str(impersonate_list))
90
+ logger.info("USER_AGENTS: " + str(user_agents_list))
91
+ logger.info("---------------------- Functionality -----------------------")
92
+ logger.info("HISTORY_DISABLED: " + str(history_disabled))
93
+ logger.info("POW_DIFFICULTY: " + str(pow_difficulty))
94
+ logger.info("RETRY_TIMES: " + str(retry_times))
95
+ logger.info("CONVERSATION_ONLY: " + str(conversation_only))
96
+ logger.info("ENABLE_LIMIT: " + str(enable_limit))
97
+ logger.info("UPLOAD_BY_URL: " + str(upload_by_url))
98
+ logger.info("CHECK_MODEL: " + str(check_model))
99
+ logger.info("SCHEDULED_REFRESH: " + str(scheduled_refresh))
100
+ logger.info("RANDOM_TOKEN: " + str(random_token))
101
+ logger.info("OAI_LANGUAGE: " + str(oai_language))
102
+ logger.info("------------------------- Gateway --------------------------")
103
+ logger.info("ENABLE_GATEWAY: " + str(enable_gateway))
104
+ logger.info("AUTO_SEED: " + str(auto_seed))
105
+ logger.info("FORCE_NO_HISTORY: " + str(force_no_history))
106
+ logger.info("-" * 60)
utils/globals.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+
4
+ import utils.configs as configs
5
+ from utils.Logger import logger
6
+
7
+ DATA_FOLDER = "data"
8
+ TOKENS_FILE = os.path.join(DATA_FOLDER, "token.txt")
9
+ REFRESH_MAP_FILE = os.path.join(DATA_FOLDER, "refresh_map.json")
10
+ ERROR_TOKENS_FILE = os.path.join(DATA_FOLDER, "error_token.txt")
11
+ WSS_MAP_FILE = os.path.join(DATA_FOLDER, "wss_map.json")
12
+ FP_FILE = os.path.join(DATA_FOLDER, "fp_map.json")
13
+ SEED_MAP_FILE = os.path.join(DATA_FOLDER, "seed_map.json")
14
+ CONVERSATION_MAP_FILE = os.path.join(DATA_FOLDER, "conversation_map.json")
15
+
16
+ count = 0
17
+ token_list = []
18
+ error_token_list = []
19
+ refresh_map = {}
20
+ wss_map = {}
21
+ fp_map = {}
22
+ seed_map = {}
23
+ conversation_map = {}
24
+ impersonate_list = [
25
+ "chrome99",
26
+ "chrome100",
27
+ "chrome101",
28
+ "chrome104",
29
+ "chrome107",
30
+ "chrome110",
31
+ "chrome116",
32
+ "chrome119",
33
+ "chrome120",
34
+ "chrome123",
35
+ "edge99",
36
+ "edge101",
37
+ ] if not configs.impersonate_list else configs.impersonate_list
38
+
39
+ if not os.path.exists(DATA_FOLDER):
40
+ os.makedirs(DATA_FOLDER)
41
+
42
+ if os.path.exists(REFRESH_MAP_FILE):
43
+ with open(REFRESH_MAP_FILE, "r") as f:
44
+ try:
45
+ refresh_map = json.load(f)
46
+ except:
47
+ refresh_map = {}
48
+ else:
49
+ refresh_map = {}
50
+
51
+ if os.path.exists(WSS_MAP_FILE):
52
+ with open(WSS_MAP_FILE, "r") as f:
53
+ try:
54
+ wss_map = json.load(f)
55
+ except:
56
+ wss_map = {}
57
+ else:
58
+ wss_map = {}
59
+
60
+ if os.path.exists(FP_FILE):
61
+ with open(FP_FILE, "r", encoding="utf-8") as f:
62
+ try:
63
+ fp_map = json.load(f)
64
+ except:
65
+ fp_map = {}
66
+ else:
67
+ fp_map = {}
68
+
69
+ if os.path.exists(SEED_MAP_FILE):
70
+ with open(SEED_MAP_FILE, "r") as f:
71
+ try:
72
+ seed_map = json.load(f)
73
+ except:
74
+ seed_map = {}
75
+ else:
76
+ seed_map = {}
77
+
78
+ if os.path.exists(CONVERSATION_MAP_FILE):
79
+ with open(CONVERSATION_MAP_FILE, "r") as f:
80
+ try:
81
+ conversation_map = json.load(f)
82
+ except:
83
+ conversation_map = {}
84
+ else:
85
+ conversation_map = {}
86
+
87
+ if os.path.exists(TOKENS_FILE):
88
+ with open(TOKENS_FILE, "r", encoding="utf-8") as f:
89
+ for line in f:
90
+ if line.strip() and not line.startswith("#"):
91
+ token_list.append(line.strip())
92
+ else:
93
+ with open(TOKENS_FILE, "w", encoding="utf-8") as f:
94
+ pass
95
+
96
+ if os.path.exists(ERROR_TOKENS_FILE):
97
+ with open(ERROR_TOKENS_FILE, "r", encoding="utf-8") as f:
98
+ for line in f:
99
+ if line.strip() and not line.startswith("#"):
100
+ error_token_list.append(line.strip())
101
+ else:
102
+ with open(ERROR_TOKENS_FILE, "w", encoding="utf-8") as f:
103
+ pass
104
+
105
+ if token_list:
106
+ logger.info(f"Token list count: {len(token_list)}, Error token list count: {len(error_token_list)}")
107
+ logger.info("-" * 60)
utils/kv_utils.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ def set_value_for_key(data, target_key, new_value):
2
+ if isinstance(data, dict):
3
+ for key, value in data.items():
4
+ if key == target_key:
5
+ data[key] = new_value
6
+ else:
7
+ set_value_for_key(value, target_key, new_value)
8
+ elif isinstance(data, list):
9
+ for item in data:
10
+ set_value_for_key(item, target_key, new_value)
utils/retry.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import HTTPException
2
+
3
+ from utils.Logger import logger
4
+ from utils.configs import retry_times
5
+
6
+
7
+ async def async_retry(func, *args, max_retries=retry_times, **kwargs):
8
+ for attempt in range(max_retries + 1):
9
+ try:
10
+ result = await func(*args, **kwargs)
11
+ return result
12
+ except HTTPException as e:
13
+ if attempt == max_retries:
14
+ logger.error(f"Throw an exception {e.status_code}, {e.detail}")
15
+ if e.status_code == 500:
16
+ raise HTTPException(status_code=500, detail="Server error")
17
+ raise HTTPException(status_code=e.status_code, detail=e.detail)
18
+ logger.info(f"Retry {attempt + 1} status code {e.status_code}, {e.detail}. Retrying...")
19
+
20
+
21
+ def retry(func, *args, max_retries=retry_times, **kwargs):
22
+ for attempt in range(max_retries + 1):
23
+ try:
24
+ result = func(*args, **kwargs)
25
+ return result
26
+ except HTTPException as e:
27
+ if attempt == max_retries:
28
+ logger.error(f"Throw an exception {e.status_code}, {e.detail}")
29
+ if e.status_code == 500:
30
+ raise HTTPException(status_code=500, detail="Server error")
31
+ raise HTTPException(status_code=e.status_code, detail=e.detail)
32
+ logger.error(f"Retry {attempt + 1} status code {e.status_code}, {e.detail}. Retrying...")
version.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ v1.7.9-beta4