2023-12-27 06:51:52 +00:00
|
|
|
import { WEBUI_API_BASE_URL } from '$lib/constants';
|
|
|
|
|
2024-05-24 07:26:00 +00:00
|
|
|
export const addNewModel = async (token: string, model: object) => {
|
2023-12-27 06:51:52 +00:00
|
|
|
let error = null;
|
|
|
|
|
2024-05-24 07:26:00 +00:00
|
|
|
const res = await fetch(`${WEBUI_API_BASE_URL}/models/add`, {
|
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();
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
2023-12-27 07:58:40 +00:00
|
|
|
error = err.detail;
|
2023-12-27 06:51:52 +00:00
|
|
|
console.log(err);
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
|
2024-05-24 09:17:48 +00:00
|
|
|
export const getModelInfos = async (token: string = '') => {
|
2023-12-27 06:51:52 +00:00
|
|
|
let error = null;
|
|
|
|
|
2024-05-25 21:24:00 +00:00
|
|
|
const res = await fetch(`${WEBUI_API_BASE_URL}/models`, {
|
2023-12-27 06:51:52 +00:00
|
|
|
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;
|
|
|
|
console.log(err);
|
|
|
|
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-05-25 21:24:00 +00:00
|
|
|
const res = await fetch(`${WEBUI_API_BASE_URL}/models?${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;
|
|
|
|
|
|
|
|
console.log(err);
|
|
|
|
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 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-05-25 21:24:00 +00:00
|
|
|
const res = await fetch(`${WEBUI_API_BASE_URL}/models/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;
|
|
|
|
|
|
|
|
console.log(err);
|
|
|
|
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-05-25 21:24:00 +00:00
|
|
|
const res = await fetch(`${WEBUI_API_BASE_URL}/models/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) => {
|
|
|
|
error = err;
|
|
|
|
|
|
|
|
console.log(err);
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
};
|