forked from open-webui/open-webui
refac: ollama gguf upload
This commit is contained in:
parent
c54fb92263
commit
af4caec4f5
4 changed files with 268 additions and 176 deletions
|
@ -390,6 +390,71 @@ export const pullModel = async (token: string, tagName: string, urlIdx: string |
|
|||
return res;
|
||||
};
|
||||
|
||||
export const downloadModel = async (
|
||||
token: string,
|
||||
download_url: string,
|
||||
urlIdx: string | null = null
|
||||
) => {
|
||||
let error = null;
|
||||
|
||||
const res = await fetch(
|
||||
`${OLLAMA_API_BASE_URL}/models/download${urlIdx !== null ? `/${urlIdx}` : ''}`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`
|
||||
},
|
||||
body: JSON.stringify({
|
||||
url: download_url
|
||||
})
|
||||
}
|
||||
).catch((err) => {
|
||||
console.log(err);
|
||||
error = err;
|
||||
|
||||
if ('detail' in err) {
|
||||
error = err.detail;
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
if (error) {
|
||||
throw error;
|
||||
}
|
||||
return res;
|
||||
};
|
||||
|
||||
export const uploadModel = async (token: string, file: File, urlIdx: string | null = null) => {
|
||||
let error = null;
|
||||
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
|
||||
const res = await fetch(
|
||||
`${OLLAMA_API_BASE_URL}/models/upload${urlIdx !== null ? `/${urlIdx}` : ''}`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`
|
||||
},
|
||||
body: formData
|
||||
}
|
||||
).catch((err) => {
|
||||
console.log(err);
|
||||
error = err;
|
||||
|
||||
if ('detail' in err) {
|
||||
error = err.detail;
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
if (error) {
|
||||
throw error;
|
||||
}
|
||||
return res;
|
||||
};
|
||||
|
||||
// export const pullModel = async (token: string, tagName: string) => {
|
||||
// return await fetch(`${OLLAMA_API_BASE_URL}/pull`, {
|
||||
// method: 'POST',
|
||||
|
|
|
@ -5,9 +5,11 @@
|
|||
import {
|
||||
createModel,
|
||||
deleteModel,
|
||||
downloadModel,
|
||||
getOllamaUrls,
|
||||
getOllamaVersion,
|
||||
pullModel
|
||||
pullModel,
|
||||
uploadModel
|
||||
} from '$lib/apis/ollama';
|
||||
import { WEBUI_API_BASE_URL, WEBUI_BASE_URL } from '$lib/constants';
|
||||
import { WEBUI_NAME, models, user } from '$lib/stores';
|
||||
|
@ -60,7 +62,7 @@
|
|||
let pullProgress = null;
|
||||
|
||||
let modelUploadMode = 'file';
|
||||
let modelInputFile = '';
|
||||
let modelInputFile: File[] | null = null;
|
||||
let modelFileUrl = '';
|
||||
let modelFileContent = `TEMPLATE """{{ .System }}\nUSER: {{ .Prompt }}\nASSISTANT: """\nPARAMETER num_ctx 4096\nPARAMETER stop "</s>"\nPARAMETER stop "USER:"\nPARAMETER stop "ASSISTANT:"`;
|
||||
let modelFileDigest = '';
|
||||
|
@ -191,30 +193,23 @@
|
|||
let name = '';
|
||||
|
||||
if (modelUploadMode === 'file') {
|
||||
const file = modelInputFile[0];
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
const file = modelInputFile ? modelInputFile[0] : null;
|
||||
|
||||
fileResponse = await fetch(`${WEBUI_API_BASE_URL}/utils/upload`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
...($user && { Authorization: `Bearer ${localStorage.token}` })
|
||||
},
|
||||
body: formData
|
||||
}).catch((error) => {
|
||||
console.log(error);
|
||||
return null;
|
||||
});
|
||||
if (file) {
|
||||
fileResponse = uploadModel(localStorage.token, file, selectedOllamaUrlIdx).catch(
|
||||
(error) => {
|
||||
toast.error(error);
|
||||
return null;
|
||||
}
|
||||
);
|
||||
}
|
||||
} else {
|
||||
fileResponse = await fetch(`${WEBUI_API_BASE_URL}/utils/download?url=${modelFileUrl}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
...($user && { Authorization: `Bearer ${localStorage.token}` })
|
||||
fileResponse = downloadModel(localStorage.token, modelFileUrl, selectedOllamaUrlIdx).catch(
|
||||
(error) => {
|
||||
toast.error(error);
|
||||
return null;
|
||||
}
|
||||
}).catch((error) => {
|
||||
console.log(error);
|
||||
return null;
|
||||
});
|
||||
);
|
||||
}
|
||||
|
||||
if (fileResponse && fileResponse.ok) {
|
||||
|
@ -318,7 +313,8 @@
|
|||
}
|
||||
|
||||
modelFileUrl = '';
|
||||
modelInputFile = '';
|
||||
modelUploadInputElement.value = '';
|
||||
modelInputFile = null;
|
||||
modelTransferring = false;
|
||||
uploadProgress = null;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue