Update app.py
Browse files
app.py
CHANGED
|
@@ -14,6 +14,8 @@ import logging
|
|
| 14 |
import os
|
| 15 |
from dotenv import load_dotenv
|
| 16 |
|
|
|
|
|
|
|
| 17 |
# 加载环境变量
|
| 18 |
load_dotenv()
|
| 19 |
|
|
@@ -129,6 +131,14 @@ class ChatCompletionRequest(BaseModel):
|
|
| 129 |
frequency_penalty: Optional[float] = 0
|
| 130 |
user: Optional[str] = None
|
| 131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
# 初始化token状态
|
| 134 |
async def initialize_token_status():
|
|
@@ -152,6 +162,7 @@ async def initialize_token_status():
|
|
| 152 |
active = False
|
| 153 |
quota_info = {}
|
| 154 |
count = 0
|
|
|
|
| 155 |
|
| 156 |
if response.status_code == 200:
|
| 157 |
data = response.json()
|
|
@@ -163,6 +174,7 @@ async def initialize_token_status():
|
|
| 163 |
item_type = item.get('type', '')
|
| 164 |
available = item.get('available', 0)
|
| 165 |
count += available
|
|
|
|
| 166 |
if available > 0:
|
| 167 |
active = True
|
| 168 |
|
|
@@ -177,7 +189,8 @@ async def initialize_token_status():
|
|
| 177 |
"quota": quota_info,
|
| 178 |
"last_checked": datetime.now(),
|
| 179 |
"failed_count": 0,
|
| 180 |
-
"count": count
|
|
|
|
| 181 |
}
|
| 182 |
|
| 183 |
logger.info(f"Token {token[:8]}... 状态:{'活跃' if active else '无效'}")
|
|
@@ -202,7 +215,7 @@ def verify_api_key(api_key: str = Header(..., alias="Authorization")):
|
|
| 202 |
|
| 203 |
def map_openai_to_deepsider_model(model: str) -> str:
|
| 204 |
"""将OpenAI模型名称映射到DeepSider模型名称"""
|
| 205 |
-
return MODEL_MAPPING.get(model,
|
| 206 |
|
| 207 |
|
| 208 |
def format_messages_for_deepsider(messages: List[ChatMessage]) -> str:
|
|
@@ -582,7 +595,7 @@ async def refresh_token_status(admin_key: str = Header(None, alias="X-Admin-Key"
|
|
| 582 |
if not admin_key or admin_key != expected_admin_key:
|
| 583 |
raise HTTPException(status_code=403, detail="Unauthorized")
|
| 584 |
|
| 585 |
-
await
|
| 586 |
return {"message": "所有token状态已刷新", "active_tokens": sum(1 for s in token_status.values() if s["active"])}
|
| 587 |
|
| 588 |
|
|
@@ -618,9 +631,15 @@ async def startup_event():
|
|
| 618 |
active_tokens = sum(1 for s in token_status.values() if s["active"])
|
| 619 |
logger.info(f"初始化完成 活跃token: {active_tokens}/{len(DEEPSIDER_TOKEN)}")
|
| 620 |
|
| 621 |
-
|
| 622 |
# 主程序
|
| 623 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 624 |
# 启动服务器
|
| 625 |
port = int(os.getenv("PORT", "3000"))
|
| 626 |
logger.info(f"启动OpenAI API代理服务 端口: {port}")
|
|
|
|
| 14 |
import os
|
| 15 |
from dotenv import load_dotenv
|
| 16 |
|
| 17 |
+
from apscheduler.schedulers.background import BackgroundScheduler
|
| 18 |
+
|
| 19 |
# 加载环境变量
|
| 20 |
load_dotenv()
|
| 21 |
|
|
|
|
| 131 |
frequency_penalty: Optional[float] = 0
|
| 132 |
user: Optional[str] = None
|
| 133 |
|
| 134 |
+
def reset_task():
|
| 135 |
+
try:
|
| 136 |
+
for key,value in token_status.items():
|
| 137 |
+
token_status[key]["total"] = value["count"]
|
| 138 |
+
token_status[key]["active"] = True
|
| 139 |
+
print(f"执行重置任务... 当前时间: {datetime.now()}")
|
| 140 |
+
except Exception as e:
|
| 141 |
+
print(f"任务执行出错: {e}")
|
| 142 |
|
| 143 |
# 初始化token状态
|
| 144 |
async def initialize_token_status():
|
|
|
|
| 162 |
active = False
|
| 163 |
quota_info = {}
|
| 164 |
count = 0
|
| 165 |
+
total = 0
|
| 166 |
|
| 167 |
if response.status_code == 200:
|
| 168 |
data = response.json()
|
|
|
|
| 174 |
item_type = item.get('type', '')
|
| 175 |
available = item.get('available', 0)
|
| 176 |
count += available
|
| 177 |
+
total += item.get('total', 0)
|
| 178 |
if available > 0:
|
| 179 |
active = True
|
| 180 |
|
|
|
|
| 189 |
"quota": quota_info,
|
| 190 |
"last_checked": datetime.now(),
|
| 191 |
"failed_count": 0,
|
| 192 |
+
"count": count,
|
| 193 |
+
'total': total
|
| 194 |
}
|
| 195 |
|
| 196 |
logger.info(f"Token {token[:8]}... 状态:{'活跃' if active else '无效'}")
|
|
|
|
| 215 |
|
| 216 |
def map_openai_to_deepsider_model(model: str) -> str:
|
| 217 |
"""将OpenAI模型名称映射到DeepSider模型名称"""
|
| 218 |
+
return MODEL_MAPPING.get(model, model)
|
| 219 |
|
| 220 |
|
| 221 |
def format_messages_for_deepsider(messages: List[ChatMessage]) -> str:
|
|
|
|
| 595 |
if not admin_key or admin_key != expected_admin_key:
|
| 596 |
raise HTTPException(status_code=403, detail="Unauthorized")
|
| 597 |
|
| 598 |
+
await ()
|
| 599 |
return {"message": "所有token状态已刷新", "active_tokens": sum(1 for s in token_status.values() if s["active"])}
|
| 600 |
|
| 601 |
|
|
|
|
| 631 |
active_tokens = sum(1 for s in token_status.values() if s["active"])
|
| 632 |
logger.info(f"初始化完成 活跃token: {active_tokens}/{len(DEEPSIDER_TOKEN)}")
|
| 633 |
|
|
|
|
| 634 |
# 主程序
|
| 635 |
if __name__ == "__main__":
|
| 636 |
+
scheduler = BackgroundScheduler()
|
| 637 |
+
|
| 638 |
+
# 添加任务,每天0点执行
|
| 639 |
+
scheduler.add_job(reset_task, 'cron', hour=0, minute=0)
|
| 640 |
+
# 启动调度器
|
| 641 |
+
scheduler.start()
|
| 642 |
+
|
| 643 |
# 启动服务器
|
| 644 |
port = int(os.getenv("PORT", "3000"))
|
| 645 |
logger.info(f"启动OpenAI API代理服务 端口: {port}")
|