tiktoken

package
v2.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: MIT Imports: 13 Imported by: ? 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 tiktoken is the byte-pair encoding OpenAI's models count tokens with: a vocabulary of byte strings ranked by merge priority, a pattern that cuts text into pieces before the merges, and special tokens, read from the rank files OpenAI publishes

Index

Constants

View Source
const (
	EndOfText   = "<|endoftext|>"
	FIMPrefix   = "<|fim_prefix|>"
	FIMMiddle   = "<|fim_middle|>"
	FIMSuffix   = "<|fim_suffix|>"
	EndOfPrompt = "<|endofprompt|>"
)

Special tokens every encoding names its own way

Variables

This section is empty.

Functions

func DataGym

func DataGym(vocabBPE, encoderJSON []byte) (map[string]int, error)

Ranks of GPT-2's original files: the merges of vocab.bpe ranked after the 256 single bytes, checked against encoder.json as tiktoken checks them, since the ranks are only right when both say the same

func ForModel

func ForModel(model string) (string, bool)

The encoding an OpenAI model reads text with, as tiktoken's encoding_name_for_model names it: the model's own name first, then the start of it

func ParseRanks

func ParseRanks(contents []byte) (map[string]int, error)

Ranks of a rank file: one token in base64 and its rank on each line, as tiktoken writes them and models such as Kimi ship them

Types

type Encoding

type Encoding struct {
	Name string
	// contains filtered or unexported fields
}

A vocabulary of ranked byte strings with the pattern that cuts text before merging and the special tokens beside it

func Build

func Build(spec Spec, contents [][]byte) (*Encoding, error)

The encoding a spec makes of the contents of its files, given in the order Files names them

func New

func New(name, split string, ranks map[string]int, specials map[string]int) (*Encoding, error)

Encoding of a split pattern in tiktoken's own syntax, the ranks of its byte strings and its special tokens by id

func (*Encoding) Count

func (e *Encoding) Count(text string, specials bool) int

Number of tokens of a text

func (*Encoding) Decode

func (e *Encoding) Decode(ids []int) string

Text of the tokens, bytes that are no text replaced as tiktoken replaces them

func (*Encoding) DecodeBytes

func (e *Encoding) DecodeBytes(ids []int) []byte

Bytes of the tokens, joined

func (*Encoding) Encode

func (e *Encoding) Encode(text string, specials bool) []int

Tokens of a text, a special token written in it read as itself when specials is set and as text otherwise

func (*Encoding) EncodeOrdinary

func (e *Encoding) EncodeOrdinary(text string) []int

Tokens of a text with every special token read as text

func (*Encoding) ID

func (e *Encoding) ID(token string) (int, bool)

Id of a byte string as one token, special tokens among them

func (*Encoding) Size

func (e *Encoding) Size() int

Ids the encoding knows, one past the highest

func (*Encoding) Spans

func (e *Encoding) Spans(text string, specials bool) ([]int, [][2]int)

Tokens of a text with the byte range of the text each covers; a token of part of a character covers the bytes it holds

func (*Encoding) Special

func (e *Encoding) Special(id int) bool

Whether the id is a special token's

func (*Encoding) Specials

func (e *Encoding) Specials() map[string]int

Special tokens by id

func (*Encoding) Token

func (e *Encoding) Token(id int) (string, bool)

Byte string of a token, special tokens among them

type File

type File struct {
	Name   string
	URL    string
	SHA256 string
	Size   int64
}

A file an encoding's ranks are read from, at the address OpenAI publishes it with the digest it publishes for it

type Spec

type Spec struct {
	Name    string
	Pattern string
	// Special tokens by id
	Specials map[string]int
	// One rank file, or GPT-2's vocab.bpe and encoder.json in that order
	Files []File
	// The vocabulary is GPT-2's data-gym pair rather than a rank file
	DataGym bool
	// Ids the encoding states it has, zero when it states none
	Vocab int
}

An encoding OpenAI publishes: how it cuts text, its special tokens and the files of its ranks

func Lookup

func Lookup(name string) (Spec, bool)

The spec of an encoding by name

func Specs

func Specs() []Spec

Every encoding OpenAI's models use, in the order tiktoken lists them

Jump to

Keyboard shortcuts

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