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

294 lines
5.4 KiB
TypeScript
Raw Normal View History

import { WEBUI_API_BASE_URL } from '$lib/constants';
2024-11-15 18:53:50 -08:00
export const getModels = async (token: string = '') => {
let error = null;
2024-11-19 16:50:45 -08:00
const res = await fetch(`${WEBUI_API_BASE_URL}/models/`, {
2024-11-15 02:05:43 -08:00
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
authorization: `Bearer ${token}`
2024-11-15 02:05:43 -08:00
}
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
2024-11-15 02:05:43 -08:00
.then((json) => {
return json;
})
.catch((err) => {
2024-11-15 02:05:43 -08:00
error = err;
console.error(err);
return null;
});
if (error) {
throw error;
}
return res;
};
export const importModels = async (token: string, models: object[]) => {
let error = null;
const res = await fetch(`${WEBUI_API_BASE_URL}/models/import`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
authorization: `Bearer ${token}`
},
body: JSON.stringify({ models: models })
})
.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-15 18:53:50 -08: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.error(err);
2024-11-15 18:53:50 -08:00
return null;
});
if (error) {
throw error;
}
return res;
};
2024-11-15 02:05:43 -08:00
export const createNewModel = async (token: string, model: object) => {
let error = null;
2024-11-15 02:05:43 -08: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 02:05:43 -08:00
},
body: JSON.stringify(model)
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.catch((err) => {
2024-11-15 02:05:43 -08:00
error = err.detail;
console.error(err);
return null;
});
if (error) {
throw error;
}
2024-05-24 00:26:00 -07:00
return res;
};
2024-05-24 00:26:00 -07:00
export const getModelById = async (token: string, id: string) => {
let error = null;
2024-05-25 14:24:00 -07:00
const searchParams = new URLSearchParams();
searchParams.append('id', id);
2024-05-25 13:48:45 -07:00
2024-11-19 18:45:26 -08:00
const res = await fetch(`${WEBUI_API_BASE_URL}/models/model?${searchParams.toString()}`, {
2024-05-24 00:26:00 -07:00
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
authorization: `Bearer ${token}`
2024-05-24 00:26:00 -07:00
}
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.then((json) => {
return json;
})
.catch((err) => {
error = err;
console.error(err);
return null;
});
if (error) {
throw error;
}
2024-05-24 00:26:00 -07:00
return res;
};
2024-11-15 18:21:41 -08:00
export const toggleModelById = async (token: string, id: string) => {
let error = null;
const searchParams = new URLSearchParams();
searchParams.append('id', id);
2024-11-19 18:45:26 -08:00
const res = await fetch(`${WEBUI_API_BASE_URL}/models/model/toggle?${searchParams.toString()}`, {
2024-11-15 18:21:41 -08: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;
console.error(err);
2024-11-15 18:21:41 -08:00
return null;
});
if (error) {
throw error;
}
return res;
};
2024-05-24 00:26:00 -07:00
export const updateModelById = async (token: string, id: string, model: object) => {
let error = null;
2024-05-25 14:24:00 -07:00
const searchParams = new URLSearchParams();
searchParams.append('id', id);
2024-05-25 13:48:45 -07:00
2024-11-19 18:45:26 -08:00
const res = await fetch(`${WEBUI_API_BASE_URL}/models/model/update?${searchParams.toString()}`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
authorization: `Bearer ${token}`
},
2024-05-24 00:26:00 -07: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.error(err);
return null;
});
if (error) {
throw error;
}
return res;
};
2024-05-24 00:26:00 -07:00
export const deleteModelById = async (token: string, id: string) => {
let error = null;
2024-05-25 14:24:00 -07:00
const searchParams = new URLSearchParams();
searchParams.append('id', id);
2024-05-25 13:48:45 -07:00
2024-11-19 18:45:26 -08:00
const res = await fetch(`${WEBUI_API_BASE_URL}/models/model/delete?${searchParams.toString()}`, {
method: 'DELETE',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
authorization: `Bearer ${token}`
2024-05-24 00:26:00 -07:00
}
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.then((json) => {
return json;
})
.catch((err) => {
2025-01-29 21:56:28 -08:00
error = err.detail;
console.error(err);
return null;
});
if (error) {
throw error;
}
return res;
};
2024-11-19 11:03:36 -08: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.error(err);
2024-11-19 11:03:36 -08:00
return null;
});
if (error) {
throw error;
}
return res;
2024-11-19 12:22:58 -08:00
};