window

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CalibratedEstimator

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

CalibratedEstimator 基于历史数据校准的 Token 估算器 包装 defaultEstimator,用模型返回的实际 prompt_tokens 动态调整估算比例

原理:

  • defaultEstimator 用固定比例(1 token ≈ 1.8 runes)估算
  • 模型返回的实际 prompt_tokens 是真实值
  • 校准比例 = Σ(actual) / Σ(estimated)
  • 后续估算 = 原始估算 × 校准比例

使用方式:

ce := NewCalibratedEstimator()
wm := window.NewManager(config, model, ce)
// 每次模型调用后,Agent 喂入校准数据:
ce.Feed(estimatedTokens, actualPromptTokens)

func NewCalibratedEstimator

func NewCalibratedEstimator() *CalibratedEstimator

func (*CalibratedEstimator) Estimate

func (ce *CalibratedEstimator) Estimate(msg *schema.Message) int

Estimate 实现 TokenEstimator 接口

func (*CalibratedEstimator) Feed

func (ce *CalibratedEstimator) Feed(estimated int, actual uint64)

Feed 实现 CalibrationFeed 接口 estimated: 调用前估算的总 token 数 actual: 模型返回的 prompt_tokens

func (*CalibratedEstimator) GetRatio

func (ce *CalibratedEstimator) GetRatio() float64

GetRatio 获取当前校准比例(用于调试)

func (*CalibratedEstimator) GetSampleCount

func (ce *CalibratedEstimator) GetSampleCount() int

GetSampleCount 获取校准样本数

type CalibrationFeed

type CalibrationFeed interface {
	// Feed 输入一次校准数据点
	// estimated: 估算的 token 数(调用前)
	// actual: 实际的 token 数(模型返回的 prompt_tokens)
	Feed(estimated int, actual uint64)
}

CalibrationFeed 校准数据输入接口 Agent 在每次模型调用后,将估算值和实际值喂入,用于动态校准估算比例

type Config

type Config struct {
	// MaxHistoryMessages 最大保留的历史消息数(不包括 System 消息)
	// 例如设置为 20,则只保留最近的 20 条 user/assistant/tool 消息
	// 0 表示不限制(默认,兼容旧行为)
	MaxHistoryMessages int

	// MaxHistoryTokens 历史消息的最大估算 Token 数(不包括 System 消息)
	// 优先级与 MaxHistoryMessages 同级,两者同时设置时取交集(更严格的)
	// 0 表示不限制(默认)
	MaxHistoryTokens int

	// ReserveTokens 自动模式下为模型输出预留的 Token 数
	// 如果设置了此值且 model 实现了 ModelContextWindow 接口,
	// Manager 会自动计算 MaxHistoryTokens = ContextWindow - ReserveTokens
	// 例如:模型 128k 上下文,预留 8k 给输出+缓冲,则历史限制为 120k
	ReserveTokens int
}

Config 对话窗口配置 提供手动限制和自动计算两种模式,两者可同时启用,取更严格的限制

type Manager

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

Manager 对话窗口管理器 在每次模型调用前截断消息列表,防止工作记忆无限增长导致 Token 爆炸

func NewManager

func NewManager(config Config, model chatmodel.BaseModel, estimator TokenEstimator) *Manager

NewManager 创建窗口管理器 model 用于自动获取上下文长度(可选,实现 ModelContextWindow 接口即可) estimator 可自定义 Token 计算方式,传 nil 使用默认估算

func (*Manager) GetConfig

func (wm *Manager) GetConfig() Config

GetConfig 返回当前生效的配置(便于调试)

func (*Manager) Truncate

func (wm *Manager) Truncate(msgs []*schema.Message) []*schema.Message

Truncate 截断消息列表,保留 System 消息,丢弃过旧的对话历史 规则:

  1. 始终保留所有 System 消息(置于开头)
  2. 保留最近的历史消息,从旧消息开始丢弃
  3. 如果截断导致 ToolResult 失去对应的 ToolCall,丢弃该孤立的 ToolResult

type ModelContextWindow

type ModelContextWindow interface {
	ContextWindow() int
}

ModelContextWindow 模型上下文窗口信息接口 各模型实现(如 OpenAI/Gemini/Claude)可实现此接口暴露上下文长度 Manager 会通过类型断言自动识别

type ShortMemory

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

ShortMemory 结合滑动窗口与摘要的短期记忆实现

func NewShortMemory

func NewShortMemory(wm *Manager, model chatmodel.BaseModel, summarizer SummaryFunc) *ShortMemory

func (*ShortMemory) AddTurn

func (ws *ShortMemory) AddTurn(sessionID string, msgs []*schema.Message)

func (*ShortMemory) Clear

func (ws *ShortMemory) Clear(sessionID string)

func (*ShortMemory) GetContextMessages

func (ws *ShortMemory) GetContextMessages(sessionID string) []*schema.Message

GetContextMessages 返回构建上下文所需的短期记忆 修复:RLock → Lock(因为会修改 sess.messages 和 sess.summary)

func (*ShortMemory) GetRecent

func (ws *ShortMemory) GetRecent(sessionID string) []*schema.Message

type SimpleWindowMemory

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

SimpleWindowMemory 纯滑动窗口短期记忆(无摘要)

func NewSimpleWindowMemory

func NewSimpleWindowMemory(wm *Manager) *SimpleWindowMemory

NewSimpleWindowMemory 创建纯滑动窗口短期记忆

func (*SimpleWindowMemory) AddTurn

func (sm *SimpleWindowMemory) AddTurn(sessionID string, msgs []*schema.Message)

AddTurn 添加一轮对话

func (*SimpleWindowMemory) Clear

func (sm *SimpleWindowMemory) Clear(sessionID string)

Clear 清空会话

func (*SimpleWindowMemory) GetContextMessages

func (sm *SimpleWindowMemory) GetContextMessages(sessionID string) []*schema.Message

GetContextMessages 返回构建上下文所需的消息

func (*SimpleWindowMemory) GetRecent

func (sm *SimpleWindowMemory) GetRecent(sessionID string) []*schema.Message

GetRecent 返回截断后的最新消息

type SummaryFunc

type SummaryFunc func(messages []*schema.Message, model chatmodel.BaseModel) string

SummaryFunc 摘要生成函数类型

func DefaultSummaryFunc

func DefaultSummaryFunc() SummaryFunc

DefaultSummaryFunc 默认摘要函数

type TokenEstimator

type TokenEstimator interface {
	Estimate(msg *schema.Message) int
}

TokenEstimator Token 估算器 如需精确控制,可接入 tiktoken 等第三方库实现此接口

Jump to

Keyboard shortcuts

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