Spaces:
Sleeping
Sleeping
File size: 2,393 Bytes
64d7fdf | 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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | from fastapi import FastAPI
from fastapi.requests import Request
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from app.api.routes import chat, documents, health
from app.api.middleware.cors import setup_cors
from app.api.middleware.error_handler import error_handler_middleware
from app.api.middleware.logging import logging_middleware
from app.db.redis_client import RedisClient
from app.db.mongodb import MongoDB
from app.db.vector_store import vector_store
from app.utils.logger import logger
from app.config import config
from contextlib import asynccontextmanager
@asynccontextmanager
async def lifespan(app: FastAPI):
logger.info("Starting RAG Chatbot application...")
redis_client = RedisClient()
await redis_client.connect()
logger.info("Redis connected")
mongodb = MongoDB()
try:
await mongodb.connect()
logger.info("MongoDB connected")
except Exception as e:
logger.warning(f"MongoDB connection failed: {str(e)}")
try:
vector_store.create_collection()
logger.info("Qdrant collection ready")
except Exception as e:
logger.warning(f"Qdrant setup warning: {str(e)}")
yield
await redis_client.disconnect()
logger.info("Redis disconnected")
try:
await mongodb.disconnect()
logger.info("MongoDB disconnected")
except:
pass
logger.info("Application shutdown complete")
app = FastAPI(
title=config["app"]["app"]["name"],
version=config["app"]["app"]["version"],
description="Enterprise RAG Chatbot with LangChain and LangGraph",
lifespan=lifespan
)
setup_cors(app)
app.middleware("http")(error_handler_middleware)
app.middleware("http")(logging_middleware)
app.include_router(chat.router)
app.include_router(documents.router)
app.include_router(health.router)
app.mount("/static", StaticFiles(directory="frontend/static"), name="static")
templates = Jinja2Templates(directory="frontend/templates")
@app.get("/")
async def root(request: Request):
return templates.TemplateResponse("index.html", {"request": request})
if __name__ == "__main__":
import uvicorn
uvicorn.run(
"app.main:app",
host=config["app"]["server"]["host"],
port=config["app"]["server"]["port"],
reload=config["app"]["app"]["debug"]
)
|