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

266 lines
4.8 KiB
TypeScript
Raw Normal View History

import { WEBUI_API_BASE_URL } from '$lib/constants';
2024-11-16 02:53:50 +00:00
export const getModels = async (token: string = '') => {
let error = null;
2024-11-15 10:05:43 +00:00
const res = await fetch(`${WEBUI_API_BASE_URL}/models`, {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
authorization: `Bearer ${token}`
2024-11-15 10:05:43 +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;
})
.catch((err) => {
2024-11-15 10:05:43 +00:00
error = err;
console.log(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;
console.log(err);
return null;
});
if (error) {
throw error;
}
return res;
};
2024-11-15 10:05:43 +00:00
export const createNewModel = async (token: string, model: object) => {
let error = null;
2024-11-15 10:05:43 +00:00
const res = await fetch(`${WEBUI_API_BASE_URL}/models/create`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
authorization: `Bearer ${token}`
2024-11-15 10:05:43 +00:00
},
body: JSON.stringify(model)
})
.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;
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-11-15 10:05:43 +00:00
const res = await fetch(`${WEBUI_API_BASE_URL}/models/id/${id}`, {
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-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);
const res = await fetch(`${WEBUI_API_BASE_URL}/models/id/${id}/toggle`, {
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;
console.log(err);
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) => {
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-15 10:05:43 +00:00
const res = await fetch(`${WEBUI_API_BASE_URL}/models/id/${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-25 21:24:00 +00:00
const searchParams = new URLSearchParams();
searchParams.append('id', id);
2024-05-25 20:48:45 +00:00
2024-11-15 10:05:43 +00:00
const res = await fetch(`${WEBUI_API_BASE_URL}/models/id/${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;
};
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;
console.log(err);
return null;
});
if (error) {
throw error;
}
return res;
};