Merge pull request #793 from mendableai/fix/issue-665

[Bug] encoding error for special token
This commit is contained in:
Nicolas 2024-10-21 12:24:46 -03:00 committed by GitHub
commit d31b85fa91
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -6,7 +6,13 @@ export function numTokensFromString(message: string, model: string): number {
const encoder = encoding_for_model(model as TiktokenModel);
// 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
encoder.free();