models.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. from typing import Optional
  2. from open_webui.models.models import (
  3. ModelForm,
  4. ModelModel,
  5. ModelResponse,
  6. ModelUserResponse,
  7. Models,
  8. )
  9. from pydantic import BaseModel
  10. from open_webui.constants import ERROR_MESSAGES
  11. from fastapi import APIRouter, Depends, HTTPException, Request, status
  12. from open_webui.utils.auth import get_admin_user, get_verified_user
  13. from open_webui.utils.access_control import has_access, has_permission
  14. from open_webui.config import ENABLE_ADMIN_WORKSPACE_CONTENT_ACCESS
  15. router = APIRouter()
  16. ###########################
  17. # GetModels
  18. ###########################
  19. @router.get("/", response_model=list[ModelUserResponse])
  20. async def get_models(id: Optional[str] = None, user=Depends(get_verified_user)):
  21. if user.role == "admin" and ENABLE_ADMIN_WORKSPACE_CONTENT_ACCESS:
  22. return Models.get_models()
  23. else:
  24. return Models.get_models_by_user_id(user.id)
  25. ###########################
  26. # GetBaseModels
  27. ###########################
  28. @router.get("/base", response_model=list[ModelResponse])
  29. async def get_base_models(user=Depends(get_admin_user)):
  30. return Models.get_base_models()
  31. ############################
  32. # CreateNewModel
  33. ############################
  34. @router.post("/create", response_model=Optional[ModelModel])
  35. async def create_new_model(
  36. request: Request,
  37. form_data: ModelForm,
  38. user=Depends(get_verified_user),
  39. ):
  40. if user.role != "admin" and not has_permission(
  41. user.id, "workspace.models", request.app.state.config.USER_PERMISSIONS
  42. ):
  43. raise HTTPException(
  44. status_code=status.HTTP_401_UNAUTHORIZED,
  45. detail=ERROR_MESSAGES.UNAUTHORIZED,
  46. )
  47. model = Models.get_model_by_id(form_data.id)
  48. if model:
  49. raise HTTPException(
  50. status_code=status.HTTP_401_UNAUTHORIZED,
  51. detail=ERROR_MESSAGES.MODEL_ID_TAKEN,
  52. )
  53. else:
  54. model = Models.insert_new_model(form_data, user.id)
  55. if model:
  56. return model
  57. else:
  58. raise HTTPException(
  59. status_code=status.HTTP_401_UNAUTHORIZED,
  60. detail=ERROR_MESSAGES.DEFAULT(),
  61. )
  62. ############################
  63. # ExportModels
  64. ############################
  65. @router.get("/export", response_model=list[ModelModel])
  66. async def export_models(user=Depends(get_admin_user)):
  67. return Models.get_models()
  68. ############################
  69. # SyncModels
  70. ############################
  71. class SyncModelsForm(BaseModel):
  72. models: list[ModelModel] = []
  73. @router.post("/sync", response_model=list[ModelModel])
  74. async def sync_models(
  75. request: Request, form_data: SyncModelsForm, user=Depends(get_admin_user)
  76. ):
  77. return Models.sync_models(user.id, form_data.models)
  78. ###########################
  79. # GetModelById
  80. ###########################
  81. # Note: We're not using the typical url path param here, but instead using a query parameter to allow '/' in the id
  82. @router.get("/model", response_model=Optional[ModelResponse])
  83. async def get_model_by_id(id: str, user=Depends(get_verified_user)):
  84. model = Models.get_model_by_id(id)
  85. if model:
  86. if (
  87. (user.role == "admin" and ENABLE_ADMIN_WORKSPACE_CONTENT_ACCESS)
  88. or model.user_id == user.id
  89. or has_access(user.id, "read", model.access_control)
  90. ):
  91. return model
  92. else:
  93. raise HTTPException(
  94. status_code=status.HTTP_401_UNAUTHORIZED,
  95. detail=ERROR_MESSAGES.NOT_FOUND,
  96. )
  97. ############################
  98. # ToggleModelById
  99. ############################
  100. @router.post("/model/toggle", response_model=Optional[ModelResponse])
  101. async def toggle_model_by_id(id: str, user=Depends(get_verified_user)):
  102. model = Models.get_model_by_id(id)
  103. if model:
  104. if (
  105. user.role == "admin"
  106. or model.user_id == user.id
  107. or has_access(user.id, "write", model.access_control)
  108. ):
  109. model = Models.toggle_model_by_id(id)
  110. if model:
  111. return model
  112. else:
  113. raise HTTPException(
  114. status_code=status.HTTP_400_BAD_REQUEST,
  115. detail=ERROR_MESSAGES.DEFAULT("Error updating function"),
  116. )
  117. else:
  118. raise HTTPException(
  119. status_code=status.HTTP_401_UNAUTHORIZED,
  120. detail=ERROR_MESSAGES.UNAUTHORIZED,
  121. )
  122. else:
  123. raise HTTPException(
  124. status_code=status.HTTP_401_UNAUTHORIZED,
  125. detail=ERROR_MESSAGES.NOT_FOUND,
  126. )
  127. ############################
  128. # UpdateModelById
  129. ############################
  130. @router.post("/model/update", response_model=Optional[ModelModel])
  131. async def update_model_by_id(
  132. id: str,
  133. form_data: ModelForm,
  134. user=Depends(get_verified_user),
  135. ):
  136. model = Models.get_model_by_id(id)
  137. if not model:
  138. raise HTTPException(
  139. status_code=status.HTTP_401_UNAUTHORIZED,
  140. detail=ERROR_MESSAGES.NOT_FOUND,
  141. )
  142. if (
  143. model.user_id != user.id
  144. and not has_access(user.id, "write", model.access_control)
  145. and user.role != "admin"
  146. ):
  147. raise HTTPException(
  148. status_code=status.HTTP_400_BAD_REQUEST,
  149. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  150. )
  151. model = Models.update_model_by_id(id, form_data)
  152. return model
  153. ############################
  154. # DeleteModelById
  155. ############################
  156. @router.delete("/model/delete", response_model=bool)
  157. async def delete_model_by_id(id: str, user=Depends(get_verified_user)):
  158. model = Models.get_model_by_id(id)
  159. if not model:
  160. raise HTTPException(
  161. status_code=status.HTTP_401_UNAUTHORIZED,
  162. detail=ERROR_MESSAGES.NOT_FOUND,
  163. )
  164. if (
  165. user.role != "admin"
  166. and model.user_id != user.id
  167. and not has_access(user.id, "write", model.access_control)
  168. ):
  169. raise HTTPException(
  170. status_code=status.HTTP_401_UNAUTHORIZED,
  171. detail=ERROR_MESSAGES.UNAUTHORIZED,
  172. )
  173. result = Models.delete_model_by_id(id)
  174. return result
  175. @router.delete("/delete/all", response_model=bool)
  176. async def delete_all_models(user=Depends(get_admin_user)):
  177. result = Models.delete_all_models()
  178. return result