azad.compression.strategies.truncation Module¶
azad.compression.strategies.truncation ¶
Truncation compression strategy.
This module provides a deterministic compression strategy that preserves the first human message and the most recent message pairs, removing older messages to fit within context limits.
We can't implement a dynamically updating sliding window as it would break prompt cache every time. To maintain the benefits of caching, we need to keep conversation history static. This operation should be performed as infrequently as possible.
Attributes¶
Classes¶
TruncationStrategy ¶
Bases: CompressionStrategy
Strategy that removes a subset of messages to reduce context size.
This strategy preserves the first human message and the most recent message pairs, while removing others to fit within limits. It is deterministic and replayable, producing the same output given the same input messages and configuration.
When a user reaches a large context, we can assume that the first half is likely irrelevant to their current task. Therefore, this function should only be called when absolutely necessary to fit within context limits, not as a continuous process.
This strategy respects task boundaries and only operates on messages within the current task level.
Initialize the truncation strategy.
Source code in azad/compression/strategies/truncation.py
Attributes¶
strategy_type
property
¶
strategy_type: CompressionStrategyType
Get the type of this compression strategy.
Functions¶
compress ¶
compress(task: Task, new_checkpoint: Optional[CompressionCheckpoint], config: CompressionConfig) -> List[Message]
Compress messages using the truncation strategy.
This method identifies which messages to keep and which to compress based on the truncation configuration. It preserves the first human message and the most recent message pairs based on the configuration.
This method respects task boundaries and only operates on messages within the current task level.
Parameters:
-
task(Task) –The task containing messages to compress
-
new_checkpoint(Optional[CompressionCheckpoint]) –The checkpoint to update (None for transform only)
-
config(CompressionConfig) –Configuration for compression
Returns:
-
List[Message]–The compressed messages list for the current task level
Source code in azad/compression/strategies/truncation.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | |