Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,6 @@ from fastapi.responses import StreamingResponse, JSONResponse
|
|
| 4 |
from pydantic import BaseModel
|
| 5 |
from typing import List, Optional
|
| 6 |
|
| 7 |
-
import asyncio
|
| 8 |
import json
|
| 9 |
import time
|
| 10 |
import uuid
|
|
@@ -28,8 +27,6 @@ logger = logging.getLogger(__name__)
|
|
| 28 |
|
| 29 |
API_KEY = "sk-your-secret-key"
|
| 30 |
|
| 31 |
-
REQUEST_TIMEOUT = 60
|
| 32 |
-
|
| 33 |
|
| 34 |
# =====================================================
|
| 35 |
# FASTAPI
|
|
@@ -37,7 +34,7 @@ REQUEST_TIMEOUT = 60
|
|
| 37 |
|
| 38 |
app = FastAPI(
|
| 39 |
title="Universal AI Gateway",
|
| 40 |
-
version="5.
|
| 41 |
)
|
| 42 |
|
| 43 |
|
|
@@ -109,12 +106,12 @@ async def root():
|
|
| 109 |
return {
|
| 110 |
"status": "online",
|
| 111 |
"service": "Universal AI Gateway",
|
| 112 |
-
"version": "5.
|
| 113 |
}
|
| 114 |
|
| 115 |
|
| 116 |
# =====================================================
|
| 117 |
-
# MODELS
|
| 118 |
# =====================================================
|
| 119 |
|
| 120 |
@app.get("/v1/models")
|
|
@@ -149,22 +146,19 @@ async def get_models():
|
|
| 149 |
|
| 150 |
# =====================================================
|
| 151 |
# SAFE COMPLETION
|
| 152 |
-
# لا
|
| 153 |
-
#
|
|
|
|
| 154 |
# =====================================================
|
| 155 |
|
| 156 |
-
|
| 157 |
|
| 158 |
client = Client()
|
| 159 |
|
| 160 |
-
response =
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
messages=messages,
|
| 165 |
-
stream=stream
|
| 166 |
-
),
|
| 167 |
-
timeout=REQUEST_TIMEOUT
|
| 168 |
)
|
| 169 |
|
| 170 |
return response
|
|
@@ -206,7 +200,7 @@ async def chat_completions(
|
|
| 206 |
|
| 207 |
try:
|
| 208 |
|
| 209 |
-
response =
|
| 210 |
model=body.model,
|
| 211 |
messages=messages,
|
| 212 |
stream=True
|
|
@@ -263,8 +257,6 @@ async def chat_completions(
|
|
| 263 |
+ "\n\n"
|
| 264 |
)
|
| 265 |
|
| 266 |
-
await asyncio.sleep(0)
|
| 267 |
-
|
| 268 |
except Exception as chunk_error:
|
| 269 |
|
| 270 |
logger.error(
|
|
@@ -308,27 +300,6 @@ async def chat_completions(
|
|
| 308 |
|
| 309 |
yield "data: [DONE]\n\n"
|
| 310 |
|
| 311 |
-
except asyncio.TimeoutError:
|
| 312 |
-
|
| 313 |
-
logger.error(
|
| 314 |
-
f"Timeout model={body.model}"
|
| 315 |
-
)
|
| 316 |
-
|
| 317 |
-
error_payload = {
|
| 318 |
-
"error": {
|
| 319 |
-
"message": "Request timeout",
|
| 320 |
-
"type": "timeout"
|
| 321 |
-
}
|
| 322 |
-
}
|
| 323 |
-
|
| 324 |
-
yield (
|
| 325 |
-
"data: "
|
| 326 |
-
+ json.dumps(error_payload)
|
| 327 |
-
+ "\n\n"
|
| 328 |
-
)
|
| 329 |
-
|
| 330 |
-
yield "data: [DONE]\n\n"
|
| 331 |
-
|
| 332 |
except Exception as e:
|
| 333 |
|
| 334 |
logger.error(
|
|
@@ -366,7 +337,7 @@ async def chat_completions(
|
|
| 366 |
|
| 367 |
try:
|
| 368 |
|
| 369 |
-
response =
|
| 370 |
model=body.model,
|
| 371 |
messages=messages,
|
| 372 |
stream=False
|
|
@@ -418,13 +389,6 @@ async def chat_completions(
|
|
| 418 |
|
| 419 |
})
|
| 420 |
|
| 421 |
-
except asyncio.TimeoutError:
|
| 422 |
-
|
| 423 |
-
raise HTTPException(
|
| 424 |
-
status_code=408,
|
| 425 |
-
detail="Request timeout"
|
| 426 |
-
)
|
| 427 |
-
|
| 428 |
except Exception as e:
|
| 429 |
|
| 430 |
logger.error(f"Chat error: {e}")
|
|
|
|
| 4 |
from pydantic import BaseModel
|
| 5 |
from typing import List, Optional
|
| 6 |
|
|
|
|
| 7 |
import json
|
| 8 |
import time
|
| 9 |
import uuid
|
|
|
|
| 27 |
|
| 28 |
API_KEY = "sk-your-secret-key"
|
| 29 |
|
|
|
|
|
|
|
| 30 |
|
| 31 |
# =====================================================
|
| 32 |
# FASTAPI
|
|
|
|
| 34 |
|
| 35 |
app = FastAPI(
|
| 36 |
title="Universal AI Gateway",
|
| 37 |
+
version="5.1.0"
|
| 38 |
)
|
| 39 |
|
| 40 |
|
|
|
|
| 106 |
return {
|
| 107 |
"status": "online",
|
| 108 |
"service": "Universal AI Gateway",
|
| 109 |
+
"version": "5.1.0"
|
| 110 |
}
|
| 111 |
|
| 112 |
|
| 113 |
# =====================================================
|
| 114 |
+
# GET MODELS
|
| 115 |
# =====================================================
|
| 116 |
|
| 117 |
@app.get("/v1/models")
|
|
|
|
| 146 |
|
| 147 |
# =====================================================
|
| 148 |
# SAFE COMPLETION
|
| 149 |
+
# لا asyncio
|
| 150 |
+
# لا to_thread
|
| 151 |
+
# لا wait_for
|
| 152 |
# =====================================================
|
| 153 |
|
| 154 |
+
def safe_completion(model, messages, stream=False):
|
| 155 |
|
| 156 |
client = Client()
|
| 157 |
|
| 158 |
+
response = client.chat.completions.create(
|
| 159 |
+
model=model,
|
| 160 |
+
messages=messages,
|
| 161 |
+
stream=stream
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
)
|
| 163 |
|
| 164 |
return response
|
|
|
|
| 200 |
|
| 201 |
try:
|
| 202 |
|
| 203 |
+
response = safe_completion(
|
| 204 |
model=body.model,
|
| 205 |
messages=messages,
|
| 206 |
stream=True
|
|
|
|
| 257 |
+ "\n\n"
|
| 258 |
)
|
| 259 |
|
|
|
|
|
|
|
| 260 |
except Exception as chunk_error:
|
| 261 |
|
| 262 |
logger.error(
|
|
|
|
| 300 |
|
| 301 |
yield "data: [DONE]\n\n"
|
| 302 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 303 |
except Exception as e:
|
| 304 |
|
| 305 |
logger.error(
|
|
|
|
| 337 |
|
| 338 |
try:
|
| 339 |
|
| 340 |
+
response = safe_completion(
|
| 341 |
model=body.model,
|
| 342 |
messages=messages,
|
| 343 |
stream=False
|
|
|
|
| 389 |
|
| 390 |
})
|
| 391 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 392 |
except Exception as e:
|
| 393 |
|
| 394 |
logger.error(f"Chat error: {e}")
|