better error handling

This commit is contained in:
Xuan Son Nguyen 2024-11-05 23:35:47 +01:00
parent 9096e5ed5e
commit 58d0588fbd
3 changed files with 36 additions and 1 deletions

View file

@ -5,6 +5,12 @@ const paramDefaults = {
let generation_settings = null; let generation_settings = null;
export class CompletionError extends Error {
constructor(message, name, data) {
super(message);
this.name = name;
}
};
// Completes the prompt as a generator. Recommended for most use cases. // Completes the prompt as a generator. Recommended for most use cases.
// //
@ -39,6 +45,18 @@ export async function* llama(prompt, params = {}, config = {}) {
signal: controller.signal, signal: controller.signal,
}); });
const status = response.status;
if (status !== 200) {
try {
const body = await response.json();
if (body && body.error && body.error.message) {
throw new CompletionError(body.error.message, 'ServerError');
}
} catch (err) {
throw new CompletionError(err.message, 'ServerError');
}
}
const reader = response.body.getReader(); const reader = response.body.getReader();
const decoder = new TextDecoder(); const decoder = new TextDecoder();

View file

@ -285,6 +285,14 @@
conv.lastModified = Date.now(); conv.lastModified = Date.now();
localStorage.setItem(convId, JSON.stringify(conv)); localStorage.setItem(convId, JSON.stringify(conv));
}, },
popMsg(convId) {
const conv = StorageUtils.getOneConversation(convId);
if (!conv) return;
const msg = conv.messages.pop();
conv.lastModified = Date.now();
localStorage.setItem(convId, JSON.stringify(conv));
return msg;
},
// manage config // manage config
getConfig() { getConfig() {
@ -425,6 +433,7 @@
top_p: this.config.top_p, top_p: this.config.top_p,
max_tokens: this.config.max_tokens, max_tokens: this.config.max_tokens,
...(this.config.custom.length ? JSON.parse(this.config.custom) : {}), ...(this.config.custom.length ? JSON.parse(this.config.custom) : {}),
...(this.config.apiKey ? { api_key: this.config.apiKey } : {}),
}; };
const config = { const config = {
controller: abortController, controller: abortController,
@ -457,13 +466,16 @@
} else { } else {
console.error(error); console.error(error);
alert(error); alert(error);
this.inputMsg = this.pendingMsg.content || ''; // pop last user message
const lastUserMsg = StorageUtils.popMsg(currConvId);
this.inputMsg = lastUserMsg ? lastUserMsg.content : '';
} }
} }
this.pendingMsg = null; this.pendingMsg = null;
this.isGenerating = false; this.isGenerating = false;
this.stopGeneration = () => {}; this.stopGeneration = () => {};
this.fetchMessages();
}, },
// message actions // message actions

View file

@ -2356,6 +2356,11 @@ int main(int argc, char ** argv) {
"/v1/models", "/v1/models",
}; };
// If this is OPTIONS request, skip validation because browsers don't include Authorization header
if (req.method == "OPTIONS") {
return true;
}
// If API key is not set, skip validation // If API key is not set, skip validation
if (params.api_keys.empty()) { if (params.api_keys.empty()) {
return true; return true;