Update api/routes.py
Browse files- api/routes.py +52 -7
api/routes.py
CHANGED
@@ -1,11 +1,13 @@
|
|
1 |
import json
|
2 |
-
from fastapi import APIRouter, Depends, HTTPException, Request
|
3 |
-
from fastapi.responses import StreamingResponse
|
4 |
-
from
|
5 |
-
|
6 |
-
from
|
7 |
-
from
|
8 |
-
from
|
|
|
|
|
9 |
|
10 |
logger = setup_logger(__name__)
|
11 |
|
@@ -49,6 +51,47 @@ async def chat_completions(
|
|
49 |
logger.info("Non-streaming response")
|
50 |
return await process_non_streaming_response(request)
|
51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
@router.route('/')
|
53 |
@router.route('/healthz')
|
54 |
@router.route('/ready')
|
@@ -57,3 +100,5 @@ async def chat_completions(
|
|
57 |
@router.get("/health")
|
58 |
def health_check(request: Request):
|
59 |
return Response(content=json.dumps({"status": "ok"}), media_type="application/json")
|
|
|
|
|
|
1 |
import json
|
2 |
+
from fastapi import APIRouter, Depends, HTTPException, Request
|
3 |
+
from fastapi.responses import StreamingResponse, JSONResponse
|
4 |
+
from typing import Optional
|
5 |
+
|
6 |
+
from .auth import verify_app_secret
|
7 |
+
from .models import ChatRequest, ImageResponse
|
8 |
+
from .utils import strip_model_prefix
|
9 |
+
from .gizai import GizAI
|
10 |
+
from .logger import setup_logger
|
11 |
|
12 |
logger = setup_logger(__name__)
|
13 |
|
|
|
51 |
logger.info("Non-streaming response")
|
52 |
return await process_non_streaming_response(request)
|
53 |
|
54 |
+
|
55 |
+
# GizAI Routes
|
56 |
+
gizai_router = APIRouter(prefix="/gizai", tags=["GizAI"])
|
57 |
+
|
58 |
+
@gizai_router.options("/v1/chat/completions")
|
59 |
+
async def gizai_chat_completions_options():
|
60 |
+
return JSONResponse(
|
61 |
+
status_code=200,
|
62 |
+
headers={
|
63 |
+
"Access-Control-Allow-Origin": "*",
|
64 |
+
"Access-Control-Allow-Methods": "POST, OPTIONS",
|
65 |
+
"Access-Control-Allow-Headers": "Content-Type, Authorization",
|
66 |
+
},
|
67 |
+
)
|
68 |
+
|
69 |
+
@gizai_router.post("/v1/chat/completions")
|
70 |
+
async def gizai_chat_completions(
|
71 |
+
request: ChatRequest, app_secret: str = Depends(verify_app_secret)
|
72 |
+
):
|
73 |
+
logger.info("Entering GizAI chat_completions route")
|
74 |
+
logger.info(f"Processing chat completion request for model: {request.model}")
|
75 |
+
|
76 |
+
# Validate model
|
77 |
+
model = GizAI.get_model(request.model)
|
78 |
+
if model not in GizAI.models:
|
79 |
+
raise HTTPException(
|
80 |
+
status_code=400,
|
81 |
+
detail=f"Model {request.model} is not supported by GizAI. Supported models are: {', '.join(GizAI.models)}",
|
82 |
+
)
|
83 |
+
|
84 |
+
try:
|
85 |
+
async_generator = GizAI.create_async_generator(model=model, messages=request.messages)
|
86 |
+
return StreamingResponse(async_generator, media_type="application/json")
|
87 |
+
except Exception as e:
|
88 |
+
logger.error(f"Error in GizAI chat_completions: {e}", exc_info=True)
|
89 |
+
raise HTTPException(status_code=500, detail=str(e))
|
90 |
+
|
91 |
+
# Include GizAI router in the main router
|
92 |
+
router.include_router(gizai_router)
|
93 |
+
|
94 |
+
|
95 |
@router.route('/')
|
96 |
@router.route('/healthz')
|
97 |
@router.route('/ready')
|
|
|
100 |
@router.get("/health")
|
101 |
def health_check(request: Request):
|
102 |
return Response(content=json.dumps({"status": "ok"}), media_type="application/json")
|
103 |
+
|
104 |
+
|