open-webui/src/lib/apis/models/index.ts

159 lines
2.8 KiB
TypeScript
Raw Normal View History

import { WEBUI_API_BASE_URL } from '$lib/constants';
2024-05-24 07:26:00 +00:00
export const addNewModel = async (token: string, model: object) => {
let error = null;
2024-05-24 07:26:00 +00:00
const res = await fetch(`${WEBUI_API_BASE_URL}/models/add`, {
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)
})
.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;
console.log(err);
return null;
});
if (error) {
throw error;
}
return res;
};
2024-05-24 07:26:00 +00:00
export const getModels = async (token: string = '') => {
let error = null;
2024-05-24 07:26:00 +00:00
const res = await fetch(`${WEBUI_API_BASE_URL}/models/`, {
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;
};
2024-05-24 07:26:00 +00:00
export const getModelById = async (token: string, id: string) => {
let error = null;
2024-05-24 07:26:00 +00:00
const res = await fetch(`${WEBUI_API_BASE_URL}/models/${id}`, {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
authorization: `Bearer ${token}`
2024-05-24 07:26:00 +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;
};
2024-05-24 07:26:00 +00:00
export const updateModelById = async (token: string, id: string, model: object) => {
let error = null;
2024-05-24 07:26:00 +00:00
const res = await fetch(`${WEBUI_API_BASE_URL}/models/${id}/update`, {
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)
})
.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) => {
let error = null;
2024-05-24 07:26:00 +00:00
const res = await fetch(`${WEBUI_API_BASE_URL}/models/${id}/delete`, {
method: 'DELETE',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
authorization: `Bearer ${token}`
2024-05-24 07:26:00 +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;
};