models.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. # UpdateModelById
  64. ############################
  65. @router.post("/id/{id}/update", response_model=Optional[ModelModel])
  66. async def update_model_by_id(
  67. id: str,
  68. form_data: ModelForm,
  69. user=Depends(get_verified_user),
  70. ):
  71. model = Models.get_model_by_id(id)
  72. if not model:
  73. raise HTTPException(
  74. status_code=status.HTTP_401_UNAUTHORIZED,
  75. detail=ERROR_MESSAGES.NOT_FOUND,
  76. )
  77. model = Models.update_model_by_id(id, form_data)
  78. return model
  79. ############################
  80. # DeleteModelById
  81. ############################
  82. @router.delete("/id/{id}/delete", response_model=bool)
  83. async def delete_model_by_id(id: str, user=Depends(get_verified_user)):
  84. model = Models.get_model_by_id(id)
  85. if not model:
  86. raise HTTPException(
  87. status_code=status.HTTP_401_UNAUTHORIZED,
  88. detail=ERROR_MESSAGES.NOT_FOUND,
  89. )
  90. if model.user_id != user.id and user.role != "admin":
  91. raise HTTPException(
  92. status_code=status.HTTP_401_UNAUTHORIZED,
  93. detail=ERROR_MESSAGES.UNAUTHORIZED,
  94. )
  95. result = Models.delete_model_by_id(id)
  96. return result