tiktoken

package module
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package tiktoken implements the core/tokenizer capabilities with OpenAI's tiktoken vocabularies, adapting github.com/pkoukk/tiktoken-go to the small interfaces Core defines.

The vocabulary is chosen explicitly, because no single encoding is correct across models:

tokenizer, err := tiktoken.New(tiktoken.O200KBase)
if err != nil {
    return err
}

count, err := tokenizer.CountText(ctx, "hello")

An unknown encoding returns ErrInvalidEncoding at construction rather than silently falling back to another vocabulary, because a wrong token count is not visible until a request is rejected for length.

This module owns no provider request, no model-to-encoding routing, no text splitting, and no cache. Splitting belongs to etl; routing belongs to whatever knows the model.

Index

Constants

Supported encodings.

Variables

View Source
var (
	ErrInvalidEncoding = errors.New("tiktoken: invalid encoding")
	ErrUninitialized   = errors.New("tiktoken: tokenizer is not initialized")
)

Functions

This section is empty.

Types

type Encoding

type Encoding string

Encoding identifies a tiktoken vocabulary. Callers choose explicitly because token counts are vocabulary-specific; there is no model-independent default.

func (Encoding) Validate

func (e Encoding) Validate() error

type Tokenizer

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

Tokenizer encodes, decodes, and counts text with one tiktoken vocabulary. It is safe for concurrent use.

func New

func New(encoding Encoding) (Tokenizer, error)

New resolves the vocabulary once at construction rather than per call, so a misspelled encoding fails where it is configured instead of on the first count. The vocabulary must be named explicitly because no single encoding is correct across models, and guessing one silently miscounts every budget derived from it.

func (Tokenizer) Decode

func (t Tokenizer) Decode(ctx context.Context, tokens []int) (string, error)

func (Tokenizer) Encode

func (t Tokenizer) Encode(ctx context.Context, text string) ([]int, error)

func (Tokenizer) EstimateText

func (t Tokenizer) EstimateText(ctx context.Context, text string) (int, error)

Jump to

Keyboard shortcuts

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