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

168 lines
3.1 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 09:17:48 +00:00
export const getModelInfos = async (token: string = '') => {
let error = null;
2024-05-25 21:24: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-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',
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-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()}`, {
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-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()}`, {
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;
};