diff --git a/.github/workflows/build_docker.yml b/.github/workflows/build_docker.yml index 22d1c75..9fad0bc 100644 --- a/.github/workflows/build_docker.yml +++ b/.github/workflows/build_docker.yml @@ -37,7 +37,7 @@ jobs: images: lanqian528/chat2api tags: | type=raw,value=latest,enable={{is_default_branch}} - type=raw,value=v1.1.2 + type=raw,value=v1.1.3 - name: Build and push uses: docker/build-push-action@v5 diff --git a/chatgpt/ChatService.py b/chatgpt/ChatService.py index 531cd98..3214026 100644 --- a/chatgpt/ChatService.py +++ b/chatgpt/ChatService.py @@ -175,7 +175,11 @@ class ChatService: raise HTTPException(status_code=500, detail=str(e)) async def prepare_send_conversation(self): - chat_messages, self.prompt_tokens = await api_messages_to_chat(self, self.api_messages) + try: + chat_messages, self.prompt_tokens = await api_messages_to_chat(self, self.api_messages) + except Exception as e: + logger.error(f"Failed to format messages: {str(e)}") + raise HTTPException(status_code=400, detail="Failed to format messages.") self.chat_headers = self.base_headers.copy() self.chat_headers.update({ 'Accept': 'text/event-stream', diff --git a/chatgpt/chatFormat.py b/chatgpt/chatFormat.py index 2436bf8..872e7e1 100644 --- a/chatgpt/chatFormat.py +++ b/chatgpt/chatFormat.py @@ -268,7 +268,7 @@ async def api_messages_to_chat(service, api_messages): "height": height }) else: - file_tokens += file_size + file_tokens += file_size // 1000 attachments.append({ "id": file_id, "size": file_size, diff --git a/chatgpt/proofofWork.py b/chatgpt/proofofWork.py index 0885cb7..5ff1199 100644 --- a/chatgpt/proofofWork.py +++ b/chatgpt/proofofWork.py @@ -295,11 +295,7 @@ async def get_dpl(service): if int(time.time()) - cached_time < 15 * 60: return True headers = service.base_headers.copy() - if len(cached_scripts) == 0: - cached_scripts.append( - "https://cdn.oaistatic.com/_next/static/cXh69klOLzS0Gy2joLDRS/_ssgManifest.js?dpl=453ebaec0d44c2decab71692e1bfe39be35a24b3") - cached_dpl = "453ebaec0d44c2decab71692e1bfe39be35a24b3" - cached_time = int(time.time()) + cached_scripts.clear() try: if conversation_only: return True @@ -307,8 +303,15 @@ async def get_dpl(service): r.raise_for_status() parser = ScriptSrcParser() parser.feed(r.text) - return True + if len(cached_scripts) == 0: + raise Exception("No scripts found") + else: + return True except Exception: + cached_scripts.append( + "https://cdn.oaistatic.com/_next/static/cXh69klOLzS0Gy2joLDRS/_ssgManifest.js?dpl=453ebaec0d44c2decab71692e1bfe39be35a24b3") + cached_dpl = "453ebaec0d44c2decab71692e1bfe39be35a24b3" + cached_time = int(time.time()) return False diff --git a/chatgpt/refreshToken.py b/chatgpt/refreshToken.py index 0d2dc50..7220ecc 100644 --- a/chatgpt/refreshToken.py +++ b/chatgpt/refreshToken.py @@ -2,7 +2,6 @@ import json import os import random import time -from functools import cache from fastapi import HTTPException @@ -17,14 +16,11 @@ REFRESH_MAP_FILE = os.path.join(DATA_FOLDER, "refresh_map.json") if not os.path.exists(DATA_FOLDER): os.makedirs(DATA_FOLDER) - -@cache -def load_refresh_map(): - if os.path.exists(REFRESH_MAP_FILE): - with open(REFRESH_MAP_FILE, "r") as file: - return json.load(file) - else: - return {} +if os.path.exists(REFRESH_MAP_FILE): + with open(REFRESH_MAP_FILE, "r") as file: + refresh_map = json.load(file) +else: + refresh_map = {} def save_refresh_map(refresh_map): @@ -33,8 +29,7 @@ def save_refresh_map(refresh_map): async def rt2ac(refresh_token): - refresh_map = load_refresh_map() - if refresh_token in refresh_map and int(time.time()) - refresh_map.get(refresh_token, {}).get("timestamp", 0) < 24 * 60 * 60: + if refresh_token in refresh_map and int(time.time()) - refresh_map.get(refresh_token, {}).get("timestamp", 0) < 2 * 24 * 60 * 60: access_token = refresh_map[refresh_token]["token"] logger.info(f"refresh_token -> access_token from cache") return access_token diff --git a/utils/authorization.py b/utils/authorization.py index 35e9ab9..681f03d 100644 --- a/utils/authorization.py +++ b/utils/authorization.py @@ -14,17 +14,19 @@ token_list = [] DATA_FOLDER = "data" TOKENS_FILE = os.path.join(DATA_FOLDER, "token.txt") + if not os.path.exists(DATA_FOLDER): os.makedirs(DATA_FOLDER) -if os.path.exists("data/token.txt"): - with open("data/token.txt", "r", encoding="utf-8") as f: +if os.path.exists(TOKENS_FILE): + with open(TOKENS_FILE, "r", encoding="utf-8") as f: for line in f: if line.strip() and not line.startswith("#"): token_list.append(line.strip()) else: - with open("data/token.txt", "w", encoding="utf-8") as f: + with open(TOKENS_FILE, "w", encoding="utf-8") as f: pass + if token_list: logger.info(f"Token list count: {len(token_list)}") diff --git a/utils/config.py b/utils/config.py index cdb9f90..29010b6 100644 --- a/utils/config.py +++ b/utils/config.py @@ -35,7 +35,7 @@ arkose_token_url_list = arkose_token_url.split(',') if arkose_token_url else [] proxy_url_list = proxy_url.split(',') if proxy_url else [] logger.info("-" * 60) -logger.info("Chat2Api v1.1.2 | https://github.com/lanqian528/chat2api") +logger.info("Chat2Api v1.1.3 | https://github.com/lanqian528/chat2api") logger.info("-" * 60) logger.info("Environment variables:") logger.info("API_PREFIX: " + str(api_prefix))