Update api/routes.py
Browse files- api/routes.py +10 -8
api/routes.py
CHANGED
|
@@ -4,9 +4,8 @@ import json
|
|
| 4 |
from fastapi import APIRouter, Depends, HTTPException, Request, Response
|
| 5 |
from fastapi.responses import StreamingResponse, JSONResponse
|
| 6 |
from api.auth import verify_app_secret
|
| 7 |
-
from api.config import GIZAI_API_ENDPOINT, GIZAI_BASE_URL
|
| 8 |
from api.models import ChatRequest, ImageResponseModel, ChatCompletionResponse
|
| 9 |
-
from api.utils import
|
| 10 |
from api.logger import setup_logger
|
| 11 |
|
| 12 |
logger = setup_logger(__name__)
|
|
@@ -14,7 +13,7 @@ logger = setup_logger(__name__)
|
|
| 14 |
router = APIRouter()
|
| 15 |
|
| 16 |
@router.options("/v1/chat/completions")
|
| 17 |
-
@router.options("/api/v1/
|
| 18 |
async def gizai_chat_completions_options():
|
| 19 |
return Response(
|
| 20 |
status_code=200,
|
|
@@ -26,12 +25,12 @@ async def gizai_chat_completions_options():
|
|
| 26 |
)
|
| 27 |
|
| 28 |
@router.get("/v1/models")
|
| 29 |
-
@router.get("/api/v1/
|
| 30 |
async def list_gizai_models():
|
| 31 |
-
return {"object": "list", "data": GizAI.models}
|
| 32 |
|
| 33 |
@router.post("/v1/chat/completions")
|
| 34 |
-
@router.post("/api/v1/
|
| 35 |
async def gizai_chat_completions(
|
| 36 |
request: ChatRequest, app_secret: str = Depends(verify_app_secret)
|
| 37 |
):
|
|
@@ -46,11 +45,14 @@ async def gizai_chat_completions(
|
|
| 46 |
)
|
| 47 |
|
| 48 |
if request.stream:
|
|
|
|
|
|
|
| 49 |
logger.info("Streaming response")
|
| 50 |
-
return StreamingResponse(
|
| 51 |
else:
|
| 52 |
logger.info("Non-streaming response")
|
| 53 |
-
|
|
|
|
| 54 |
|
| 55 |
@router.route('/')
|
| 56 |
@router.route('/healthz')
|
|
|
|
| 4 |
from fastapi import APIRouter, Depends, HTTPException, Request, Response
|
| 5 |
from fastapi.responses import StreamingResponse, JSONResponse
|
| 6 |
from api.auth import verify_app_secret
|
|
|
|
| 7 |
from api.models import ChatRequest, ImageResponseModel, ChatCompletionResponse
|
| 8 |
+
from api.utils import process_gizai_stream_response, process_gizai_non_stream_response, GizAI
|
| 9 |
from api.logger import setup_logger
|
| 10 |
|
| 11 |
logger = setup_logger(__name__)
|
|
|
|
| 13 |
router = APIRouter()
|
| 14 |
|
| 15 |
@router.options("/v1/chat/completions")
|
| 16 |
+
@router.options("/api/v1/chat/completions")
|
| 17 |
async def gizai_chat_completions_options():
|
| 18 |
return Response(
|
| 19 |
status_code=200,
|
|
|
|
| 25 |
)
|
| 26 |
|
| 27 |
@router.get("/v1/models")
|
| 28 |
+
@router.get("/api/v1/models")
|
| 29 |
async def list_gizai_models():
|
| 30 |
+
return {"object": "list", "data": GizAI.models}
|
| 31 |
|
| 32 |
@router.post("/v1/chat/completions")
|
| 33 |
+
@router.post("/api/v1/chat/completions")
|
| 34 |
async def gizai_chat_completions(
|
| 35 |
request: ChatRequest, app_secret: str = Depends(verify_app_secret)
|
| 36 |
):
|
|
|
|
| 45 |
)
|
| 46 |
|
| 47 |
if request.stream:
|
| 48 |
+
if GizAI.is_image_model(model):
|
| 49 |
+
raise HTTPException(status_code=400, detail="Image generation does not support streaming.")
|
| 50 |
logger.info("Streaming response")
|
| 51 |
+
return StreamingResponse(process_gizai_stream_response(request, model), media_type="text/event-stream")
|
| 52 |
else:
|
| 53 |
logger.info("Non-streaming response")
|
| 54 |
+
response = await process_gizai_non_stream_response(request, model)
|
| 55 |
+
return response
|
| 56 |
|
| 57 |
@router.route('/')
|
| 58 |
@router.route('/healthz')
|