bpe

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package bpe implements Byte Pair Encoding tokenization.

Index

Constants

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

Special token constants.

View Source
const (
	EncodingO200kBase  = "o200k_base"
	EncodingCL100kBase = "cl100k_base"
	EncodingP50kBase   = "p50k_base"
	EncodingP50kEdit   = "p50k_edit"
	EncodingR50kBase   = "r50k_base"
)

Encoding names.

Variables

This section is empty.

Functions

This section is empty.

Types

type BPETokenizer

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

BPETokenizer is the main tokenizer that wraps a BPE Encoder.

func NewEncoderByName

func NewEncoderByName(encodingName string) (*BPETokenizer, error)

NewEncoderByName returns a BPETokenizer for the named encoding.

func (*BPETokenizer) Decode

func (tok *BPETokenizer) Decode(tokens []int) string

Decode converts token IDs back to text.

func (*BPETokenizer) Encode

func (tok *BPETokenizer) Encode(text string, allowedSpecial []string, disallowedSpecial []string) ([]int, error)

Encode tokenizes text with optional special token handling. Returns an error if text contains a disallowed special token.

func (*BPETokenizer) EncodeOrdinary

func (tok *BPETokenizer) EncodeOrdinary(text string) []int

EncodeOrdinary tokenizes text without special token handling.

type Definition

type Definition struct {
	Name           string
	PatStr         string
	MergeableRanks map[string]int
	SpecialTokens  map[string]int
	ExplicitNVocab int
}

Definition holds the specification for a BPE encoding scheme.

type Encoder

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

Encoder is the core BPE encoder/decoder.

func NewEncoder

func NewEncoder(encoder map[string]int, specialTokensEncoder map[string]int, pattern string) (*Encoder, error)

NewEncoder creates a new Encoder from encoder maps and a regex pattern.

Jump to

Keyboard shortcuts

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