fiedler-compress 0.1.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.
- fiedler_compress-0.1.0/.gitignore +26 -0
- fiedler_compress-0.1.0/LICENSE +105 -0
- fiedler_compress-0.1.0/PKG-INFO +190 -0
- fiedler_compress-0.1.0/README.md +156 -0
- fiedler_compress-0.1.0/fiedler_optimizer/__init__.py +36 -0
- fiedler_compress-0.1.0/fiedler_optimizer/_tier.py +29 -0
- fiedler_compress-0.1.0/fiedler_optimizer/benchmarks/__init__.py +1 -0
- fiedler_compress-0.1.0/fiedler_optimizer/benchmarks/latency.py +525 -0
- fiedler_compress-0.1.0/fiedler_optimizer/benchmarks/quality.py +5834 -0
- fiedler_compress-0.1.0/fiedler_optimizer/caveman.py +483 -0
- fiedler_compress-0.1.0/fiedler_optimizer/chunker.py +219 -0
- fiedler_compress-0.1.0/fiedler_optimizer/cli.py +697 -0
- fiedler_compress-0.1.0/fiedler_optimizer/core.py +506 -0
- fiedler_compress-0.1.0/fiedler_optimizer/docs/worked_example_6.md +59 -0
- fiedler_compress-0.1.0/fiedler_optimizer/docs/worked_example_7.md +77 -0
- fiedler_compress-0.1.0/fiedler_optimizer/graph.py +293 -0
- fiedler_compress-0.1.0/fiedler_optimizer/pinning.py +85 -0
- fiedler_compress-0.1.0/fiedler_optimizer/zones.py +135 -0
- fiedler_compress-0.1.0/pyproject.toml +78 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
__pycache__/
|
|
2
|
+
*.py[cod]
|
|
3
|
+
*$py.class
|
|
4
|
+
*.egg-info/
|
|
5
|
+
dist/
|
|
6
|
+
build/
|
|
7
|
+
.eggs/
|
|
8
|
+
*.egg
|
|
9
|
+
.pytest_cache/
|
|
10
|
+
.ruff_cache/
|
|
11
|
+
.venv/
|
|
12
|
+
venv/
|
|
13
|
+
.env
|
|
14
|
+
*.so
|
|
15
|
+
.coverage
|
|
16
|
+
htmlcov/
|
|
17
|
+
fiedler_env/
|
|
18
|
+
fiedler_env.*_copy_*/
|
|
19
|
+
.claude/
|
|
20
|
+
|
|
21
|
+
# Commercial/paid tier — never published
|
|
22
|
+
_private_paid/
|
|
23
|
+
|
|
24
|
+
# Secrets / keys
|
|
25
|
+
*.key
|
|
26
|
+
*.pem
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# Functional Source License, Version 1.1, ALv2 Future License
|
|
2
|
+
|
|
3
|
+
## Abbreviation
|
|
4
|
+
|
|
5
|
+
FSL-1.1-ALv2
|
|
6
|
+
|
|
7
|
+
## Notice
|
|
8
|
+
|
|
9
|
+
Copyright 2026 Tensor-Earth-Intelligence LLC
|
|
10
|
+
|
|
11
|
+
## Terms and Conditions
|
|
12
|
+
|
|
13
|
+
### Licensor ("We")
|
|
14
|
+
|
|
15
|
+
The party offering the Software under these Terms and Conditions.
|
|
16
|
+
|
|
17
|
+
### The Software
|
|
18
|
+
|
|
19
|
+
The "Software" is each version of the software that we make available under
|
|
20
|
+
these Terms and Conditions, as indicated by our inclusion of these Terms and
|
|
21
|
+
Conditions with the Software.
|
|
22
|
+
|
|
23
|
+
### License Grant
|
|
24
|
+
|
|
25
|
+
Subject to your compliance with this License Grant and the Patents,
|
|
26
|
+
Redistribution and Trademark clauses below, we hereby grant you the right to
|
|
27
|
+
use, copy, modify, create derivative works, publicly perform, publicly display
|
|
28
|
+
and redistribute the Software for any Permitted Purpose identified below.
|
|
29
|
+
|
|
30
|
+
### Permitted Purpose
|
|
31
|
+
|
|
32
|
+
A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
|
|
33
|
+
means making the Software available to others in a commercial product or
|
|
34
|
+
service that:
|
|
35
|
+
|
|
36
|
+
1. substitutes for the Software;
|
|
37
|
+
|
|
38
|
+
2. substitutes for any other product or service we offer using the Software
|
|
39
|
+
that exists as of the date we make the Software available; or
|
|
40
|
+
|
|
41
|
+
3. offers the same or substantially similar functionality as the Software.
|
|
42
|
+
|
|
43
|
+
Permitted Purposes specifically include using the Software:
|
|
44
|
+
|
|
45
|
+
1. for your internal use and access;
|
|
46
|
+
|
|
47
|
+
2. for non-commercial education;
|
|
48
|
+
|
|
49
|
+
3. for non-commercial research; and
|
|
50
|
+
|
|
51
|
+
4. in connection with professional services that you provide to a licensee
|
|
52
|
+
using the Software in accordance with these Terms and Conditions.
|
|
53
|
+
|
|
54
|
+
### Patents
|
|
55
|
+
|
|
56
|
+
To the extent your use for a Permitted Purpose would necessarily infringe our
|
|
57
|
+
patents, the license grant above includes a license under our patents. If you
|
|
58
|
+
make a claim against any party that the Software infringes or contributes to
|
|
59
|
+
the infringement of any patent, then your patent license to the Software ends
|
|
60
|
+
immediately.
|
|
61
|
+
|
|
62
|
+
### Redistribution
|
|
63
|
+
|
|
64
|
+
The Terms and Conditions apply to all copies, modifications and derivatives of
|
|
65
|
+
the Software.
|
|
66
|
+
|
|
67
|
+
If you redistribute any copies, modifications or derivatives of the Software,
|
|
68
|
+
you must include a copy of or a link to these Terms and Conditions and not
|
|
69
|
+
remove any copyright notices provided in or with the Software.
|
|
70
|
+
|
|
71
|
+
### Disclaimer
|
|
72
|
+
|
|
73
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
|
|
74
|
+
IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
|
|
75
|
+
PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
|
|
76
|
+
|
|
77
|
+
IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
|
|
78
|
+
SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES,
|
|
79
|
+
EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
|
|
80
|
+
|
|
81
|
+
### Trademarks
|
|
82
|
+
|
|
83
|
+
Except for displaying the License Details and identifying us as the origin of
|
|
84
|
+
the Software, you have no right under these Terms and Conditions to use our
|
|
85
|
+
trademarks, trade names, service marks or product names.
|
|
86
|
+
|
|
87
|
+
## Grant of Future License
|
|
88
|
+
|
|
89
|
+
We hereby irrevocably grant you an additional license to use the Software under
|
|
90
|
+
the Apache License, Version 2.0 that is effective on the second anniversary of
|
|
91
|
+
the date we make the Software available. On or after that date, you may use the
|
|
92
|
+
Software under the Apache License, Version 2.0, in which case the following
|
|
93
|
+
will apply:
|
|
94
|
+
|
|
95
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
96
|
+
this file except in compliance with the License.
|
|
97
|
+
|
|
98
|
+
You may obtain a copy of the License at
|
|
99
|
+
|
|
100
|
+
<http://www.apache.org/licenses/LICENSE-2.0>
|
|
101
|
+
|
|
102
|
+
Unless required by applicable law or agreed to in writing, software distributed
|
|
103
|
+
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
104
|
+
CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
105
|
+
specific language governing permissions and limitations under the License.
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: fiedler-compress
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Spectral graph-theoretic prompt compression using Fiedler vector decomposition
|
|
5
|
+
Project-URL: Homepage, https://github.com/Tensor-Earth-Intelligence/fiedler-compress
|
|
6
|
+
Project-URL: Issues, https://github.com/Tensor-Earth-Intelligence/fiedler-compress/issues
|
|
7
|
+
Author: Mark Chappell
|
|
8
|
+
License-Expression: FSL-1.1-ALv2
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Keywords: context-window,fiedler-vector,llm,prompt-compression,spectral-graph,token-optimization
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
18
|
+
Classifier: Topic :: Text Processing :: Linguistic
|
|
19
|
+
Requires-Python: >=3.10
|
|
20
|
+
Requires-Dist: numpy<3,>=1.24
|
|
21
|
+
Requires-Dist: scipy<2,>=1.10
|
|
22
|
+
Provides-Extra: benchmark
|
|
23
|
+
Requires-Dist: datasets<5,>=2.14; extra == 'benchmark'
|
|
24
|
+
Requires-Dist: httpx<1,>=0.24; extra == 'benchmark'
|
|
25
|
+
Requires-Dist: rouge-score<1,>=0.1; extra == 'benchmark'
|
|
26
|
+
Provides-Extra: caveman
|
|
27
|
+
Requires-Dist: spacy<4,>=3.5; extra == 'caveman'
|
|
28
|
+
Requires-Dist: tiktoken<1,>=0.5; extra == 'caveman'
|
|
29
|
+
Provides-Extra: dev
|
|
30
|
+
Requires-Dist: pytest-cov<8,>=4.0; extra == 'dev'
|
|
31
|
+
Requires-Dist: pytest<10,>=7.0; extra == 'dev'
|
|
32
|
+
Requires-Dist: ruff<1,>=0.4; extra == 'dev'
|
|
33
|
+
Description-Content-Type: text/markdown
|
|
34
|
+
|
|
35
|
+
# fiedler-compress
|
|
36
|
+
|
|
37
|
+
**Spectral graph-theoretic prompt compression — a transparent middleware layer for LLM and agentic pipelines.**
|
|
38
|
+
|
|
39
|
+
fiedler-compress uses the Fiedler vector — the second-smallest eigenvector of the graph Laplacian — to identify and remove semantically disconnected content from LLM prompts. The result: shorter prompts that preserve meaning, save tokens, and fit more useful context into your model's window. It runs as a lightweight, CPU-only preprocessing step, making it a drop-in middleware stage in front of any LLM.
|
|
40
|
+
|
|
41
|
+
## Why Spectral Compression?
|
|
42
|
+
|
|
43
|
+
Existing prompt compression tools (LLMLingua, TOON, selective summarization) treat text as a linear sequence and use statistical or neural methods to decide what to cut. They work, but they can't see **structural relationships** — they don't know that paragraph 3 is semantically load-bearing because it bridges two otherwise disconnected topics, or that paragraphs 7 and 8 say the same thing in different words.
|
|
44
|
+
|
|
45
|
+
fiedler-compress builds a **similarity graph** over your text chunks, computes the spectral decomposition, and uses the Fiedler vector to find the natural semantic partitions. Chunks at the spectral periphery — weakly connected to the rest of the content — are the ones that can be safely removed. Chunks that bridge partitions are preserved, because removing them would fragment the prompt's information structure.
|
|
46
|
+
|
|
47
|
+
This isn't summarization. It's **graph surgery**.
|
|
48
|
+
|
|
49
|
+
## Why Middleware
|
|
50
|
+
|
|
51
|
+
The pipeline is designed to sit transparently between your application and the model: text goes in, a smaller functionally-equivalent prompt comes out, and the downstream LLM call proceeds unchanged. It's CPU-only (no GPU, no neural model required for the core path) and fast enough to run synchronously in a request path without perceptible overhead.
|
|
52
|
+
|
|
53
|
+
## For Agentic and Long-Context Workflows
|
|
54
|
+
|
|
55
|
+
Agentic systems accumulate context fast — tool outputs, retrieved documents, prior reasoning, and multi-turn history all compete for a finite context window. fiedler-compress is built for this setting: compress retrieved documents and accumulated context to fit more useful material into the window. **Zone-aware protection** automatically detects instruction content (directives, constraints, format specs) and shields it from removal, so the parts of the prompt that steer the model survive compression while redundant context is pruned. This makes it a practical component for RAG pipelines, long-document processing, and multi-step agent memory.
|
|
56
|
+
|
|
57
|
+
## Installation
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
pip install fiedler-compress
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
The open-core package runs entirely on NumPy and SciPy. A separate commercial tier offers additional capabilities (see below).
|
|
64
|
+
|
|
65
|
+
## Quick Start
|
|
66
|
+
|
|
67
|
+
### Python API
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
from fiedler_optimizer import optimize
|
|
71
|
+
|
|
72
|
+
result = optimize("""
|
|
73
|
+
You are an expert financial analyst. Always respond in JSON.
|
|
74
|
+
|
|
75
|
+
Context: The company was founded in 2015. Revenue was $45M in Q3,
|
|
76
|
+
up 23% YoY. Operating expenses were $38M. Cash reserves: $120M.
|
|
77
|
+
Their competitor reported $52M but is losing mid-market share.
|
|
78
|
+
Industry analysts predict 15% sector growth. The CFO noted
|
|
79
|
+
international expansion costs impacted margins. Retention is 94%.
|
|
80
|
+
Average contract value up 18% to $85K.
|
|
81
|
+
|
|
82
|
+
Task: Analyze Q3 performance.
|
|
83
|
+
""")
|
|
84
|
+
|
|
85
|
+
print(result.compressed)
|
|
86
|
+
print(f"Saved ~{result.tokens_saved} tokens ({result.compression_ratio:.0%} reduction)")
|
|
87
|
+
print(f"Algebraic connectivity λ₂ = {result.algebraic_connectivity:.4f}")
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### CLI
|
|
91
|
+
|
|
92
|
+
```bash
|
|
93
|
+
# Compress a prompt
|
|
94
|
+
fiedler optimize "Your long prompt here..."
|
|
95
|
+
|
|
96
|
+
# Compress from file
|
|
97
|
+
fiedler optimize --file my_prompt.txt
|
|
98
|
+
|
|
99
|
+
# Aggressive compression (30% target removal)
|
|
100
|
+
fiedler optimize --file my_prompt.txt --target 0.30
|
|
101
|
+
|
|
102
|
+
# JSON output for piping
|
|
103
|
+
fiedler optimize --file my_prompt.txt --json
|
|
104
|
+
|
|
105
|
+
# Show what was removed
|
|
106
|
+
fiedler optimize --file my_prompt.txt --verbose
|
|
107
|
+
|
|
108
|
+
# Run the built-in benchmark
|
|
109
|
+
fiedler benchmark
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## How It Works
|
|
113
|
+
|
|
114
|
+
1. **Chunk** the input text into semantically meaningful segments (sentences, paragraphs, or adaptive)
|
|
115
|
+
2. **Build a similarity graph** — each chunk is a node, edge weights are cosine similarity (TF-IDF by default, neural embeddings optional)
|
|
116
|
+
3. **Compute the Fiedler vector** — the eigenvector for λ₂ of the graph Laplacian
|
|
117
|
+
4. **Score chunks** by combining spectral centrality with weighted degree
|
|
118
|
+
5. **Zone-aware protection** — instruction content (directives, constraints, format specs) gets 2–3× protection weight; context content is the compression target
|
|
119
|
+
6. **Prune** the lowest-scoring context chunks up to the target ratio
|
|
120
|
+
|
|
121
|
+
## Performance
|
|
122
|
+
|
|
123
|
+
The core pipeline is CPU-only and fast enough to run inline. Representative end-to-end latency (TF-IDF backend, single CPU core, no GPU):
|
|
124
|
+
|
|
125
|
+
| Input size | End-to-end latency |
|
|
126
|
+
|-----------:|-------------------:|
|
|
127
|
+
| 1,000 tokens | ~12 ms |
|
|
128
|
+
| 10,000 tokens | ~110 ms |
|
|
129
|
+
| 50,000 tokens | ~550 ms |
|
|
130
|
+
|
|
131
|
+
At typical production prompt sizes (1,000–10,000 tokens) the pipeline adds on the order of tens of milliseconds, negligible against typical LLM inference latency. Compression is a lossy operation — recall of fine-grained facts decreases as the target ratio increases — so keep must-keep content in protected instruction zones and tune the target ratio to your task. Detailed benchmarks will be published in a dedicated benchmarks document.
|
|
132
|
+
|
|
133
|
+
## Key Features
|
|
134
|
+
|
|
135
|
+
- **Dependency-free core** — only NumPy and SciPy required. No neural models, no API calls, runs entirely local
|
|
136
|
+
- **Zone-aware compression** — distinguishes instructions from context, applies differential protection so directives survive
|
|
137
|
+
- **Algebraic connectivity metric** — λ₂ tells you how tightly structured your prompt is *before* compression
|
|
138
|
+
- **Transparent middleware** — smaller prompt in, unchanged LLM call out; runs inline with negligible latency
|
|
139
|
+
- **JSON output** — pipe results into your existing toolchain
|
|
140
|
+
|
|
141
|
+
## Configuration
|
|
142
|
+
|
|
143
|
+
| Parameter | Default | Description |
|
|
144
|
+
|-----------|---------|-------------|
|
|
145
|
+
| `target_ratio` | `0.20` | Fraction of text to remove (0.0–1.0) |
|
|
146
|
+
| `strategy` | `adaptive` | Chunking: `adaptive`, `sentence`, `paragraph`, `window` |
|
|
147
|
+
| `protect_instructions` | `True` | Shield instruction zones from removal |
|
|
148
|
+
| `min_chunks` | `4` | Minimum chunks required for spectral analysis |
|
|
149
|
+
| `vectors` | `None` | Pre-computed embeddings (n_chunks × d) |
|
|
150
|
+
|
|
151
|
+
## Project Structure
|
|
152
|
+
|
|
153
|
+
```
|
|
154
|
+
fiedler-compress/
|
|
155
|
+
├── fiedler_optimizer/
|
|
156
|
+
│ ├── __init__.py # Public API
|
|
157
|
+
│ ├── core.py # optimize() pipeline
|
|
158
|
+
│ ├── chunker.py # Text segmentation strategies
|
|
159
|
+
│ ├── graph.py # Similarity graph + Fiedler vector
|
|
160
|
+
│ ├── zones.py # Instruction/context zone detection
|
|
161
|
+
│ └── cli.py # Command-line interface
|
|
162
|
+
├── tests/
|
|
163
|
+
│ └── test_core.py # Test suite
|
|
164
|
+
├── pyproject.toml # Package config
|
|
165
|
+
├── LICENSE # FSL-1.1-ALv2
|
|
166
|
+
└── README.md
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
## Commercial Tier
|
|
170
|
+
|
|
171
|
+
This package is the open-core distribution: the TF-IDF + single-eigenvector (k=1) spectral compression pipeline. Additional capabilities — including **content attestation / certification** — are available as a commercial add-on.
|
|
172
|
+
|
|
173
|
+
For commercial licensing or attestation inquiries:
|
|
174
|
+
**Tensor Earth Intelligence (TEI), LLC** — tensor.earth.intelligence@gmail.com (Mark Chappell).
|
|
175
|
+
|
|
176
|
+
## Roadmap
|
|
177
|
+
|
|
178
|
+
- [x] Core spectral compression with TF-IDF similarity
|
|
179
|
+
- [x] Zone-aware instruction protection
|
|
180
|
+
- [x] CLI with JSON output
|
|
181
|
+
- [ ] VS Code extension with semantic density visualization
|
|
182
|
+
- [ ] Additional capabilities available in the commercial tier
|
|
183
|
+
|
|
184
|
+
## Background
|
|
185
|
+
|
|
186
|
+
The Fiedler vector was originally conceived by Miroslav Fiedler (1973) for graph partitioning. This implementation applies it to natural language, treating text chunks as nodes in a semantic similarity graph. The spectral decomposition reveals the natural information structure of a prompt — which parts are tightly interconnected and which are peripheral — enabling principled, structure-aware compression.
|
|
187
|
+
|
|
188
|
+
## License
|
|
189
|
+
|
|
190
|
+
Licensed under the Functional Source License, Version 1.1, Apache 2.0 Future License (FSL-1.1-ALv2). See [LICENSE](LICENSE) for full terms.
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
# fiedler-compress
|
|
2
|
+
|
|
3
|
+
**Spectral graph-theoretic prompt compression — a transparent middleware layer for LLM and agentic pipelines.**
|
|
4
|
+
|
|
5
|
+
fiedler-compress uses the Fiedler vector — the second-smallest eigenvector of the graph Laplacian — to identify and remove semantically disconnected content from LLM prompts. The result: shorter prompts that preserve meaning, save tokens, and fit more useful context into your model's window. It runs as a lightweight, CPU-only preprocessing step, making it a drop-in middleware stage in front of any LLM.
|
|
6
|
+
|
|
7
|
+
## Why Spectral Compression?
|
|
8
|
+
|
|
9
|
+
Existing prompt compression tools (LLMLingua, TOON, selective summarization) treat text as a linear sequence and use statistical or neural methods to decide what to cut. They work, but they can't see **structural relationships** — they don't know that paragraph 3 is semantically load-bearing because it bridges two otherwise disconnected topics, or that paragraphs 7 and 8 say the same thing in different words.
|
|
10
|
+
|
|
11
|
+
fiedler-compress builds a **similarity graph** over your text chunks, computes the spectral decomposition, and uses the Fiedler vector to find the natural semantic partitions. Chunks at the spectral periphery — weakly connected to the rest of the content — are the ones that can be safely removed. Chunks that bridge partitions are preserved, because removing them would fragment the prompt's information structure.
|
|
12
|
+
|
|
13
|
+
This isn't summarization. It's **graph surgery**.
|
|
14
|
+
|
|
15
|
+
## Why Middleware
|
|
16
|
+
|
|
17
|
+
The pipeline is designed to sit transparently between your application and the model: text goes in, a smaller functionally-equivalent prompt comes out, and the downstream LLM call proceeds unchanged. It's CPU-only (no GPU, no neural model required for the core path) and fast enough to run synchronously in a request path without perceptible overhead.
|
|
18
|
+
|
|
19
|
+
## For Agentic and Long-Context Workflows
|
|
20
|
+
|
|
21
|
+
Agentic systems accumulate context fast — tool outputs, retrieved documents, prior reasoning, and multi-turn history all compete for a finite context window. fiedler-compress is built for this setting: compress retrieved documents and accumulated context to fit more useful material into the window. **Zone-aware protection** automatically detects instruction content (directives, constraints, format specs) and shields it from removal, so the parts of the prompt that steer the model survive compression while redundant context is pruned. This makes it a practical component for RAG pipelines, long-document processing, and multi-step agent memory.
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
pip install fiedler-compress
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
The open-core package runs entirely on NumPy and SciPy. A separate commercial tier offers additional capabilities (see below).
|
|
30
|
+
|
|
31
|
+
## Quick Start
|
|
32
|
+
|
|
33
|
+
### Python API
|
|
34
|
+
|
|
35
|
+
```python
|
|
36
|
+
from fiedler_optimizer import optimize
|
|
37
|
+
|
|
38
|
+
result = optimize("""
|
|
39
|
+
You are an expert financial analyst. Always respond in JSON.
|
|
40
|
+
|
|
41
|
+
Context: The company was founded in 2015. Revenue was $45M in Q3,
|
|
42
|
+
up 23% YoY. Operating expenses were $38M. Cash reserves: $120M.
|
|
43
|
+
Their competitor reported $52M but is losing mid-market share.
|
|
44
|
+
Industry analysts predict 15% sector growth. The CFO noted
|
|
45
|
+
international expansion costs impacted margins. Retention is 94%.
|
|
46
|
+
Average contract value up 18% to $85K.
|
|
47
|
+
|
|
48
|
+
Task: Analyze Q3 performance.
|
|
49
|
+
""")
|
|
50
|
+
|
|
51
|
+
print(result.compressed)
|
|
52
|
+
print(f"Saved ~{result.tokens_saved} tokens ({result.compression_ratio:.0%} reduction)")
|
|
53
|
+
print(f"Algebraic connectivity λ₂ = {result.algebraic_connectivity:.4f}")
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### CLI
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
# Compress a prompt
|
|
60
|
+
fiedler optimize "Your long prompt here..."
|
|
61
|
+
|
|
62
|
+
# Compress from file
|
|
63
|
+
fiedler optimize --file my_prompt.txt
|
|
64
|
+
|
|
65
|
+
# Aggressive compression (30% target removal)
|
|
66
|
+
fiedler optimize --file my_prompt.txt --target 0.30
|
|
67
|
+
|
|
68
|
+
# JSON output for piping
|
|
69
|
+
fiedler optimize --file my_prompt.txt --json
|
|
70
|
+
|
|
71
|
+
# Show what was removed
|
|
72
|
+
fiedler optimize --file my_prompt.txt --verbose
|
|
73
|
+
|
|
74
|
+
# Run the built-in benchmark
|
|
75
|
+
fiedler benchmark
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## How It Works
|
|
79
|
+
|
|
80
|
+
1. **Chunk** the input text into semantically meaningful segments (sentences, paragraphs, or adaptive)
|
|
81
|
+
2. **Build a similarity graph** — each chunk is a node, edge weights are cosine similarity (TF-IDF by default, neural embeddings optional)
|
|
82
|
+
3. **Compute the Fiedler vector** — the eigenvector for λ₂ of the graph Laplacian
|
|
83
|
+
4. **Score chunks** by combining spectral centrality with weighted degree
|
|
84
|
+
5. **Zone-aware protection** — instruction content (directives, constraints, format specs) gets 2–3× protection weight; context content is the compression target
|
|
85
|
+
6. **Prune** the lowest-scoring context chunks up to the target ratio
|
|
86
|
+
|
|
87
|
+
## Performance
|
|
88
|
+
|
|
89
|
+
The core pipeline is CPU-only and fast enough to run inline. Representative end-to-end latency (TF-IDF backend, single CPU core, no GPU):
|
|
90
|
+
|
|
91
|
+
| Input size | End-to-end latency |
|
|
92
|
+
|-----------:|-------------------:|
|
|
93
|
+
| 1,000 tokens | ~12 ms |
|
|
94
|
+
| 10,000 tokens | ~110 ms |
|
|
95
|
+
| 50,000 tokens | ~550 ms |
|
|
96
|
+
|
|
97
|
+
At typical production prompt sizes (1,000–10,000 tokens) the pipeline adds on the order of tens of milliseconds, negligible against typical LLM inference latency. Compression is a lossy operation — recall of fine-grained facts decreases as the target ratio increases — so keep must-keep content in protected instruction zones and tune the target ratio to your task. Detailed benchmarks will be published in a dedicated benchmarks document.
|
|
98
|
+
|
|
99
|
+
## Key Features
|
|
100
|
+
|
|
101
|
+
- **Dependency-free core** — only NumPy and SciPy required. No neural models, no API calls, runs entirely local
|
|
102
|
+
- **Zone-aware compression** — distinguishes instructions from context, applies differential protection so directives survive
|
|
103
|
+
- **Algebraic connectivity metric** — λ₂ tells you how tightly structured your prompt is *before* compression
|
|
104
|
+
- **Transparent middleware** — smaller prompt in, unchanged LLM call out; runs inline with negligible latency
|
|
105
|
+
- **JSON output** — pipe results into your existing toolchain
|
|
106
|
+
|
|
107
|
+
## Configuration
|
|
108
|
+
|
|
109
|
+
| Parameter | Default | Description |
|
|
110
|
+
|-----------|---------|-------------|
|
|
111
|
+
| `target_ratio` | `0.20` | Fraction of text to remove (0.0–1.0) |
|
|
112
|
+
| `strategy` | `adaptive` | Chunking: `adaptive`, `sentence`, `paragraph`, `window` |
|
|
113
|
+
| `protect_instructions` | `True` | Shield instruction zones from removal |
|
|
114
|
+
| `min_chunks` | `4` | Minimum chunks required for spectral analysis |
|
|
115
|
+
| `vectors` | `None` | Pre-computed embeddings (n_chunks × d) |
|
|
116
|
+
|
|
117
|
+
## Project Structure
|
|
118
|
+
|
|
119
|
+
```
|
|
120
|
+
fiedler-compress/
|
|
121
|
+
├── fiedler_optimizer/
|
|
122
|
+
│ ├── __init__.py # Public API
|
|
123
|
+
│ ├── core.py # optimize() pipeline
|
|
124
|
+
│ ├── chunker.py # Text segmentation strategies
|
|
125
|
+
│ ├── graph.py # Similarity graph + Fiedler vector
|
|
126
|
+
│ ├── zones.py # Instruction/context zone detection
|
|
127
|
+
│ └── cli.py # Command-line interface
|
|
128
|
+
├── tests/
|
|
129
|
+
│ └── test_core.py # Test suite
|
|
130
|
+
├── pyproject.toml # Package config
|
|
131
|
+
├── LICENSE # FSL-1.1-ALv2
|
|
132
|
+
└── README.md
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## Commercial Tier
|
|
136
|
+
|
|
137
|
+
This package is the open-core distribution: the TF-IDF + single-eigenvector (k=1) spectral compression pipeline. Additional capabilities — including **content attestation / certification** — are available as a commercial add-on.
|
|
138
|
+
|
|
139
|
+
For commercial licensing or attestation inquiries:
|
|
140
|
+
**Tensor Earth Intelligence (TEI), LLC** — tensor.earth.intelligence@gmail.com (Mark Chappell).
|
|
141
|
+
|
|
142
|
+
## Roadmap
|
|
143
|
+
|
|
144
|
+
- [x] Core spectral compression with TF-IDF similarity
|
|
145
|
+
- [x] Zone-aware instruction protection
|
|
146
|
+
- [x] CLI with JSON output
|
|
147
|
+
- [ ] VS Code extension with semantic density visualization
|
|
148
|
+
- [ ] Additional capabilities available in the commercial tier
|
|
149
|
+
|
|
150
|
+
## Background
|
|
151
|
+
|
|
152
|
+
The Fiedler vector was originally conceived by Miroslav Fiedler (1973) for graph partitioning. This implementation applies it to natural language, treating text chunks as nodes in a semantic similarity graph. The spectral decomposition reveals the natural information structure of a prompt — which parts are tightly interconnected and which are peripheral — enabling principled, structure-aware compression.
|
|
153
|
+
|
|
154
|
+
## License
|
|
155
|
+
|
|
156
|
+
Licensed under the Functional Source License, Version 1.1, Apache 2.0 Future License (FSL-1.1-ALv2). See [LICENSE](LICENSE) for full terms.
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Fiedler Optimizer — Spectral graph-theoretic prompt compression.
|
|
3
|
+
|
|
4
|
+
Uses the Fiedler vector (second-smallest eigenvector of the graph Laplacian)
|
|
5
|
+
to identify and remove semantically disconnected chunks from LLM prompts,
|
|
6
|
+
reducing token count while preserving information fidelity.
|
|
7
|
+
|
|
8
|
+
Basic usage:
|
|
9
|
+
from fiedler_optimizer import optimize
|
|
10
|
+
|
|
11
|
+
result = optimize("Your long prompt text here...")
|
|
12
|
+
print(result.compressed)
|
|
13
|
+
print(f"Saved {result.tokens_saved} tokens ({result.compression_ratio:.1%})")
|
|
14
|
+
|
|
15
|
+
This is the open-core distribution: the TF-IDF + single-eigenvector (k=1)
|
|
16
|
+
spectral compression pipeline. Additional capabilities are available in a
|
|
17
|
+
separate commercial tier.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
__version__ = "0.1.0"
|
|
21
|
+
|
|
22
|
+
from fiedler_optimizer.core import optimize, FiedlerResult
|
|
23
|
+
from fiedler_optimizer.graph import build_similarity_graph, compute_fiedler_vector
|
|
24
|
+
from fiedler_optimizer.chunker import chunk_text, ChunkingStrategy
|
|
25
|
+
from fiedler_optimizer.zones import detect_zones, Zone
|
|
26
|
+
|
|
27
|
+
__all__ = [
|
|
28
|
+
"optimize",
|
|
29
|
+
"FiedlerResult",
|
|
30
|
+
"build_similarity_graph",
|
|
31
|
+
"compute_fiedler_vector",
|
|
32
|
+
"chunk_text",
|
|
33
|
+
"ChunkingStrategy",
|
|
34
|
+
"detect_zones",
|
|
35
|
+
"Zone",
|
|
36
|
+
]
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"""Commercial-tier feature gating for the open-core distribution.
|
|
2
|
+
|
|
3
|
+
Some capabilities are not included in the open-core package. The modules that
|
|
4
|
+
implement them are absent here, so a lazy ``import`` of one raises
|
|
5
|
+
:class:`ImportError`. The helpers below translate that into a clear,
|
|
6
|
+
actionable message instead of an opaque import crash.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class CommercialTierError(RuntimeError):
|
|
13
|
+
"""Raised when an open-core caller requests a commercial-tier capability."""
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def commercial_tier_error(feature: str | None = None) -> "CommercialTierError":
|
|
17
|
+
"""Return a generic :class:`CommercialTierError` for a gated capability.
|
|
18
|
+
|
|
19
|
+
The message intentionally does not name the specific capability.
|
|
20
|
+
|
|
21
|
+
Parameters
|
|
22
|
+
----------
|
|
23
|
+
feature : str, optional
|
|
24
|
+
Accepted for backward compatibility; ignored.
|
|
25
|
+
"""
|
|
26
|
+
return CommercialTierError(
|
|
27
|
+
"This feature requires a commercial license of fiedler-optimizer. "
|
|
28
|
+
"See the project README for details."
|
|
29
|
+
)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Benchmark harnesses for Fiedler Optimizer compression quality."""
|