Update app.py
Browse files
app.py
CHANGED
|
@@ -252,13 +252,17 @@ async def handle_api_request(url: str, headers: dict, method: str = "GET", body:
|
|
| 252 |
while current_try < max_retries:
|
| 253 |
try:
|
| 254 |
# 获取key
|
| 255 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
if not key:
|
| 257 |
raise HTTPException(status_code=500, detail="No API keys available")
|
| 258 |
|
| 259 |
headers["Authorization"] = f"Bearer {key}"
|
| 260 |
|
| 261 |
-
async with httpx.AsyncClient() as client:
|
| 262 |
response = await client.request(
|
| 263 |
method=method,
|
| 264 |
url=url,
|
|
@@ -267,7 +271,8 @@ async def handle_api_request(url: str, headers: dict, method: str = "GET", body:
|
|
| 267 |
)
|
| 268 |
|
| 269 |
# 检查配额不足
|
| 270 |
-
if response.status_code == 429 or "insufficient_quota" in response.text:
|
|
|
|
| 271 |
set_key_cooling(key)
|
| 272 |
current_try += 1
|
| 273 |
continue
|
|
@@ -275,40 +280,58 @@ async def handle_api_request(url: str, headers: dict, method: str = "GET", body:
|
|
| 275 |
return response
|
| 276 |
|
| 277 |
except Exception as e:
|
|
|
|
| 278 |
current_try += 1
|
| 279 |
if current_try == max_retries:
|
| 280 |
-
raise
|
| 281 |
|
| 282 |
# 聊天完成路由
|
| 283 |
@app.post("/api/v1/chat/completions")
|
| 284 |
async def chat_completions(request: Request):
|
| 285 |
try:
|
| 286 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 287 |
headers = {
|
| 288 |
"Content-Type": "application/json",
|
| 289 |
-
"Accept": "text/event-stream" if
|
| 290 |
}
|
| 291 |
|
|
|
|
| 292 |
response = await handle_api_request(
|
| 293 |
url=f"{Config.OPENAI_API_BASE}/chat/completions",
|
| 294 |
headers=headers,
|
| 295 |
method="POST",
|
| 296 |
-
body=
|
| 297 |
for_chat=True
|
| 298 |
)
|
| 299 |
|
| 300 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 301 |
return StreamingResponse(
|
| 302 |
stream_generator(response),
|
| 303 |
-
media_type="text/event-stream"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 304 |
)
|
| 305 |
|
| 306 |
return Response(
|
| 307 |
content=response.text,
|
| 308 |
-
media_type="application/json"
|
|
|
|
| 309 |
)
|
| 310 |
|
| 311 |
except Exception as e:
|
|
|
|
| 312 |
raise HTTPException(status_code=500, detail=str(e))
|
| 313 |
|
| 314 |
# 代理其他请求
|
|
|
|
| 252 |
while current_try < max_retries:
|
| 253 |
try:
|
| 254 |
# 获取key
|
| 255 |
+
if for_chat:
|
| 256 |
+
key = get_chat_key()
|
| 257 |
+
else:
|
| 258 |
+
key = first_key
|
| 259 |
+
|
| 260 |
if not key:
|
| 261 |
raise HTTPException(status_code=500, detail="No API keys available")
|
| 262 |
|
| 263 |
headers["Authorization"] = f"Bearer {key}"
|
| 264 |
|
| 265 |
+
async with httpx.AsyncClient(timeout=60.0) as client:
|
| 266 |
response = await client.request(
|
| 267 |
method=method,
|
| 268 |
url=url,
|
|
|
|
| 271 |
)
|
| 272 |
|
| 273 |
# 检查配额不足
|
| 274 |
+
if response.status_code == 429 or "insufficient_quota" in response.text.lower():
|
| 275 |
+
print(f"Key {key} quota exceeded, trying next key...")
|
| 276 |
set_key_cooling(key)
|
| 277 |
current_try += 1
|
| 278 |
continue
|
|
|
|
| 280 |
return response
|
| 281 |
|
| 282 |
except Exception as e:
|
| 283 |
+
print(f"Request error: {str(e)}")
|
| 284 |
current_try += 1
|
| 285 |
if current_try == max_retries:
|
| 286 |
+
raise HTTPException(status_code=500, detail=f"API request failed after {max_retries} retries")
|
| 287 |
|
| 288 |
# 聊天完成路由
|
| 289 |
@app.post("/api/v1/chat/completions")
|
| 290 |
async def chat_completions(request: Request):
|
| 291 |
try:
|
| 292 |
+
# 获取请求体
|
| 293 |
+
body = await request.body()
|
| 294 |
+
body_json = json.loads(body)
|
| 295 |
+
|
| 296 |
+
# 获取headers
|
| 297 |
headers = {
|
| 298 |
"Content-Type": "application/json",
|
| 299 |
+
"Accept": "text/event-stream" if body_json.get("stream") else "application/json"
|
| 300 |
}
|
| 301 |
|
| 302 |
+
print("Starting chat completion request...")
|
| 303 |
response = await handle_api_request(
|
| 304 |
url=f"{Config.OPENAI_API_BASE}/chat/completions",
|
| 305 |
headers=headers,
|
| 306 |
method="POST",
|
| 307 |
+
body=body_json,
|
| 308 |
for_chat=True
|
| 309 |
)
|
| 310 |
|
| 311 |
+
if not response:
|
| 312 |
+
raise HTTPException(status_code=500, detail="Failed to get response from API")
|
| 313 |
+
|
| 314 |
+
print(f"Chat completion response status: {response.status_code}")
|
| 315 |
+
|
| 316 |
+
if body_json.get("stream"):
|
| 317 |
return StreamingResponse(
|
| 318 |
stream_generator(response),
|
| 319 |
+
media_type="text/event-stream",
|
| 320 |
+
headers={
|
| 321 |
+
"Cache-Control": "no-cache",
|
| 322 |
+
"Connection": "keep-alive",
|
| 323 |
+
"Content-Type": "text/event-stream"
|
| 324 |
+
}
|
| 325 |
)
|
| 326 |
|
| 327 |
return Response(
|
| 328 |
content=response.text,
|
| 329 |
+
media_type="application/json",
|
| 330 |
+
status_code=response.status_code
|
| 331 |
)
|
| 332 |
|
| 333 |
except Exception as e:
|
| 334 |
+
print(f"Chat completion error: {str(e)}")
|
| 335 |
raise HTTPException(status_code=500, detail=str(e))
|
| 336 |
|
| 337 |
# 代理其他请求
|