2023-12-27 06:51:52 +00:00
|
|
|
import { WEBUI_API_BASE_URL } from '$lib/constants';
|
|
|
|
|
|
2024-11-16 02:53:50 +00:00
|
|
|
export const getModels = async (token: string = '') => {
|
2023-12-27 06:51:52 +00:00
|
|
|
let error = null;
|
|
|
|
|
|
2024-11-20 00:50:45 +00:00
|
|
|
const res = await fetch(`${WEBUI_API_BASE_URL}/models/`, {
|
2024-11-15 10:05:43 +00:00
|
|
|
method: 'GET',
|
2023-12-27 06:51:52 +00:00
|
|
|
headers: {
|
|
|
|
|
Accept: 'application/json',
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
authorization: `Bearer ${token}`
|
2024-11-15 10:05:43 +00:00
|
|
|
}
|
2023-12-27 06:51:52 +00:00
|
|
|
})
|
|
|
|
|
.then(async (res) => {
|
|
|
|
|
if (!res.ok) throw await res.json();
|
|
|
|
|
return res.json();
|
|
|
|
|
})
|
2024-11-15 10:05:43 +00:00
|
|
|
.then((json) => {
|
|
|
|
|
return json;
|
|
|
|
|
})
|
2023-12-27 06:51:52 +00:00
|
|
|
.catch((err) => {
|
2024-11-15 10:05:43 +00:00
|
|
|
error = err;
|
2025-05-15 19:35:07 +00:00
|
|
|
console.error(err);
|
2023-12-27 06:51:52 +00:00
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
};
|
|
|
|
|
|
2025-09-28 22:49:42 +00:00
|
|
|
export const importModels = async (token: string, models: object[]) => {
|
2025-09-28 22:09:58 +00:00
|
|
|
let error = null;
|
|
|
|
|
|
|
|
|
|
const res = await fetch(`${WEBUI_API_BASE_URL}/models/import`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
2025-09-28 22:49:42 +00:00
|
|
|
'Content-Type': 'application/json',
|
2025-09-28 22:09:58 +00:00
|
|
|
authorization: `Bearer ${token}`
|
|
|
|
|
},
|
2025-09-28 22:49:42 +00:00
|
|
|
body: JSON.stringify({ models: models })
|
2025-09-28 22:09:58 +00:00
|
|
|
})
|
|
|
|
|
.then(async (res) => {
|
|
|
|
|
if (!res.ok) throw await res.json();
|
|
|
|
|
return res.json();
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
error = err;
|
|
|
|
|
console.error(err);
|
|
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
};
|
|
|
|
|
|
2024-11-16 02:53:50 +00:00
|
|
|
export const getBaseModels = async (token: string = '') => {
|
|
|
|
|
let error = null;
|
|
|
|
|
|
|
|
|
|
const res = await fetch(`${WEBUI_API_BASE_URL}/models/base`, {
|
|
|
|
|
method: 'GET',
|
|
|
|
|
headers: {
|
|
|
|
|
Accept: 'application/json',
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
authorization: `Bearer ${token}`
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.then(async (res) => {
|
|
|
|
|
if (!res.ok) throw await res.json();
|
|
|
|
|
return res.json();
|
|
|
|
|
})
|
|
|
|
|
.then((json) => {
|
|
|
|
|
return json;
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
error = err;
|
2025-05-15 19:35:07 +00:00
|
|
|
console.error(err);
|
2024-11-16 02:53:50 +00:00
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
};
|
|
|
|
|
|
2024-11-15 10:05:43 +00:00
|
|
|
export const createNewModel = async (token: string, model: object) => {
|
2023-12-27 06:51:52 +00:00
|
|
|
let error = null;
|
|
|
|
|
|
2024-11-15 10:05:43 +00:00
|
|
|
const res = await fetch(`${WEBUI_API_BASE_URL}/models/create`, {
|
|
|
|
|
method: 'POST',
|
2023-12-27 06:51:52 +00:00
|
|
|
headers: {
|
|
|
|
|
Accept: 'application/json',
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
authorization: `Bearer ${token}`
|
2024-11-15 10:05:43 +00:00
|
|
|
},
|
|
|
|
|
body: JSON.stringify(model)
|
2023-12-27 06:51:52 +00:00
|
|
|
})
|
|
|
|
|
.then(async (res) => {
|
|
|
|
|
if (!res.ok) throw await res.json();
|
|
|
|
|
return res.json();
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
2024-11-15 10:05:43 +00:00
|
|
|
error = err.detail;
|
2025-05-15 19:35:07 +00:00
|
|
|
console.error(err);
|
2023-12-27 06:51:52 +00:00
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-24 07:26:00 +00:00
|
|
|
return res;
|
2023-12-27 06:51:52 +00:00
|
|
|
};
|
|
|
|
|
|
2024-05-24 07:26:00 +00:00
|
|
|
export const getModelById = async (token: string, id: string) => {
|
2023-12-27 06:51:52 +00:00
|
|
|
let error = null;
|
|
|
|
|
|
2024-05-25 21:24:00 +00:00
|
|
|
const searchParams = new URLSearchParams();
|
|
|
|
|
searchParams.append('id', id);
|
2024-05-25 20:48:45 +00:00
|
|
|
|
2024-11-20 02:45:26 +00:00
|
|
|
const res = await fetch(`${WEBUI_API_BASE_URL}/models/model?${searchParams.toString()}`, {
|
2024-05-24 07:26:00 +00:00
|
|
|
method: 'GET',
|
2023-12-27 06:51:52 +00:00
|
|
|
headers: {
|
|
|
|
|
Accept: 'application/json',
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
authorization: `Bearer ${token}`
|
2024-05-24 07:26:00 +00:00
|
|
|
}
|
2023-12-27 06:51:52 +00:00
|
|
|
})
|
|
|
|
|
.then(async (res) => {
|
|
|
|
|
if (!res.ok) throw await res.json();
|
|
|
|
|
return res.json();
|
|
|
|
|
})
|
|
|
|
|
.then((json) => {
|
|
|
|
|
return json;
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
error = err;
|
|
|
|
|
|
2025-05-15 19:35:07 +00:00
|
|
|
console.error(err);
|
2023-12-27 06:51:52 +00:00
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-24 07:26:00 +00:00
|
|
|
return res;
|
2023-12-27 06:51:52 +00:00
|
|
|
};
|
|
|
|
|
|
2024-11-16 02:21:41 +00:00
|
|
|
export const toggleModelById = async (token: string, id: string) => {
|
|
|
|
|
let error = null;
|
|
|
|
|
|
|
|
|
|
const searchParams = new URLSearchParams();
|
|
|
|
|
searchParams.append('id', id);
|
|
|
|
|
|
2024-11-20 02:45:26 +00:00
|
|
|
const res = await fetch(`${WEBUI_API_BASE_URL}/models/model/toggle?${searchParams.toString()}`, {
|
2024-11-16 02:21:41 +00:00
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
Accept: 'application/json',
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
authorization: `Bearer ${token}`
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.then(async (res) => {
|
|
|
|
|
if (!res.ok) throw await res.json();
|
|
|
|
|
return res.json();
|
|
|
|
|
})
|
|
|
|
|
.then((json) => {
|
|
|
|
|
return json;
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
error = err;
|
|
|
|
|
|
2025-05-15 19:35:07 +00:00
|
|
|
console.error(err);
|
2024-11-16 02:21:41 +00:00
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
};
|
|
|
|
|
|
2024-05-24 07:26:00 +00:00
|
|
|
export const updateModelById = async (token: string, id: string, model: object) => {
|
2023-12-27 06:51:52 +00:00
|
|
|
let error = null;
|
|
|
|
|
|
2024-05-25 21:24:00 +00:00
|
|
|
const searchParams = new URLSearchParams();
|
|
|
|
|
searchParams.append('id', id);
|
2024-05-25 20:48:45 +00:00
|
|
|
|
2024-11-20 02:45:26 +00:00
|
|
|
const res = await fetch(`${WEBUI_API_BASE_URL}/models/model/update?${searchParams.toString()}`, {
|
2023-12-27 06:51:52 +00:00
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
Accept: 'application/json',
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
authorization: `Bearer ${token}`
|
|
|
|
|
},
|
2024-05-24 07:26:00 +00:00
|
|
|
body: JSON.stringify(model)
|
2023-12-27 06:51:52 +00:00
|
|
|
})
|
|
|
|
|
.then(async (res) => {
|
|
|
|
|
if (!res.ok) throw await res.json();
|
|
|
|
|
return res.json();
|
|
|
|
|
})
|
|
|
|
|
.then((json) => {
|
|
|
|
|
return json;
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
error = err;
|
|
|
|
|
|
2025-05-15 19:35:07 +00:00
|
|
|
console.error(err);
|
2023-12-27 06:51:52 +00:00
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
};
|
|
|
|
|
|
2024-05-24 07:26:00 +00:00
|
|
|
export const deleteModelById = async (token: string, id: string) => {
|
2023-12-27 06:51:52 +00:00
|
|
|
let error = null;
|
|
|
|
|
|
2024-05-25 21:24:00 +00:00
|
|
|
const searchParams = new URLSearchParams();
|
|
|
|
|
searchParams.append('id', id);
|
2024-05-25 20:48:45 +00:00
|
|
|
|
2024-11-20 02:45:26 +00:00
|
|
|
const res = await fetch(`${WEBUI_API_BASE_URL}/models/model/delete?${searchParams.toString()}`, {
|
2023-12-27 06:51:52 +00:00
|
|
|
method: 'DELETE',
|
|
|
|
|
headers: {
|
|
|
|
|
Accept: 'application/json',
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
authorization: `Bearer ${token}`
|
2024-05-24 07:26:00 +00:00
|
|
|
}
|
2023-12-27 06:51:52 +00:00
|
|
|
})
|
|
|
|
|
.then(async (res) => {
|
|
|
|
|
if (!res.ok) throw await res.json();
|
|
|
|
|
return res.json();
|
|
|
|
|
})
|
|
|
|
|
.then((json) => {
|
|
|
|
|
return json;
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
2025-01-30 05:56:28 +00:00
|
|
|
error = err.detail;
|
2023-12-27 06:51:52 +00:00
|
|
|
|
2025-05-15 19:35:07 +00:00
|
|
|
console.error(err);
|
2023-12-27 06:51:52 +00:00
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
};
|
2024-11-19 19:03:36 +00:00
|
|
|
|
|
|
|
|
export const deleteAllModels = async (token: string) => {
|
|
|
|
|
let error = null;
|
|
|
|
|
|
|
|
|
|
const res = await fetch(`${WEBUI_API_BASE_URL}/models/delete/all`, {
|
|
|
|
|
method: 'DELETE',
|
|
|
|
|
headers: {
|
|
|
|
|
Accept: 'application/json',
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
authorization: `Bearer ${token}`
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.then(async (res) => {
|
|
|
|
|
if (!res.ok) throw await res.json();
|
|
|
|
|
return res.json();
|
|
|
|
|
})
|
|
|
|
|
.then((json) => {
|
|
|
|
|
return json;
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
error = err;
|
|
|
|
|
|
2025-05-15 19:35:07 +00:00
|
|
|
console.error(err);
|
2024-11-19 19:03:36 +00:00
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res;
|
2024-11-19 20:22:58 +00:00
|
|
|
};
|