mirror of
https://github.com/QuivrHQ/quivr.git
synced 2024-12-14 17:03:29 +03:00
e3b6114248
* feat: add prompts table * feat: add Prompt entity * feat: add prompt router * refactor(promptRepository): use common reposority
80 lines
2.3 KiB
Python
80 lines
2.3 KiB
Python
import os
|
|
|
|
import pypandoc
|
|
import sentry_sdk
|
|
from fastapi import FastAPI, HTTPException, Request, status
|
|
from fastapi.exceptions import RequestValidationError
|
|
from fastapi.responses import JSONResponse
|
|
from logger import get_logger
|
|
from middlewares.cors import add_cors_middleware
|
|
from routes.api_key_routes import api_key_router
|
|
from routes.brain_routes import brain_router
|
|
from routes.chat_routes import chat_router
|
|
from routes.crawl_routes import crawl_router
|
|
from routes.explore_routes import explore_router
|
|
from routes.misc_routes import misc_router
|
|
from routes.prompt_routes import prompt_router
|
|
from routes.subscription_routes import subscription_router
|
|
from routes.upload_routes import upload_router
|
|
from routes.user_routes import user_router
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
sentry_dsn = os.getenv("SENTRY_DSN")
|
|
if sentry_dsn:
|
|
sentry_sdk.init(
|
|
dsn=sentry_dsn,
|
|
traces_sample_rate=1.0,
|
|
)
|
|
|
|
app = FastAPI()
|
|
|
|
add_cors_middleware(app)
|
|
|
|
|
|
@app.on_event("startup")
|
|
async def startup_event():
|
|
if not os.path.exists(pypandoc.get_pandoc_path()):
|
|
pypandoc.download_pandoc()
|
|
|
|
|
|
app.include_router(brain_router)
|
|
app.include_router(chat_router)
|
|
app.include_router(crawl_router)
|
|
app.include_router(explore_router)
|
|
app.include_router(misc_router)
|
|
app.include_router(upload_router)
|
|
app.include_router(user_router)
|
|
app.include_router(api_key_router)
|
|
app.include_router(subscription_router)
|
|
app.include_router(prompt_router)
|
|
|
|
|
|
@app.exception_handler(HTTPException)
|
|
async def http_exception_handler(_, exc):
|
|
return JSONResponse(
|
|
status_code=exc.status_code,
|
|
content={"detail": exc.detail},
|
|
)
|
|
|
|
|
|
# log more details about validation errors (422)
|
|
def handle_request_validation_error(app: FastAPI):
|
|
@app.exception_handler(RequestValidationError)
|
|
async def validation_exception_handler(
|
|
request: Request, exc: RequestValidationError
|
|
):
|
|
exc_str = f"{exc}".replace("\n", " ").replace(" ", " ")
|
|
logger.error(request, exc_str)
|
|
content = {
|
|
"status_code": status.HTTP_422_UNPROCESSABLE_ENTITY,
|
|
"message": exc_str,
|
|
"data": None,
|
|
}
|
|
return JSONResponse(
|
|
content=content, status_code=status.HTTP_422_UNPROCESSABLE_ENTITY
|
|
)
|
|
|
|
|
|
handle_request_validation_error(app)
|