mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-18 09:20:15 +01:00
fix(tokenizer): error handle encoding for invalid encoding data (#385)
This commit is contained in:
parent
b912e7a3dd
commit
4f17e69f1b
1 changed files with 18 additions and 8 deletions
|
|
@ -68,18 +68,28 @@ const askClient = async ({
|
||||||
...(parentMessageId && conversationId ? { parentMessageId, conversationId } : {})
|
...(parentMessageId && conversationId ? { parentMessageId, conversationId } : {})
|
||||||
};
|
};
|
||||||
|
|
||||||
const enc = encoding_for_model(tiktokenModels.has(model) ? model : 'gpt-3.5-turbo');
|
let usage = null;
|
||||||
const usage = {
|
let enc = null;
|
||||||
prompt_tokens: (enc.encode(promptText)).length + (enc.encode(text)).length,
|
try {
|
||||||
|
enc = encoding_for_model(tiktokenModels.has(model) ? model : 'gpt-3.5-turbo');
|
||||||
|
usage = {
|
||||||
|
prompt_tokens: (enc.encode(promptText)).length + (enc.encode(text)).length,
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.log('Error encoding prompt text', e);
|
||||||
}
|
}
|
||||||
|
|
||||||
const res = await client.sendMessage(text, { ...options, userId });
|
const res = await client.sendMessage(text, { ...options, userId });
|
||||||
usage.completion_tokens = (enc.encode(res.response)).length;
|
|
||||||
usage.total_tokens = usage.prompt_tokens + usage.completion_tokens;
|
try {
|
||||||
return {
|
usage.completion_tokens = (enc.encode(res.response)).length;
|
||||||
...res,
|
usage.total_tokens = usage.prompt_tokens + usage.completion_tokens;
|
||||||
usage,
|
res.usage = usage;
|
||||||
|
} catch (e) {
|
||||||
|
console.log('Error encoding response text', e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = { askClient };
|
module.exports = { askClient };
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue