refac: models endpoint

This commit is contained in:
Timothy Jaeryang Baek 2025-11-19 03:18:16 -05:00
parent 0c47cbd16a
commit af1db82c7d
2 changed files with 18 additions and 21 deletions

View file

@ -35,7 +35,7 @@ log = logging.getLogger(__name__)
router = APIRouter()
def validate_model_id(model_id: str) -> bool:
def is_valid_model_id(model_id: str) -> bool:
return model_id and len(model_id) <= 256
@ -90,7 +90,7 @@ async def create_new_model(
detail=ERROR_MESSAGES.MODEL_ID_TAKEN,
)
if not validate_model_id(form_data.id):
if not is_valid_model_id(form_data.id):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ERROR_MESSAGES.MODEL_ID_TOO_LONG,
@ -157,7 +157,7 @@ async def import_models(
# Here, you can add logic to validate model_data if needed
model_id = model_data.get("id")
if model_id and validate_model_id(model_id):
if model_id and is_valid_model_id(model_id):
existing_model = Models.get_model_by_id(model_id)
if existing_model:
# Update existing model
@ -203,6 +203,10 @@ async def sync_models(
###########################
class ModelIdForm(BaseModel):
id: str
# 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])
async def get_model_by_id(id: str, user=Depends(get_verified_user)):
@ -296,12 +300,10 @@ async def toggle_model_by_id(id: str, user=Depends(get_verified_user)):
@router.post("/model/update", response_model=Optional[ModelModel])
async def update_model_by_id(
id: str,
form_data: ModelForm,
user=Depends(get_verified_user),
):
model = Models.get_model_by_id(id)
model = Models.get_model_by_id(form_data.id)
if not model:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
@ -327,9 +329,9 @@ async def update_model_by_id(
############################
@router.delete("/model/delete", response_model=bool)
async def delete_model_by_id(id: str, user=Depends(get_verified_user)):
model = Models.get_model_by_id(id)
@router.post("/model/delete", response_model=bool)
async def delete_model_by_id(form_data: ModelIdForm, user=Depends(get_verified_user)):
model = Models.get_model_by_id(form_data.id)
if not model:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
@ -346,7 +348,7 @@ async def delete_model_by_id(id: str, user=Depends(get_verified_user)):
detail=ERROR_MESSAGES.UNAUTHORIZED,
)
result = Models.delete_model_by_id(id)
result = Models.delete_model_by_id(form_data.id)
return result

View file

@ -192,17 +192,14 @@ export const toggleModelById = async (token: string, id: string) => {
export const updateModelById = async (token: string, id: string, model: object) => {
let error = null;
const searchParams = new URLSearchParams();
searchParams.append('id', id);
const res = await fetch(`${WEBUI_API_BASE_URL}/models/model/update?${searchParams.toString()}`, {
const res = await fetch(`${WEBUI_API_BASE_URL}/models/model/update`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
authorization: `Bearer ${token}`
},
body: JSON.stringify(model)
body: JSON.stringify({ ...model, id })
})
.then(async (res) => {
if (!res.ok) throw await res.json();
@ -228,16 +225,14 @@ export const updateModelById = async (token: string, id: string, model: object)
export const deleteModelById = async (token: string, id: string) => {
let error = null;
const searchParams = new URLSearchParams();
searchParams.append('id', id);
const res = await fetch(`${WEBUI_API_BASE_URL}/models/model/delete?${searchParams.toString()}`, {
method: 'DELETE',
const res = await fetch(`${WEBUI_API_BASE_URL}/models/model/delete`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
authorization: `Bearer ${token}`
}
},
body: JSON.stringify({ id })
})
.then(async (res) => {
if (!res.ok) throw await res.json();