Documentation
¶
Overview ¶
Package tokenizers encodes text into model input ids exactly as the Hugging Face tokenizers library (version 0.23.3, through its Python binding) does for the tokenizer.json files it supports: the same ids, type ids, masks, sequence ids and offsets, the offsets counted in Unicode code points of the sequence each token belongs to.
It implements the components those files use (the BertNormalizer and NFC normalizers, the BertPreTokenizer and ByteLevel pre-tokenizers, the WordPiece and BPE models, the added vocabulary and the TemplateProcessing post-processor) and refuses any other component or option, rather than encoding differently. The Unicode data comes from the libraries Hugging Face tokenizers links (Unicode 9.0.0 for normalization and character categories, Oniguruma's Unicode 16.0 for the ByteLevel pattern), not from Go's unicode package, whose tables are newer.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrSequenceTooShort is returned (wrapped in Hugging Face tokenizers' // message) when the sequence a strategy truncates is too short to bring // the encoding down to the maximum length. ErrSequenceTooShort = errors.New("tokenizers: sequence to truncate too short to respect the provided max_length") // ErrSecondSequenceNotProvided is returned (wrapped in Hugging Face // tokenizers' message) when OnlySecond has to truncate a single sequence. ErrSecondSequenceNotProvided = errors.New("tokenizers: second sequence not provided") // ErrUnsupported is returned by New for a component, option or value // this package does not implement. ErrUnsupported = errors.New("tokenizers: unsupported tokenizer.json") // ErrInvalidFile is returned by New for a tokenizer.json that is not // well formed, or that Hugging Face tokenizers would refuse. ErrInvalidFile = errors.New("tokenizers: invalid tokenizer.json") // ErrInvalidInput is returned for text that is not valid UTF-8, or a // negative maximum length. ErrInvalidInput = errors.New("tokenizers: invalid input") // ErrInternal is returned if encoding hits a state Hugging Face tokenizers // would panic on. ErrInternal = errors.New("tokenizers: internal error") )
Functions ¶
This section is empty.
Types ¶
type Direction ¶
type Direction int
Direction says which end of a sequence truncation removes tokens from.
type Encoding ¶
type Encoding struct {
IDs []int64
TypeIDs []int64
AttentionMask []int64
Tokens []string
Offsets []Offset
SpecialTokensMask []int
// SequenceIDs is the sequence (0 or 1) of each token, -1 for the special
// tokens.
SequenceIDs []int
}
Encoding is the encoding of one sequence or a pair, as Hugging Face tokenizers' Encoding reports it. All slices have one element per token.
type Offset ¶
type Offset struct{ Start, End int }
Offset is a token's range in the sequence it belongs to, in Unicode code points. Special tokens added by the template have (0, 0).
type Tokenizer ¶
type Tokenizer struct {
// contains filtered or unexported fields
}
Tokenizer encodes text. It is immutable after New, and safe for concurrent use.
func New ¶
New parses tokenizer.json bytes. It returns an error naming any component, option or value it doesn't implement, rather than encoding differently.
func (*Tokenizer) Encode ¶
func (t *Tokenizer) Encode(text string, trunc *Truncation) (Encoding, error)
Encode encodes one sequence with the special tokens of the template, and truncates it by trunc (nil: no truncation). It never pads.
func (*Tokenizer) EncodePair ¶
func (t *Tokenizer) EncodePair(first, second string, trunc *Truncation) (Encoding, error)
EncodePair encodes a pair of sequences with the special tokens of the template, and truncates them by trunc (nil: no truncation). It never pads.
func (*Tokenizer) Truncation ¶
func (t *Tokenizer) Truncation() *Truncation
Truncation is the truncation the file sets, or nil.
type Truncation ¶
Truncation limits the length of an encoding, special tokens included.