Update api/app.py
Browse files- api/app.py +50 -41
api/app.py
CHANGED
@@ -1,41 +1,50 @@
|
|
1 |
-
from fastapi import FastAPI, Request
|
2 |
-
from
|
3 |
-
from
|
4 |
-
from api.
|
5 |
-
from api.
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
)
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, Depends, HTTPException, Request, Response
|
2 |
+
from fastapi.responses import JSONResponse, StreamingResponse
|
3 |
+
from api.auth import verify_app_secret
|
4 |
+
from api.config import ALLOWED_MODELS
|
5 |
+
from api.models import ChatRequest
|
6 |
+
from api.utils import process_response
|
7 |
+
from api.logger import setup_logger
|
8 |
+
|
9 |
+
logger = setup_logger(__name__)
|
10 |
+
|
11 |
+
app = FastAPI(
|
12 |
+
title="Your API",
|
13 |
+
docs_url=None, # Disable Swagger UI
|
14 |
+
redoc_url=None, # Disable ReDoc
|
15 |
+
openapi_url=None, # Disable OpenAPI schema
|
16 |
+
)
|
17 |
+
|
18 |
+
# Include your authentication if needed
|
19 |
+
# from api.auth import verify_app_secret
|
20 |
+
|
21 |
+
@app.post("/v1/chat/completions")
|
22 |
+
async def chat_completions(
|
23 |
+
request: ChatRequest, app_secret: str = Depends(verify_app_secret)
|
24 |
+
):
|
25 |
+
logger.info("Processing chat completion request")
|
26 |
+
if request.model not in [model["id"] for model in ALLOWED_MODELS]:
|
27 |
+
raise HTTPException(
|
28 |
+
status_code=400,
|
29 |
+
detail=f"Model {request.model} is not allowed."
|
30 |
+
)
|
31 |
+
|
32 |
+
# Process the response using Editee API
|
33 |
+
if request.stream:
|
34 |
+
# If streaming is required
|
35 |
+
generator = process_response(request, stream=True)
|
36 |
+
return StreamingResponse(generator, media_type="text/event-stream")
|
37 |
+
else:
|
38 |
+
# Non-streaming response
|
39 |
+
response_data = await process_response(request)
|
40 |
+
return JSONResponse(content=response_data)
|
41 |
+
|
42 |
+
# Health check endpoints
|
43 |
+
@app.get("/")
|
44 |
+
@app.get("/healthz")
|
45 |
+
@app.get("/ready")
|
46 |
+
@app.get("/alive")
|
47 |
+
@app.get("/status")
|
48 |
+
@app.get("/health")
|
49 |
+
def health_check(request: Request):
|
50 |
+
return Response(content='{"status": "ok"}', media_type="application/json")
|