Documentation
Namespaces
Model
interfaces
ITokenizer

Interface: ITokenizer

Generic interface for a model tokenizer

Methods

countTokens()

countTokens(input?): number

Count the number of tokens in a string or ChatMessage(s). A single ChatMessage is counted as a completion and an array as a prompt. Strings are counted as is.

Parameters

ParameterType
input?string | ChatMessage | ChatMessage[]

Returns

number

Source

src/model/types.ts:203 (opens in a new tab)


decode()

decode(tokens): string

Decode an array of integer tokens into a string

Parameters

ParameterType
tokensnumber[] | Uint32Array

Returns

string

Source

src/model/types.ts:197 (opens in a new tab)


encode()

encode(text): Uint32Array

Tokenize a string into an array of integer tokens

Parameters

ParameterType
textstring

Returns

Uint32Array

Source

src/model/types.ts:195 (opens in a new tab)


truncate()

truncate(args): string

Truncate a string to a maximum number of tokens

Parameters

ParameterTypeDescription
argsobject-
args.from?"start" | "end"Truncate from the start or end of the text
args.maxnumberMaximum number of tokens to keep (inclusive)
args.textstringText to truncate

Returns

string

Source

src/model/types.ts:205 (opens in a new tab)