fix encoding if error

This commit is contained in:
rafaelsideguide 2024-10-18 11:50:58 -03:00
parent 6ebfcc85cf
commit aed11e72a6

View File

@ -6,7 +6,13 @@ export function numTokensFromString(message: string, model: string): number {
const encoder = encoding_for_model(model as TiktokenModel); const encoder = encoding_for_model(model as TiktokenModel);
// Encode the message into tokens // Encode the message into tokens
const tokens = encoder.encode(message); let tokens: Uint32Array;
try {
tokens = encoder.encode(message);
} catch (error) {
message = message.replace("<|endoftext|>", "");
tokens = encoder.encode(message);
}
// Free the encoder resources after use // Free the encoder resources after use
encoder.free(); encoder.free();