ctxprune 0.2.0__tar.gz → 0.2.1__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.
- {ctxprune-0.2.0 → ctxprune-0.2.1}/PKG-INFO +13 -6
- {ctxprune-0.2.0 → ctxprune-0.2.1}/README.md +12 -5
- {ctxprune-0.2.0 → ctxprune-0.2.1}/ctxprune/__init__.py +1 -1
- {ctxprune-0.2.0 → ctxprune-0.2.1}/ctxprune/cli.py +3 -3
- {ctxprune-0.2.0 → ctxprune-0.2.1}/ctxprune/hook.py +7 -5
- {ctxprune-0.2.0 → ctxprune-0.2.1}/ctxprune/serve.py +3 -3
- {ctxprune-0.2.0 → ctxprune-0.2.1}/pyproject.toml +1 -1
- {ctxprune-0.2.0 → ctxprune-0.2.1}/space/static/README.md +4 -1
- {ctxprune-0.2.0 → ctxprune-0.2.1}/tests/test_hook.py +26 -0
- {ctxprune-0.2.0 → ctxprune-0.2.1}/.gitignore +0 -0
- {ctxprune-0.2.0 → ctxprune-0.2.1}/LICENSE +0 -0
- {ctxprune-0.2.0 → ctxprune-0.2.1}/ctxprune/__main__.py +0 -0
- {ctxprune-0.2.0 → ctxprune-0.2.1}/ctxprune/align.py +0 -0
- {ctxprune-0.2.0 → ctxprune-0.2.1}/ctxprune/atoms.py +0 -0
- {ctxprune-0.2.0 → ctxprune-0.2.1}/ctxprune/chunking.py +0 -0
- {ctxprune-0.2.0 → ctxprune-0.2.1}/ctxprune/compress.py +0 -0
- {ctxprune-0.2.0 → ctxprune-0.2.1}/ctxprune/idcheck.py +0 -0
- {ctxprune-0.2.0 → ctxprune-0.2.1}/ctxprune/integrations.py +0 -0
- {ctxprune-0.2.0 → ctxprune-0.2.1}/ctxprune/sources.py +0 -0
- {ctxprune-0.2.0 → ctxprune-0.2.1}/ctxprune/teacher.py +0 -0
- {ctxprune-0.2.0 → ctxprune-0.2.1}/ctxprune/tokenlabels.py +0 -0
- {ctxprune-0.2.0 → ctxprune-0.2.1}/tests/test_core.py +0 -0
- {ctxprune-0.2.0 → ctxprune-0.2.1}/tests/test_integrations.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: ctxprune
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.1
|
|
4
4
|
Summary: Context compression for AI agents: an LLMLingua-2 successor that keeps identifiers intact
|
|
5
5
|
Project-URL: Homepage, https://github.com/riceharvest/ctxprune
|
|
6
6
|
Project-URL: Model, https://huggingface.co/darioooooo0o/ctxprune-small
|
|
@@ -56,7 +56,9 @@ Runs on CPU in about 0.1 s per 500 tokens. Apache-2.0.
|
|
|
56
56
|
|
|
57
57
|
At 50% of tokens kept, a reader LLM answers **51.5%** of SQuAD v2 + HotpotQA questions exactly from ctxprune's
|
|
58
58
|
output vs **36.5%** from [headroom](https://github.com/headroomlabs-ai/headroom)'s Kompress-v2 (400 questions,
|
|
59
|
-
95% CI of the gap +10.5 to +19.5
|
|
59
|
+
95% CI of the gap +10.5 to +19.5; the reader there is Qwen3.8-27B, which also labeled ctxprune's training data, so
|
|
60
|
+
this may flatter ctxprune), and **68.8% vs 53.4%** for LLMLingua-2-large on our agent-text set with an independent
|
|
61
|
+
reader (DeepSeek V4 Flash).
|
|
60
62
|
**[Try it in your browser](https://huggingface.co/spaces/darioooooo0o/ctxprune)** (nothing is uploaded).
|
|
61
63
|
|
|
62
64
|
## Quick start
|
|
@@ -78,7 +80,7 @@ short = ctxprune.prune(tool_output, keep=0.5) # protect=True never drops IDs,
|
|
|
78
80
|
| Shrink big Bash/MCP outputs inside **Claude Code** | `ctxprune install claude-code` | or the plugin: `/plugin install ctxprune --marketplace riceharvest/ctxprune` |
|
|
79
81
|
| Compress a file or a pipe from the shell | `ctxprune FILE --keep 0.5` | `--protect`, `--stats`, reads stdin |
|
|
80
82
|
| Call it from Python | `ctxprune.prune(text, keep=0.5)` | `Compressor` for batching and options |
|
|
81
|
-
| Call it from another language / avoid the model load | `ctxprune serve` | `POST localhost:
|
|
83
|
+
| Call it from another language / avoid the model load | `ctxprune serve` | `POST localhost:8719/compress {"text": ...}` |
|
|
82
84
|
| Compress retrieved documents in **LangChain** | `CtxpruneDocumentCompressor` | see below |
|
|
83
85
|
| Compress retrieved nodes in **LlamaIndex** | `CtxpruneNodePostprocessor` | see below |
|
|
84
86
|
| Use it inside **LLMLingua** | `PromptCompressor(model_name="darioooooo0o/ctxprune-small", use_llmlingua2=True)` | needs [LLMLingua#261](https://github.com/microsoft/LLMLingua/pull/261) |
|
|
@@ -101,7 +103,12 @@ original output. Settings: `CTXPRUNE_RATE` (0.5), `CTXPRUNE_MIN_CHARS` (6000). E
|
|
|
101
103
|
(~2 s); run `ctxprune serve` in the background and the hook uses it instead.
|
|
102
104
|
|
|
103
105
|
As a plugin, without touching settings files (needs `ctxprune` on PATH, or `uv` so it can run via `uvx`):
|
|
104
|
-
`/plugin install ctxprune --marketplace riceharvest/ctxprune`.
|
|
106
|
+
`/plugin install ctxprune --marketplace riceharvest/ctxprune`. The plugin does not pre-download the model, so the
|
|
107
|
+
first large outputs pass through uncompressed while it downloads (~180 MB); run `ctxprune serve` once, or
|
|
108
|
+
`python -c "import ctxprune; ctxprune.prune('x')"`, to fetch it up front.
|
|
109
|
+
|
|
110
|
+
Saved originals in `~/.cache/ctxprune/outputs/` are raw tool output (which can include secrets) and are never
|
|
111
|
+
deleted automatically; clear them with `rm -rf ~/.cache/ctxprune/outputs`.
|
|
105
112
|
|
|
106
113
|
## LangChain and LlamaIndex
|
|
107
114
|
|
|
@@ -120,8 +127,8 @@ engine = index.as_query_engine(node_postprocessors=[CtxpruneNodePostprocessor(ra
|
|
|
120
127
|
Use it where the token savings are worth that, e.g. long logs, test output, search results, retrieved docs.
|
|
121
128
|
- Weak spots: source code (the teacher rarely deleted code, so the model keeps most of it) and non-English web prose,
|
|
122
129
|
where LLMLingua-2-large still does better.
|
|
123
|
-
- Our 378-question set is from our own training distribution,
|
|
124
|
-
|
|
130
|
+
- Our 378-question set is from our own training distribution, and the neutral SQuAD/HotpotQA comparison used our
|
|
131
|
+
labeling teacher as the reader; neither is a fully independent benchmark. Per-source tables and caveats: [model card](https://huggingface.co/darioooooo0o/ctxprune-small).
|
|
125
132
|
|
|
126
133
|
By [@imdariotoo](https://x.com/imdariotoo).
|
|
127
134
|
|
|
@@ -8,7 +8,9 @@ Runs on CPU in about 0.1 s per 500 tokens. Apache-2.0.
|
|
|
8
8
|
|
|
9
9
|
At 50% of tokens kept, a reader LLM answers **51.5%** of SQuAD v2 + HotpotQA questions exactly from ctxprune's
|
|
10
10
|
output vs **36.5%** from [headroom](https://github.com/headroomlabs-ai/headroom)'s Kompress-v2 (400 questions,
|
|
11
|
-
95% CI of the gap +10.5 to +19.5
|
|
11
|
+
95% CI of the gap +10.5 to +19.5; the reader there is Qwen3.8-27B, which also labeled ctxprune's training data, so
|
|
12
|
+
this may flatter ctxprune), and **68.8% vs 53.4%** for LLMLingua-2-large on our agent-text set with an independent
|
|
13
|
+
reader (DeepSeek V4 Flash).
|
|
12
14
|
**[Try it in your browser](https://huggingface.co/spaces/darioooooo0o/ctxprune)** (nothing is uploaded).
|
|
13
15
|
|
|
14
16
|
## Quick start
|
|
@@ -30,7 +32,7 @@ short = ctxprune.prune(tool_output, keep=0.5) # protect=True never drops IDs,
|
|
|
30
32
|
| Shrink big Bash/MCP outputs inside **Claude Code** | `ctxprune install claude-code` | or the plugin: `/plugin install ctxprune --marketplace riceharvest/ctxprune` |
|
|
31
33
|
| Compress a file or a pipe from the shell | `ctxprune FILE --keep 0.5` | `--protect`, `--stats`, reads stdin |
|
|
32
34
|
| Call it from Python | `ctxprune.prune(text, keep=0.5)` | `Compressor` for batching and options |
|
|
33
|
-
| Call it from another language / avoid the model load | `ctxprune serve` | `POST localhost:
|
|
35
|
+
| Call it from another language / avoid the model load | `ctxprune serve` | `POST localhost:8719/compress {"text": ...}` |
|
|
34
36
|
| Compress retrieved documents in **LangChain** | `CtxpruneDocumentCompressor` | see below |
|
|
35
37
|
| Compress retrieved nodes in **LlamaIndex** | `CtxpruneNodePostprocessor` | see below |
|
|
36
38
|
| Use it inside **LLMLingua** | `PromptCompressor(model_name="darioooooo0o/ctxprune-small", use_llmlingua2=True)` | needs [LLMLingua#261](https://github.com/microsoft/LLMLingua/pull/261) |
|
|
@@ -53,7 +55,12 @@ original output. Settings: `CTXPRUNE_RATE` (0.5), `CTXPRUNE_MIN_CHARS` (6000). E
|
|
|
53
55
|
(~2 s); run `ctxprune serve` in the background and the hook uses it instead.
|
|
54
56
|
|
|
55
57
|
As a plugin, without touching settings files (needs `ctxprune` on PATH, or `uv` so it can run via `uvx`):
|
|
56
|
-
`/plugin install ctxprune --marketplace riceharvest/ctxprune`.
|
|
58
|
+
`/plugin install ctxprune --marketplace riceharvest/ctxprune`. The plugin does not pre-download the model, so the
|
|
59
|
+
first large outputs pass through uncompressed while it downloads (~180 MB); run `ctxprune serve` once, or
|
|
60
|
+
`python -c "import ctxprune; ctxprune.prune('x')"`, to fetch it up front.
|
|
61
|
+
|
|
62
|
+
Saved originals in `~/.cache/ctxprune/outputs/` are raw tool output (which can include secrets) and are never
|
|
63
|
+
deleted automatically; clear them with `rm -rf ~/.cache/ctxprune/outputs`.
|
|
57
64
|
|
|
58
65
|
## LangChain and LlamaIndex
|
|
59
66
|
|
|
@@ -72,8 +79,8 @@ engine = index.as_query_engine(node_postprocessors=[CtxpruneNodePostprocessor(ra
|
|
|
72
79
|
Use it where the token savings are worth that, e.g. long logs, test output, search results, retrieved docs.
|
|
73
80
|
- Weak spots: source code (the teacher rarely deleted code, so the model keeps most of it) and non-English web prose,
|
|
74
81
|
where LLMLingua-2-large still does better.
|
|
75
|
-
- Our 378-question set is from our own training distribution,
|
|
76
|
-
|
|
82
|
+
- Our 378-question set is from our own training distribution, and the neutral SQuAD/HotpotQA comparison used our
|
|
83
|
+
labeling teacher as the reader; neither is a fully independent benchmark. Per-source tables and caveats: [model card](https://huggingface.co/darioooooo0o/ctxprune-small).
|
|
77
84
|
|
|
78
85
|
By [@imdariotoo](https://x.com/imdariotoo).
|
|
79
86
|
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
ctxprune build.log # keep ~50% of the text, print it
|
|
4
4
|
cat build.log | ctxprune --keep 0.33 --protect --stats
|
|
5
5
|
ctxprune install claude-code # compress big Bash/MCP outputs inside Claude Code
|
|
6
|
-
ctxprune serve # local HTTP API on :
|
|
6
|
+
ctxprune serve # local HTTP API on :8719 (skips the model load per call)
|
|
7
7
|
"""
|
|
8
8
|
|
|
9
9
|
from __future__ import annotations
|
|
@@ -17,7 +17,7 @@ USAGE = """\
|
|
|
17
17
|
ctxprune FILE [--keep 0.5] [--protect] [--stats] compress a file (or stdin) and print it
|
|
18
18
|
ctxprune install claude-code [--scope user|project|local]
|
|
19
19
|
ctxprune uninstall claude-code [--scope ...]
|
|
20
|
-
ctxprune serve [--port
|
|
20
|
+
ctxprune serve [--port 8719] [--host 127.0.0.1]
|
|
21
21
|
ctxprune hook claude-code (called by Claude Code, reads JSON on stdin)"""
|
|
22
22
|
|
|
23
23
|
|
|
@@ -96,7 +96,7 @@ def main(argv: list[str] | None = None) -> None:
|
|
|
96
96
|
if cmd == "serve":
|
|
97
97
|
ap = argparse.ArgumentParser(prog="ctxprune serve")
|
|
98
98
|
ap.add_argument("--host", default="127.0.0.1")
|
|
99
|
-
ap.add_argument("--port", type=int, default=
|
|
99
|
+
ap.add_argument("--port", type=int, default=8719)
|
|
100
100
|
ap.add_argument("--model", default=DEFAULT_MODEL)
|
|
101
101
|
ap.add_argument("--fp32", action="store_true")
|
|
102
102
|
args = ap.parse_args(argv[1:])
|
|
@@ -15,7 +15,7 @@ What it does, and what it deliberately leaves alone:
|
|
|
15
15
|
- Any error means no output: Claude then gets the original text (fail open).
|
|
16
16
|
|
|
17
17
|
Settings (environment of the Claude Code process): CTXPRUNE_RATE (default 0.5),
|
|
18
|
-
CTXPRUNE_MIN_CHARS (6000), CTXPRUNE_SERVER (default http://127.0.0.1:
|
|
18
|
+
CTXPRUNE_MIN_CHARS (6000), CTXPRUNE_SERVER (default http://127.0.0.1:8719; used when
|
|
19
19
|
`ctxprune serve` is running, which avoids the ~2 s model load per compressed call).
|
|
20
20
|
"""
|
|
21
21
|
|
|
@@ -51,7 +51,7 @@ def _save_original(text: str) -> str:
|
|
|
51
51
|
|
|
52
52
|
def _compress(texts: list[str], rate: float) -> list[str]:
|
|
53
53
|
"""Compress via a running `ctxprune serve` if there is one, else in process."""
|
|
54
|
-
server = os.environ.get("CTXPRUNE_SERVER", "http://127.0.0.1:
|
|
54
|
+
server = os.environ.get("CTXPRUNE_SERVER", "http://127.0.0.1:8719")
|
|
55
55
|
if server:
|
|
56
56
|
try:
|
|
57
57
|
import urllib.request
|
|
@@ -61,9 +61,11 @@ def _compress(texts: list[str], rate: float) -> list[str]:
|
|
|
61
61
|
data=json.dumps({"texts": texts, "rate": rate,
|
|
62
62
|
"force_protected": True}).encode())
|
|
63
63
|
with urllib.request.urlopen(req, timeout=60) as r:
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
64
|
+
outs = [o["text"] for o in json.load(r)["results"]]
|
|
65
|
+
if len(outs) == len(texts) and all(isinstance(o, str) for o in outs):
|
|
66
|
+
return outs
|
|
67
|
+
except Exception:
|
|
68
|
+
pass # not running, or something else owns the port: load the model here
|
|
67
69
|
from .compress import Compressor
|
|
68
70
|
|
|
69
71
|
c = Compressor()
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"""Small local HTTP API so other processes (the Claude Code hook, Node/Go/shell tools) skip the model load.
|
|
2
2
|
|
|
3
|
-
ctxprune serve # http://127.0.0.1:
|
|
4
|
-
curl -s localhost:
|
|
3
|
+
ctxprune serve # http://127.0.0.1:8719
|
|
4
|
+
curl -s localhost:8719/compress -d '{"text": "...", "rate": 0.5}'
|
|
5
5
|
|
|
6
6
|
POST /compress {"text": str} or {"texts": [str]}, optional "rate" (0.5), "force_protected" (false),
|
|
7
7
|
"keep_structure" (false) -> {"text", "kept", "total"} or {"results": [...]}
|
|
@@ -43,7 +43,7 @@ class _State:
|
|
|
43
43
|
return out
|
|
44
44
|
|
|
45
45
|
|
|
46
|
-
def make_server(host: str = "127.0.0.1", port: int =
|
|
46
|
+
def make_server(host: str = "127.0.0.1", port: int = 8719, model: str = DEFAULT_MODEL,
|
|
47
47
|
onnx_file: str = INT8) -> ThreadingHTTPServer:
|
|
48
48
|
state = _State(model, onnx_file)
|
|
49
49
|
|
|
@@ -7,7 +7,7 @@ sdk: static
|
|
|
7
7
|
app_file: index.html
|
|
8
8
|
pinned: false
|
|
9
9
|
license: apache-2.0
|
|
10
|
-
short_description:
|
|
10
|
+
short_description: Paste text, see what ctxprune keeps (runs in your browser)
|
|
11
11
|
models:
|
|
12
12
|
- darioooooo0o/ctxprune-small
|
|
13
13
|
- microsoft/llmlingua-2-bert-base-multilingual-cased-meetingbank
|
|
@@ -15,3 +15,6 @@ models:
|
|
|
15
15
|
|
|
16
16
|
Real side-by-side outputs of [ctxprune-small](https://huggingface.co/darioooooo0o/ctxprune-small) and LLMLingua-2.
|
|
17
17
|
Generated by `space/static/build.py` in [riceharvest/ctxprune](https://github.com/riceharvest/ctxprune).
|
|
18
|
+
|
|
19
|
+
The "Try it" box runs the int8 model in your browser with transformers.js (`js/ctxprune.js` is a port of the
|
|
20
|
+
Python package's atomizer and selection). The side-by-side section shows precomputed outputs.
|
|
@@ -65,3 +65,29 @@ def test_hook_fails_open(monkeypatch, capsys):
|
|
|
65
65
|
monkeypatch.setattr("sys.stdin", io.StringIO("not json"))
|
|
66
66
|
hook.main_claude_code()
|
|
67
67
|
assert capsys.readouterr().out == ""
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def test_foreign_server_on_port_falls_back(monkeypatch):
|
|
71
|
+
"""Something other than `ctxprune serve` answering on the port must not break compression."""
|
|
72
|
+
import io
|
|
73
|
+
import urllib.request
|
|
74
|
+
|
|
75
|
+
class Resp(io.BytesIO):
|
|
76
|
+
def __enter__(self):
|
|
77
|
+
return self
|
|
78
|
+
|
|
79
|
+
def __exit__(self, *a):
|
|
80
|
+
return False
|
|
81
|
+
|
|
82
|
+
monkeypatch.setattr(urllib.request, "urlopen", lambda *a, **k: Resp(b'{"object": "not ctxprune"}'))
|
|
83
|
+
called = {}
|
|
84
|
+
|
|
85
|
+
class FakeCompressor:
|
|
86
|
+
def compress(self, t, rate, force_protected):
|
|
87
|
+
called["yes"] = True
|
|
88
|
+
return {"text": t[:10]}
|
|
89
|
+
|
|
90
|
+
import ctxprune.compress
|
|
91
|
+
|
|
92
|
+
monkeypatch.setattr(ctxprune.compress, "Compressor", lambda *a, **k: FakeCompressor())
|
|
93
|
+
assert hook._compress(["x" * 100], 0.5) == ["x" * 10] and called
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|