fix(tokenizer): error handle encoding for invalid encoding data (#385)

This commit is contained in:
Danny Avila 2023-05-26 09:40:08 -04:00 committed by GitHub
parent b912e7a3dd
commit 4f17e69f1b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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 };