tokenizer

package
v0.19.0 Latest Latest
Warning

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

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

Documentation

Overview

Package tokenizer defines small, provider-neutral capabilities for counting and encoding text tokens. Concrete vocabularies live in implementation packages such as github.com/Tangerg/scope/tokenizers/tiktoken.

Example
package main

import (
	"context"
	"fmt"
	"strings"

	"github.com/Tangerg/scope/core/tokenizer"
)

type wordEstimator struct{}

func (wordEstimator) EstimateText(_ context.Context, text string) (int, error) {
	return len(strings.Fields(text)), nil
}

func main() {
	var estimator tokenizer.TextEstimator = wordEstimator{}
	count, err := estimator.EstimateText(context.Background(), "small stable contract")
	if err != nil {
		panic(err)
	}
	fmt.Println(count)
}
Output:
3

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Decoder

type Decoder interface {
	// Decode rejects token IDs outside the selected vocabulary and does not
	// retain the caller's slice. Context cancellation remains identifiable.
	Decode(ctx context.Context, tokens []int) (string, error)
}

Decoder converts vocabulary token IDs back into text.

type Encoder

type Encoder interface {
	// Encode returns token IDs in vocabulary order. The caller owns the returned
	// slice; implementations must not expose a mutable internal buffer.
	Encode(ctx context.Context, text string) ([]int, error)
}

Encoder converts text into vocabulary token IDs.

type TextEstimator

type TextEstimator interface {
	// EstimateText returns a non-negative count under the implementation's
	// explicitly selected vocabulary or provider model. Remote implementations
	// must honor cancellation and preserve context errors.
	EstimateText(ctx context.Context, text string) (int, error)
}

TextEstimator reports the token count a model would assign to text. Implementations may use a local vocabulary or a provider API.

type Tokenizer

type Tokenizer interface {
	Encoder
	Decoder
}

Tokenizer combines the encoding capabilities required by token-aware text splitters. Providers that only count tokens should implement TextEstimator instead.

Jump to

Keyboard shortcuts

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