main.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. from bs4 import BeautifulSoup
  2. import json
  3. import markdown
  4. import time
  5. import os
  6. import sys
  7. from fastapi import FastAPI, Request, Depends
  8. from fastapi.staticfiles import StaticFiles
  9. from fastapi import HTTPException
  10. from fastapi.responses import JSONResponse
  11. from fastapi.middleware.wsgi import WSGIMiddleware
  12. from fastapi.middleware.cors import CORSMiddleware
  13. from starlette.exceptions import HTTPException as StarletteHTTPException
  14. from litellm.proxy.proxy_server import ProxyConfig, initialize
  15. from litellm.proxy.proxy_server import app as litellm_app
  16. from apps.ollama.main import app as ollama_app
  17. from apps.openai.main import app as openai_app
  18. from apps.audio.main import app as audio_app
  19. from apps.images.main import app as images_app
  20. from apps.rag.main import app as rag_app
  21. from apps.web.main import app as webui_app
  22. from config import WEBUI_NAME, ENV, VERSION, CHANGELOG, FRONTEND_BUILD_DIR
  23. from utils.utils import get_http_authorization_cred, get_current_user
  24. class SPAStaticFiles(StaticFiles):
  25. async def get_response(self, path: str, scope):
  26. try:
  27. return await super().get_response(path, scope)
  28. except (HTTPException, StarletteHTTPException) as ex:
  29. if ex.status_code == 404:
  30. return await super().get_response("index.html", scope)
  31. else:
  32. raise ex
  33. proxy_config = ProxyConfig()
  34. async def config():
  35. router, model_list, general_settings = await proxy_config.load_config(
  36. router=None, config_file_path="./data/litellm/config.yaml"
  37. )
  38. await initialize(config="./data/litellm/config.yaml", telemetry=False)
  39. async def startup():
  40. await config()
  41. app = FastAPI(docs_url="/docs" if ENV == "dev" else None, redoc_url=None)
  42. origins = ["*"]
  43. app.add_middleware(
  44. CORSMiddleware,
  45. allow_origins=origins,
  46. allow_credentials=True,
  47. allow_methods=["*"],
  48. allow_headers=["*"],
  49. )
  50. @app.on_event("startup")
  51. async def on_startup():
  52. await startup()
  53. @app.middleware("http")
  54. async def check_url(request: Request, call_next):
  55. start_time = int(time.time())
  56. response = await call_next(request)
  57. process_time = int(time.time()) - start_time
  58. response.headers["X-Process-Time"] = str(process_time)
  59. return response
  60. @litellm_app.middleware("http")
  61. async def auth_middleware(request: Request, call_next):
  62. auth_header = request.headers.get("Authorization", "")
  63. if ENV != "dev":
  64. try:
  65. user = get_current_user(get_http_authorization_cred(auth_header))
  66. print(user)
  67. except Exception as e:
  68. return JSONResponse(status_code=400, content={"detail": str(e)})
  69. response = await call_next(request)
  70. return response
  71. app.mount("/api/v1", webui_app)
  72. app.mount("/litellm/api", litellm_app)
  73. app.mount("/ollama/api", ollama_app)
  74. app.mount("/openai/api", openai_app)
  75. app.mount("/images/api/v1", images_app)
  76. app.mount("/audio/api/v1", audio_app)
  77. app.mount("/rag/api/v1", rag_app)
  78. @app.get("/api/config")
  79. async def get_app_config():
  80. return {
  81. "status": True,
  82. "name": WEBUI_NAME,
  83. "version": VERSION,
  84. "images": images_app.state.ENABLED,
  85. "default_models": webui_app.state.DEFAULT_MODELS,
  86. "default_prompt_suggestions": webui_app.state.DEFAULT_PROMPT_SUGGESTIONS,
  87. }
  88. @app.get("/api/changelog")
  89. async def get_app_changelog():
  90. return CHANGELOG
  91. app.mount("/static", StaticFiles(directory="static"), name="static")
  92. app.mount(
  93. "/",
  94. SPAStaticFiles(directory=FRONTEND_BUILD_DIR, html=True),
  95. name="spa-static-files",
  96. )