Spaces:
Paused
Paused
OraCatQAQ commited on
Commit ·
e40cdb9
1
Parent(s): 289605c
Enhance API key verification to include ADMIN_KEY check and return DEEPSIDER_TOKEN; improve error handling in create_chat_completion function.
Browse files
app.py
CHANGED
|
@@ -172,7 +172,23 @@ def verify_api_key(api_key: str = Header(..., alias="Authorization")):
|
|
| 172 |
"""验证API密钥"""
|
| 173 |
if not api_key.startswith("Bearer "):
|
| 174 |
raise HTTPException(status_code=401, detail="Invalid API key format")
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
def map_openai_to_deepsider_model(model: str) -> str:
|
| 178 |
"""将OpenAI模型名称映射到DeepSider模型名称"""
|
|
@@ -350,7 +366,7 @@ async def list_models(api_key: str = Depends(verify_api_key)):
|
|
| 350 |
@app.post("/v1/chat/completions")
|
| 351 |
async def create_chat_completion(
|
| 352 |
request: Request,
|
| 353 |
-
api_key: str = Depends(verify_api_key)
|
| 354 |
):
|
| 355 |
"""创建聊天完成API - 支持普通请求和流式请求"""
|
| 356 |
# 解析请求体
|
|
@@ -370,15 +386,12 @@ async def create_chat_completion(
|
|
| 370 |
payload = {
|
| 371 |
"model": deepsider_model,
|
| 372 |
"prompt": prompt,
|
| 373 |
-
"webAccess": "close",
|
| 374 |
"timezone": "Asia/Shanghai"
|
| 375 |
}
|
| 376 |
|
| 377 |
-
# 获取请求头(
|
| 378 |
-
headers = get_headers(api_key)
|
| 379 |
-
# 获取当前使用的token
|
| 380 |
-
tokens = api_key.split(',')
|
| 381 |
-
current_token_index = (TOKEN_INDEX - 1) % len(tokens) if len(tokens) > 0 else 0
|
| 382 |
|
| 383 |
try:
|
| 384 |
response = requests.post(
|
|
@@ -386,7 +399,7 @@ async def create_chat_completion(
|
|
| 386 |
headers=headers,
|
| 387 |
json=payload,
|
| 388 |
stream=True,
|
| 389 |
-
timeout=30
|
| 390 |
)
|
| 391 |
|
| 392 |
# 新增调试日志
|
|
@@ -407,13 +420,13 @@ async def create_chat_completion(
|
|
| 407 |
error_msg += f" - {response.text}"
|
| 408 |
|
| 409 |
logger.error(error_msg)
|
| 410 |
-
raise HTTPException(status_code=response.status_code, detail=
|
| 411 |
|
| 412 |
# 处理流式或非流式响应
|
| 413 |
if chat_request.stream:
|
| 414 |
# 返回流式响应
|
| 415 |
return StreamingResponse(
|
| 416 |
-
stream_openai_response(response, request_id, chat_request.model, api_key,
|
| 417 |
media_type="text/event-stream"
|
| 418 |
)
|
| 419 |
else:
|
|
@@ -438,7 +451,7 @@ async def create_chat_completion(
|
|
| 438 |
# 返回OpenAI格式的完整响应
|
| 439 |
return await generate_openai_response(full_response, request_id, chat_request.model)
|
| 440 |
|
| 441 |
-
except requests.
|
| 442 |
logger.error(f"请求超时: {str(e)}")
|
| 443 |
raise HTTPException(status_code=504, detail="上游服务响应超时")
|
| 444 |
|
|
|
|
| 172 |
"""验证API密钥"""
|
| 173 |
if not api_key.startswith("Bearer "):
|
| 174 |
raise HTTPException(status_code=401, detail="Invalid API key format")
|
| 175 |
+
|
| 176 |
+
# 获取环境变量中的 ADMIN_KEY
|
| 177 |
+
admin_key = os.getenv("ADMIN_KEY")
|
| 178 |
+
if not admin_key:
|
| 179 |
+
raise HTTPException(status_code=500, detail="ADMIN_KEY not configured")
|
| 180 |
+
|
| 181 |
+
# 验证传入的 key 是否匹配 ADMIN_KEY
|
| 182 |
+
provided_key = api_key.replace("Bearer ", "").strip()
|
| 183 |
+
if provided_key != admin_key:
|
| 184 |
+
raise HTTPException(status_code=401, detail="Invalid API key")
|
| 185 |
+
|
| 186 |
+
# 验证通过后,返回 DEEPSIDER_TOKEN
|
| 187 |
+
deepsider_token = os.getenv("DEEPSIDER_TOKEN")
|
| 188 |
+
if not deepsider_token:
|
| 189 |
+
raise HTTPException(status_code=500, detail="DEEPSIDER_TOKEN not configured")
|
| 190 |
+
|
| 191 |
+
return deepsider_token
|
| 192 |
|
| 193 |
def map_openai_to_deepsider_model(model: str) -> str:
|
| 194 |
"""将OpenAI模型名称映射到DeepSider模型名称"""
|
|
|
|
| 366 |
@app.post("/v1/chat/completions")
|
| 367 |
async def create_chat_completion(
|
| 368 |
request: Request,
|
| 369 |
+
api_key: str = Depends(verify_api_key) # 这里返回的是 DEEPSIDER_TOKEN
|
| 370 |
):
|
| 371 |
"""创建聊天完成API - 支持普通请求和流式请求"""
|
| 372 |
# 解析请求体
|
|
|
|
| 386 |
payload = {
|
| 387 |
"model": deepsider_model,
|
| 388 |
"prompt": prompt,
|
| 389 |
+
"webAccess": "close",
|
| 390 |
"timezone": "Asia/Shanghai"
|
| 391 |
}
|
| 392 |
|
| 393 |
+
# 获取请求头(使用 DEEPSIDER_TOKEN)
|
| 394 |
+
headers = get_headers(api_key) # api_key 现在是 DEEPSIDER_TOKEN
|
|
|
|
|
|
|
|
|
|
| 395 |
|
| 396 |
try:
|
| 397 |
response = requests.post(
|
|
|
|
| 399 |
headers=headers,
|
| 400 |
json=payload,
|
| 401 |
stream=True,
|
| 402 |
+
timeout=30
|
| 403 |
)
|
| 404 |
|
| 405 |
# 新增调试日志
|
|
|
|
| 420 |
error_msg += f" - {response.text}"
|
| 421 |
|
| 422 |
logger.error(error_msg)
|
| 423 |
+
raise HTTPException(status_code=response.status_code, detail=error_msg)
|
| 424 |
|
| 425 |
# 处理流式或非流式响应
|
| 426 |
if chat_request.stream:
|
| 427 |
# 返回流式响应
|
| 428 |
return StreamingResponse(
|
| 429 |
+
stream_openai_response(response, request_id, chat_request.model, api_key, TOKEN_INDEX),
|
| 430 |
media_type="text/event-stream"
|
| 431 |
)
|
| 432 |
else:
|
|
|
|
| 451 |
# 返回OpenAI格式的完整响应
|
| 452 |
return await generate_openai_response(full_response, request_id, chat_request.model)
|
| 453 |
|
| 454 |
+
except requests.Timeout as e:
|
| 455 |
logger.error(f"请求超时: {str(e)}")
|
| 456 |
raise HTTPException(status_code=504, detail="上游服务响应超时")
|
| 457 |
|