models.py 9.2 KB

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