trajectorycompressor

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: MIT Imports: 34 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const OpenRouterBaseURL = "https://openrouter.ai/api/v1"

OpenRouterBaseURL is the default summarization endpoint

Variables

This section is empty.

Functions

func EffectiveTemperatureForModel

func EffectiveTemperatureForModel(model string, requested *float64, baseURL string) (*float64, error)

Temperature a direct call sends, nil when the model manages temperature itself and the call must omit it

func Main

func Main(ctx context.Context, args []string) int

Process entry of the compressor binary, the .env files loaded first and a Python style error line on failure

func NewCommand

func NewCommand() *cobra.Command

Command line of the compressor with fire style flags and positional arguments bound in parameter order

func Run

func Run(ctx context.Context, o Options) error

Compresses a JSONL file or a directory of them into the target token budget, optionally sampling first

Types

type AggregateMetrics

type AggregateMetrics struct {
	TotalTrajectories              int
	TrajectoriesCompressed         int
	TrajectoriesSkippedUnderTarget int
	TrajectoriesStillOverLimit     int
	TrajectoriesFailed             int
	TotalTokensBefore              int
	TotalTokensAfter               int
	TotalTokensSaved               int
	TotalTurnsBefore               int
	TotalTurnsAfter                int
	TotalTurnsRemoved              int
	TotalSummarizationCalls        int
	TotalSummarizationErrors       int
	CompressionRatios              []float64
	TokensSavedList                []int
	TurnsRemovedList               []int
	ProcessingStartTime            string
	ProcessingEndTime              string
	ProcessingDurationSeconds      float64
}

AggregateMetrics are the totals across every trajectory of a run

func (*AggregateMetrics) AddTrajectoryMetrics

func (a *AggregateMetrics) AddTrajectoryMetrics(m *TrajectoryMetrics)

Adds one trajectory's metrics to the totals

func (*AggregateMetrics) ToDict

func (a *AggregateMetrics) ToDict() *pyjson.Object

Ordered dict of summary, tokens, turns, averages, summarization and processing sections

type CompressionConfig

type CompressionConfig struct {
	TokenizerName         string
	TargetMaxTokens       int
	SummaryTargetTokens   int
	ProtectFirstSystem    bool
	ProtectFirstHuman     bool
	ProtectFirstGPT       bool
	ProtectFirstTool      bool
	ProtectLastNTurns     int
	SummarizationModel    string
	BaseURL               string
	APIKeyEnv             string
	Temperature           float64
	MaxRetries            int
	RetryDelay            float64
	AddSummaryNotice      bool
	SummaryNoticeText     string
	OutputSuffix          string
	NumWorkers            int
	MaxConcurrentRequests int
	SkipUnderTarget       bool
	SaveOverLimit         bool
	PerTrajectoryTimeout  int
	MetricsEnabled        bool
	MetricsPerTrajectory  bool
	MetricsOutputFile     string
}

CompressionConfig holds tokenizer, targets, protected turns, summarizer, output, processing and metrics settings

func ConfigFromYAML

func ConfigFromYAML(path string) (*CompressionConfig, error)

Loads a YAML config file, missing sections and keys keeping the defaults and a null base_url keeping its default

func DefaultConfig

func DefaultConfig() *CompressionConfig

Configuration with the dataclass defaults

type Compressor

type Compressor struct {
	Config    *CompressionConfig
	Aggregate *AggregateMetrics
	// contains filtered or unexported fields
}

Compressor fits agent trajectories into a token budget by summarizing part of the middle

func NewCompressor

func NewCompressor(ctx context.Context, cfg *CompressionConfig) (*Compressor, error)

Compressor with its tokenizer loaded and summarizer route resolved

func (*Compressor) CompressTrajectoryAsync

func (c *Compressor) CompressTrajectoryAsync(ctx context.Context, trajectory any) (any, *TrajectoryMetrics, error)

Compresses one trajectory into the target budget through the async call_llm surface

func (*Compressor) CountTokens

func (c *Compressor) CountTokens(text any) (int, error)

Token count of a turn value, zero when empty, a quarter of its length when it is not encodable text

func (*Compressor) CountTrajectoryTokens

func (c *Compressor) CountTrajectoryTokens(traj []any) (int, error)

Total tokens of a trajectory

func (*Compressor) CountTurnTokens

func (c *Compressor) CountTurnTokens(traj []any) ([]int, error)

Token counts of every turn value in order

func (*Compressor) ProcessDirectory

func (c *Compressor) ProcessDirectory(ctx context.Context, inputDir, outputDir string) error

Compresses every *.jsonl of inputDir into outputDir with concurrent summary calls, then reports and saves metrics

func (*Compressor) ProcessEntry

func (c *Compressor) ProcessEntry(ctx context.Context, entry any) (any, *TrajectoryMetrics, error)

Compresses the conversations of one JSONL entry, attaching per trajectory metrics when it was compressed

type Encoder

type Encoder = tokenizers.Encoder

Encoder gives the token ids of a text

func LoadTokenizer

func LoadTokenizer(name string) (Encoder, error)

LoadTokenizer loads the tokenizer of a local folder or a hub model from the tokenizer files it ships

type Options

type Options struct {
	Input           string
	Output          string
	Config          string
	TargetMaxTokens int
	Tokenizer       string
	SamplePercent   *SamplePercent
	Seed            int64
	DryRun          bool
}

Options are the arguments of the trajectory compressor command

type SamplePercent

type SamplePercent struct {
	Value float64
	Repr  string
}

SamplePercent is a sampling percentage with the Python repr it was given as, an int or a float

type TrajectoryMetrics

type TrajectoryMetrics struct {
	OriginalTokens          int
	CompressedTokens        int
	TokensSaved             int
	CompressionRatio        float64
	OriginalTurns           int
	CompressedTurns         int
	TurnsRemoved            int
	TurnsCompressedStartIdx int
	TurnsCompressedEndIdx   int
	TurnsInCompressedRegion int
	WasCompressed           bool
	StillOverLimit          bool
	SkippedUnderTarget      bool
	SummarizationAPICalls   int
	SummarizationErrors     int
}

TrajectoryMetrics are the counts of one trajectory compression

func NewTrajectoryMetrics

func NewTrajectoryMetrics() *TrajectoryMetrics

Metrics with the dataclass defaults

func (*TrajectoryMetrics) ToDict

func (m *TrajectoryMetrics) ToDict() *pyjson.Object

Ordered dict of the metrics with the compression region nested after turns_removed

Jump to

Keyboard shortcuts

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