2024-08-27 22:10:27 +00:00
|
|
|
from typing import Optional
|
2024-05-24 07:26:00 +00:00
|
|
|
|
2024-09-04 14:54:48 +00:00
|
|
|
from open_webui.apps.webui.models.models import (
|
|
|
|
ModelForm,
|
|
|
|
ModelModel,
|
|
|
|
ModelResponse,
|
2024-11-18 13:37:04 +00:00
|
|
|
ModelUserResponse,
|
2024-09-04 14:54:48 +00:00
|
|
|
Models,
|
|
|
|
)
|
|
|
|
from open_webui.constants import ERROR_MESSAGES
|
2024-08-27 22:10:27 +00:00
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Request, status
|
2024-11-15 09:29:07 +00:00
|
|
|
|
|
|
|
|
2024-11-17 00:51:55 +00:00
|
|
|
from open_webui.utils.utils import get_admin_user, get_verified_user
|
2024-11-17 11:04:31 +00:00
|
|
|
from open_webui.utils.access_control import has_access, has_permission
|
2024-11-17 00:51:55 +00:00
|
|
|
|
2024-05-24 07:26:00 +00:00
|
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
2024-11-15 09:29:07 +00:00
|
|
|
|
2024-05-24 07:26:00 +00:00
|
|
|
###########################
|
2024-11-15 09:29:07 +00:00
|
|
|
# GetModels
|
2024-05-24 07:26:00 +00:00
|
|
|
###########################
|
|
|
|
|
|
|
|
|
2024-11-18 13:37:04 +00:00
|
|
|
@router.get("/", response_model=list[ModelUserResponse])
|
2024-09-13 04:41:20 +00:00
|
|
|
async def get_models(id: Optional[str] = None, user=Depends(get_verified_user)):
|
2024-11-15 09:29:07 +00:00
|
|
|
if user.role == "admin":
|
|
|
|
return Models.get_models()
|
2024-09-13 04:41:20 +00:00
|
|
|
else:
|
2024-11-15 09:29:07 +00:00
|
|
|
return Models.get_models_by_user_id(user.id)
|
2024-05-24 07:26:00 +00:00
|
|
|
|
|
|
|
|
2024-11-16 02:53:50 +00:00
|
|
|
###########################
|
|
|
|
# GetBaseModels
|
|
|
|
###########################
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/base", response_model=list[ModelResponse])
|
|
|
|
async def get_base_models(user=Depends(get_admin_user)):
|
|
|
|
return Models.get_base_models()
|
|
|
|
|
|
|
|
|
2024-05-24 07:26:00 +00:00
|
|
|
############################
|
2024-11-15 09:29:07 +00:00
|
|
|
# CreateNewModel
|
2024-05-24 07:26:00 +00:00
|
|
|
############################
|
|
|
|
|
|
|
|
|
2024-11-15 09:29:07 +00:00
|
|
|
@router.post("/create", response_model=Optional[ModelModel])
|
|
|
|
async def create_new_model(
|
2024-11-17 11:04:31 +00:00
|
|
|
request: Request,
|
2024-06-18 13:03:31 +00:00
|
|
|
form_data: ModelForm,
|
2024-11-15 09:29:07 +00:00
|
|
|
user=Depends(get_verified_user),
|
2024-05-25 05:21:57 +00:00
|
|
|
):
|
2024-11-17 11:04:31 +00:00
|
|
|
if user.role != "admin" and not has_permission(
|
|
|
|
user.id, "workspace.models", request.app.state.config.USER_PERMISSIONS
|
|
|
|
):
|
|
|
|
raise HTTPException(
|
|
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
|
detail=ERROR_MESSAGES.UNAUTHORIZED,
|
|
|
|
)
|
2024-11-15 09:29:07 +00:00
|
|
|
|
|
|
|
model = Models.get_model_by_id(form_data.id)
|
|
|
|
if model:
|
2024-05-24 07:26:00 +00:00
|
|
|
raise HTTPException(
|
|
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
2024-05-25 05:21:57 +00:00
|
|
|
detail=ERROR_MESSAGES.MODEL_ID_TAKEN,
|
2024-05-24 07:26:00 +00:00
|
|
|
)
|
2024-11-15 09:29:07 +00:00
|
|
|
|
2024-05-25 05:21:57 +00:00
|
|
|
else:
|
2024-06-21 12:58:57 +00:00
|
|
|
model = Models.insert_new_model(form_data, user.id)
|
2024-05-25 05:21:57 +00:00
|
|
|
if model:
|
|
|
|
return model
|
|
|
|
else:
|
|
|
|
raise HTTPException(
|
|
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
|
detail=ERROR_MESSAGES.DEFAULT(),
|
|
|
|
)
|
2024-05-24 07:26:00 +00:00
|
|
|
|
|
|
|
|
2024-11-15 09:29:07 +00:00
|
|
|
###########################
|
|
|
|
# GetModelById
|
|
|
|
###########################
|
|
|
|
|
|
|
|
|
2024-11-20 02:45:26 +00:00
|
|
|
# Note: We're not using the typical url path param here, but instead using a query parameter to allow '/' in the id
|
|
|
|
@router.get("/model", response_model=Optional[ModelResponse])
|
2024-11-15 09:29:07 +00:00
|
|
|
async def get_model_by_id(id: str, user=Depends(get_verified_user)):
|
|
|
|
model = Models.get_model_by_id(id)
|
|
|
|
if model:
|
|
|
|
if (
|
|
|
|
user.role == "admin"
|
|
|
|
or model.user_id == user.id
|
|
|
|
or has_access(user.id, "read", model.access_control)
|
|
|
|
):
|
|
|
|
return model
|
|
|
|
else:
|
|
|
|
raise HTTPException(
|
|
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
|
detail=ERROR_MESSAGES.NOT_FOUND,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-11-16 02:21:41 +00:00
|
|
|
############################
|
|
|
|
# ToggelModelById
|
|
|
|
############################
|
|
|
|
|
|
|
|
|
2024-11-20 02:45:26 +00:00
|
|
|
@router.post("/model/toggle", response_model=Optional[ModelResponse])
|
2024-11-16 02:21:41 +00:00
|
|
|
async def toggle_model_by_id(id: str, user=Depends(get_verified_user)):
|
|
|
|
model = Models.get_model_by_id(id)
|
|
|
|
if model:
|
|
|
|
if (
|
|
|
|
user.role == "admin"
|
|
|
|
or model.user_id == user.id
|
|
|
|
or has_access(user.id, "write", model.access_control)
|
|
|
|
):
|
|
|
|
model = Models.toggle_model_by_id(id)
|
|
|
|
|
|
|
|
if model:
|
|
|
|
return model
|
|
|
|
else:
|
|
|
|
raise HTTPException(
|
|
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
|
|
detail=ERROR_MESSAGES.DEFAULT("Error updating function"),
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
raise HTTPException(
|
|
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
|
detail=ERROR_MESSAGES.UNAUTHORIZED,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
raise HTTPException(
|
|
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
|
detail=ERROR_MESSAGES.NOT_FOUND,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-05-24 07:26:00 +00:00
|
|
|
############################
|
|
|
|
# UpdateModelById
|
|
|
|
############################
|
|
|
|
|
|
|
|
|
2024-11-20 02:45:26 +00:00
|
|
|
@router.post("/model/update", response_model=Optional[ModelModel])
|
2024-05-24 07:26:00 +00:00
|
|
|
async def update_model_by_id(
|
2024-06-18 13:03:31 +00:00
|
|
|
id: str,
|
|
|
|
form_data: ModelForm,
|
2024-11-15 09:29:07 +00:00
|
|
|
user=Depends(get_verified_user),
|
2024-05-24 07:26:00 +00:00
|
|
|
):
|
2024-06-21 12:58:57 +00:00
|
|
|
model = Models.get_model_by_id(id)
|
2024-11-15 09:29:07 +00:00
|
|
|
|
|
|
|
if not model:
|
|
|
|
raise HTTPException(
|
|
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
|
detail=ERROR_MESSAGES.NOT_FOUND,
|
|
|
|
)
|
|
|
|
|
|
|
|
model = Models.update_model_by_id(id, form_data)
|
|
|
|
return model
|
2024-05-24 07:26:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
############################
|
|
|
|
# DeleteModelById
|
|
|
|
############################
|
|
|
|
|
|
|
|
|
2024-11-20 02:45:26 +00:00
|
|
|
@router.delete("/model/delete", response_model=bool)
|
2024-11-15 09:29:07 +00:00
|
|
|
async def delete_model_by_id(id: str, user=Depends(get_verified_user)):
|
|
|
|
model = Models.get_model_by_id(id)
|
|
|
|
if not model:
|
|
|
|
raise HTTPException(
|
|
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
|
detail=ERROR_MESSAGES.NOT_FOUND,
|
|
|
|
)
|
|
|
|
|
2024-11-15 11:02:08 +00:00
|
|
|
if model.user_id != user.id and user.role != "admin":
|
2024-11-15 09:29:07 +00:00
|
|
|
raise HTTPException(
|
|
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
|
detail=ERROR_MESSAGES.UNAUTHORIZED,
|
|
|
|
)
|
|
|
|
|
2024-06-21 12:58:57 +00:00
|
|
|
result = Models.delete_model_by_id(id)
|
2024-05-24 07:26:00 +00:00
|
|
|
return result
|
2024-11-19 19:03:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
@router.delete("/delete/all", response_model=bool)
|
|
|
|
async def delete_all_models(user=Depends(get_admin_user)):
|
|
|
|
result = Models.delete_all_models()
|
|
|
|
return result
|