tokenizer

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package tokenizer provides a fast, pure-Go token counter.

It embeds the OpenAI cl100k_base vocabulary and implements byte-level BPE scoring, so token counts are deterministic and require no network calls.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BPE

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

BPE is a byte-level byte-pair encoding tokenizer.

var Encoder *BPE

Encoder is a ready-to-use byte-level BPE tokenizer for the cl100k_base vocabulary (GPT-4 family, claude token counting compatible at the UTF-8 bytes level).

func Load

func Load(fs embed.FS) (*BPE, error)

Load builds a BPE from an embedded tiktoken vocabulary file.

Each line is "<base64-token-bytes> <rank>". Tokens are decoded back to raw bytes so both lookup and BPE merges operate on the true token text.

func (*BPE) Count

func (b *BPE) Count(s string) int

Count returns the number of tokens in s.

func (*BPE) Encode

func (b *BPE) Encode(s string) []int

Encode returns the token IDs for s.

Jump to

Keyboard shortcuts

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