main.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. import sys
  2. from fastapi import FastAPI, Depends, HTTPException
  3. from fastapi.routing import APIRoute
  4. from fastapi.middleware.cors import CORSMiddleware
  5. import logging
  6. from fastapi import FastAPI, Request, Depends, status, Response
  7. from fastapi.responses import JSONResponse
  8. from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
  9. from starlette.responses import StreamingResponse
  10. import json
  11. import time
  12. import requests
  13. from pydantic import BaseModel, ConfigDict
  14. from typing import Optional, List
  15. from utils.utils import get_verified_user, get_current_user, get_admin_user
  16. from config import SRC_LOG_LEVELS, ENV
  17. from constants import MESSAGES
  18. import os
  19. log = logging.getLogger(__name__)
  20. log.setLevel(SRC_LOG_LEVELS["LITELLM"])
  21. from config import (
  22. ENABLE_LITELLM,
  23. ENABLE_MODEL_FILTER,
  24. MODEL_FILTER_LIST,
  25. DATA_DIR,
  26. LITELLM_PROXY_PORT,
  27. LITELLM_PROXY_HOST,
  28. )
  29. from litellm.utils import get_llm_provider
  30. import asyncio
  31. import subprocess
  32. import yaml
  33. app = FastAPI()
  34. origins = ["*"]
  35. app.add_middleware(
  36. CORSMiddleware,
  37. allow_origins=origins,
  38. allow_credentials=True,
  39. allow_methods=["*"],
  40. allow_headers=["*"],
  41. )
  42. LITELLM_CONFIG_DIR = f"{DATA_DIR}/litellm/config.yaml"
  43. with open(LITELLM_CONFIG_DIR, "r") as file:
  44. litellm_config = yaml.safe_load(file)
  45. app.state.ENABLE = ENABLE_LITELLM
  46. app.state.CONFIG = litellm_config
  47. # Global variable to store the subprocess reference
  48. background_process = None
  49. CONFLICT_ENV_VARS = [
  50. # Uvicorn uses PORT, so LiteLLM might use it as well
  51. "PORT",
  52. # LiteLLM uses DATABASE_URL for Prisma connections
  53. "DATABASE_URL",
  54. ]
  55. async def run_background_process(command):
  56. global background_process
  57. log.info("run_background_process")
  58. try:
  59. # Log the command to be executed
  60. log.info(f"Executing command: {command}")
  61. # Filter environment variables known to conflict with litellm
  62. env = {k: v for k, v in os.environ.items() if k not in CONFLICT_ENV_VARS}
  63. # Execute the command and create a subprocess
  64. process = await asyncio.create_subprocess_exec(
  65. *command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env
  66. )
  67. background_process = process
  68. log.info("Subprocess started successfully.")
  69. # Capture STDERR for debugging purposes
  70. stderr_output = await process.stderr.read()
  71. stderr_text = stderr_output.decode().strip()
  72. if stderr_text:
  73. log.info(f"Subprocess STDERR: {stderr_text}")
  74. # log.info output line by line
  75. async for line in process.stdout:
  76. log.info(line.decode().strip())
  77. # Wait for the process to finish
  78. returncode = await process.wait()
  79. log.info(f"Subprocess exited with return code {returncode}")
  80. except Exception as e:
  81. log.error(f"Failed to start subprocess: {e}")
  82. raise # Optionally re-raise the exception if you want it to propagate
  83. async def start_litellm_background():
  84. log.info("start_litellm_background")
  85. # Command to run in the background
  86. command = [
  87. "litellm",
  88. "--port",
  89. str(LITELLM_PROXY_PORT),
  90. "--host",
  91. LITELLM_PROXY_HOST,
  92. "--telemetry",
  93. "False",
  94. "--config",
  95. LITELLM_CONFIG_DIR,
  96. ]
  97. await run_background_process(command)
  98. async def shutdown_litellm_background():
  99. log.info("shutdown_litellm_background")
  100. global background_process
  101. if background_process:
  102. background_process.terminate()
  103. await background_process.wait() # Ensure the process has terminated
  104. log.info("Subprocess terminated")
  105. background_process = None
  106. @app.on_event("startup")
  107. async def startup_event():
  108. log.info("startup_event")
  109. # TODO: Check config.yaml file and create one
  110. asyncio.create_task(start_litellm_background())
  111. app.state.ENABLE_MODEL_FILTER = ENABLE_MODEL_FILTER
  112. app.state.MODEL_FILTER_LIST = MODEL_FILTER_LIST
  113. @app.get("/")
  114. async def get_status():
  115. return {"status": True}
  116. async def restart_litellm():
  117. """
  118. Endpoint to restart the litellm background service.
  119. """
  120. log.info("Requested restart of litellm service.")
  121. try:
  122. # Shut down the existing process if it is running
  123. await shutdown_litellm_background()
  124. log.info("litellm service shutdown complete.")
  125. # Restart the background service
  126. asyncio.create_task(start_litellm_background())
  127. log.info("litellm service restart complete.")
  128. return {
  129. "status": "success",
  130. "message": "litellm service restarted successfully.",
  131. }
  132. except Exception as e:
  133. log.info(f"Error restarting litellm service: {e}")
  134. raise HTTPException(
  135. status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e)
  136. )
  137. @app.get("/restart")
  138. async def restart_litellm_handler(user=Depends(get_admin_user)):
  139. return await restart_litellm()
  140. @app.get("/config")
  141. async def get_config(user=Depends(get_admin_user)):
  142. return app.state.CONFIG
  143. class LiteLLMConfigForm(BaseModel):
  144. general_settings: Optional[dict] = None
  145. litellm_settings: Optional[dict] = None
  146. model_list: Optional[List[dict]] = None
  147. router_settings: Optional[dict] = None
  148. model_config = ConfigDict(protected_namespaces=())
  149. @app.post("/config/update")
  150. async def update_config(form_data: LiteLLMConfigForm, user=Depends(get_admin_user)):
  151. app.state.CONFIG = form_data.model_dump(exclude_none=True)
  152. with open(LITELLM_CONFIG_DIR, "w") as file:
  153. yaml.dump(app.state.CONFIG, file)
  154. await restart_litellm()
  155. return app.state.CONFIG
  156. @app.get("/models")
  157. @app.get("/v1/models")
  158. async def get_models(user=Depends(get_current_user)):
  159. if app.state.ENABLE:
  160. while not background_process:
  161. await asyncio.sleep(0.1)
  162. url = f"http://localhost:{LITELLM_PROXY_PORT}/v1"
  163. r = None
  164. try:
  165. r = requests.request(method="GET", url=f"{url}/models")
  166. r.raise_for_status()
  167. data = r.json()
  168. if app.state.ENABLE_MODEL_FILTER:
  169. if user and user.role == "user":
  170. data["data"] = list(
  171. filter(
  172. lambda model: model["id"] in app.state.MODEL_FILTER_LIST,
  173. data["data"],
  174. )
  175. )
  176. return data
  177. except Exception as e:
  178. log.exception(e)
  179. error_detail = "Open WebUI: Server Connection Error"
  180. if r is not None:
  181. try:
  182. res = r.json()
  183. if "error" in res:
  184. error_detail = f"External: {res['error']}"
  185. except:
  186. error_detail = f"External: {e}"
  187. return {
  188. "data": [
  189. {
  190. "id": model["model_name"],
  191. "object": "model",
  192. "created": int(time.time()),
  193. "owned_by": "openai",
  194. }
  195. for model in app.state.CONFIG["model_list"]
  196. ],
  197. "object": "list",
  198. }
  199. else:
  200. return {
  201. "data": [],
  202. "object": "list",
  203. }
  204. @app.get("/model/info")
  205. async def get_model_list(user=Depends(get_admin_user)):
  206. return {"data": app.state.CONFIG["model_list"]}
  207. class AddLiteLLMModelForm(BaseModel):
  208. model_name: str
  209. litellm_params: dict
  210. model_config = ConfigDict(protected_namespaces=())
  211. @app.post("/model/new")
  212. async def add_model_to_config(
  213. form_data: AddLiteLLMModelForm, user=Depends(get_admin_user)
  214. ):
  215. try:
  216. get_llm_provider(model=form_data.model_name)
  217. app.state.CONFIG["model_list"].append(form_data.model_dump())
  218. with open(LITELLM_CONFIG_DIR, "w") as file:
  219. yaml.dump(app.state.CONFIG, file)
  220. await restart_litellm()
  221. return {"message": MESSAGES.MODEL_ADDED(form_data.model_name)}
  222. except Exception as e:
  223. print(e)
  224. raise HTTPException(
  225. status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e)
  226. )
  227. class DeleteLiteLLMModelForm(BaseModel):
  228. id: str
  229. @app.post("/model/delete")
  230. async def delete_model_from_config(
  231. form_data: DeleteLiteLLMModelForm, user=Depends(get_admin_user)
  232. ):
  233. app.state.CONFIG["model_list"] = [
  234. model
  235. for model in app.state.CONFIG["model_list"]
  236. if model["model_name"] != form_data.id
  237. ]
  238. with open(LITELLM_CONFIG_DIR, "w") as file:
  239. yaml.dump(app.state.CONFIG, file)
  240. await restart_litellm()
  241. return {"message": MESSAGES.MODEL_DELETED(form_data.id)}
  242. @app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE"])
  243. async def proxy(path: str, request: Request, user=Depends(get_verified_user)):
  244. body = await request.body()
  245. url = f"http://localhost:{LITELLM_PROXY_PORT}"
  246. target_url = f"{url}/{path}"
  247. headers = {}
  248. # headers["Authorization"] = f"Bearer {key}"
  249. headers["Content-Type"] = "application/json"
  250. r = None
  251. try:
  252. r = requests.request(
  253. method=request.method,
  254. url=target_url,
  255. data=body,
  256. headers=headers,
  257. stream=True,
  258. )
  259. r.raise_for_status()
  260. # Check if response is SSE
  261. if "text/event-stream" in r.headers.get("Content-Type", ""):
  262. return StreamingResponse(
  263. r.iter_content(chunk_size=8192),
  264. status_code=r.status_code,
  265. headers=dict(r.headers),
  266. )
  267. else:
  268. response_data = r.json()
  269. return response_data
  270. except Exception as e:
  271. log.exception(e)
  272. error_detail = "Open WebUI: Server Connection Error"
  273. if r is not None:
  274. try:
  275. res = r.json()
  276. if "error" in res:
  277. error_detail = f"External: {res['error']['message'] if 'message' in res['error'] else res['error']}"
  278. except:
  279. error_detail = f"External: {e}"
  280. raise HTTPException(
  281. status_code=r.status_code if r else 500, detail=error_detail
  282. )