main.py 11 KB

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