Skip to content

Compression Package

The azad.compression package provides utilities for managing context windows in large conversations. It implements various compression strategies to ensure that conversations fit within the model's context limits.

Core Components

Core

The Core module defines the core interfaces and classes for the compression system. It includes:

  • CompressionStrategy interface for implementing different compression strategies
  • Compressor class for managing the compression process
  • Type definitions for compression configurations
  • Checkpoint tracking for compression operations

Registry

The Registry module provides a registry for compression strategies. It:

  • Registers compression strategies by type
  • Retrieves strategies by type
  • Manages strategy instantiation

Strategies

The Strategies subpackage contains implementations of different compression strategies:

  • Compact: A strategy that summarizes conversations to reduce context size
  • Truncation: A simple strategy that truncates older messages

How Compression Works

  1. The Compressor loads a task and initializes with a specific compression strategy
  2. When the context window is exceeded, the AINetwork calls the compressor to transform the message history
  3. The compression strategy is applied to reduce the context size
  4. A CompressionCheckpoint is created to track which messages were compressed
  5. A CompressionMessage is added to the task to record the compression operation
  6. The compressed message history is returned and used for the next request

Creating Custom Compression Strategies

To create a custom compression strategy:

  1. Implement the CompressionStrategy interface
  2. Define a configuration class for your strategy
  3. Register your strategy with the CompressionStrategyRegistry

See the Extending Azad guide for more details.