chore: handle GGUFv1 error

This commit is contained in:
Louis 2023-11-14 19:38:08 +07:00
parent 7c2c1a2b3d
commit d7efd9813a
2 changed files with 47 additions and 2 deletions

View File

@ -38,7 +38,8 @@ function initModel(modelFile: string): Promise<InitModelResponse> {
return ( return (
// 1. Check if the port is used, if used, attempt to unload model / kill nitro process // 1. Check if the port is used, if used, attempt to unload model / kill nitro process
checkAndUnloadNitro() validateModelVersion()
.then(checkAndUnloadNitro)
// 2. Spawn the Nitro subprocess // 2. Spawn the Nitro subprocess
.then(spawnNitroProcess) .then(spawnNitroProcess)
// 3. Wait until the port is used (Nitro http server is up) // 3. Wait until the port is used (Nitro http server is up)
@ -47,6 +48,9 @@ function initModel(modelFile: string): Promise<InitModelResponse> {
.then(loadLLMModel) .then(loadLLMModel)
// 5. Check if the model is loaded successfully // 5. Check if the model is loaded successfully
.then(validateModelStatus) .then(validateModelStatus)
.catch((err) => {
return { error: err };
})
); );
} }
@ -71,6 +75,10 @@ function loadLLMModel(): Promise<Response> {
body: JSON.stringify(config), body: JSON.stringify(config),
retries: 3, retries: 3,
retryDelay: 500, retryDelay: 500,
}).catch((err) => {
console.error(err);
// Fetch error, Nitro server might not started properly
throw new Error("Model loading failed.");
}); });
} }
@ -193,6 +201,44 @@ function spawnNitroProcess() {
}); });
} }
/**
* Validate the model version, if it is GGUFv1, reject the promise
* @returns A Promise that resolves when the model is loaded successfully, or rejects with an error message if the model is not found or fails to load.
*/
function validateModelVersion(): Promise<void> {
// Read the file
return new Promise((resolve, reject) => {
fs.open(currentModelFile, "r", (err, fd) => {
if (err) {
console.error(err.message);
return;
}
// Buffer to store the byte
const buffer = Buffer.alloc(1);
// Model version will be the 5th byte of the file
fs.read(fd, buffer, 0, 1, 4, (err, bytesRead, buffer) => {
if (err) {
console.error(err.message);
} else {
// Interpret the byte as ASCII
if (buffer[0] === 0x01) {
// This is GGUFv1, which is deprecated
reject("GGUFv1 model is deprecated, please try another model.");
}
}
// Close the file descriptor
fs.close(fd, (err) => {
if (err) console.error(err.message);
});
resolve();
});
});
});
}
/** /**
* Cleans up any registered resources. * Cleans up any registered resources.
* Its module specific function, should be called when application is closed * Its module specific function, should be called when application is closed

View File

@ -46,7 +46,6 @@ export function useActiveModel() {
const res = await initModel(`models/${modelId}`) const res = await initModel(`models/${modelId}`)
if (res?.error) { if (res?.error) {
const errorMessage = `${res.error}` const errorMessage = `${res.error}`
console.error(errorMessage)
alert(errorMessage) alert(errorMessage)
setStateModel(() => ({ setStateModel(() => ({
state: 'start', state: 'start',