This commit is contained in:
LanQian 2024-05-22 14:34:58 +08:00
parent 2b439cd342
commit f6565ac817
7 changed files with 28 additions and 24 deletions

View File

@ -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

View File

@ -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',

View File

@ -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,

View File

@ -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

View File

@ -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

View File

@ -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)}")

View File

@ -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))