2024-08-28 06:10:27 +08:00
|
|
|
from typing import Optional
|
2024-05-24 15:26:00 +08:00
|
|
|
|
2024-12-10 16:54:13 +08:00
|
|
|
from open_webui.models.models import (
|
2024-09-04 22:54:48 +08:00
|
|
|
ModelForm,
|
|
|
|
ModelModel,
|
|
|
|
ModelResponse,
|
2024-11-18 21:37:04 +08:00
|
|
|
ModelUserResponse,
|
2024-09-04 22:54:48 +08:00
|
|
|
Models,
|
|
|
|
)
|
2025-07-28 17:06:05 +08:00
|
|
|
|
|
|
|
from pydantic import BaseModel
|
2024-09-04 22:54:48 +08:00
|
|
|
from open_webui.constants import ERROR_MESSAGES
|
2024-08-28 06:10:27 +08:00
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Request, status
|
2024-11-15 17:29:07 +08:00
|
|
|
|
|
|
|
|
2024-12-09 08:01:56 +08:00
|
|
|
from open_webui.utils.auth import get_admin_user, get_verified_user
|
2024-11-17 19:04:31 +08:00
|
|
|
from open_webui.utils.access_control import has_access, has_permission
|
2025-08-06 05:44:52 +08:00
|
|
|
from open_webui.config import ENABLE_ADMIN_WORKSPACE_CONTENT_ACCESS
|
2024-05-24 15:26:00 +08:00
|
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
2024-11-15 17:29:07 +08:00
|
|
|
|
2024-05-24 15:26:00 +08:00
|
|
|
###########################
|
2024-11-15 17:29:07 +08:00
|
|
|
# GetModels
|
2024-05-24 15:26:00 +08:00
|
|
|
###########################
|
|
|
|
|
|
|
|
|
2024-11-18 21:37:04 +08:00
|
|
|
@router.get("/", response_model=list[ModelUserResponse])
|
2024-09-13 12:41:20 +08:00
|
|
|
async def get_models(id: Optional[str] = None, user=Depends(get_verified_user)):
|
2025-08-06 05:44:52 +08:00
|
|
|
if user.role == "admin" and ENABLE_ADMIN_WORKSPACE_CONTENT_ACCESS:
|
2024-11-15 17:29:07 +08:00
|
|
|
return Models.get_models()
|
2024-09-13 12:41:20 +08:00
|
|
|
else:
|
2024-11-15 17:29:07 +08:00
|
|
|
return Models.get_models_by_user_id(user.id)
|
2024-05-24 15:26:00 +08:00
|
|
|
|
|
|
|
|
2025-03-04 13:27:48 +08: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 15:26:00 +08:00
|
|
|
############################
|
2024-11-15 17:29:07 +08:00
|
|
|
# CreateNewModel
|
2024-05-24 15:26:00 +08:00
|
|
|
############################
|
|
|
|
|
|
|
|
|
2024-11-15 17:29:07 +08:00
|
|
|
@router.post("/create", response_model=Optional[ModelModel])
|
|
|
|
async def create_new_model(
|
2024-11-17 19:04:31 +08:00
|
|
|
request: Request,
|
2024-06-18 21:03:31 +08:00
|
|
|
form_data: ModelForm,
|
2024-11-15 17:29:07 +08:00
|
|
|
user=Depends(get_verified_user),
|
2024-05-25 13:21:57 +08:00
|
|
|
):
|
2024-11-17 19:04:31 +08: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 17:29:07 +08:00
|
|
|
|
|
|
|
model = Models.get_model_by_id(form_data.id)
|
|
|
|
if model:
|
2024-05-24 15:26:00 +08:00
|
|
|
raise HTTPException(
|
|
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
2024-05-25 13:21:57 +08:00
|
|
|
detail=ERROR_MESSAGES.MODEL_ID_TAKEN,
|
2024-05-24 15:26:00 +08:00
|
|
|
)
|
2024-11-15 17:29:07 +08:00
|
|
|
|
2024-05-25 13:21:57 +08:00
|
|
|
else:
|
2024-06-21 20:58:57 +08:00
|
|
|
model = Models.insert_new_model(form_data, user.id)
|
2024-05-25 13:21:57 +08:00
|
|
|
if model:
|
|
|
|
return model
|
|
|
|
else:
|
|
|
|
raise HTTPException(
|
|
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
|
detail=ERROR_MESSAGES.DEFAULT(),
|
|
|
|
)
|
2024-05-24 15:26:00 +08:00
|
|
|
|
|
|
|
|
2025-07-28 17:12:38 +08:00
|
|
|
############################
|
|
|
|
# ExportModels
|
|
|
|
############################
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/export", response_model=list[ModelModel])
|
|
|
|
async def export_models(user=Depends(get_admin_user)):
|
|
|
|
return Models.get_models()
|
|
|
|
|
|
|
|
|
2025-07-28 17:06:05 +08:00
|
|
|
############################
|
|
|
|
# SyncModels
|
|
|
|
############################
|
|
|
|
|
|
|
|
|
|
|
|
class SyncModelsForm(BaseModel):
|
|
|
|
models: list[ModelModel] = []
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/sync", response_model=list[ModelModel])
|
|
|
|
async def sync_models(
|
|
|
|
request: Request, form_data: SyncModelsForm, user=Depends(get_admin_user)
|
|
|
|
):
|
|
|
|
return Models.sync_models(user.id, form_data.models)
|
|
|
|
|
|
|
|
|
2024-11-15 17:29:07 +08:00
|
|
|
###########################
|
|
|
|
# GetModelById
|
|
|
|
###########################
|
|
|
|
|
|
|
|
|
2024-11-20 10:45:26 +08: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 17:29:07 +08:00
|
|
|
async def get_model_by_id(id: str, user=Depends(get_verified_user)):
|
|
|
|
model = Models.get_model_by_id(id)
|
|
|
|
if model:
|
|
|
|
if (
|
2025-08-12 05:23:44 +08:00
|
|
|
(user.role == "admin" and ENABLE_ADMIN_WORKSPACE_CONTENT_ACCESS)
|
2024-11-15 17:29:07 +08:00
|
|
|
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 10:21:41 +08:00
|
|
|
############################
|
2025-07-28 17:12:38 +08:00
|
|
|
# ToggleModelById
|
2024-11-16 10:21:41 +08:00
|
|
|
############################
|
|
|
|
|
|
|
|
|
2024-11-20 10:45:26 +08:00
|
|
|
@router.post("/model/toggle", response_model=Optional[ModelResponse])
|
2024-11-16 10:21:41 +08: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 15:26:00 +08:00
|
|
|
############################
|
|
|
|
# UpdateModelById
|
|
|
|
############################
|
|
|
|
|
|
|
|
|
2024-11-20 10:45:26 +08:00
|
|
|
@router.post("/model/update", response_model=Optional[ModelModel])
|
2024-05-24 15:26:00 +08:00
|
|
|
async def update_model_by_id(
|
2024-06-18 21:03:31 +08:00
|
|
|
id: str,
|
|
|
|
form_data: ModelForm,
|
2024-11-15 17:29:07 +08:00
|
|
|
user=Depends(get_verified_user),
|
2024-05-24 15:26:00 +08:00
|
|
|
):
|
2024-06-21 20:58:57 +08:00
|
|
|
model = Models.get_model_by_id(id)
|
2024-11-15 17:29:07 +08:00
|
|
|
|
|
|
|
if not model:
|
|
|
|
raise HTTPException(
|
|
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
|
detail=ERROR_MESSAGES.NOT_FOUND,
|
|
|
|
)
|
|
|
|
|
2025-01-24 02:40:49 +08:00
|
|
|
if (
|
|
|
|
model.user_id != user.id
|
|
|
|
and not has_access(user.id, "write", model.access_control)
|
|
|
|
and user.role != "admin"
|
|
|
|
):
|
|
|
|
raise HTTPException(
|
|
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
|
|
detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
|
|
|
|
)
|
|
|
|
|
2024-11-15 17:29:07 +08:00
|
|
|
model = Models.update_model_by_id(id, form_data)
|
|
|
|
return model
|
2024-05-24 15:26:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
############################
|
|
|
|
# DeleteModelById
|
|
|
|
############################
|
|
|
|
|
|
|
|
|
2024-11-20 10:45:26 +08:00
|
|
|
@router.delete("/model/delete", response_model=bool)
|
2024-11-15 17:29:07 +08: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,
|
|
|
|
)
|
|
|
|
|
2025-01-28 02:11:52 +08:00
|
|
|
if (
|
2025-01-30 13:31:18 +08:00
|
|
|
user.role != "admin"
|
2025-01-30 13:32:07 +08:00
|
|
|
and model.user_id != user.id
|
|
|
|
and not has_access(user.id, "write", model.access_control)
|
2025-01-30 13:31:18 +08:00
|
|
|
):
|
2024-11-15 17:29:07 +08:00
|
|
|
raise HTTPException(
|
|
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
|
detail=ERROR_MESSAGES.UNAUTHORIZED,
|
|
|
|
)
|
|
|
|
|
2024-06-21 20:58:57 +08:00
|
|
|
result = Models.delete_model_by_id(id)
|
2024-05-24 15:26:00 +08:00
|
|
|
return result
|
2024-11-20 03:03:36 +08: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
|