Update api/routes.py
Browse files- api/routes.py +22 -15
api/routes.py
CHANGED
@@ -1,9 +1,8 @@
|
|
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 |
|
@@ -31,29 +30,37 @@ async def list_models():
|
|
31 |
@router.post("/v1/chat/completions")
|
32 |
@router.post("/api/v1/chat/completions")
|
33 |
async def chat_completions(
|
34 |
-
request:
|
35 |
):
|
36 |
logger.info("Entering chat_completions route")
|
37 |
-
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
if
|
40 |
raise HTTPException(
|
41 |
status_code=400,
|
42 |
-
detail=f"Model {
|
43 |
)
|
44 |
|
45 |
-
|
|
|
|
|
46 |
logger.info("Streaming response")
|
47 |
-
|
|
|
48 |
else:
|
49 |
logger.info("Non-streaming response")
|
50 |
-
|
|
|
51 |
|
52 |
-
@router.
|
53 |
-
@router.
|
54 |
-
@router.
|
55 |
-
@router.
|
56 |
-
@router.
|
57 |
@router.get("/health")
|
58 |
def health_check(request: Request):
|
59 |
-
return
|
|
|
1 |
import json
|
2 |
from fastapi import APIRouter, Depends, HTTPException, Request, Response
|
3 |
+
from fastapi.responses import StreamingResponse, JSONResponse
|
4 |
from api.auth import verify_app_secret
|
5 |
from api.config import ALLOWED_MODELS
|
|
|
6 |
from api.utils import process_non_streaming_response, process_streaming_response
|
7 |
from api.logger import setup_logger
|
8 |
|
|
|
30 |
@router.post("/v1/chat/completions")
|
31 |
@router.post("/api/v1/chat/completions")
|
32 |
async def chat_completions(
|
33 |
+
request: Request, app_secret: str = Depends(verify_app_secret)
|
34 |
):
|
35 |
logger.info("Entering chat_completions route")
|
36 |
+
request_data = await request.json()
|
37 |
+
model = request_data.get('model')
|
38 |
+
if not model:
|
39 |
+
raise HTTPException(status_code=400, detail="Model is required")
|
40 |
+
logger.info(f"Processing chat completion request for model: {model}")
|
41 |
|
42 |
+
if model not in [model_dict["id"] for model_dict in ALLOWED_MODELS]:
|
43 |
raise HTTPException(
|
44 |
status_code=400,
|
45 |
+
detail=f"Model {model} is not allowed. Allowed models are: {', '.join(model['id'] for model in ALLOWED_MODELS)}",
|
46 |
)
|
47 |
|
48 |
+
stream = request_data.get('stream', False)
|
49 |
+
|
50 |
+
if stream:
|
51 |
logger.info("Streaming response")
|
52 |
+
response = await process_streaming_response(request_data)
|
53 |
+
return StreamingResponse(response, media_type="application/json")
|
54 |
else:
|
55 |
logger.info("Non-streaming response")
|
56 |
+
response = await process_non_streaming_response(request_data)
|
57 |
+
return JSONResponse(content=response)
|
58 |
|
59 |
+
@router.get('/')
|
60 |
+
@router.get('/healthz')
|
61 |
+
@router.get('/ready')
|
62 |
+
@router.get('/alive')
|
63 |
+
@router.get('/status')
|
64 |
@router.get("/health")
|
65 |
def health_check(request: Request):
|
66 |
+
return JSONResponse(content={"status": "ok"})
|