models.py 4.4 KB

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