models.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. from typing import Optional
  2. from open_webui.models.models import (
  3. ModelForm,
  4. ModelModel,
  5. ModelResponse,
  6. ModelUserResponse,
  7. Models,
  8. )
  9. from pydantic import BaseModel
  10. from open_webui.constants import ERROR_MESSAGES
  11. from fastapi import APIRouter, Depends, HTTPException, Request, status
  12. from open_webui.utils.auth import get_admin_user, get_verified_user
  13. from open_webui.utils.access_control import has_access, has_permission
  14. router = APIRouter()
  15. ###########################
  16. # GetModels
  17. ###########################
  18. @router.get("/", response_model=list[ModelUserResponse])
  19. async def get_models(id: Optional[str] = None, user=Depends(get_verified_user)):
  20. if user.role == "admin":
  21. return Models.get_models()
  22. else:
  23. return Models.get_models_by_user_id(user.id)
  24. ###########################
  25. # GetBaseModels
  26. ###########################
  27. @router.get("/base", response_model=list[ModelResponse])
  28. async def get_base_models(user=Depends(get_admin_user)):
  29. return Models.get_base_models()
  30. ############################
  31. # CreateNewModel
  32. ############################
  33. @router.post("/create", response_model=Optional[ModelModel])
  34. async def create_new_model(
  35. request: Request,
  36. form_data: ModelForm,
  37. user=Depends(get_verified_user),
  38. ):
  39. if user.role != "admin" and not has_permission(
  40. user.id, "workspace.models", request.app.state.config.USER_PERMISSIONS
  41. ):
  42. raise HTTPException(
  43. status_code=status.HTTP_401_UNAUTHORIZED,
  44. detail=ERROR_MESSAGES.UNAUTHORIZED,
  45. )
  46. model = Models.get_model_by_id(form_data.id)
  47. if model:
  48. raise HTTPException(
  49. status_code=status.HTTP_401_UNAUTHORIZED,
  50. detail=ERROR_MESSAGES.MODEL_ID_TAKEN,
  51. )
  52. else:
  53. model = Models.insert_new_model(form_data, user.id)
  54. if model:
  55. return model
  56. else:
  57. raise HTTPException(
  58. status_code=status.HTTP_401_UNAUTHORIZED,
  59. detail=ERROR_MESSAGES.DEFAULT(),
  60. )
  61. ############################
  62. # ExportModels
  63. ############################
  64. @router.get("/export", response_model=list[ModelModel])
  65. async def export_models(user=Depends(get_admin_user)):
  66. return Models.get_models()
  67. ############################
  68. # SyncModels
  69. ############################
  70. class SyncModelsForm(BaseModel):
  71. models: list[ModelModel] = []
  72. @router.post("/sync", response_model=list[ModelModel])
  73. async def sync_models(
  74. request: Request, form_data: SyncModelsForm, user=Depends(get_admin_user)
  75. ):
  76. return Models.sync_models(user.id, form_data.models)
  77. ###########################
  78. # GetModelById
  79. ###########################
  80. # Note: We're not using the typical url path param here, but instead using a query parameter to allow '/' in the id
  81. @router.get("/model", response_model=Optional[ModelResponse])
  82. async def get_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, "read", model.access_control)
  89. ):
  90. return model
  91. else:
  92. raise HTTPException(
  93. status_code=status.HTTP_401_UNAUTHORIZED,
  94. detail=ERROR_MESSAGES.NOT_FOUND,
  95. )
  96. ############################
  97. # ToggleModelById
  98. ############################
  99. @router.post("/model/toggle", response_model=Optional[ModelResponse])
  100. async def toggle_model_by_id(id: str, user=Depends(get_verified_user)):
  101. model = Models.get_model_by_id(id)
  102. if model:
  103. if (
  104. user.role == "admin"
  105. or model.user_id == user.id
  106. or has_access(user.id, "write", model.access_control)
  107. ):
  108. model = Models.toggle_model_by_id(id)
  109. if model:
  110. return model
  111. else:
  112. raise HTTPException(
  113. status_code=status.HTTP_400_BAD_REQUEST,
  114. detail=ERROR_MESSAGES.DEFAULT("Error updating function"),
  115. )
  116. else:
  117. raise HTTPException(
  118. status_code=status.HTTP_401_UNAUTHORIZED,
  119. detail=ERROR_MESSAGES.UNAUTHORIZED,
  120. )
  121. else:
  122. raise HTTPException(
  123. status_code=status.HTTP_401_UNAUTHORIZED,
  124. detail=ERROR_MESSAGES.NOT_FOUND,
  125. )
  126. ############################
  127. # UpdateModelById
  128. ############################
  129. @router.post("/model/update", response_model=Optional[ModelModel])
  130. async def update_model_by_id(
  131. id: str,
  132. form_data: ModelForm,
  133. user=Depends(get_verified_user),
  134. ):
  135. model = Models.get_model_by_id(id)
  136. if not model:
  137. raise HTTPException(
  138. status_code=status.HTTP_401_UNAUTHORIZED,
  139. detail=ERROR_MESSAGES.NOT_FOUND,
  140. )
  141. if (
  142. model.user_id != user.id
  143. and not has_access(user.id, "write", model.access_control)
  144. and user.role != "admin"
  145. ):
  146. raise HTTPException(
  147. status_code=status.HTTP_400_BAD_REQUEST,
  148. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  149. )
  150. model = Models.update_model_by_id(id, form_data)
  151. return model
  152. ############################
  153. # DeleteModelById
  154. ############################
  155. @router.delete("/model/delete", response_model=bool)
  156. async def delete_model_by_id(id: str, user=Depends(get_verified_user)):
  157. model = Models.get_model_by_id(id)
  158. if not model:
  159. raise HTTPException(
  160. status_code=status.HTTP_401_UNAUTHORIZED,
  161. detail=ERROR_MESSAGES.NOT_FOUND,
  162. )
  163. if (
  164. user.role != "admin"
  165. and model.user_id != user.id
  166. and not has_access(user.id, "write", model.access_control)
  167. ):
  168. raise HTTPException(
  169. status_code=status.HTTP_401_UNAUTHORIZED,
  170. detail=ERROR_MESSAGES.UNAUTHORIZED,
  171. )
  172. result = Models.delete_model_by_id(id)
  173. return result
  174. @router.delete("/delete/all", response_model=bool)
  175. async def delete_all_models(user=Depends(get_admin_user)):
  176. result = Models.delete_all_models()
  177. return result