Spaces:
Running
Running
Commit ·
6f626ee
1
Parent(s): 1929382
ok
Browse files- app/api/v1/chat.py +18 -1
app/api/v1/chat.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
from __future__ import annotations
|
| 2 |
|
|
|
|
|
|
|
| 3 |
from typing import Any, Dict, List, Optional
|
| 4 |
|
| 5 |
from fastapi import APIRouter, Depends, HTTPException, Request
|
|
@@ -7,6 +9,8 @@ from fastapi import APIRouter, Depends, HTTPException, Request
|
|
| 7 |
from app.api.deps import require_auth
|
| 8 |
from app.services.chat_service import chat_completion
|
| 9 |
|
|
|
|
|
|
|
| 10 |
VALID_MODELS = ["agentdeck-1.0", "agentdeck-flash", "agentdeck-0.1"]
|
| 11 |
VALID_PROVIDERS = ["openprovider", "meganova", "aionlabs"]
|
| 12 |
|
|
@@ -19,6 +23,9 @@ async def create_chat_completion(
|
|
| 19 |
request: Request,
|
| 20 |
token: str = Depends(require_auth),
|
| 21 |
) -> Dict[str, Any]:
|
|
|
|
|
|
|
|
|
|
| 22 |
messages: Optional[List[Dict[str, str]]] = body.get("messages")
|
| 23 |
if not messages or not isinstance(messages, list):
|
| 24 |
raise HTTPException(status_code=400, detail="messages is required and must be a non-empty array")
|
|
@@ -58,6 +65,11 @@ async def create_chat_completion(
|
|
| 58 |
if stream and return_json:
|
| 59 |
raise HTTPException(status_code=400, detail="stream and return_json cannot both be true")
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
max_tokens = body.get("max_tokens", 1024)
|
| 62 |
temperature = body.get("temperature", 0.7)
|
| 63 |
top_p = body.get("top_p", 0.9)
|
|
@@ -79,11 +91,16 @@ async def create_chat_completion(
|
|
| 79 |
redis=redis,
|
| 80 |
scripts=scripts,
|
| 81 |
)
|
|
|
|
|
|
|
| 82 |
return result
|
| 83 |
except RuntimeError as e:
|
| 84 |
-
|
|
|
|
|
|
|
| 85 |
raise HTTPException(
|
| 86 |
status_code=503,
|
| 87 |
detail="All API keys are currently locked. Retry after a few minutes.",
|
| 88 |
)
|
|
|
|
| 89 |
raise HTTPException(status_code=502, detail=str(e))
|
|
|
|
| 1 |
from __future__ import annotations
|
| 2 |
|
| 3 |
+
import logging
|
| 4 |
+
import time
|
| 5 |
from typing import Any, Dict, List, Optional
|
| 6 |
|
| 7 |
from fastapi import APIRouter, Depends, HTTPException, Request
|
|
|
|
| 9 |
from app.api.deps import require_auth
|
| 10 |
from app.services.chat_service import chat_completion
|
| 11 |
|
| 12 |
+
logger = logging.getLogger(__name__)
|
| 13 |
+
|
| 14 |
VALID_MODELS = ["agentdeck-1.0", "agentdeck-flash", "agentdeck-0.1"]
|
| 15 |
VALID_PROVIDERS = ["openprovider", "meganova", "aionlabs"]
|
| 16 |
|
|
|
|
| 23 |
request: Request,
|
| 24 |
token: str = Depends(require_auth),
|
| 25 |
) -> Dict[str, Any]:
|
| 26 |
+
start = time.monotonic()
|
| 27 |
+
req_id = hex(int(time.time() * 1_000_000))[-8:]
|
| 28 |
+
|
| 29 |
messages: Optional[List[Dict[str, str]]] = body.get("messages")
|
| 30 |
if not messages or not isinstance(messages, list):
|
| 31 |
raise HTTPException(status_code=400, detail="messages is required and must be a non-empty array")
|
|
|
|
| 65 |
if stream and return_json:
|
| 66 |
raise HTTPException(status_code=400, detail="stream and return_json cannot both be true")
|
| 67 |
|
| 68 |
+
logger.info(
|
| 69 |
+
"[%s] POST /chat/completions model=%s provider=%s messages=%d stream=%s",
|
| 70 |
+
req_id, model, provider, len(messages), stream,
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
max_tokens = body.get("max_tokens", 1024)
|
| 74 |
temperature = body.get("temperature", 0.7)
|
| 75 |
top_p = body.get("top_p", 0.9)
|
|
|
|
| 91 |
redis=redis,
|
| 92 |
scripts=scripts,
|
| 93 |
)
|
| 94 |
+
elapsed = time.monotonic() - start
|
| 95 |
+
logger.info("[%s] success in %.0fms", req_id, elapsed * 1000)
|
| 96 |
return result
|
| 97 |
except RuntimeError as e:
|
| 98 |
+
elapsed = time.monotonic() - start
|
| 99 |
+
if "All AI providers exhausted" in str(e):
|
| 100 |
+
logger.warning("[%s] service_unavailable in %.0fms", req_id, elapsed * 1000)
|
| 101 |
raise HTTPException(
|
| 102 |
status_code=503,
|
| 103 |
detail="All API keys are currently locked. Retry after a few minutes.",
|
| 104 |
)
|
| 105 |
+
logger.warning("[%s] upstream_error in %.0fms: %s", req_id, elapsed * 1000, e)
|
| 106 |
raise HTTPException(status_code=502, detail=str(e))
|