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 ¶
- type Ask
- type Chat
- type Count
- type Counter
- type How
- type Kept
- type Model
- func (m *Model) Head(text string, tokens int) string
- func (m *Model) Messages(messages []*sessionsv1.Message) int
- func (m *Model) Name() string
- func (m *Model) Request(r Request) int
- func (m *Model) Tail(text string, tokens int) string
- func (m *Model) Text(text string) int
- func (m *Model) Tokenizer() tokenize.Tokenizer
- type Registrant
- type Registry
- type Request
- type Source
- type Tokenizers
- func (t *Tokenizers) Counter(cfg *config.Config, route provider.Route) *Counter
- func (t *Tokenizers) Folder() string
- func (t *Tokenizers) Kept() []Kept
- func (t *Tokenizers) Local(ctx context.Context, cfg *config.Config, route provider.Route, request Request) Count
- func (t *Tokenizers) Measure(ctx context.Context, cfg *config.Config, route provider.Route, request Request) Count
- func (t *Tokenizers) Model(ctx context.Context, cfg *config.Config, route provider.Route) (*Model, error)
- func (t *Tokenizers) Named(ctx context.Context, cfg *config.Config, name string) (*Model, error)
- func (t *Tokenizers) Offer(ctx context.Context, cfg *config.Config, route provider.Route, session string, ...)
- func (t *Tokenizers) Problems(k Kept) []string
- func (t *Tokenizers) Register(registrant Registrant)
- func (t *Tokenizers) Remove(k Kept) error
- func (t *Tokenizers) Request(ctx context.Context, cfg *config.Config, route provider.Route, request Request) Count
- func (t *Tokenizers) Size() int64
- func (t *Tokenizers) Text(ctx context.Context, cfg *config.Config, route provider.Route, text string) Count
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Ask ¶
Ask puts a question through a session's approval gate under the key, nil once the person agreed.
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 ¶
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 ¶
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
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
type Model ¶
type Model struct {
// contains filtered or unexported fields
}
A loaded tokenizer with what frames a request for its model
func (*Model) Head ¶
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) Request ¶
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 ¶
The longest end of a text within that many tokens, as the model counts it alone
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)
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
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 (*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, downloaded where a session agreed; an error when none of the sources the providers name loads
func (*Tokenizers) Named ¶
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