Update api/routes.py
Browse files- api/routes.py +60 -59
api/routes.py
CHANGED
|
@@ -1,59 +1,60 @@
|
|
| 1 |
-
import json
|
| 2 |
-
from fastapi import APIRouter, Depends, HTTPException, Request, Response
|
| 3 |
-
from fastapi.responses import StreamingResponse
|
| 4 |
-
from api.auth import verify_app_secret
|
| 5 |
-
from api.config import ALLOWED_MODELS
|
| 6 |
-
from api.models import ChatRequest
|
| 7 |
-
from api.utils import process_non_streaming_response, process_streaming_response
|
| 8 |
-
from api.logger import setup_logger
|
| 9 |
-
|
| 10 |
-
logger = setup_logger(__name__)
|
| 11 |
-
|
| 12 |
-
router = APIRouter()
|
| 13 |
-
|
| 14 |
-
@router.options("/v1/chat/completions")
|
| 15 |
-
@router.options("/api/v1/chat/completions")
|
| 16 |
-
async def chat_completions_options():
|
| 17 |
-
return Response(
|
| 18 |
-
status_code=200,
|
| 19 |
-
headers={
|
| 20 |
-
"Access-Control-Allow-Origin": "*",
|
| 21 |
-
"Access-Control-Allow-Methods": "POST, OPTIONS",
|
| 22 |
-
"Access-Control-Allow-Headers": "Content-Type, Authorization",
|
| 23 |
-
},
|
| 24 |
-
)
|
| 25 |
-
|
| 26 |
-
@router.get("/v1/models")
|
| 27 |
-
@router.get("/api/v1/models")
|
| 28 |
-
async def list_models():
|
| 29 |
-
return {"object": "list", "data": ALLOWED_MODELS}
|
| 30 |
-
|
| 31 |
-
@router.post("/v1/chat/completions")
|
| 32 |
-
@router.post("/api/v1/chat/completions")
|
| 33 |
-
async def chat_completions(
|
| 34 |
-
request: ChatRequest, app_secret: str = Depends(verify_app_secret)
|
| 35 |
-
):
|
| 36 |
-
logger.info("Entering chat_completions route")
|
| 37 |
-
logger.info(f"Processing chat completion request for model: {request.model}")
|
| 38 |
-
|
| 39 |
-
if request.model not in [model["id"] for model in ALLOWED_MODELS]:
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
@router.
|
| 55 |
-
@router.
|
| 56 |
-
@router.
|
| 57 |
-
@router.get("/
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
from fastapi import APIRouter, Depends, HTTPException, Request, Response
|
| 3 |
+
from fastapi.responses import StreamingResponse
|
| 4 |
+
from api.auth import verify_app_secret
|
| 5 |
+
from api.config import ALLOWED_MODELS
|
| 6 |
+
from api.models import ChatRequest
|
| 7 |
+
from api.utils import process_non_streaming_response, process_streaming_response
|
| 8 |
+
from api.logger import setup_logger
|
| 9 |
+
|
| 10 |
+
logger = setup_logger(__name__)
|
| 11 |
+
|
| 12 |
+
router = APIRouter()
|
| 13 |
+
|
| 14 |
+
@router.options("/v1/chat/completions")
|
| 15 |
+
@router.options("/api/v1/chat/completions")
|
| 16 |
+
async def chat_completions_options():
|
| 17 |
+
return Response(
|
| 18 |
+
status_code=200,
|
| 19 |
+
headers={
|
| 20 |
+
"Access-Control-Allow-Origin": "*",
|
| 21 |
+
"Access-Control-Allow-Methods": "POST, OPTIONS",
|
| 22 |
+
"Access-Control-Allow-Headers": "Content-Type, Authorization",
|
| 23 |
+
},
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
@router.get("/v1/models")
|
| 27 |
+
@router.get("/api/v1/models")
|
| 28 |
+
async def list_models():
|
| 29 |
+
return {"object": "list", "data": ALLOWED_MODELS}
|
| 30 |
+
|
| 31 |
+
@router.post("/v1/chat/completions")
|
| 32 |
+
@router.post("/api/v1/chat/completions")
|
| 33 |
+
async def chat_completions(
|
| 34 |
+
request: ChatRequest, app_secret: str = Depends(verify_app_secret)
|
| 35 |
+
):
|
| 36 |
+
logger.info("Entering chat_completions route")
|
| 37 |
+
logger.info(f"Processing chat completion request for model: {request.model}")
|
| 38 |
+
|
| 39 |
+
if request.model not in [model["id"] for model in ALLOWED_MODELS]:
|
| 40 |
+
allowed = ', '.join(model['id'] for model in ALLOWED_MODELS)
|
| 41 |
+
raise HTTPException(
|
| 42 |
+
status_code=400,
|
| 43 |
+
detail=f"Model '{request.model}' is not allowed. Allowed models are: {allowed}",
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
if request.stream:
|
| 47 |
+
logger.info("Streaming response")
|
| 48 |
+
return StreamingResponse(process_streaming_response(request), media_type="text/event-stream")
|
| 49 |
+
else:
|
| 50 |
+
logger.info("Non-streaming response")
|
| 51 |
+
return await process_non_streaming_response(request)
|
| 52 |
+
|
| 53 |
+
# Health check endpoints
|
| 54 |
+
@router.get("/health")
|
| 55 |
+
@router.get("/healthz")
|
| 56 |
+
@router.get("/ready")
|
| 57 |
+
@router.get("/alive")
|
| 58 |
+
@router.get("/status")
|
| 59 |
+
def health_check(request: Request):
|
| 60 |
+
return Response(content=json.dumps({"status": "ok"}), media_type="application/json")
|