tokenizers

package
v2.0.6 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2026 License: MIT Imports: 36 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 a text's or a request's tokens for a model. A decision that needs the exact number asks the provider to count the request first. Otherwise the model's facts lead to its tokenizer: the Hugging Face repository in the provider's listing, a model file on this computer, the model's owner/name id as a hub repository, or the encoding that the provider's kind declares for all its models. The host saves fetched tokenizers in icebath's folder, loads each one once, and estimates from the text's length when no tokenizer is available. Counting never waits on a download: a missing tokenizer is fetched in the background and counts after that.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Ask

type Ask func(ctx context.Context, key, what, about string) error

Ask puts a question through a session's approval gate under the key, nil once the user agreed.

type Catalog

type Catalog interface {
	Facts(ctx context.Context, cfg *config.Config, route provider.Route) provider.Facts
	Spec(route provider.Route) provider.Spec
}

Catalog is what the host reads about a route's model: the facts that the model's sources state, and the kind of the route's provider. *models.Models is one.

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
}

Count is a number of tokens, how it was counted, and what counted it: an encoding, a hub repository, a file or the provider. By is empty when the count is 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 user reads of how a count was had

type Kept

type Kept struct {
	// Name is an encoding's name, or a repository as owner/name.
	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 returns tokenizers for the route's model, the most exact first. It returns an empty list for a route of another kind.
	Tokenizers(ctx context.Context, cfg *config.Config, route provider.Route) []Source
}

Registrant is a plugin that states tokenizers for a route's model beyond the model's facts, such as a local server's loaded model file or the tokenizers that an extension's provider declares.

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 RepoSource

func RepoSource(stated string) (Source, bool)

RepoSource returns the source of a hub repository written as owner/name, with an optional revision after an @, framed by its own chat template. ok is false for text that is not a repository.

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, catalog Catalog) *Tokenizers

New returns the host. The catalog states each route's model's facts and its provider's kind, which *models.Models does.

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

Measure counts a request's tokens for a decision that needs the exact number. The provider counts the request when Provider counting is on and the provider has a count endpoint, within the context's deadline. Otherwise Request counts it.

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, downloaded where a session agreed. 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) Offer

func (t *Tokenizers) Offer(ctx context.Context, cfg *config.Config, route provider.Route, session string, ask Ask)

Offer asks the session before the route's model's tokenizer is downloaded, and starts the download once agreed. Counts are estimated until it loads. A source a session declined is not asked of it again.

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

func (*Tokenizers) Tokenizers

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

Tokenizers returns the tokenizers the route's model counts with, the most exact first, reading its facts and asking the hub about its repositories now where they are not known yet.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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