tokenizers

package
v2.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: MIT Imports: 35 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 counts the tokens of a text or a request for a model with the model's own tokenizer: it learns from the provider plugins which tokenizer each model counts with, keeps the tokenizers it fetched in icebath's folder, loads them once, and counts with them, the provider's own counting or the estimate icebath always had standing in where no tokenizer is at hand. Counting never waits on a download: a tokenizer missing is fetched in the background and counts from the next time on

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Chat

type Chat int

How a request's messages are framed for a model before its tokens are counted

const (
	// The tokenizer's own chat template, the text of each part alone where it carries none
	Template Chat = iota
	// The framing OpenAI's cookbook counts for its chat models: three tokens a message, three priming the reply, and its rule for tools
	OpenAI
	// The text of each part alone, nothing around it
	Plain
)

type Count

type Count struct {
	Tokens int
	How    How
	By     string
}

A number of tokens with how it was had and what counted it, such as o200k_base or Qwen/Qwen3-8B; By is empty for an estimate

type Counter

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

Counts for one route's model, as the context engines and the agent ask them: a contexts.Counter

func (*Counter) By

func (c *Counter) By() string

What counts the route's model: the name of its tokenizer when one is loaded, empty when counts are estimated

func (*Counter) Messages

func (c *Counter) Messages(messages []*sessionsv1.Message) (int, bool)

Tokens of messages as the provider is sent them, the framing of the model's chat format with them, and whether the model's tokenizer counted them

func (*Counter) Request

func (c *Counter) Request(system string, messages []*sessionsv1.Message, definitions []map[string]any) (int, bool)

Tokens of a whole request, and whether the model's tokenizer counted them

func (*Counter) Text

func (c *Counter) Text(text string) (int, bool)

Tokens of a text, and whether the model's tokenizer counted them

type How

type How int

How a count was had

const (
	// From the length of the text, as icebath counted before: no tokenizer of the model is at hand
	Estimated How = iota
	// The provider's own counting of the request
	Provider
	// The model's own tokenizer
	Exact
)

func (How) String

func (h How) String() string

What a person reads of how a count was had

type Kept

type Kept struct {
	// What it is called, such as o200k_base or Qwen/Qwen3-8B
	Name string
	// An encoding or a repository of the hub
	Encoding bool
	// The revision of a repository, such as main, and the commit it was at when its files were fetched
	Revision, Commit string
	// Its files and their bytes
	Files int
	Size  int64
	// When its files were fetched
	Fetched time.Time
	// Where its files are
	Folder string
}

A tokenizer kept in icebath's folder: an encoding of OpenAI's or the tokenizer files of a repository of the hub at a revision

func (Kept) Title

func (k Kept) Title() string

What the tokenizer is listed as: its name, with the revision of a repository other than main

type Model

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

A loaded tokenizer with what frames a request for its model

func (*Model) Head

func (m *Model) Head(text string, tokens int) string

The longest start of a text within that many tokens, as the model counts it alone

func (*Model) Messages

func (m *Model) Messages(messages []*sessionsv1.Message) int

Tokens of messages as the provider is sent them, the framing of the model's chat format with them

func (*Model) Name

func (m *Model) Name() string

What the model counts with

func (*Model) Request

func (m *Model) Request(r Request) int

Tokens of a request: the system prompt, the messages and the tool definitions as the model's chat format frames them, pictures at the flat price icebath counts them at

func (*Model) Tail

func (m *Model) Tail(text string, tokens int) string

The longest end of a text within that many tokens, as the model counts it alone

func (*Model) Text

func (m *Model) Text(text string) int

Tokens of a text as the model reads it inside a message: special tokens written in it read as text, nothing added around it

func (*Model) Tokenizer

func (m *Model) Tokenizer() tokenize.Tokenizer

The tokenizer itself

type Registrant

type Registrant interface {
	// Tokenizers the route's model counts with, the most exact first; none for a route of another kind
	Tokenizers(ctx context.Context, cfg *config.Config, route provider.Route) []Source
}

What a provider knows of the tokenizers the models of its kind count with

type Registry

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

func (*Registry) Host

func (r *Registry) Host() *Tokenizers

Host the registry is nested in, through which plugins count tokens and reach the tokenizers of models

func (*Registry) Register

func (r *Registry) Register(registrant Registrant)

func (*Registry) Tokenizers

func (r *Registry) Tokenizers(ctx context.Context, cfg *config.Config, route provider.Route) []Source

type Request

type Request struct {
	System   string
	Messages []*sessionsv1.Message
	Tools    []map[string]any
}

What a count is of: the system prompt, the messages and the tool definitions of a request, as a provider is sent them

type Source

type Source struct {
	// A tiktoken encoding by name, such as o200k_base
	Encoding string
	// A repository of the Hugging Face hub holding the tokenizer files, and its revision, main when empty
	Repo, Revision string
	// A snapshot folder or a GGUF model file on this computer
	Path string
	Chat Chat
}

One tokenizer a model counts with

func (Source) Name

func (s Source) Name() string

What the source is called in a count and on the lists of what is kept

type Tokenizers

type Tokenizers struct {
	Registry
	// contains filtered or unexported fields
}

The tokenizers of this computer: those loaded, those loading, and how to fetch and keep more

func New

func New(cfg *config.Config, pk *packages.Packages, transports *transport.Registry) *Tokenizers

func (*Tokenizers) Counter

func (t *Tokenizers) Counter(cfg *config.Config, route provider.Route) *Counter

Counts for the route's model, exact where its tokenizer is loaded

func (*Tokenizers) Folder

func (t *Tokenizers) Folder() string

The folder in icebath's folder the tokenizers it fetched are kept in, shared by every profile

func (*Tokenizers) Kept

func (t *Tokenizers) Kept() []Kept

The tokenizers kept in icebath's folder, by name

func (*Tokenizers) Local

func (t *Tokenizers) Local(ctx context.Context, cfg *config.Config, route provider.Route, request Request) Count

Tokens of a request as Request counts them, the model's tokenizer loaded first when nothing of it is to be downloaded, such as the tokenizer a model file of this computer carries; one that must be downloaded starts in the background and the request is estimated meanwhile. For a choice the exact number decides before the request goes out, such as the window a local model is loaded with

func (*Tokenizers) Measure

func (t *Tokenizers) Measure(ctx context.Context, cfg *config.Config, route provider.Route, request Request) Count

Tokens of a request as Request counts them, the provider asked to count it where no tokenizer of the model is loaded and the settings allow, within the context's deadline

func (*Tokenizers) Model

func (t *Tokenizers) Model(ctx context.Context, cfg *config.Config, route provider.Route) (*Model, error)

The loaded tokenizer of the route's model, loading it now, fetched when the settings allow; an error when none of the sources the providers name loads

func (*Tokenizers) Named

func (t *Tokenizers) Named(ctx context.Context, cfg *config.Config, name string) (*Model, error)

The loaded tokenizer of a name, loading it now: a repository of the Hugging Face hub (org/name, @revision after it), a snapshot folder or a GGUF file on this computer, or a tiktoken encoding

func (*Tokenizers) Problems

func (t *Tokenizers) Problems(k Kept) []string

What is wrong with a tokenizer's files: one missing, or not the bytes that were downloaded and checked, each a sentence

func (*Tokenizers) Register

func (t *Tokenizers) Register(registrant Registrant)

Adds a registrant; a registry nested in the host reaches it through Host

func (*Tokenizers) Remove

func (t *Tokenizers) Remove(k Kept) error

Removes a tokenizer's files, which are downloaded again the next time a model counts with it, the tokenizer loaded from them let go

func (*Tokenizers) Request

func (t *Tokenizers) Request(ctx context.Context, cfg *config.Config, route provider.Route, request Request) Count

Tokens of a request for the route's model, the framing of the model's chat format with them: exact where its tokenizer is loaded, estimated otherwise; never waits

func (*Tokenizers) Size

func (t *Tokenizers) Size() int64

Bytes of every tokenizer kept

func (*Tokenizers) Text

func (t *Tokenizers) Text(ctx context.Context, cfg *config.Config, route provider.Route, text string) Count

Tokens of a text for the route's model: exact where its tokenizer is loaded, estimated otherwise; never waits

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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