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 ¶
- type Ask
- type Catalog
- 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
- func (t *Tokenizers) Tokenizers(ctx context.Context, cfg *config.Config, route provider.Route) []Source
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 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 ¶
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 ¶
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 {
// 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
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 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)
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 ¶
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.
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) 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 ¶
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) 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.