models.py 4.2 KB

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