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,
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
from open_webui.utils.utils import get_admin_user, get_verified_user, has_access
|
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-08-14 12:46:31 +00:00
|
|
|
@router.get("/", response_model=list[ModelResponse])
|
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-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-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
|
|
|
|
###########################
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/id/{id}", response_model=Optional[ModelResponse])
|
|
|
|
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
|
|
|
|
############################
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/id/{id}/toggle", response_model=Optional[ModelResponse])
|
|
|
|
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-15 09:29:07 +00:00
|
|
|
@router.post("/id/{id}/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-15 09:29:07 +00:00
|
|
|
@router.delete("/id/{id}/delete", response_model=bool)
|
|
|
|
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
|