tokenizer

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterOpenAITokenizers

func RegisterOpenAITokenizers()

注册 OpenAI 用户登记所有已知的 OpenAI 模型的标识器。

func RegisterTokenizer

func RegisterTokenizer(model string, t Tokenizer)

RegisterTokenizer 为给定的模型名称注册分词器.

Types

type EstimatorTokenizer

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

估计器Tokenizer是一个基于字符计数的符号估计器. 它区分 CJK 和 ASCII 字符, 使其更准确 与天真的Len/4方法相比

func NewEstimatorTokenizer

func NewEstimatorTokenizer(model string, maxTokens int) *EstimatorTokenizer

新估计器Tokenizer创建了通用估计器.

func (*EstimatorTokenizer) CountMessages

func (e *EstimatorTokenizer) CountMessages(messages []Message) (int, error)

func (*EstimatorTokenizer) CountTokens

func (e *EstimatorTokenizer) CountTokens(text string) (int, error)

func (*EstimatorTokenizer) Decode

func (e *EstimatorTokenizer) Decode(_ []int) (string, error)

func (*EstimatorTokenizer) Encode

func (e *EstimatorTokenizer) Encode(text string) ([]int, error)

func (*EstimatorTokenizer) MaxTokens

func (e *EstimatorTokenizer) MaxTokens() int

func (*EstimatorTokenizer) Name

func (e *EstimatorTokenizer) Name() string

func (*EstimatorTokenizer) WithCharsPerToken

func (e *EstimatorTokenizer) WithCharsPerToken(ratio float64) *EstimatorTokenizer

使用 CharsPerToken 来覆盖默认的字符/ 每个字的比值 。

type Message

type Message struct {
	Role    string
	Content string
}

Message 是一个轻量级消息结构, 由 tokenizer 包使用 以避免与 llm 包的循环依赖。

type TiktokenTokenizer

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

TiktokenTokenizer为OpenAI-家庭模型改造tiktoken.

func NewTiktokenTokenizer

func NewTiktokenTokenizer(model string) (*TiktokenTokenizer, error)

NewTiktokenTokenizer为给定型号创建了以tiktoken为主的代号.

func (*TiktokenTokenizer) CountMessages

func (t *TiktokenTokenizer) CountMessages(messages []Message) (int, error)

func (*TiktokenTokenizer) CountTokens

func (t *TiktokenTokenizer) CountTokens(text string) (int, error)

func (*TiktokenTokenizer) Decode

func (t *TiktokenTokenizer) Decode(tokens []int) (string, error)

func (*TiktokenTokenizer) Encode

func (t *TiktokenTokenizer) Encode(text string) ([]int, error)

func (*TiktokenTokenizer) MaxTokens

func (t *TiktokenTokenizer) MaxTokens() int

func (*TiktokenTokenizer) Name

func (t *TiktokenTokenizer) Name() string

type Tokenizer

type Tokenizer interface {
	// CountTokens 返回给定文本的 token 数.
	CountTokens(text string) (int, error)

	// CountMessages 返回消息列表的总 token 数,
	// 包括每条消息的开销(角色标记、分隔符等)。
	CountMessages(messages []Message) (int, error)

	// Encode 将文本转换为 token ID 列表.
	Encode(text string) ([]int, error)

	// Decode 将 token ID 转换回文本.
	Decode(tokens []int) (string, error)

	// MaxTokens 返回模型的最大上下文长度.
	MaxTokens() int

	// Name 返回分词器的名称.
	Name() string
}

Tokenizer是统一的代号计数界面.

注意:项目中存在三个 Tokenizer 接口,各自服务不同层次,无法统一:

  • types.Tokenizer — 框架层,面向 Message/ToolSchema,无 error 返回
  • llm/tokenizer.Tokenizer(本接口)— LLM 层,完整编解码 + error 返回 + 模型感知
  • rag.Tokenizer — RAG 分块专用,最小接口(CountTokens + Encode),无 error

本接口返回 error 以支持真实 tokenizer(如 tiktoken)的错误处理。 使用 rag.NewLLMTokenizerAdapter() 可将本接口适配为 rag.Tokenizer。

func GetTokenizer

func GetTokenizer(model string) (Tokenizer, error)

GetTokenizer 返回为给定型号注册的标定器 。 它也尝试了前缀匹配(如"gpt-4o"匹配"gpt-4o-mini").

func GetTokenizerOrEstimator

func GetTokenizerOrEstimator(model string) Tokenizer

GetTokenizer OrEstimator 返回该模型的注册代号器, 如果没有登记,则回到一般估计器。

Jump to

Keyboard shortcuts

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