models.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. from typing import Optional
  2. import io
  3. import base64
  4. import json
  5. import asyncio
  6. import logging
  7. from open_webui.models.models import (
  8. ModelForm,
  9. ModelModel,
  10. ModelResponse,
  11. ModelUserResponse,
  12. Models,
  13. )
  14. from pydantic import BaseModel
  15. from open_webui.constants import ERROR_MESSAGES
  16. from fastapi import (
  17. APIRouter,
  18. Depends,
  19. HTTPException,
  20. Request,
  21. status,
  22. Response,
  23. )
  24. from fastapi.responses import FileResponse, StreamingResponse
  25. from open_webui.utils.auth import get_admin_user, get_verified_user
  26. from open_webui.utils.access_control import has_access, has_permission
  27. from open_webui.config import BYPASS_ADMIN_ACCESS_CONTROL, STATIC_DIR
  28. log = logging.getLogger(__name__)
  29. router = APIRouter()
  30. def validate_model_id(model_id: str) -> bool:
  31. return model_id and len(model_id) <= 256
  32. ###########################
  33. # GetModels
  34. ###########################
  35. @router.get(
  36. "/list", response_model=list[ModelUserResponse]
  37. ) # do NOT use "/" as path, conflicts with main.py
  38. async def get_models(id: Optional[str] = None, user=Depends(get_verified_user)):
  39. if user.role == "admin" and BYPASS_ADMIN_ACCESS_CONTROL:
  40. return Models.get_models()
  41. else:
  42. return Models.get_models_by_user_id(user.id)
  43. ###########################
  44. # GetBaseModels
  45. ###########################
  46. @router.get("/base", response_model=list[ModelResponse])
  47. async def get_base_models(user=Depends(get_admin_user)):
  48. return Models.get_base_models()
  49. ############################
  50. # CreateNewModel
  51. ############################
  52. @router.post("/create", response_model=Optional[ModelModel])
  53. async def create_new_model(
  54. request: Request,
  55. form_data: ModelForm,
  56. user=Depends(get_verified_user),
  57. ):
  58. if user.role != "admin" and not has_permission(
  59. user.id, "workspace.models", request.app.state.config.USER_PERMISSIONS
  60. ):
  61. raise HTTPException(
  62. status_code=status.HTTP_401_UNAUTHORIZED,
  63. detail=ERROR_MESSAGES.UNAUTHORIZED,
  64. )
  65. model = Models.get_model_by_id(form_data.id)
  66. if model:
  67. raise HTTPException(
  68. status_code=status.HTTP_401_UNAUTHORIZED,
  69. detail=ERROR_MESSAGES.MODEL_ID_TAKEN,
  70. )
  71. if not validate_model_id(form_data.id):
  72. raise HTTPException(
  73. status_code=status.HTTP_400_BAD_REQUEST,
  74. detail=ERROR_MESSAGES.MODEL_ID_TOO_LONG,
  75. )
  76. else:
  77. model = Models.insert_new_model(form_data, user.id)
  78. if model:
  79. return model
  80. else:
  81. raise HTTPException(
  82. status_code=status.HTTP_401_UNAUTHORIZED,
  83. detail=ERROR_MESSAGES.DEFAULT(),
  84. )
  85. ############################
  86. # ExportModels
  87. ############################
  88. @router.get("/export", response_model=list[ModelModel])
  89. async def export_models(user=Depends(get_admin_user)):
  90. return Models.get_models()
  91. ############################
  92. # ImportModels
  93. ############################
  94. class ModelsImportForm(BaseModel):
  95. models: list[dict]
  96. @router.post("/import", response_model=bool)
  97. async def import_models(
  98. user: str = Depends(get_admin_user), form_data: ModelsImportForm = (...)
  99. ):
  100. try:
  101. data = form_data.models
  102. if isinstance(data, list):
  103. for model_data in data:
  104. # Here, you can add logic to validate model_data if needed
  105. model_id = model_data.get("id")
  106. if model_id and validate_model_id(model_id):
  107. existing_model = Models.get_model_by_id(model_id)
  108. if existing_model:
  109. # Update existing model
  110. model_data["meta"] = model_data.get("meta", {})
  111. model_data["params"] = model_data.get("params", {})
  112. updated_model = ModelForm(
  113. **{**existing_model.model_dump(), **model_data}
  114. )
  115. Models.update_model_by_id(model_id, updated_model)
  116. else:
  117. # Insert new model
  118. model_data["meta"] = model_data.get("meta", {})
  119. model_data["params"] = model_data.get("params", {})
  120. new_model = ModelForm(**model_data)
  121. Models.insert_new_model(user_id=user.id, form_data=new_model)
  122. return True
  123. else:
  124. raise HTTPException(status_code=400, detail="Invalid JSON format")
  125. except Exception as e:
  126. log.exception(e)
  127. raise HTTPException(status_code=500, detail=str(e))
  128. ############################
  129. # SyncModels
  130. ############################
  131. class SyncModelsForm(BaseModel):
  132. models: list[ModelModel] = []
  133. @router.post("/sync", response_model=list[ModelModel])
  134. async def sync_models(
  135. request: Request, form_data: SyncModelsForm, user=Depends(get_admin_user)
  136. ):
  137. return Models.sync_models(user.id, form_data.models)
  138. ###########################
  139. # GetModelById
  140. ###########################
  141. # Note: We're not using the typical url path param here, but instead using a query parameter to allow '/' in the id
  142. @router.get("/model", response_model=Optional[ModelResponse])
  143. async def get_model_by_id(id: str, user=Depends(get_verified_user)):
  144. model = Models.get_model_by_id(id)
  145. if model:
  146. if (
  147. (user.role == "admin" and BYPASS_ADMIN_ACCESS_CONTROL)
  148. or model.user_id == user.id
  149. or has_access(user.id, "read", model.access_control)
  150. ):
  151. return model
  152. else:
  153. raise HTTPException(
  154. status_code=status.HTTP_401_UNAUTHORIZED,
  155. detail=ERROR_MESSAGES.NOT_FOUND,
  156. )
  157. ###########################
  158. # GetModelById
  159. ###########################
  160. @router.get("/model/profile/image")
  161. async def get_model_profile_image(id: str, user=Depends(get_verified_user)):
  162. model = Models.get_model_by_id(id)
  163. if model:
  164. if model.meta.profile_image_url:
  165. if model.meta.profile_image_url.startswith("http"):
  166. return Response(
  167. status_code=status.HTTP_302_FOUND,
  168. headers={"Location": model.meta.profile_image_url},
  169. )
  170. elif model.meta.profile_image_url.startswith("data:image"):
  171. try:
  172. header, base64_data = model.meta.profile_image_url.split(",", 1)
  173. image_data = base64.b64decode(base64_data)
  174. image_buffer = io.BytesIO(image_data)
  175. return StreamingResponse(
  176. image_buffer,
  177. media_type="image/png",
  178. headers={"Content-Disposition": "inline; filename=image.png"},
  179. )
  180. except Exception as e:
  181. pass
  182. return FileResponse(f"{STATIC_DIR}/favicon.png")
  183. else:
  184. return FileResponse(f"{STATIC_DIR}/favicon.png")
  185. ############################
  186. # ToggleModelById
  187. ############################
  188. @router.post("/model/toggle", response_model=Optional[ModelResponse])
  189. async def toggle_model_by_id(id: str, user=Depends(get_verified_user)):
  190. model = Models.get_model_by_id(id)
  191. if model:
  192. if (
  193. user.role == "admin"
  194. or model.user_id == user.id
  195. or has_access(user.id, "write", model.access_control)
  196. ):
  197. model = Models.toggle_model_by_id(id)
  198. if model:
  199. return model
  200. else:
  201. raise HTTPException(
  202. status_code=status.HTTP_400_BAD_REQUEST,
  203. detail=ERROR_MESSAGES.DEFAULT("Error updating function"),
  204. )
  205. else:
  206. raise HTTPException(
  207. status_code=status.HTTP_401_UNAUTHORIZED,
  208. detail=ERROR_MESSAGES.UNAUTHORIZED,
  209. )
  210. else:
  211. raise HTTPException(
  212. status_code=status.HTTP_401_UNAUTHORIZED,
  213. detail=ERROR_MESSAGES.NOT_FOUND,
  214. )
  215. ############################
  216. # UpdateModelById
  217. ############################
  218. @router.post("/model/update", response_model=Optional[ModelModel])
  219. async def update_model_by_id(
  220. id: str,
  221. form_data: ModelForm,
  222. user=Depends(get_verified_user),
  223. ):
  224. model = Models.get_model_by_id(id)
  225. if not model:
  226. raise HTTPException(
  227. status_code=status.HTTP_401_UNAUTHORIZED,
  228. detail=ERROR_MESSAGES.NOT_FOUND,
  229. )
  230. if (
  231. model.user_id != user.id
  232. and not has_access(user.id, "write", model.access_control)
  233. and user.role != "admin"
  234. ):
  235. raise HTTPException(
  236. status_code=status.HTTP_400_BAD_REQUEST,
  237. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  238. )
  239. model = Models.update_model_by_id(id, form_data)
  240. return model
  241. ############################
  242. # DeleteModelById
  243. ############################
  244. @router.delete("/model/delete", response_model=bool)
  245. async def delete_model_by_id(id: str, user=Depends(get_verified_user)):
  246. model = Models.get_model_by_id(id)
  247. if not model:
  248. raise HTTPException(
  249. status_code=status.HTTP_401_UNAUTHORIZED,
  250. detail=ERROR_MESSAGES.NOT_FOUND,
  251. )
  252. if (
  253. user.role != "admin"
  254. and model.user_id != user.id
  255. and not has_access(user.id, "write", model.access_control)
  256. ):
  257. raise HTTPException(
  258. status_code=status.HTTP_401_UNAUTHORIZED,
  259. detail=ERROR_MESSAGES.UNAUTHORIZED,
  260. )
  261. result = Models.delete_model_by_id(id)
  262. return result
  263. @router.delete("/delete/all", response_model=bool)
  264. async def delete_all_models(user=Depends(get_admin_user)):
  265. result = Models.delete_all_models()
  266. return result