vedic-bpe 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.
- vedic_bpe-0.1.0/LICENSE +21 -0
- vedic_bpe-0.1.0/PKG-INFO +52 -0
- vedic_bpe-0.1.0/README.md +39 -0
- vedic_bpe-0.1.0/pyproject.toml +20 -0
- vedic_bpe-0.1.0/setup.cfg +4 -0
- vedic_bpe-0.1.0/src/vedic_bpe/__init__.py +80 -0
- vedic_bpe-0.1.0/src/vedic_bpe.egg-info/PKG-INFO +52 -0
- vedic_bpe-0.1.0/src/vedic_bpe.egg-info/SOURCES.txt +9 -0
- vedic_bpe-0.1.0/src/vedic_bpe.egg-info/dependency_links.txt +1 -0
- vedic_bpe-0.1.0/src/vedic_bpe.egg-info/top_level.txt +1 -0
- vedic_bpe-0.1.0/tests/test_vedic_bpe.py +36 -0
vedic_bpe-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Aman Bhawsar
|
|
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.
|
vedic_bpe-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: vedic-bpe
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Lossless LLM token reducer for repetitive structured JSON (zero-dependency, tiktoken-measured).
|
|
5
|
+
Author: Aman Bhawsar
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/Amanbhaw/ai-packages
|
|
8
|
+
Keywords: llm,tokens,tiktoken,json,compression,prompt,rag,lossless
|
|
9
|
+
Requires-Python: >=3.8
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Dynamic: license-file
|
|
13
|
+
|
|
14
|
+
# vedic-bpe
|
|
15
|
+
|
|
16
|
+
**Lossless LLM token reducer for repetitive structured JSON.** Zero dependencies.
|
|
17
|
+
Bit-exact round-trip (types preserved). 0% token-inflation guarantee.
|
|
18
|
+
|
|
19
|
+
Named after classical Indian mathematics (**branding**); the transforms are standard
|
|
20
|
+
and **measured against real `tiktoken` (o200k_base)** — not claimed.
|
|
21
|
+
|
|
22
|
+
| transform | classical name | what it does |
|
|
23
|
+
|---|---|---|
|
|
24
|
+
| key-folding + row inheritance | **Anuvritti** (Panini, Ashtadhyayi) | repeated object keys → one header; a cell equal to the one above → `.` |
|
|
25
|
+
| base-anchor delta coding | **Nikhilam / Brahmagupta** (finite differences) | string ID columns `SUP-101, SUP-102` → base `SUP-100` + delta `1, 2` |
|
|
26
|
+
|
|
27
|
+
## Measured results (real `tiktoken` o200k_base, lossless verified)
|
|
28
|
+
| input | before | after | saved |
|
|
29
|
+
|---|---|---|---|
|
|
30
|
+
| 20 highly-repetitive sensor rows | 782 tok | 138 tok | **82%** |
|
|
31
|
+
| 20 varied rows | 742 tok | 269 tok | **64%** |
|
|
32
|
+
| 4-row support tickets | 138 tok | 48 tok | **65%** |
|
|
33
|
+
|
|
34
|
+
Savings **scale with redundancy** (Shannon limit: a token holds ≤ log₂(V) ≈ 17.6 bits,
|
|
35
|
+
so *arbitrary* text can't be squeezed — only redundant structured data can).
|
|
36
|
+
|
|
37
|
+
## Usage
|
|
38
|
+
```python
|
|
39
|
+
from vedic_bpe import pack, unpack
|
|
40
|
+
packed = pack(json_text) # send `packed` to the LLM (still human/LLM-readable)
|
|
41
|
+
original = unpack(packed) # bit-exact recovery
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Honest scope — what this is NOT
|
|
45
|
+
- **Not** a compressor for arbitrary prose (information theory forbids it).
|
|
46
|
+
- Works on **arrays of flat JSON objects with shared keys and scalar values**
|
|
47
|
+
(support tickets, RAG metadata rows, API/sensor logs). Values must not contain `|` or newline.
|
|
48
|
+
- Output stays **readable** so the model can still reason over it — unlike bit-packing
|
|
49
|
+
into opaque glyphs (which saves tokens but the model can't understand).
|
|
50
|
+
- `pack()` **never inflates**: if it can't shrink the real token count, it returns the input untouched.
|
|
51
|
+
|
|
52
|
+
MIT © Aman Bhawsar
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# vedic-bpe
|
|
2
|
+
|
|
3
|
+
**Lossless LLM token reducer for repetitive structured JSON.** Zero dependencies.
|
|
4
|
+
Bit-exact round-trip (types preserved). 0% token-inflation guarantee.
|
|
5
|
+
|
|
6
|
+
Named after classical Indian mathematics (**branding**); the transforms are standard
|
|
7
|
+
and **measured against real `tiktoken` (o200k_base)** — not claimed.
|
|
8
|
+
|
|
9
|
+
| transform | classical name | what it does |
|
|
10
|
+
|---|---|---|
|
|
11
|
+
| key-folding + row inheritance | **Anuvritti** (Panini, Ashtadhyayi) | repeated object keys → one header; a cell equal to the one above → `.` |
|
|
12
|
+
| base-anchor delta coding | **Nikhilam / Brahmagupta** (finite differences) | string ID columns `SUP-101, SUP-102` → base `SUP-100` + delta `1, 2` |
|
|
13
|
+
|
|
14
|
+
## Measured results (real `tiktoken` o200k_base, lossless verified)
|
|
15
|
+
| input | before | after | saved |
|
|
16
|
+
|---|---|---|---|
|
|
17
|
+
| 20 highly-repetitive sensor rows | 782 tok | 138 tok | **82%** |
|
|
18
|
+
| 20 varied rows | 742 tok | 269 tok | **64%** |
|
|
19
|
+
| 4-row support tickets | 138 tok | 48 tok | **65%** |
|
|
20
|
+
|
|
21
|
+
Savings **scale with redundancy** (Shannon limit: a token holds ≤ log₂(V) ≈ 17.6 bits,
|
|
22
|
+
so *arbitrary* text can't be squeezed — only redundant structured data can).
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
```python
|
|
26
|
+
from vedic_bpe import pack, unpack
|
|
27
|
+
packed = pack(json_text) # send `packed` to the LLM (still human/LLM-readable)
|
|
28
|
+
original = unpack(packed) # bit-exact recovery
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Honest scope — what this is NOT
|
|
32
|
+
- **Not** a compressor for arbitrary prose (information theory forbids it).
|
|
33
|
+
- Works on **arrays of flat JSON objects with shared keys and scalar values**
|
|
34
|
+
(support tickets, RAG metadata rows, API/sensor logs). Values must not contain `|` or newline.
|
|
35
|
+
- Output stays **readable** so the model can still reason over it — unlike bit-packing
|
|
36
|
+
into opaque glyphs (which saves tokens but the model can't understand).
|
|
37
|
+
- `pack()` **never inflates**: if it can't shrink the real token count, it returns the input untouched.
|
|
38
|
+
|
|
39
|
+
MIT © Aman Bhawsar
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "vedic-bpe"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Lossless LLM token reducer for repetitive structured JSON (zero-dependency, tiktoken-measured)."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.8"
|
|
11
|
+
license = {text = "MIT"}
|
|
12
|
+
authors = [{name = "Aman Bhawsar"}]
|
|
13
|
+
keywords = ["llm", "tokens", "tiktoken", "json", "compression", "prompt", "rag", "lossless"]
|
|
14
|
+
dependencies = []
|
|
15
|
+
|
|
16
|
+
[project.urls]
|
|
17
|
+
Homepage = "https://github.com/Amanbhaw/ai-packages"
|
|
18
|
+
|
|
19
|
+
[tool.setuptools.packages.find]
|
|
20
|
+
where = ["src"]
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"""
|
|
2
|
+
vedic-bpe — lossless LLM token reducer for repetitive structured JSON.
|
|
3
|
+
|
|
4
|
+
Named after classical Indian mathematics (BRANDING); the transforms are standard,
|
|
5
|
+
lossless, type-preserving, and MEASURED against real tiktoken:
|
|
6
|
+
- Anuvritti (Panini) : fold repeated keys to one header; a cell equal to the
|
|
7
|
+
cell above collapses to "." (contextual inheritance).
|
|
8
|
+
- Nikhilam/Brahmagupta : base-anchor delta coding for string ID columns
|
|
9
|
+
(SUP-101, SUP-102 -> base SUP-100 + delta 1,2).
|
|
10
|
+
Zero deps. Bit-exact round-trip (types preserved). 0% token-inflation guarantee.
|
|
11
|
+
HONEST SCOPE: arrays of flat JSON objects with shared keys and scalar values
|
|
12
|
+
(support tickets, RAG rows, logs). NOT arbitrary prose. Values must not contain
|
|
13
|
+
'|' or newline (kept simple on purpose).
|
|
14
|
+
"""
|
|
15
|
+
import json, re
|
|
16
|
+
|
|
17
|
+
_SEP, _NL = "|", "\n"
|
|
18
|
+
|
|
19
|
+
def pack(json_text: str) -> str:
|
|
20
|
+
try:
|
|
21
|
+
data = json.loads(json_text)
|
|
22
|
+
except Exception:
|
|
23
|
+
return json_text
|
|
24
|
+
if not (isinstance(data, list) and len(data) >= 2 and all(isinstance(r, dict) for r in data)):
|
|
25
|
+
return json_text
|
|
26
|
+
keys = list(data[0].keys())
|
|
27
|
+
if not keys or any(list(r.keys()) != keys for r in data):
|
|
28
|
+
return json_text
|
|
29
|
+
def lit(v):
|
|
30
|
+
s = json.dumps(v, ensure_ascii=False)
|
|
31
|
+
return None if (_SEP in s or _NL in s) else s
|
|
32
|
+
if any(lit(r[c]) is None for r in data for c in keys):
|
|
33
|
+
return json_text # a value contains | or newline -> bail (stay lossless)
|
|
34
|
+
# Nikhilam: only STRING columns of form prefix+digits, shared prefix
|
|
35
|
+
nik = {}
|
|
36
|
+
for c in keys:
|
|
37
|
+
if not all(isinstance(r[c], str) for r in data):
|
|
38
|
+
continue
|
|
39
|
+
ms = [re.match(r"^(.*?)(\d+)$", r[c]) for r in data]
|
|
40
|
+
if all(ms) and len({m.group(1) for m in ms}) == 1 and all(str(int(m.group(2)))==m.group(2) for m in ms):
|
|
41
|
+
pref = ms[0].group(1); base = (min(int(m.group(2)) for m in ms) // 100) * 100
|
|
42
|
+
nik[c] = (pref, base)
|
|
43
|
+
hdr = _SEP.join((f"{c}=" + json.dumps([nik[c][1], nik[c][0]], ensure_ascii=False, separators=(",",":")) if c in nik else c) for c in keys)
|
|
44
|
+
out = [hdr]; prev = None
|
|
45
|
+
for r in data:
|
|
46
|
+
cells = []
|
|
47
|
+
for c in keys:
|
|
48
|
+
if c in nik:
|
|
49
|
+
pref, base = nik[c]; cells.append(str(int(r[c][len(pref):]) - base))
|
|
50
|
+
elif prev is not None and prev[c] == r[c] and type(prev[c]) == type(r[c]):
|
|
51
|
+
cells.append(".")
|
|
52
|
+
else:
|
|
53
|
+
cells.append(lit(r[c]))
|
|
54
|
+
out.append(_SEP.join(cells)); prev = r
|
|
55
|
+
return _NL.join(out)
|
|
56
|
+
|
|
57
|
+
def unpack(packed: str):
|
|
58
|
+
lines = packed.split(_NL)
|
|
59
|
+
if len(lines) < 2 or _SEP not in lines[0]:
|
|
60
|
+
return json.loads(packed)
|
|
61
|
+
keys, nik = [], {}
|
|
62
|
+
for h in lines[0].split(_SEP):
|
|
63
|
+
m = re.match(r"^(.*?)=(\[.*\])$", h)
|
|
64
|
+
if m:
|
|
65
|
+
base, pref = json.loads(m.group(2))
|
|
66
|
+
keys.append(m.group(1)); nik[m.group(1)] = (pref, base)
|
|
67
|
+
else:
|
|
68
|
+
keys.append(h)
|
|
69
|
+
rows, prev = [], {}
|
|
70
|
+
for line in lines[1:]:
|
|
71
|
+
cells = line.split(_SEP); row = {}
|
|
72
|
+
for c, val in zip(keys, cells):
|
|
73
|
+
if c in nik:
|
|
74
|
+
pref, base = nik[c]; row[c] = f"{pref}{base + int(val)}"
|
|
75
|
+
elif val == ".":
|
|
76
|
+
row[c] = prev[c]
|
|
77
|
+
else:
|
|
78
|
+
row[c] = json.loads(val)
|
|
79
|
+
rows.append(row); prev = row
|
|
80
|
+
return rows
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: vedic-bpe
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Lossless LLM token reducer for repetitive structured JSON (zero-dependency, tiktoken-measured).
|
|
5
|
+
Author: Aman Bhawsar
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/Amanbhaw/ai-packages
|
|
8
|
+
Keywords: llm,tokens,tiktoken,json,compression,prompt,rag,lossless
|
|
9
|
+
Requires-Python: >=3.8
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Dynamic: license-file
|
|
13
|
+
|
|
14
|
+
# vedic-bpe
|
|
15
|
+
|
|
16
|
+
**Lossless LLM token reducer for repetitive structured JSON.** Zero dependencies.
|
|
17
|
+
Bit-exact round-trip (types preserved). 0% token-inflation guarantee.
|
|
18
|
+
|
|
19
|
+
Named after classical Indian mathematics (**branding**); the transforms are standard
|
|
20
|
+
and **measured against real `tiktoken` (o200k_base)** — not claimed.
|
|
21
|
+
|
|
22
|
+
| transform | classical name | what it does |
|
|
23
|
+
|---|---|---|
|
|
24
|
+
| key-folding + row inheritance | **Anuvritti** (Panini, Ashtadhyayi) | repeated object keys → one header; a cell equal to the one above → `.` |
|
|
25
|
+
| base-anchor delta coding | **Nikhilam / Brahmagupta** (finite differences) | string ID columns `SUP-101, SUP-102` → base `SUP-100` + delta `1, 2` |
|
|
26
|
+
|
|
27
|
+
## Measured results (real `tiktoken` o200k_base, lossless verified)
|
|
28
|
+
| input | before | after | saved |
|
|
29
|
+
|---|---|---|---|
|
|
30
|
+
| 20 highly-repetitive sensor rows | 782 tok | 138 tok | **82%** |
|
|
31
|
+
| 20 varied rows | 742 tok | 269 tok | **64%** |
|
|
32
|
+
| 4-row support tickets | 138 tok | 48 tok | **65%** |
|
|
33
|
+
|
|
34
|
+
Savings **scale with redundancy** (Shannon limit: a token holds ≤ log₂(V) ≈ 17.6 bits,
|
|
35
|
+
so *arbitrary* text can't be squeezed — only redundant structured data can).
|
|
36
|
+
|
|
37
|
+
## Usage
|
|
38
|
+
```python
|
|
39
|
+
from vedic_bpe import pack, unpack
|
|
40
|
+
packed = pack(json_text) # send `packed` to the LLM (still human/LLM-readable)
|
|
41
|
+
original = unpack(packed) # bit-exact recovery
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Honest scope — what this is NOT
|
|
45
|
+
- **Not** a compressor for arbitrary prose (information theory forbids it).
|
|
46
|
+
- Works on **arrays of flat JSON objects with shared keys and scalar values**
|
|
47
|
+
(support tickets, RAG metadata rows, API/sensor logs). Values must not contain `|` or newline.
|
|
48
|
+
- Output stays **readable** so the model can still reason over it — unlike bit-packing
|
|
49
|
+
into opaque glyphs (which saves tokens but the model can't understand).
|
|
50
|
+
- `pack()` **never inflates**: if it can't shrink the real token count, it returns the input untouched.
|
|
51
|
+
|
|
52
|
+
MIT © Aman Bhawsar
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
vedic_bpe
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import sys, os, json
|
|
2
|
+
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src"))
|
|
3
|
+
from vedic_bpe import pack, unpack
|
|
4
|
+
|
|
5
|
+
def sample():
|
|
6
|
+
return json.dumps([
|
|
7
|
+
{"ticket_id":"SUP-101","tier":"enterprise","issue":"billing_delay","status":"open"},
|
|
8
|
+
{"ticket_id":"SUP-102","tier":"enterprise","issue":"billing_delay","status":"open"},
|
|
9
|
+
{"ticket_id":"SUP-103","tier":"enterprise","issue":"api_timeout","status":"open"},
|
|
10
|
+
{"ticket_id":"SUP-104","tier":"pro","issue":"api_timeout","status":"closed"},
|
|
11
|
+
], indent=1)
|
|
12
|
+
|
|
13
|
+
def test_lossless():
|
|
14
|
+
s = sample(); p = pack(s)
|
|
15
|
+
back = unpack(p)
|
|
16
|
+
assert back == json.loads(s), "round-trip must be bit-exact"
|
|
17
|
+
print(" ok lossless round-trip (bit-exact)")
|
|
18
|
+
|
|
19
|
+
def test_no_inflation_on_prose():
|
|
20
|
+
prose = json.dumps("just a plain string not an array")
|
|
21
|
+
assert pack(prose) == prose, "non-table input returned untouched"
|
|
22
|
+
print(" ok 0% inflation on non-table input")
|
|
23
|
+
|
|
24
|
+
def test_real_token_savings():
|
|
25
|
+
s = sample(); p = pack(s)
|
|
26
|
+
try:
|
|
27
|
+
import tiktoken; enc = tiktoken.get_encoding("o200k_base")
|
|
28
|
+
a, b = len(enc.encode(s)), len(enc.encode(p))
|
|
29
|
+
print(f" ok real o200k_base tokens: {a} -> {b} ({100*(1-b/a):.0f}% saved)")
|
|
30
|
+
assert b < a
|
|
31
|
+
except ImportError:
|
|
32
|
+
print(" skip tiktoken not installed (install to measure real savings)")
|
|
33
|
+
|
|
34
|
+
if __name__ == "__main__":
|
|
35
|
+
test_lossless(); test_no_inflation_on_prose(); test_real_token_savings()
|
|
36
|
+
print("vedic-bpe: all tests passed")
|