tokentoken 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- tokentoken-1.0.0/LICENSE +21 -0
- tokentoken-1.0.0/MANIFEST.in +5 -0
- tokentoken-1.0.0/PKG-INFO +252 -0
- tokentoken-1.0.0/README.md +215 -0
- tokentoken-1.0.0/pyproject.toml +66 -0
- tokentoken-1.0.0/setup.cfg +4 -0
- tokentoken-1.0.0/tests/__init__.py +1 -0
- tokentoken-1.0.0/tests/conftest.py +29 -0
- tokentoken-1.0.0/tests/test_cli.py +324 -0
- tokentoken-1.0.0/tests/test_prompts.py +50 -0
- tokentoken-1.0.0/tests/test_router.py +46 -0
- tokentoken-1.0.0/tests/test_sdk.py +205 -0
- tokentoken-1.0.0/tests/test_utils.py +147 -0
- tokentoken-1.0.0/tokentoken/__init__.py +32 -0
- tokentoken-1.0.0/tokentoken/cli.py +400 -0
- tokentoken-1.0.0/tokentoken/prompts.py +466 -0
- tokentoken-1.0.0/tokentoken/router.py +46 -0
- tokentoken-1.0.0/tokentoken/sdk.py +332 -0
- tokentoken-1.0.0/tokentoken/utils.py +176 -0
- tokentoken-1.0.0/tokentoken.egg-info/PKG-INFO +252 -0
- tokentoken-1.0.0/tokentoken.egg-info/SOURCES.txt +23 -0
- tokentoken-1.0.0/tokentoken.egg-info/dependency_links.txt +1 -0
- tokentoken-1.0.0/tokentoken.egg-info/entry_points.txt +2 -0
- tokentoken-1.0.0/tokentoken.egg-info/requires.txt +14 -0
- tokentoken-1.0.0/tokentoken.egg-info/top_level.txt +1 -0
tokentoken-1.0.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Sumit Banik
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: tokentoken
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Compile human prompts into dense LLM machine language to save 70% on token costs.
|
|
5
|
+
Author-email: Sumit Banik <thesumitbanik02@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/thesumitbanik/tokentoken
|
|
8
|
+
Project-URL: Documentation, https://github.com/thesumitbanik/tokentoken#readme
|
|
9
|
+
Project-URL: Repository, https://github.com/thesumitbanik/tokentoken
|
|
10
|
+
Project-URL: Issues, https://github.com/thesumitbanik/tokentoken/issues
|
|
11
|
+
Keywords: llm,compression,token,ai,nlp
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
20
|
+
Requires-Python: >=3.9
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
License-File: LICENSE
|
|
23
|
+
Requires-Dist: typer>=0.9.0
|
|
24
|
+
Requires-Dist: rich>=13.0.0
|
|
25
|
+
Requires-Dist: pydantic>=2.0.0
|
|
26
|
+
Requires-Dist: tiktoken>=0.7.0
|
|
27
|
+
Requires-Dist: openai>=1.14.0
|
|
28
|
+
Requires-Dist: google-genai>=0.3.0
|
|
29
|
+
Requires-Dist: anthropic>=0.21.0
|
|
30
|
+
Requires-Dist: ollama>=0.1.7
|
|
31
|
+
Requires-Dist: python-dotenv>=1.0.1
|
|
32
|
+
Provides-Extra: dev
|
|
33
|
+
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
34
|
+
Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
|
|
35
|
+
Requires-Dist: pytest-mock>=3.10.0; extra == "dev"
|
|
36
|
+
Dynamic: license-file
|
|
37
|
+
|
|
38
|
+
# tokentoken
|
|
39
|
+
|
|
40
|
+
[](LICENSE)
|
|
41
|
+
[](https://www.python.org/downloads/)
|
|
42
|
+
|
|
43
|
+
**Compile human prompts into dense LLM machine language — save up to 70% on token costs.**
|
|
44
|
+
|
|
45
|
+
`tokentoken` is a Python SDK and CLI that compresses verbose text into a minimal, LLM-native representation that any capable model can interpret without a codebook — and recovers it on demand.
|
|
46
|
+
|
|
47
|
+
> Based on the research paper [*Large Language Models Do Not Always Need Readable Language*](https://arxiv.org/abs/2606.19857) (arXiv:2606.19857).
|
|
48
|
+
|
|
49
|
+
## Features
|
|
50
|
+
|
|
51
|
+
- **14 compression modes** — every prompt variant from the paper (`default` + `bt_p1`–`bt_p13`)
|
|
52
|
+
- **4 providers** — OpenAI, Google Gemini, Anthropic, and local Ollama
|
|
53
|
+
- **Cross-model readable** — text compressed by one LLM can be read by another
|
|
54
|
+
- **Built-in analytics** — token savings, retention ratio, readability and efficiency metrics
|
|
55
|
+
- **Agentic workflows** — agent memory compression, multi-agent messaging, context-window extension
|
|
56
|
+
- **Clean CLI** — inline text or file input, one-line errors, meaningful exit codes
|
|
57
|
+
|
|
58
|
+
## Installation
|
|
59
|
+
|
|
60
|
+
Requires Python 3.9+.
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
git clone https://github.com/thesumitbanik/tokentoken.git
|
|
64
|
+
cd tokentoken
|
|
65
|
+
pip install .
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
For development:
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
pip install -e ".[dev]"
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Configuration
|
|
75
|
+
|
|
76
|
+
API keys are read from environment variables — never hardcode them:
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
export GEMINI_API_KEY="..." # default provider
|
|
80
|
+
export OPENAI_API_KEY="..."
|
|
81
|
+
export ANTHROPIC_API_KEY="..."
|
|
82
|
+
# Ollama needs no key (local, http://localhost:11434)
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
| Provider | `--provider` | Environment variable | Example `--model` |
|
|
86
|
+
|---|---|---|---|
|
|
87
|
+
| Google Gemini | `gemini` *(default)* | `GEMINI_API_KEY` | `gemini-3.5-flash` *(default)* |
|
|
88
|
+
| OpenAI | `openai` | `OPENAI_API_KEY` | `gpt-4` |
|
|
89
|
+
| Anthropic | `anthropic` | `ANTHROPIC_API_KEY` | `claude-3-5-sonnet` |
|
|
90
|
+
| Ollama | `ollama` | — | `llama3` |
|
|
91
|
+
|
|
92
|
+
### OpenAI-compatible endpoints
|
|
93
|
+
|
|
94
|
+
Point the `openai` provider at any OpenAI-compatible server — vLLM, LM Studio, LocalAI, OpenRouter, or Ollama's `/v1` API:
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
export OPENAI_API_KEY="not-needed" # placeholder for keyless local servers
|
|
98
|
+
tokentoken compress --text "..." --provider openai --model my-model \
|
|
99
|
+
--base-url http://localhost:1234/v1
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
```python
|
|
103
|
+
tt = TokenToken(provider="openai", model="my-model", base_url="http://localhost:1234/v1")
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
No flag needed if you prefer environment configuration: the OpenAI client reads `OPENAI_BASE_URL` automatically.
|
|
107
|
+
|
|
108
|
+
## Quick Start
|
|
109
|
+
|
|
110
|
+
### Python SDK
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
from tokentoken import TokenToken, CompressionMode
|
|
114
|
+
|
|
115
|
+
# Uses GEMINI_API_KEY from the environment (or pass api_key="...")
|
|
116
|
+
tt = TokenToken(provider="gemini", model="gemini-3.5-flash")
|
|
117
|
+
|
|
118
|
+
result = tt.compress("Your verbose text here...", mode=CompressionMode.DEFAULT)
|
|
119
|
+
|
|
120
|
+
print(f"Original: {result.original_tokens} tokens")
|
|
121
|
+
print(f"Compressed: {result.compressed_tokens} tokens")
|
|
122
|
+
print(f"Reduction: {result.savings_pct}%")
|
|
123
|
+
|
|
124
|
+
# Any capable LLM can interpret it — no codebook required
|
|
125
|
+
answer = tt.decompress(result.dense_text, question="What is the main topic?")
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### CLI
|
|
129
|
+
|
|
130
|
+
```bash
|
|
131
|
+
# Compress a file
|
|
132
|
+
tokentoken compress input.txt --out dense.txt
|
|
133
|
+
|
|
134
|
+
# Or inline text — no file needed
|
|
135
|
+
tokentoken compress --text "Your verbose text here..."
|
|
136
|
+
|
|
137
|
+
# Read it back
|
|
138
|
+
tokentoken decompress dense.txt --question "What is the main topic?"
|
|
139
|
+
|
|
140
|
+
# Explore
|
|
141
|
+
tokentoken --help
|
|
142
|
+
tokentoken list-modes
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## CLI Reference
|
|
146
|
+
|
|
147
|
+
Run any command with `--help` for full options.
|
|
148
|
+
|
|
149
|
+
| Command | Description |
|
|
150
|
+
|---|---|
|
|
151
|
+
| `compress` | Compress a file or inline `--text`; writes `--out` (default `dense.txt`) |
|
|
152
|
+
| `decompress` | Interpret compressed text; optional `--question` to answer from it |
|
|
153
|
+
| `analyze` | Measure compression potential; add `--compressed` to compare against an output |
|
|
154
|
+
| `list-modes` | List all 14 compression modes with paper references |
|
|
155
|
+
| `estimate` | Count tokens in a file |
|
|
156
|
+
| `agent-memory` | Compress agent conversation histories from a directory |
|
|
157
|
+
| `multi-agent` | Compress an inter-agent message between `--sender` and `--receiver` |
|
|
158
|
+
| `cross-model` | Verify compressed text remains readable by a different model |
|
|
159
|
+
| `extend-context` | Compress a long document down to a `--target-tokens` budget |
|
|
160
|
+
|
|
161
|
+
**Common options:** `--provider` (default `gemini`), `--model` (default `gemini-3.5-flash`), `--out/-o`, `--mode` (for `compress`), and `--base-url` (OpenAI-compatible endpoints).
|
|
162
|
+
|
|
163
|
+
Failures print a single-line error and exit with code `1` — never a traceback.
|
|
164
|
+
|
|
165
|
+
## Python API
|
|
166
|
+
|
|
167
|
+
### `TokenToken(provider, model, api_key=None, host=None, base_url=None)`
|
|
168
|
+
|
|
169
|
+
| Method | Returns | Description |
|
|
170
|
+
|---|---|---|
|
|
171
|
+
| `compress(text, mode=CompressionMode.DEFAULT)` | `CompressionResult` | Compress text into dense form |
|
|
172
|
+
| `decompress(text, question=None)` | `str` | Interpret compressed text / answer a question |
|
|
173
|
+
| `compress_for_agent_memory(memories, session_id=None)` | `AgentMemoryResult` | Compress conversation histories |
|
|
174
|
+
| `compress_for_multi_agent(message, sender, receiver)` | `MultiAgentMessage` | Compress inter-agent messages |
|
|
175
|
+
| `check_cross_model_compatibility(text, reader_provider, reader_model)` | `CrossModelCompatibility` | Test readability across models |
|
|
176
|
+
| `extend_context_window(long_text, target_tokens=200000)` | `str` | Compress long documents to a token budget |
|
|
177
|
+
| `get_compression_analytics(original, compressed)` | `dict` | Detailed compression metrics |
|
|
178
|
+
|
|
179
|
+
Utility functions are exported at package level: `count_tokens`, `check_breakeven_threshold`, `chunk_text`, `calculate_compression_metrics`, `estimate_readability`, `calculate_token_efficiency`, `validate_compression_result`.
|
|
180
|
+
|
|
181
|
+
### `CompressionResult`
|
|
182
|
+
|
|
183
|
+
| Field | Type | Description |
|
|
184
|
+
|---|---|---|
|
|
185
|
+
| `dense_text` | `str` | The compressed output |
|
|
186
|
+
| `original_tokens` | `int` | Input token count |
|
|
187
|
+
| `compressed_tokens` | `int` | Output token count |
|
|
188
|
+
| `savings_pct` | `float` | Token reduction in percent |
|
|
189
|
+
| `retention_ratio` | `float` | Output size ÷ input size |
|
|
190
|
+
| `readability_metrics` | `dict` | Dale-Chall estimate, difficult-word ratio, word count |
|
|
191
|
+
| `efficiency_metrics` | `dict` | Tokens per word, chars per token, compression potential |
|
|
192
|
+
| `validation` | `dict` | Quality checks + chain-of-thought tax warning |
|
|
193
|
+
|
|
194
|
+
### Compression Modes
|
|
195
|
+
|
|
196
|
+
Select with `--mode bt_p7` (CLI) or `mode=CompressionMode.BT_P7` (SDK).
|
|
197
|
+
|
|
198
|
+
| Mode | Paper ref | Description |
|
|
199
|
+
|---|---|---|
|
|
200
|
+
| `default` | C.1 | Default compression prompt |
|
|
201
|
+
| `bt_p1` | C.2.1 | Adaptive Symbolic Collapse |
|
|
202
|
+
| `bt_p2` | C.2.2 | Refined Zero-Overhead Compression |
|
|
203
|
+
| `bt_p3` | C.2.3 | Minimal Lossless Objective |
|
|
204
|
+
| `bt_p4` | C.2.4 | Structured Omnilingual Mapping |
|
|
205
|
+
| `bt_p5` | C.2.5 | Canonical Omnilingual-Symbolic |
|
|
206
|
+
| `bt_p6` | C.2.6 | Structured Mapping Control |
|
|
207
|
+
| `bt_p7` | C.2.7 | Canonical Compression Objective |
|
|
208
|
+
| `bt_p8` | C.2.8 | Fixed Symbolic Mapping Rules |
|
|
209
|
+
| `bt_p9` | C.2.9 | Structured Semantic Mapping |
|
|
210
|
+
| `bt_p10` | C.2.10 | LLM-Native Compressor |
|
|
211
|
+
| `bt_p11` | C.2.11 | Compact Symbolic Mapping |
|
|
212
|
+
| `bt_p12` | C.2.12 | Free-Emergence Attention Checklist |
|
|
213
|
+
| `bt_p13` | C.2.13 | ASCII Anchor Skeleton |
|
|
214
|
+
|
|
215
|
+
## How It Works
|
|
216
|
+
|
|
217
|
+
```mermaid
|
|
218
|
+
---
|
|
219
|
+
config:
|
|
220
|
+
look: handDrawn
|
|
221
|
+
theme: neutral
|
|
222
|
+
---
|
|
223
|
+
flowchart LR
|
|
224
|
+
A["Verbose human text"] --> B["Compress: paper prompt + your LLM"]
|
|
225
|
+
B --> C["Dense text (~30% of tokens)"]
|
|
226
|
+
C --> D["Decompress: any capable LLM"]
|
|
227
|
+
D --> E["Answers / full meaning"]
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
Compression swaps verbose natural language for high-density multilingual and symbolic forms. No decoder, codebook, or fine-tuning is involved — interpretation is a capability the reading LLM already has.
|
|
231
|
+
|
|
232
|
+
## Use Cases
|
|
233
|
+
|
|
234
|
+
- **Document QA** - compress long documents while preserving semantic fidelity for question answering
|
|
235
|
+
- **Agent memory** - compress conversation histories to reduce storage while maintaining reliable recall
|
|
236
|
+
- **Multi-agent communication** - cut context overhead between collaborating agents
|
|
237
|
+
- **Context window extension** - handle documents that exceed model limits by compressing chunks
|
|
238
|
+
|
|
239
|
+
## Contributing
|
|
240
|
+
|
|
241
|
+
```bash
|
|
242
|
+
git clone https://github.com/sumitbanik/tokentoken.git
|
|
243
|
+
cd tokentoken
|
|
244
|
+
pip install -e ".[dev]"
|
|
245
|
+
pytest
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
Issues and pull requests are welcome.
|
|
249
|
+
|
|
250
|
+
## License
|
|
251
|
+
|
|
252
|
+
MIT — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
# tokentoken
|
|
2
|
+
|
|
3
|
+
[](LICENSE)
|
|
4
|
+
[](https://www.python.org/downloads/)
|
|
5
|
+
|
|
6
|
+
**Compile human prompts into dense LLM machine language — save up to 70% on token costs.**
|
|
7
|
+
|
|
8
|
+
`tokentoken` is a Python SDK and CLI that compresses verbose text into a minimal, LLM-native representation that any capable model can interpret without a codebook — and recovers it on demand.
|
|
9
|
+
|
|
10
|
+
> Based on the research paper [*Large Language Models Do Not Always Need Readable Language*](https://arxiv.org/abs/2606.19857) (arXiv:2606.19857).
|
|
11
|
+
|
|
12
|
+
## Features
|
|
13
|
+
|
|
14
|
+
- **14 compression modes** — every prompt variant from the paper (`default` + `bt_p1`–`bt_p13`)
|
|
15
|
+
- **4 providers** — OpenAI, Google Gemini, Anthropic, and local Ollama
|
|
16
|
+
- **Cross-model readable** — text compressed by one LLM can be read by another
|
|
17
|
+
- **Built-in analytics** — token savings, retention ratio, readability and efficiency metrics
|
|
18
|
+
- **Agentic workflows** — agent memory compression, multi-agent messaging, context-window extension
|
|
19
|
+
- **Clean CLI** — inline text or file input, one-line errors, meaningful exit codes
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
|
|
23
|
+
Requires Python 3.9+.
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
git clone https://github.com/thesumitbanik/tokentoken.git
|
|
27
|
+
cd tokentoken
|
|
28
|
+
pip install .
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
For development:
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
pip install -e ".[dev]"
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Configuration
|
|
38
|
+
|
|
39
|
+
API keys are read from environment variables — never hardcode them:
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
export GEMINI_API_KEY="..." # default provider
|
|
43
|
+
export OPENAI_API_KEY="..."
|
|
44
|
+
export ANTHROPIC_API_KEY="..."
|
|
45
|
+
# Ollama needs no key (local, http://localhost:11434)
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
| Provider | `--provider` | Environment variable | Example `--model` |
|
|
49
|
+
|---|---|---|---|
|
|
50
|
+
| Google Gemini | `gemini` *(default)* | `GEMINI_API_KEY` | `gemini-3.5-flash` *(default)* |
|
|
51
|
+
| OpenAI | `openai` | `OPENAI_API_KEY` | `gpt-4` |
|
|
52
|
+
| Anthropic | `anthropic` | `ANTHROPIC_API_KEY` | `claude-3-5-sonnet` |
|
|
53
|
+
| Ollama | `ollama` | — | `llama3` |
|
|
54
|
+
|
|
55
|
+
### OpenAI-compatible endpoints
|
|
56
|
+
|
|
57
|
+
Point the `openai` provider at any OpenAI-compatible server — vLLM, LM Studio, LocalAI, OpenRouter, or Ollama's `/v1` API:
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
export OPENAI_API_KEY="not-needed" # placeholder for keyless local servers
|
|
61
|
+
tokentoken compress --text "..." --provider openai --model my-model \
|
|
62
|
+
--base-url http://localhost:1234/v1
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
tt = TokenToken(provider="openai", model="my-model", base_url="http://localhost:1234/v1")
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
No flag needed if you prefer environment configuration: the OpenAI client reads `OPENAI_BASE_URL` automatically.
|
|
70
|
+
|
|
71
|
+
## Quick Start
|
|
72
|
+
|
|
73
|
+
### Python SDK
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
from tokentoken import TokenToken, CompressionMode
|
|
77
|
+
|
|
78
|
+
# Uses GEMINI_API_KEY from the environment (or pass api_key="...")
|
|
79
|
+
tt = TokenToken(provider="gemini", model="gemini-3.5-flash")
|
|
80
|
+
|
|
81
|
+
result = tt.compress("Your verbose text here...", mode=CompressionMode.DEFAULT)
|
|
82
|
+
|
|
83
|
+
print(f"Original: {result.original_tokens} tokens")
|
|
84
|
+
print(f"Compressed: {result.compressed_tokens} tokens")
|
|
85
|
+
print(f"Reduction: {result.savings_pct}%")
|
|
86
|
+
|
|
87
|
+
# Any capable LLM can interpret it — no codebook required
|
|
88
|
+
answer = tt.decompress(result.dense_text, question="What is the main topic?")
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### CLI
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
# Compress a file
|
|
95
|
+
tokentoken compress input.txt --out dense.txt
|
|
96
|
+
|
|
97
|
+
# Or inline text — no file needed
|
|
98
|
+
tokentoken compress --text "Your verbose text here..."
|
|
99
|
+
|
|
100
|
+
# Read it back
|
|
101
|
+
tokentoken decompress dense.txt --question "What is the main topic?"
|
|
102
|
+
|
|
103
|
+
# Explore
|
|
104
|
+
tokentoken --help
|
|
105
|
+
tokentoken list-modes
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## CLI Reference
|
|
109
|
+
|
|
110
|
+
Run any command with `--help` for full options.
|
|
111
|
+
|
|
112
|
+
| Command | Description |
|
|
113
|
+
|---|---|
|
|
114
|
+
| `compress` | Compress a file or inline `--text`; writes `--out` (default `dense.txt`) |
|
|
115
|
+
| `decompress` | Interpret compressed text; optional `--question` to answer from it |
|
|
116
|
+
| `analyze` | Measure compression potential; add `--compressed` to compare against an output |
|
|
117
|
+
| `list-modes` | List all 14 compression modes with paper references |
|
|
118
|
+
| `estimate` | Count tokens in a file |
|
|
119
|
+
| `agent-memory` | Compress agent conversation histories from a directory |
|
|
120
|
+
| `multi-agent` | Compress an inter-agent message between `--sender` and `--receiver` |
|
|
121
|
+
| `cross-model` | Verify compressed text remains readable by a different model |
|
|
122
|
+
| `extend-context` | Compress a long document down to a `--target-tokens` budget |
|
|
123
|
+
|
|
124
|
+
**Common options:** `--provider` (default `gemini`), `--model` (default `gemini-3.5-flash`), `--out/-o`, `--mode` (for `compress`), and `--base-url` (OpenAI-compatible endpoints).
|
|
125
|
+
|
|
126
|
+
Failures print a single-line error and exit with code `1` — never a traceback.
|
|
127
|
+
|
|
128
|
+
## Python API
|
|
129
|
+
|
|
130
|
+
### `TokenToken(provider, model, api_key=None, host=None, base_url=None)`
|
|
131
|
+
|
|
132
|
+
| Method | Returns | Description |
|
|
133
|
+
|---|---|---|
|
|
134
|
+
| `compress(text, mode=CompressionMode.DEFAULT)` | `CompressionResult` | Compress text into dense form |
|
|
135
|
+
| `decompress(text, question=None)` | `str` | Interpret compressed text / answer a question |
|
|
136
|
+
| `compress_for_agent_memory(memories, session_id=None)` | `AgentMemoryResult` | Compress conversation histories |
|
|
137
|
+
| `compress_for_multi_agent(message, sender, receiver)` | `MultiAgentMessage` | Compress inter-agent messages |
|
|
138
|
+
| `check_cross_model_compatibility(text, reader_provider, reader_model)` | `CrossModelCompatibility` | Test readability across models |
|
|
139
|
+
| `extend_context_window(long_text, target_tokens=200000)` | `str` | Compress long documents to a token budget |
|
|
140
|
+
| `get_compression_analytics(original, compressed)` | `dict` | Detailed compression metrics |
|
|
141
|
+
|
|
142
|
+
Utility functions are exported at package level: `count_tokens`, `check_breakeven_threshold`, `chunk_text`, `calculate_compression_metrics`, `estimate_readability`, `calculate_token_efficiency`, `validate_compression_result`.
|
|
143
|
+
|
|
144
|
+
### `CompressionResult`
|
|
145
|
+
|
|
146
|
+
| Field | Type | Description |
|
|
147
|
+
|---|---|---|
|
|
148
|
+
| `dense_text` | `str` | The compressed output |
|
|
149
|
+
| `original_tokens` | `int` | Input token count |
|
|
150
|
+
| `compressed_tokens` | `int` | Output token count |
|
|
151
|
+
| `savings_pct` | `float` | Token reduction in percent |
|
|
152
|
+
| `retention_ratio` | `float` | Output size ÷ input size |
|
|
153
|
+
| `readability_metrics` | `dict` | Dale-Chall estimate, difficult-word ratio, word count |
|
|
154
|
+
| `efficiency_metrics` | `dict` | Tokens per word, chars per token, compression potential |
|
|
155
|
+
| `validation` | `dict` | Quality checks + chain-of-thought tax warning |
|
|
156
|
+
|
|
157
|
+
### Compression Modes
|
|
158
|
+
|
|
159
|
+
Select with `--mode bt_p7` (CLI) or `mode=CompressionMode.BT_P7` (SDK).
|
|
160
|
+
|
|
161
|
+
| Mode | Paper ref | Description |
|
|
162
|
+
|---|---|---|
|
|
163
|
+
| `default` | C.1 | Default compression prompt |
|
|
164
|
+
| `bt_p1` | C.2.1 | Adaptive Symbolic Collapse |
|
|
165
|
+
| `bt_p2` | C.2.2 | Refined Zero-Overhead Compression |
|
|
166
|
+
| `bt_p3` | C.2.3 | Minimal Lossless Objective |
|
|
167
|
+
| `bt_p4` | C.2.4 | Structured Omnilingual Mapping |
|
|
168
|
+
| `bt_p5` | C.2.5 | Canonical Omnilingual-Symbolic |
|
|
169
|
+
| `bt_p6` | C.2.6 | Structured Mapping Control |
|
|
170
|
+
| `bt_p7` | C.2.7 | Canonical Compression Objective |
|
|
171
|
+
| `bt_p8` | C.2.8 | Fixed Symbolic Mapping Rules |
|
|
172
|
+
| `bt_p9` | C.2.9 | Structured Semantic Mapping |
|
|
173
|
+
| `bt_p10` | C.2.10 | LLM-Native Compressor |
|
|
174
|
+
| `bt_p11` | C.2.11 | Compact Symbolic Mapping |
|
|
175
|
+
| `bt_p12` | C.2.12 | Free-Emergence Attention Checklist |
|
|
176
|
+
| `bt_p13` | C.2.13 | ASCII Anchor Skeleton |
|
|
177
|
+
|
|
178
|
+
## How It Works
|
|
179
|
+
|
|
180
|
+
```mermaid
|
|
181
|
+
---
|
|
182
|
+
config:
|
|
183
|
+
look: handDrawn
|
|
184
|
+
theme: neutral
|
|
185
|
+
---
|
|
186
|
+
flowchart LR
|
|
187
|
+
A["Verbose human text"] --> B["Compress: paper prompt + your LLM"]
|
|
188
|
+
B --> C["Dense text (~30% of tokens)"]
|
|
189
|
+
C --> D["Decompress: any capable LLM"]
|
|
190
|
+
D --> E["Answers / full meaning"]
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
Compression swaps verbose natural language for high-density multilingual and symbolic forms. No decoder, codebook, or fine-tuning is involved — interpretation is a capability the reading LLM already has.
|
|
194
|
+
|
|
195
|
+
## Use Cases
|
|
196
|
+
|
|
197
|
+
- **Document QA** - compress long documents while preserving semantic fidelity for question answering
|
|
198
|
+
- **Agent memory** - compress conversation histories to reduce storage while maintaining reliable recall
|
|
199
|
+
- **Multi-agent communication** - cut context overhead between collaborating agents
|
|
200
|
+
- **Context window extension** - handle documents that exceed model limits by compressing chunks
|
|
201
|
+
|
|
202
|
+
## Contributing
|
|
203
|
+
|
|
204
|
+
```bash
|
|
205
|
+
git clone https://github.com/sumitbanik/tokentoken.git
|
|
206
|
+
cd tokentoken
|
|
207
|
+
pip install -e ".[dev]"
|
|
208
|
+
pytest
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
Issues and pull requests are welcome.
|
|
212
|
+
|
|
213
|
+
## License
|
|
214
|
+
|
|
215
|
+
MIT — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[tool.setuptools.packages.find]
|
|
6
|
+
include = ["tokentoken*"]
|
|
7
|
+
exclude = ["tests*"]
|
|
8
|
+
|
|
9
|
+
[project]
|
|
10
|
+
name = "tokentoken"
|
|
11
|
+
version = "1.0.0"
|
|
12
|
+
description = "Compile human prompts into dense LLM machine language to save 70% on token costs."
|
|
13
|
+
readme = "README.md"
|
|
14
|
+
requires-python = ">=3.9"
|
|
15
|
+
license = "MIT"
|
|
16
|
+
authors = [{name = "Sumit Banik", email = "thesumitbanik02@gmail.com"}]
|
|
17
|
+
keywords = ["llm", "compression", "token", "ai", "nlp"]
|
|
18
|
+
classifiers = [
|
|
19
|
+
"Development Status :: 4 - Beta",
|
|
20
|
+
"Intended Audience :: Developers",
|
|
21
|
+
"Programming Language :: Python :: 3",
|
|
22
|
+
"Programming Language :: Python :: 3.9",
|
|
23
|
+
"Programming Language :: Python :: 3.10",
|
|
24
|
+
"Programming Language :: Python :: 3.11",
|
|
25
|
+
"Programming Language :: Python :: 3.12",
|
|
26
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
|
27
|
+
]
|
|
28
|
+
dependencies = [
|
|
29
|
+
"typer>=0.9.0",
|
|
30
|
+
"rich>=13.0.0",
|
|
31
|
+
"pydantic>=2.0.0",
|
|
32
|
+
"tiktoken>=0.7.0",
|
|
33
|
+
"openai>=1.14.0",
|
|
34
|
+
"google-genai>=0.3.0",
|
|
35
|
+
"anthropic>=0.21.0",
|
|
36
|
+
"ollama>=0.1.7",
|
|
37
|
+
"python-dotenv>=1.0.1"
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
[project.urls]
|
|
41
|
+
Homepage = "https://github.com/thesumitbanik/tokentoken"
|
|
42
|
+
Documentation = "https://github.com/thesumitbanik/tokentoken#readme"
|
|
43
|
+
Repository = "https://github.com/thesumitbanik/tokentoken"
|
|
44
|
+
Issues = "https://github.com/thesumitbanik/tokentoken/issues"
|
|
45
|
+
|
|
46
|
+
[project.scripts]
|
|
47
|
+
# This line is the magic that makes your CLI work globally!
|
|
48
|
+
tokentoken = "tokentoken.cli:app"
|
|
49
|
+
|
|
50
|
+
[project.optional-dependencies]
|
|
51
|
+
dev = [
|
|
52
|
+
"pytest>=7.0.0",
|
|
53
|
+
"pytest-cov>=4.0.0",
|
|
54
|
+
"pytest-mock>=3.10.0",
|
|
55
|
+
]
|
|
56
|
+
|
|
57
|
+
[tool.pytest.ini_options]
|
|
58
|
+
testpaths = ["tests"]
|
|
59
|
+
python_files = ["test_*.py"]
|
|
60
|
+
python_classes = ["Test*"]
|
|
61
|
+
python_functions = ["test_*"]
|
|
62
|
+
addopts = "-v --tb=short"
|
|
63
|
+
markers = [
|
|
64
|
+
"slow: marks tests as slow (deselect with '-m \"not slow\"')",
|
|
65
|
+
"integration: marks tests as integration tests",
|
|
66
|
+
]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# tests/__init__.py
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# tests/conftest.py
|
|
2
|
+
import pytest
|
|
3
|
+
import tempfile
|
|
4
|
+
import os
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@pytest.fixture
|
|
8
|
+
def sample_text():
|
|
9
|
+
"""Sample text for testing."""
|
|
10
|
+
return """Large language models (LLMs) have become a dominant interface in contemporary intelligent systems.
|
|
11
|
+
Since GPT-3 demonstrated strong few-shot generalization through text-based prompting,
|
|
12
|
+
the field has largely followed a unified paradigm: knowledge is represented in natural language,
|
|
13
|
+
instructions are issued in natural language, and model outputs are returned in natural language."""
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@pytest.fixture
|
|
17
|
+
def compressed_text():
|
|
18
|
+
"""Sample compressed text for testing."""
|
|
19
|
+
return """LLM>dominant iface. GPT-3 fewshot. Paradigm: knowledge∈NL, instr∈NL, output∈NL."""
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@pytest.fixture
|
|
23
|
+
def tmp_text_file(tmp_path):
|
|
24
|
+
"""Create a temporary text file for CLI testing."""
|
|
25
|
+
def _create(content, filename="test.txt"):
|
|
26
|
+
file_path = tmp_path / filename
|
|
27
|
+
file_path.write_text(content)
|
|
28
|
+
return file_path
|
|
29
|
+
return _create
|