-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
49 lines (40 loc) · 1.44 KB
/
main.py
File metadata and controls
49 lines (40 loc) · 1.44 KB
1
2
3
4
5
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
42
43
44
45
46
47
48
49
import os
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from fastapi.exceptions import RequestValidationError
from fastapi.encoders import jsonable_encoder
from typing import Optional
from api.routes import openai_routes
from api.dependencies import openai_service
from utils.logger import get_logger
from config.settings import settings
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(openai_routes.router)
logger = get_logger()
@app.on_event("startup")
async def startup_event():
logger.info("Starting the AI Interface for OpenAI Responses service.")
openai_service.init_openai_service(settings.OPENAI_API_KEY)
@app.on_event("shutdown")
async def shutdown_event():
logger.info("Shutting down the AI Interface for OpenAI Responses service.")
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
"""Custom exception handler for validation errors."""
errors = exc.errors()
logger.error(f"Validation error: {errors}")
return JSONResponse(
status_code=400,
content=jsonable_encoder({"detail": errors}),
)
@app.get("/")
async def root():
return {"message": "Welcome to the AI Interface for OpenAI Responses service!"}