main.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import logging
  2. from litellm.proxy.proxy_server import ProxyConfig, initialize
  3. from litellm.proxy.proxy_server import app
  4. from fastapi import FastAPI, Request, Depends, status
  5. from fastapi.responses import JSONResponse
  6. from utils.utils import get_http_authorization_cred, get_current_user
  7. from config import SRC_LOG_LEVELS, ENV
  8. log = logging.getLogger(__name__)
  9. log.setLevel(SRC_LOG_LEVELS["LITELLM"])
  10. proxy_config = ProxyConfig()
  11. async def config():
  12. router, model_list, general_settings = await proxy_config.load_config(
  13. router=None, config_file_path="./data/litellm/config.yaml"
  14. )
  15. await initialize(config="./data/litellm/config.yaml", telemetry=False)
  16. async def startup():
  17. await config()
  18. @app.on_event("startup")
  19. async def on_startup():
  20. await startup()
  21. @app.middleware("http")
  22. async def auth_middleware(request: Request, call_next):
  23. auth_header = request.headers.get("Authorization", "")
  24. if ENV != "dev":
  25. try:
  26. user = get_current_user(get_http_authorization_cred(auth_header))
  27. log.debug(f"user: {user}")
  28. except Exception as e:
  29. return JSONResponse(status_code=400, content={"detail": str(e)})
  30. response = await call_next(request)
  31. return response