models.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. from typing import Optional
  2. import io
  3. import base64
  4. from open_webui.models.models import (
  5. ModelForm,
  6. ModelModel,
  7. ModelResponse,
  8. ModelUserResponse,
  9. Models,
  10. )
  11. from pydantic import BaseModel
  12. from open_webui.constants import ERROR_MESSAGES
  13. from fastapi import APIRouter, Depends, HTTPException, Request, status, Response
  14. from fastapi.responses import FileResponse, StreamingResponse
  15. from open_webui.utils.auth import get_admin_user, get_verified_user
  16. from open_webui.utils.access_control import has_access, has_permission
  17. from open_webui.config import BYPASS_ADMIN_ACCESS_CONTROL, STATIC_DIR
  18. router = APIRouter()
  19. ###########################
  20. # GetModels
  21. ###########################
  22. @router.get("/", response_model=list[ModelUserResponse])
  23. async def get_models(id: Optional[str] = None, user=Depends(get_verified_user)):
  24. if user.role == "admin" and BYPASS_ADMIN_ACCESS_CONTROL:
  25. return Models.get_models()
  26. else:
  27. return Models.get_models_by_user_id(user.id)
  28. ###########################
  29. # GetBaseModels
  30. ###########################
  31. @router.get("/base", response_model=list[ModelResponse])
  32. async def get_base_models(user=Depends(get_admin_user)):
  33. return Models.get_base_models()
  34. ############################
  35. # CreateNewModel
  36. ############################
  37. @router.post("/create", response_model=Optional[ModelModel])
  38. async def create_new_model(
  39. request: Request,
  40. form_data: ModelForm,
  41. user=Depends(get_verified_user),
  42. ):
  43. if user.role != "admin" and not has_permission(
  44. user.id, "workspace.models", request.app.state.config.USER_PERMISSIONS
  45. ):
  46. raise HTTPException(
  47. status_code=status.HTTP_401_UNAUTHORIZED,
  48. detail=ERROR_MESSAGES.UNAUTHORIZED,
  49. )
  50. model = Models.get_model_by_id(form_data.id)
  51. if model:
  52. raise HTTPException(
  53. status_code=status.HTTP_401_UNAUTHORIZED,
  54. detail=ERROR_MESSAGES.MODEL_ID_TAKEN,
  55. )
  56. else:
  57. model = Models.insert_new_model(form_data, user.id)
  58. if model:
  59. return model
  60. else:
  61. raise HTTPException(
  62. status_code=status.HTTP_401_UNAUTHORIZED,
  63. detail=ERROR_MESSAGES.DEFAULT(),
  64. )
  65. ############################
  66. # ExportModels
  67. ############################
  68. @router.get("/export", response_model=list[ModelModel])
  69. async def export_models(user=Depends(get_admin_user)):
  70. return Models.get_models()
  71. ############################
  72. # SyncModels
  73. ############################
  74. class SyncModelsForm(BaseModel):
  75. models: list[ModelModel] = []
  76. @router.post("/sync", response_model=list[ModelModel])
  77. async def sync_models(
  78. request: Request, form_data: SyncModelsForm, user=Depends(get_admin_user)
  79. ):
  80. return Models.sync_models(user.id, form_data.models)
  81. ###########################
  82. # GetModelById
  83. ###########################
  84. # Note: We're not using the typical url path param here, but instead using a query parameter to allow '/' in the id
  85. @router.get("/model", response_model=Optional[ModelResponse])
  86. async def get_model_by_id(id: str, user=Depends(get_verified_user)):
  87. model = Models.get_model_by_id(id)
  88. if model:
  89. if (
  90. (user.role == "admin" and BYPASS_ADMIN_ACCESS_CONTROL)
  91. or model.user_id == user.id
  92. or has_access(user.id, "read", model.access_control)
  93. ):
  94. return model
  95. else:
  96. raise HTTPException(
  97. status_code=status.HTTP_401_UNAUTHORIZED,
  98. detail=ERROR_MESSAGES.NOT_FOUND,
  99. )
  100. ###########################
  101. # GetModelById
  102. ###########################
  103. @router.get("/model/profile/image")
  104. async def get_model_profile_image(id: str, user=Depends(get_verified_user)):
  105. model = Models.get_model_by_id(id)
  106. if model:
  107. if model.meta.profile_image_url:
  108. if model.meta.profile_image_url.startswith("http"):
  109. return Response(
  110. status_code=status.HTTP_302_FOUND,
  111. headers={"Location": model.meta.profile_image_url},
  112. )
  113. elif model.meta.profile_image_url.startswith("data:image"):
  114. try:
  115. header, base64_data = model.meta.profile_image_url.split(",", 1)
  116. image_data = base64.b64decode(base64_data)
  117. image_buffer = io.BytesIO(image_data)
  118. return StreamingResponse(
  119. image_buffer,
  120. media_type="image/png",
  121. headers={"Content-Disposition": "inline; filename=image.png"},
  122. )
  123. except Exception as e:
  124. pass
  125. return FileResponse(f"{STATIC_DIR}/favicon.png")
  126. else:
  127. return FileResponse(f"{STATIC_DIR}/favicon.png")
  128. ############################
  129. # ToggleModelById
  130. ############################
  131. @router.post("/model/toggle", response_model=Optional[ModelResponse])
  132. async def toggle_model_by_id(id: str, user=Depends(get_verified_user)):
  133. model = Models.get_model_by_id(id)
  134. if model:
  135. if (
  136. user.role == "admin"
  137. or model.user_id == user.id
  138. or has_access(user.id, "write", model.access_control)
  139. ):
  140. model = Models.toggle_model_by_id(id)
  141. if model:
  142. return model
  143. else:
  144. raise HTTPException(
  145. status_code=status.HTTP_400_BAD_REQUEST,
  146. detail=ERROR_MESSAGES.DEFAULT("Error updating function"),
  147. )
  148. else:
  149. raise HTTPException(
  150. status_code=status.HTTP_401_UNAUTHORIZED,
  151. detail=ERROR_MESSAGES.UNAUTHORIZED,
  152. )
  153. else:
  154. raise HTTPException(
  155. status_code=status.HTTP_401_UNAUTHORIZED,
  156. detail=ERROR_MESSAGES.NOT_FOUND,
  157. )
  158. ############################
  159. # UpdateModelById
  160. ############################
  161. @router.post("/model/update", response_model=Optional[ModelModel])
  162. async def update_model_by_id(
  163. id: str,
  164. form_data: ModelForm,
  165. user=Depends(get_verified_user),
  166. ):
  167. model = Models.get_model_by_id(id)
  168. if not model:
  169. raise HTTPException(
  170. status_code=status.HTTP_401_UNAUTHORIZED,
  171. detail=ERROR_MESSAGES.NOT_FOUND,
  172. )
  173. if (
  174. model.user_id != user.id
  175. and not has_access(user.id, "write", model.access_control)
  176. and user.role != "admin"
  177. ):
  178. raise HTTPException(
  179. status_code=status.HTTP_400_BAD_REQUEST,
  180. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  181. )
  182. model = Models.update_model_by_id(id, form_data)
  183. return model
  184. ############################
  185. # DeleteModelById
  186. ############################
  187. @router.delete("/model/delete", response_model=bool)
  188. async def delete_model_by_id(id: str, user=Depends(get_verified_user)):
  189. model = Models.get_model_by_id(id)
  190. if not model:
  191. raise HTTPException(
  192. status_code=status.HTTP_401_UNAUTHORIZED,
  193. detail=ERROR_MESSAGES.NOT_FOUND,
  194. )
  195. if (
  196. user.role != "admin"
  197. and model.user_id != user.id
  198. and not has_access(user.id, "write", model.access_control)
  199. ):
  200. raise HTTPException(
  201. status_code=status.HTTP_401_UNAUTHORIZED,
  202. detail=ERROR_MESSAGES.UNAUTHORIZED,
  203. )
  204. result = Models.delete_model_by_id(id)
  205. return result
  206. @router.delete("/delete/all", response_model=bool)
  207. async def delete_all_models(user=Depends(get_admin_user)):
  208. result = Models.delete_all_models()
  209. return result