models.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. from typing import Optional
  2. from open_webui.apps.webui.models.models import (
  3. ModelForm,
  4. ModelModel,
  5. ModelResponse,
  6. ModelUserResponse,
  7. Models,
  8. )
  9. from open_webui.constants import ERROR_MESSAGES
  10. from fastapi import APIRouter, Depends, HTTPException, Request, status
  11. from open_webui.utils.utils import get_admin_user, get_verified_user
  12. from open_webui.utils.access_control import has_access, has_permission
  13. router = APIRouter()
  14. ###########################
  15. # GetModels
  16. ###########################
  17. @router.get("/", response_model=list[ModelUserResponse])
  18. async def get_models(id: Optional[str] = None, user=Depends(get_verified_user)):
  19. if user.role == "admin":
  20. return Models.get_models()
  21. else:
  22. return Models.get_models_by_user_id(user.id)
  23. ###########################
  24. # GetBaseModels
  25. ###########################
  26. @router.get("/base", response_model=list[ModelResponse])
  27. async def get_base_models(user=Depends(get_admin_user)):
  28. return Models.get_base_models()
  29. ############################
  30. # CreateNewModel
  31. ############################
  32. @router.post("/create", response_model=Optional[ModelModel])
  33. async def create_new_model(
  34. request: Request,
  35. form_data: ModelForm,
  36. user=Depends(get_verified_user),
  37. ):
  38. if user.role != "admin" and not has_permission(
  39. user.id, "workspace.models", request.app.state.config.USER_PERMISSIONS
  40. ):
  41. raise HTTPException(
  42. status_code=status.HTTP_401_UNAUTHORIZED,
  43. detail=ERROR_MESSAGES.UNAUTHORIZED,
  44. )
  45. model = Models.get_model_by_id(form_data.id)
  46. if model:
  47. raise HTTPException(
  48. status_code=status.HTTP_401_UNAUTHORIZED,
  49. detail=ERROR_MESSAGES.MODEL_ID_TAKEN,
  50. )
  51. else:
  52. model = Models.insert_new_model(form_data, user.id)
  53. if model:
  54. return model
  55. else:
  56. raise HTTPException(
  57. status_code=status.HTTP_401_UNAUTHORIZED,
  58. detail=ERROR_MESSAGES.DEFAULT(),
  59. )
  60. ###########################
  61. # GetModelById
  62. ###########################
  63. @router.get("/id/{id}", response_model=Optional[ModelResponse])
  64. async def get_model_by_id(id: str, user=Depends(get_verified_user)):
  65. model = Models.get_model_by_id(id)
  66. if model:
  67. if (
  68. user.role == "admin"
  69. or model.user_id == user.id
  70. or has_access(user.id, "read", model.access_control)
  71. ):
  72. return model
  73. else:
  74. raise HTTPException(
  75. status_code=status.HTTP_401_UNAUTHORIZED,
  76. detail=ERROR_MESSAGES.NOT_FOUND,
  77. )
  78. ############################
  79. # ToggelModelById
  80. ############################
  81. @router.post("/id/{id}/toggle", response_model=Optional[ModelResponse])
  82. async def toggle_model_by_id(id: str, user=Depends(get_verified_user)):
  83. model = Models.get_model_by_id(id)
  84. if model:
  85. if (
  86. user.role == "admin"
  87. or model.user_id == user.id
  88. or has_access(user.id, "write", model.access_control)
  89. ):
  90. model = Models.toggle_model_by_id(id)
  91. if model:
  92. return model
  93. else:
  94. raise HTTPException(
  95. status_code=status.HTTP_400_BAD_REQUEST,
  96. detail=ERROR_MESSAGES.DEFAULT("Error updating function"),
  97. )
  98. else:
  99. raise HTTPException(
  100. status_code=status.HTTP_401_UNAUTHORIZED,
  101. detail=ERROR_MESSAGES.UNAUTHORIZED,
  102. )
  103. else:
  104. raise HTTPException(
  105. status_code=status.HTTP_401_UNAUTHORIZED,
  106. detail=ERROR_MESSAGES.NOT_FOUND,
  107. )
  108. ############################
  109. # UpdateModelById
  110. ############################
  111. @router.post("/id/{id}/update", response_model=Optional[ModelModel])
  112. async def update_model_by_id(
  113. id: str,
  114. form_data: ModelForm,
  115. user=Depends(get_verified_user),
  116. ):
  117. model = Models.get_model_by_id(id)
  118. if not model:
  119. raise HTTPException(
  120. status_code=status.HTTP_401_UNAUTHORIZED,
  121. detail=ERROR_MESSAGES.NOT_FOUND,
  122. )
  123. model = Models.update_model_by_id(id, form_data)
  124. return model
  125. ############################
  126. # DeleteModelById
  127. ############################
  128. @router.delete("/id/{id}/delete", response_model=bool)
  129. async def delete_model_by_id(id: str, user=Depends(get_verified_user)):
  130. model = Models.get_model_by_id(id)
  131. if not model:
  132. raise HTTPException(
  133. status_code=status.HTTP_401_UNAUTHORIZED,
  134. detail=ERROR_MESSAGES.NOT_FOUND,
  135. )
  136. if model.user_id != user.id and user.role != "admin":
  137. raise HTTPException(
  138. status_code=status.HTTP_401_UNAUTHORIZED,
  139. detail=ERROR_MESSAGES.UNAUTHORIZED,
  140. )
  141. result = Models.delete_model_by_id(id)
  142. return result