main.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. from fastapi import FastAPI, Depends, HTTPException
  2. from fastapi.routing import APIRoute
  3. from fastapi.middleware.cors import CORSMiddleware
  4. import logging
  5. from fastapi import FastAPI, Request, Depends, status, Response
  6. from fastapi.responses import JSONResponse
  7. from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
  8. from starlette.responses import StreamingResponse
  9. import json
  10. import requests
  11. from pydantic import BaseModel
  12. from typing import Optional, List
  13. from utils.utils import get_verified_user, get_current_user, get_admin_user
  14. from config import SRC_LOG_LEVELS, ENV
  15. from constants import ERROR_MESSAGES
  16. log = logging.getLogger(__name__)
  17. log.setLevel(SRC_LOG_LEVELS["LITELLM"])
  18. from config import MODEL_FILTER_ENABLED, MODEL_FILTER_LIST, DATA_DIR
  19. import asyncio
  20. import subprocess
  21. import yaml
  22. app = FastAPI()
  23. origins = ["*"]
  24. app.add_middleware(
  25. CORSMiddleware,
  26. allow_origins=origins,
  27. allow_credentials=True,
  28. allow_methods=["*"],
  29. allow_headers=["*"],
  30. )
  31. LITELLM_CONFIG_DIR = f"{DATA_DIR}/litellm/config.yaml"
  32. with open(LITELLM_CONFIG_DIR, "r") as file:
  33. litellm_config = yaml.safe_load(file)
  34. app.state.CONFIG = litellm_config
  35. # Global variable to store the subprocess reference
  36. background_process = None
  37. async def run_background_process(command):
  38. global background_process
  39. log.info("run_background_process")
  40. try:
  41. # Log the command to be executed
  42. log.info(f"Executing command: {command}")
  43. # Execute the command and create a subprocess
  44. process = await asyncio.create_subprocess_exec(
  45. *command.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE
  46. )
  47. background_process = process
  48. log.info("Subprocess started successfully.")
  49. # Capture STDERR for debugging purposes
  50. stderr_output = await process.stderr.read()
  51. stderr_text = stderr_output.decode().strip()
  52. if stderr_text:
  53. log.info(f"Subprocess STDERR: {stderr_text}")
  54. # log.info output line by line
  55. async for line in process.stdout:
  56. log.info(line.decode().strip())
  57. # Wait for the process to finish
  58. returncode = await process.wait()
  59. log.info(f"Subprocess exited with return code {returncode}")
  60. except Exception as e:
  61. log.error(f"Failed to start subprocess: {e}")
  62. raise # Optionally re-raise the exception if you want it to propagate
  63. async def start_litellm_background():
  64. log.info("start_litellm_background")
  65. # Command to run in the background
  66. command = (
  67. "litellm --port 14365 --telemetry False --config ./data/litellm/config.yaml"
  68. )
  69. await run_background_process(command)
  70. async def shutdown_litellm_background():
  71. log.info("shutdown_litellm_background")
  72. global background_process
  73. if background_process:
  74. background_process.terminate()
  75. await background_process.wait() # Ensure the process has terminated
  76. log.info("Subprocess terminated")
  77. @app.on_event("startup")
  78. async def startup_event():
  79. log.info("startup_event")
  80. # TODO: Check config.yaml file and create one
  81. asyncio.create_task(start_litellm_background())
  82. app.state.MODEL_FILTER_ENABLED = MODEL_FILTER_ENABLED
  83. app.state.MODEL_FILTER_LIST = MODEL_FILTER_LIST
  84. @app.get("/")
  85. async def get_status():
  86. return {"status": True}
  87. async def restart_litellm():
  88. """
  89. Endpoint to restart the litellm background service.
  90. """
  91. log.info("Requested restart of litellm service.")
  92. try:
  93. # Shut down the existing process if it is running
  94. await shutdown_litellm_background()
  95. log.info("litellm service shutdown complete.")
  96. # Restart the background service
  97. asyncio.create_task(start_litellm_background())
  98. log.info("litellm service restart complete.")
  99. return {
  100. "status": "success",
  101. "message": "litellm service restarted successfully.",
  102. }
  103. except Exception as e:
  104. log.info(f"Error restarting litellm service: {e}")
  105. raise HTTPException(
  106. status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e)
  107. )
  108. @app.get("/restart")
  109. async def restart_litellm_handler(user=Depends(get_admin_user)):
  110. return await restart_litellm()
  111. @app.get("/config")
  112. async def get_config(user=Depends(get_admin_user)):
  113. return app.state.CONFIG
  114. class LiteLLMConfigForm(BaseModel):
  115. general_settings: Optional[dict] = None
  116. litellm_settings: Optional[dict] = None
  117. model_list: Optional[List[dict]] = None
  118. router_settings: Optional[dict] = None
  119. @app.post("/config/update")
  120. async def update_config(form_data: LiteLLMConfigForm, user=Depends(get_admin_user)):
  121. app.state.CONFIG = form_data.model_dump(exclude_none=True)
  122. with open(LITELLM_CONFIG_DIR, "w") as file:
  123. yaml.dump(app.state.CONFIG, file)
  124. await restart_litellm()
  125. return app.state.CONFIG
  126. @app.get("/models")
  127. @app.get("/v1/models")
  128. async def get_models(user=Depends(get_current_user)):
  129. url = "http://localhost:14365/v1"
  130. r = None
  131. try:
  132. r = requests.request(method="GET", url=f"{url}/models")
  133. r.raise_for_status()
  134. data = r.json()
  135. if app.state.MODEL_FILTER_ENABLED:
  136. if user and user.role == "user":
  137. data["data"] = list(
  138. filter(
  139. lambda model: model["id"] in app.state.MODEL_FILTER_LIST,
  140. data["data"],
  141. )
  142. )
  143. return data
  144. except Exception as e:
  145. log.exception(e)
  146. error_detail = "Open WebUI: Server Connection Error"
  147. if r is not None:
  148. try:
  149. res = r.json()
  150. if "error" in res:
  151. error_detail = f"External: {res['error']}"
  152. except:
  153. error_detail = f"External: {e}"
  154. raise HTTPException(
  155. status_code=r.status_code if r else 500,
  156. detail=error_detail,
  157. )
  158. @app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE"])
  159. async def proxy(path: str, request: Request, user=Depends(get_verified_user)):
  160. body = await request.body()
  161. url = "http://localhost:14365"
  162. target_url = f"{url}/{path}"
  163. headers = {}
  164. # headers["Authorization"] = f"Bearer {key}"
  165. headers["Content-Type"] = "application/json"
  166. r = None
  167. try:
  168. r = requests.request(
  169. method=request.method,
  170. url=target_url,
  171. data=body,
  172. headers=headers,
  173. stream=True,
  174. )
  175. r.raise_for_status()
  176. # Check if response is SSE
  177. if "text/event-stream" in r.headers.get("Content-Type", ""):
  178. return StreamingResponse(
  179. r.iter_content(chunk_size=8192),
  180. status_code=r.status_code,
  181. headers=dict(r.headers),
  182. )
  183. else:
  184. response_data = r.json()
  185. return response_data
  186. except Exception as e:
  187. log.exception(e)
  188. error_detail = "Open WebUI: Server Connection Error"
  189. if r is not None:
  190. try:
  191. res = r.json()
  192. if "error" in res:
  193. error_detail = f"External: {res['error']['message'] if 'message' in res['error'] else res['error']}"
  194. except:
  195. error_detail = f"External: {e}"
  196. raise HTTPException(
  197. status_code=r.status_code if r else 500, detail=error_detail
  198. )