Configure special token handling with `EncodeOptions`
mainBy default, all special tokens are disallowed during encoding. You can customize this behavior using the encodeOptions parameter in functions like encode, countTokens, and isWithinTokenLimit.
Allowing Special Tokens
Pass a Set of allowed tokens to allowedSpecialTokens, or use the shorthand { allowedSpecial: 'all' } to allow all special tokens.
import { encode, EndOfPrompt } from 'gpt-tokenizer'
const inputText = `Some Text ${EndOfPrompt}`
const allowedSpecialTokens = new Set([EndOfPrompt])
const encoded = encode(inputText, { allowedSpecialTokens })Disallowing Special Tokens
Pass a Set of tokens to disallowedSpecial. If a disallowed token is encountered in the input, an error will be thrown. If both allowedSpecialTokens and disallowedSpecial are provided, disallowedSpecial takes precedence.
import { encode, EndOfText } from 'gpt-tokenizer'
const inputText = `Some Text ${EndOfText}`
const disallowedSpecial = new Set([EndOfText])
// throws an error:
const encoded = encode(inputText, { disallowedSpecial })