Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,3 @@
|
|
| 1 |
-
import random
|
| 2 |
-
import httpx
|
| 3 |
-
|
| 4 |
from fastapi import FastAPI, Request, HTTPException
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
from fastapi.responses import StreamingResponse, JSONResponse
|
|
@@ -31,11 +28,11 @@ logger = logging.getLogger(__name__)
|
|
| 31 |
|
| 32 |
API_KEY = "sk-your-secret-key"
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
REQUEST_TIMEOUT =
|
| 36 |
|
| 37 |
-
# عد
|
| 38 |
-
MAX_RETRIES =
|
| 39 |
|
| 40 |
|
| 41 |
# =====================================================
|
|
@@ -201,7 +198,6 @@ async def safe_completion(
|
|
| 201 |
|
| 202 |
client = Client()
|
| 203 |
|
| 204 |
-
# timeout لمنع التعليق
|
| 205 |
response = await asyncio.wait_for(
|
| 206 |
asyncio.to_thread(
|
| 207 |
client.chat.completions.create,
|
|
@@ -214,6 +210,14 @@ async def safe_completion(
|
|
| 214 |
|
| 215 |
return response
|
| 216 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
except Exception as e:
|
| 218 |
|
| 219 |
last_error = e
|
|
@@ -222,7 +226,7 @@ async def safe_completion(
|
|
| 222 |
f"Attempt failed {attempt + 1}: {e}"
|
| 223 |
)
|
| 224 |
|
| 225 |
-
|
| 226 |
|
| 227 |
raise Exception(last_error)
|
| 228 |
|
|
@@ -269,7 +273,7 @@ async def chat_completions(
|
|
| 269 |
|
| 270 |
chunk_id = f"chatcmpl-{uuid.uuid4().hex}"
|
| 271 |
|
| 272 |
-
|
| 273 |
|
| 274 |
for chunk in response:
|
| 275 |
|
|
@@ -287,7 +291,7 @@ async def chat_completions(
|
|
| 287 |
|
| 288 |
if content:
|
| 289 |
|
| 290 |
-
|
| 291 |
|
| 292 |
payload = {
|
| 293 |
"id": chunk_id,
|
|
@@ -318,8 +322,8 @@ async def chat_completions(
|
|
| 318 |
f"Chunk error: {chunk_error}"
|
| 319 |
)
|
| 320 |
|
| 321 |
-
#
|
| 322 |
-
if
|
| 323 |
|
| 324 |
error_payload = {
|
| 325 |
"error": {
|
|
@@ -354,22 +358,6 @@ async def chat_completions(
|
|
| 354 |
|
| 355 |
yield "data: [DONE]\n\n"
|
| 356 |
|
| 357 |
-
except asyncio.TimeoutError:
|
| 358 |
-
|
| 359 |
-
logger.error("Streaming timeout")
|
| 360 |
-
|
| 361 |
-
error_payload = {
|
| 362 |
-
"error": {
|
| 363 |
-
"message": "Request timeout",
|
| 364 |
-
"type": "timeout"
|
| 365 |
-
}
|
| 366 |
-
}
|
| 367 |
-
|
| 368 |
-
yield (
|
| 369 |
-
f"data: "
|
| 370 |
-
f"{json.dumps(error_payload)}\n\n"
|
| 371 |
-
)
|
| 372 |
-
|
| 373 |
except Exception as e:
|
| 374 |
|
| 375 |
logger.error(f"Streaming error: {e}")
|
|
@@ -442,15 +430,6 @@ async def chat_completions(
|
|
| 442 |
}
|
| 443 |
})
|
| 444 |
|
| 445 |
-
except asyncio.TimeoutError:
|
| 446 |
-
|
| 447 |
-
logger.error("Request timeout")
|
| 448 |
-
|
| 449 |
-
raise HTTPException(
|
| 450 |
-
status_code=408,
|
| 451 |
-
detail="Request timeout"
|
| 452 |
-
)
|
| 453 |
-
|
| 454 |
except Exception as e:
|
| 455 |
|
| 456 |
logger.error(f"Chat error: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI, Request, HTTPException
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
from fastapi.responses import StreamingResponse, JSONResponse
|
|
|
|
| 28 |
|
| 29 |
API_KEY = "sk-your-secret-key"
|
| 30 |
|
| 31 |
+
# timeout لمنع التعليق الأبدي
|
| 32 |
+
REQUEST_TIMEOUT = 45
|
| 33 |
|
| 34 |
+
# إعادة المحاولة
|
| 35 |
+
MAX_RETRIES = 2
|
| 36 |
|
| 37 |
|
| 38 |
# =====================================================
|
|
|
|
| 198 |
|
| 199 |
client = Client()
|
| 200 |
|
|
|
|
| 201 |
response = await asyncio.wait_for(
|
| 202 |
asyncio.to_thread(
|
| 203 |
client.chat.completions.create,
|
|
|
|
| 210 |
|
| 211 |
return response
|
| 212 |
|
| 213 |
+
except asyncio.TimeoutError:
|
| 214 |
+
|
| 215 |
+
last_error = "Request timeout"
|
| 216 |
+
|
| 217 |
+
logger.warning(
|
| 218 |
+
f"Timeout attempt {attempt + 1}"
|
| 219 |
+
)
|
| 220 |
+
|
| 221 |
except Exception as e:
|
| 222 |
|
| 223 |
last_error = e
|
|
|
|
| 226 |
f"Attempt failed {attempt + 1}: {e}"
|
| 227 |
)
|
| 228 |
|
| 229 |
+
await asyncio.sleep(1)
|
| 230 |
|
| 231 |
raise Exception(last_error)
|
| 232 |
|
|
|
|
| 273 |
|
| 274 |
chunk_id = f"chatcmpl-{uuid.uuid4().hex}"
|
| 275 |
|
| 276 |
+
has_content = False
|
| 277 |
|
| 278 |
for chunk in response:
|
| 279 |
|
|
|
|
| 291 |
|
| 292 |
if content:
|
| 293 |
|
| 294 |
+
has_content = True
|
| 295 |
|
| 296 |
payload = {
|
| 297 |
"id": chunk_id,
|
|
|
|
| 322 |
f"Chunk error: {chunk_error}"
|
| 323 |
)
|
| 324 |
|
| 325 |
+
# provider لم يرسل أي شيء
|
| 326 |
+
if not has_content:
|
| 327 |
|
| 328 |
error_payload = {
|
| 329 |
"error": {
|
|
|
|
| 358 |
|
| 359 |
yield "data: [DONE]\n\n"
|
| 360 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 361 |
except Exception as e:
|
| 362 |
|
| 363 |
logger.error(f"Streaming error: {e}")
|
|
|
|
| 430 |
}
|
| 431 |
})
|
| 432 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 433 |
except Exception as e:
|
| 434 |
|
| 435 |
logger.error(f"Chat error: {e}")
|