-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathcompressor.py
More file actions
283 lines (228 loc) · 9.89 KB
/
compressor.py
File metadata and controls
283 lines (228 loc) · 9.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
"""
compressor.py
-------------
Context Compression & Token Budget Enforcement.
The core problem context engineering solves:
You have 10,000+ characters of potentially relevant context.
Your model can only use ~2,000. What do you keep?
Three strategies:
- TRUNCATE → Fastest, lossy (head truncation)
- SENTENCE → Preserves sentence boundaries
- EXTRACTIVE → Query-aware, best quality (recommended)
Pure Python. No external dependencies.
"""
import re
import logging
from dataclasses import dataclass
from typing import List, Literal
logger = logging.getLogger(__name__)
# ─────────────────────────────────────────────
# Type aliases
# ─────────────────────────────────────────────
Strategy = Literal["truncate", "sentence", "extractive"]
# ─────────────────────────────────────────────
# Utilities
# ─────────────────────────────────────────────
def _split_sentences(text: str) -> List[str]:
"""
Simple sentence splitter using punctuation boundaries.
Preserves the closing punctuation.
"""
parts = re.split(r'(?<=[.!?])\s+', text.strip())
return [p.strip() for p in parts if p.strip()]
def _sentence_score(sentence: str, query: str) -> float:
"""Score sentence by token overlap with the query."""
if not query or not query.strip():
return 0.0
q_tokens = set(query.lower().split())
s_tokens = set(sentence.lower().split())
return len(q_tokens & s_tokens) / len(q_tokens) if q_tokens else 0.0
def estimate_tokens(text: str, chars_per_token: int = 4) -> int:
"""
Rough token estimation (1 token ≈ 4 characters for English prose).
Pass chars_per_token=3 for code, =2 for dense languages.
"""
if chars_per_token < 1:
raise ValueError(f"chars_per_token must be >= 1, got {chars_per_token}.")
return len(text) // chars_per_token
# ─────────────────────────────────────────────
# Compression Result
# ─────────────────────────────────────────────
@dataclass
class CompressionResult:
text: str
original_chars: int
compressed_chars: int
strategy_used: str
estimated_tokens_saved: int
@property
def compression_ratio(self) -> float:
"""Fraction of original size retained (1.0 = no compression)."""
if self.original_chars == 0:
return 1.0
return round(self.compressed_chars / self.original_chars, 3)
def __repr__(self) -> str:
return (
f"CompressionResult("
f"ratio={self.compression_ratio:.1%}, "
f"chars={self.original_chars}→{self.compressed_chars}, "
f"tokens_saved≈{self.estimated_tokens_saved})"
)
# ─────────────────────────────────────────────
# Compressor
# ─────────────────────────────────────────────
class Compressor:
"""
Compresses retrieved documents to fit within a token/character budget.
"""
VALID_STRATEGIES: tuple = ("truncate", "sentence", "extractive")
def __init__(
self,
max_chars: int = 1500,
strategy: Strategy = "extractive",
min_chunk: int = 20,
) -> None:
if strategy not in self.VALID_STRATEGIES:
raise ValueError(
f"strategy must be one of {self.VALID_STRATEGIES}, got {strategy!r}."
)
if max_chars < 1:
raise ValueError(f"max_chars must be >= 1, got {max_chars}.")
self.max_chars = max_chars
self.strategy = strategy
self.min_chunk = min_chunk
def compress(self, chunks: List[str], query: str = "") -> CompressionResult:
"""
Main compression method.
"""
valid_chunks = [c for c in chunks if c and len(c) >= self.min_chunk]
if not valid_chunks:
logger.debug("compress(): no valid chunks after filtering.")
return CompressionResult(
text="",
original_chars=0,
compressed_chars=0,
strategy_used="none (empty input)",
estimated_tokens_saved=0,
)
original = "\n\n".join(valid_chunks)
original_chars = len(original)
# No compression needed
if original_chars <= self.max_chars:
logger.debug("compress(): input already fits budget.")
return CompressionResult(
text=original,
original_chars=original_chars,
compressed_chars=original_chars,
strategy_used="none (fits budget)",
estimated_tokens_saved=0,
)
# Apply chosen strategy
if self.strategy == "truncate":
compressed = self._truncate(valid_chunks)
elif self.strategy == "sentence":
compressed = self._sentence(valid_chunks)
else: # extractive
compressed = self._extractive(valid_chunks, query)
compressed_chars = len(compressed)
tokens_saved = estimate_tokens(original) - estimate_tokens(compressed)
logger.debug(
"compress(): %s | %d → %d chars | ~%d tokens saved",
self.strategy, original_chars, compressed_chars, tokens_saved
)
return CompressionResult(
text=compressed,
original_chars=original_chars,
compressed_chars=compressed_chars,
strategy_used=self.strategy,
estimated_tokens_saved=max(0, tokens_saved),
)
# ── Strategy implementations ──────────────
def _truncate(self, chunks: List[str]) -> str:
"""Simple head truncation."""
budget_per_chunk = self.max_chars // max(len(chunks), 1)
result = "\n\n".join(chunk[:budget_per_chunk] for chunk in chunks)
return result[:self.max_chars]
def _sentence(self, chunks: List[str]) -> str:
"""Greedy sentence-level truncation."""
all_sentences: List[str] = []
for chunk in chunks:
all_sentences.extend(_split_sentences(chunk))
parts: List[str] = []
used = 0
for sentence in all_sentences:
needed = len(sentence) + (1 if parts else 0)
if used + needed > self.max_chars:
break
parts.append(sentence)
used += needed
return " ".join(parts).strip()
def _extractive(self, chunks: List[str], query: str) -> str:
"""
Query-aware extractive compression — best quality strategy.
Improved: consistent separator and cost calculation.
"""
# Build indexed sentences: (chunk_idx, sent_idx, score, text)
indexed: List[tuple] = []
for c_idx, chunk in enumerate(chunks):
for s_idx, sent in enumerate(_split_sentences(chunk)):
score = _sentence_score(sent, query)
indexed.append((c_idx, s_idx, score, sent))
# Rank by relevance
ranked = sorted(indexed, key=lambda x: x[2], reverse=True)
# Greedily select within budget
budget = self.max_chars
selected_keys: List[tuple] = [] # (c_idx, s_idx)
separator = " " # Consistent with final join
for c_idx, s_idx, score, sent in ranked:
# Cost = sentence length + separator length (except for first sentence)
cost = len(sent) + (len(separator) if selected_keys else 0)
if cost <= budget:
selected_keys.append((c_idx, s_idx))
budget -= cost
if budget <= 10: # Small safety margin
break
if not selected_keys:
logger.warning("_extractive: budget too tight, falling back to truncate.")
return self._truncate(chunks)
# Restore original order
selected_set = set(selected_keys)
ordered_text = [
sent
for c_idx, s_idx, score, sent in indexed
if (c_idx, s_idx) in selected_set
]
return separator.join(ordered_text).strip()
# ─────────────────────────────────────────────
# Token Budget Enforcer
# ─────────────────────────────────────────────
class TokenBudget:
"""
Enforces token budget across different context slots.
"""
def __init__(self, total: int = 2048) -> None:
if total < 1:
raise ValueError(f"TokenBudget total must be >= 1, got {total}.")
self.total = total
self._used: dict = {}
def reserve(self, slot: str, tokens: int) -> None:
if tokens < 0:
raise ValueError(f"Token reservation for '{slot}' must be >= 0.")
self._used[slot] = tokens
def reserve_text(self, slot: str, text: str) -> None:
self._used[slot] = estimate_tokens(text)
def remaining(self) -> int:
return max(0, self.total - sum(self._used.values()))
def remaining_chars(self) -> int:
return self.remaining() * 4
def summary(self) -> dict:
usage = dict(self._used)
usage["_remaining"] = self.remaining()
usage["_total"] = self.total
return usage
def __repr__(self) -> str:
return (
f"TokenBudget(total={self.total}, "
f"used={sum(self._used.values())}, "
f"remaining={self.remaining()})"
)