mirror of
https://github.com/open-webui/open-webui.git
synced 2025-12-11 20:05:19 +00:00
This refactors the model import functionality to improve performance and user experience by centralizing the logic on the backend. Previously, the frontend would parse an imported JSON file and send an individual API request for each model, which was slow and inefficient. This change introduces a new backend endpoint, `/api/v1/models/import`, that accepts a list of model objects. The frontend now reads the selected JSON file, parses it, and sends the entire payload to the backend in a single request. The backend then processes this list, creating or updating models as necessary. This commit also includes the following fixes: - Handles cases where the imported JSON contains models without `meta` or `params` fields by providing default empty values.
293 lines
5.4 KiB
TypeScript
293 lines
5.4 KiB
TypeScript
import { WEBUI_API_BASE_URL } from '$lib/constants';
|
|
|
|
export const getModels = async (token: string = '') => {
|
|
let error = null;
|
|
|
|
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.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;
|
|
};
|
|
|
|
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);
|
|
return null;
|
|
});
|
|
|
|
if (error) {
|
|
throw error;
|
|
}
|
|
|
|
return res;
|
|
};
|
|
|
|
export const createNewModel = async (token: string, model: object) => {
|
|
let error = null;
|
|
|
|
const res = await fetch(`${WEBUI_API_BASE_URL}/models/create`, {
|
|
method: 'POST',
|
|
headers: {
|
|
Accept: 'application/json',
|
|
'Content-Type': 'application/json',
|
|
authorization: `Bearer ${token}`
|
|
},
|
|
body: JSON.stringify(model)
|
|
})
|
|
.then(async (res) => {
|
|
if (!res.ok) throw await res.json();
|
|
return res.json();
|
|
})
|
|
.catch((err) => {
|
|
error = err.detail;
|
|
console.error(err);
|
|
return null;
|
|
});
|
|
|
|
if (error) {
|
|
throw error;
|
|
}
|
|
|
|
return res;
|
|
};
|
|
|
|
export const getModelById = 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/model?${searchParams.toString()}`, {
|
|
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);
|
|
return null;
|
|
});
|
|
|
|
if (error) {
|
|
throw error;
|
|
}
|
|
|
|
return res;
|
|
};
|
|
|
|
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/model/toggle?${searchParams.toString()}`, {
|
|
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);
|
|
return null;
|
|
});
|
|
|
|
if (error) {
|
|
throw error;
|
|
}
|
|
|
|
return res;
|
|
};
|
|
|
|
export const updateModelById = async (token: string, id: string, model: object) => {
|
|
let error = null;
|
|
|
|
const searchParams = new URLSearchParams();
|
|
searchParams.append('id', id);
|
|
|
|
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}`
|
|
},
|
|
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;
|
|
};
|
|
|
|
export const deleteModelById = 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/model/delete?${searchParams.toString()}`, {
|
|
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.detail;
|
|
|
|
console.error(err);
|
|
return null;
|
|
});
|
|
|
|
if (error) {
|
|
throw error;
|
|
}
|
|
|
|
return res;
|
|
};
|
|
|
|
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);
|
|
return null;
|
|
});
|
|
|
|
if (error) {
|
|
throw error;
|
|
}
|
|
|
|
return res;
|
|
};
|