models.py 4.8 KB

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