tokenizers

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2026 License: BSD-2-Clause Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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.

const (
	// Right removes tokens from the end.
	Right Direction = iota
	// Left removes tokens from the start.
	Left
)

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 Strategy

type Strategy int

Strategy says which sequence of a pair truncation shortens.

const (
	// LongestFirst shortens the longer sequence first, then both.
	LongestFirst Strategy = iota
	// OnlyFirst shortens the first sequence only.
	OnlyFirst
	// OnlySecond shortens the second sequence only.
	OnlySecond
)

type Tokenizer

type Tokenizer struct {
	// contains filtered or unexported fields
}

Tokenizer encodes text. It is immutable after New, and safe for concurrent use.

func FromFile

func FromFile(path string) (*Tokenizer, error)

FromFile reads and parses a tokenizer.json file.

func New

func New(data []byte) (*Tokenizer, error)

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

type Truncation struct {
	MaxLength int
	Strategy  Strategy
	Direction Direction
}

Truncation limits the length of an encoding, special tokens included.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL