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:
CompressionStrategyinterface for implementing different compression strategiesCompressorclass 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¶
- The
Compressorloads a task and initializes with a specific compression strategy - When the context window is exceeded, the
AINetworkcalls the compressor to transform the message history - The compression strategy is applied to reduce the context size
- A
CompressionCheckpointis created to track which messages were compressed - A
CompressionMessageis added to the task to record the compression operation - The compressed message history is returned and used for the next request
Creating Custom Compression Strategies¶
To create a custom compression strategy:
- Implement the
CompressionStrategyinterface - Define a configuration class for your strategy
- Register your strategy with the
CompressionStrategyRegistry
See the Extending Azad guide for more details.