brevit 0.1.2 → 0.1.4
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.
- package/README.md +128 -12
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -213,7 +213,23 @@ const jsonString = '{"order": {"id": "o-456", "status": "SHIPPED"}}';
|
|
|
213
213
|
|
|
214
214
|
// Brevit automatically detects JSON strings
|
|
215
215
|
const optimized = await brevit.brevity(jsonString);
|
|
216
|
-
// Output:
|
|
216
|
+
// Output (with abbreviations enabled by default):
|
|
217
|
+
// @o=order
|
|
218
|
+
// @o.id:o-456
|
|
219
|
+
// @o.status:SHIPPED
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
#### Example 1.2a: Abbreviations Disabled
|
|
223
|
+
|
|
224
|
+
```javascript
|
|
225
|
+
const brevitNoAbbr = new BrevitClient(new BrevitConfig({
|
|
226
|
+
jsonMode: JsonOptimizationMode.Flatten,
|
|
227
|
+
enableAbbreviations: false // Disable abbreviations
|
|
228
|
+
}));
|
|
229
|
+
|
|
230
|
+
const jsonString = '{"order": {"id": "o-456", "status": "SHIPPED"}}';
|
|
231
|
+
const optimized = await brevitNoAbbr.brevity(jsonString);
|
|
232
|
+
// Output (without abbreviations):
|
|
217
233
|
// order.id:o-456
|
|
218
234
|
// order.status:SHIPPED
|
|
219
235
|
```
|
|
@@ -249,7 +265,54 @@ const complexData = {
|
|
|
249
265
|
};
|
|
250
266
|
|
|
251
267
|
const optimized = await brevit.brevity(complexData);
|
|
252
|
-
// Output:
|
|
268
|
+
// Output (with abbreviations enabled by default):
|
|
269
|
+
// @c=context
|
|
270
|
+
// @c.task:Our favorite hikes together
|
|
271
|
+
// @c.location:Boulder
|
|
272
|
+
// @c.season:spring_2025
|
|
273
|
+
// friends[3]:ana,luis,sam
|
|
274
|
+
// hikes[2]{companion,distanceKm,elevationGain,id,name,wasSunny}:
|
|
275
|
+
// ana,7.5,320,1,Blue Lake Trail,true
|
|
276
|
+
// luis,9.2,540,2,Ridge Overlook,false
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
#### Example 1.3a: Complex Data with Abbreviations Disabled
|
|
280
|
+
|
|
281
|
+
```javascript
|
|
282
|
+
const brevitNoAbbr = new BrevitClient(new BrevitConfig({
|
|
283
|
+
jsonMode: JsonOptimizationMode.Flatten,
|
|
284
|
+
enableAbbreviations: false // Disable abbreviations
|
|
285
|
+
}));
|
|
286
|
+
|
|
287
|
+
const complexData = {
|
|
288
|
+
context: {
|
|
289
|
+
task: "Our favorite hikes together",
|
|
290
|
+
location: "Boulder",
|
|
291
|
+
season: "spring_2025"
|
|
292
|
+
},
|
|
293
|
+
friends: ["ana", "luis", "sam"],
|
|
294
|
+
hikes: [
|
|
295
|
+
{
|
|
296
|
+
id: 1,
|
|
297
|
+
name: "Blue Lake Trail",
|
|
298
|
+
distanceKm: 7.5,
|
|
299
|
+
elevationGain: 320,
|
|
300
|
+
companion: "ana",
|
|
301
|
+
wasSunny: true
|
|
302
|
+
},
|
|
303
|
+
{
|
|
304
|
+
id: 2,
|
|
305
|
+
name: "Ridge Overlook",
|
|
306
|
+
distanceKm: 9.2,
|
|
307
|
+
elevationGain: 540,
|
|
308
|
+
companion: "luis",
|
|
309
|
+
wasSunny: false
|
|
310
|
+
}
|
|
311
|
+
]
|
|
312
|
+
};
|
|
313
|
+
|
|
314
|
+
const optimized = await brevitNoAbbr.brevity(complexData);
|
|
315
|
+
// Output (without abbreviations):
|
|
253
316
|
// context.task:Our favorite hikes together
|
|
254
317
|
// context.location:Boulder
|
|
255
318
|
// context.season:spring_2025
|
|
@@ -617,7 +680,9 @@ const config = new BrevitConfig({
|
|
|
617
680
|
textMode: 'Clean', // Text optimization strategy
|
|
618
681
|
imageMode: 'Ocr', // Image optimization strategy
|
|
619
682
|
jsonPathsToKeep: [], // Paths to keep for Filter mode
|
|
620
|
-
longTextThreshold: 500
|
|
683
|
+
longTextThreshold: 500, // Character threshold for text optimization
|
|
684
|
+
enableAbbreviations: true, // Enable abbreviation feature (default: true)
|
|
685
|
+
abbreviationThreshold: 2 // Minimum occurrences to create abbreviation (default: 2)
|
|
621
686
|
});
|
|
622
687
|
```
|
|
623
688
|
|
|
@@ -670,6 +735,8 @@ const config: BrevitConfigOptions = {
|
|
|
670
735
|
imageMode: ImageOptimizationMode.Ocr,
|
|
671
736
|
jsonPathsToKeep: ['user.name', 'order.orderId'],
|
|
672
737
|
longTextThreshold: 1000,
|
|
738
|
+
enableAbbreviations: true, // Default: true
|
|
739
|
+
abbreviationThreshold: 2 // Default: 2
|
|
673
740
|
};
|
|
674
741
|
|
|
675
742
|
const client = new BrevitClient(new BrevitConfig(config));
|
|
@@ -879,12 +946,15 @@ Consider alternatives when:
|
|
|
879
946
|
|
|
880
947
|
### Token Reduction
|
|
881
948
|
|
|
882
|
-
| Object Type | Original Tokens | Brevit
|
|
883
|
-
|
|
884
|
-
| Simple Object | 45 | 28 |
|
|
885
|
-
| Complex Object | 234 | 127 |
|
|
886
|
-
| Nested Arrays | 156 | 89 |
|
|
887
|
-
| API Response | 312 | 178 |
|
|
949
|
+
| Object Type | Original Tokens | Brevit (No Abbr) | Brevit (With Abbr) | Total Reduction |
|
|
950
|
+
|-------------|----------------|------------------|-------------------|-----------------|
|
|
951
|
+
| Simple Object | 45 | 28 | 26 | 42% |
|
|
952
|
+
| Complex Object | 234 | 127 | 105 | 55% |
|
|
953
|
+
| Nested Arrays | 156 | 89 | 75 | 52% |
|
|
954
|
+
| API Response | 312 | 178 | 145 | 54% |
|
|
955
|
+
| Deeply Nested | 95 | 78 | 65 | 32% |
|
|
956
|
+
|
|
957
|
+
**Note**: Abbreviations are enabled by default and provide additional 10-25% savings on top of base optimization.
|
|
888
958
|
|
|
889
959
|
### Performance
|
|
890
960
|
|
|
@@ -969,13 +1039,23 @@ const order = {
|
|
|
969
1039
|
};
|
|
970
1040
|
```
|
|
971
1041
|
|
|
972
|
-
**Output (with tabular optimization):**
|
|
1042
|
+
**Output (with tabular optimization and abbreviations enabled by default):**
|
|
1043
|
+
```
|
|
1044
|
+
orderId: o-456
|
|
1045
|
+
friends[3]: ana,luis,sam
|
|
1046
|
+
@i=items
|
|
1047
|
+
@i[2]{quantity,sku}:
|
|
1048
|
+
1,A-88
|
|
1049
|
+
2,T-22
|
|
1050
|
+
```
|
|
1051
|
+
|
|
1052
|
+
**Output (with abbreviations disabled):**
|
|
973
1053
|
```
|
|
974
1054
|
orderId: o-456
|
|
975
1055
|
friends[3]: ana,luis,sam
|
|
976
1056
|
items[2]{quantity,sku}:
|
|
977
|
-
|
|
978
|
-
|
|
1057
|
+
1,A-88
|
|
1058
|
+
2,T-22
|
|
979
1059
|
```
|
|
980
1060
|
|
|
981
1061
|
**For non-uniform arrays (fallback):**
|
|
@@ -1004,10 +1084,44 @@ items[2].quantity: 2
|
|
|
1004
1084
|
- **Nested Objects**: Dot notation for nested properties
|
|
1005
1085
|
- **Tabular Arrays**: Uniform object arrays automatically formatted in compact tabular format (`items[2]{field1,field2}:`)
|
|
1006
1086
|
- **Primitive Arrays**: Comma-separated format (`friends[3]: ana,luis,sam`)
|
|
1087
|
+
- **Abbreviation System** (Default: Enabled): Automatically creates short aliases for repeated prefixes (`@u=user`, `@o=order`)
|
|
1007
1088
|
- **Hybrid Approach**: Automatically detects optimal format, falls back to indexed format for mixed data
|
|
1008
1089
|
- **Null Handling**: Explicit `null` values
|
|
1009
1090
|
- **Type Preservation**: Numbers, booleans preserved as strings
|
|
1010
1091
|
|
|
1092
|
+
### Abbreviation System (Default: Enabled)
|
|
1093
|
+
|
|
1094
|
+
Brevit automatically creates abbreviations for frequently repeated key prefixes, placing definitions at the top of the output:
|
|
1095
|
+
|
|
1096
|
+
**Example:**
|
|
1097
|
+
```
|
|
1098
|
+
@u=user
|
|
1099
|
+
@o=order
|
|
1100
|
+
@u.name:John Doe
|
|
1101
|
+
@u.email:john@example.com
|
|
1102
|
+
@o.id:o-456
|
|
1103
|
+
@o.status:SHIPPED
|
|
1104
|
+
```
|
|
1105
|
+
|
|
1106
|
+
**Benefits:**
|
|
1107
|
+
- **10-25% additional token savings** on nested data
|
|
1108
|
+
- **Self-documenting**: Abbreviations are defined at the top
|
|
1109
|
+
- **LLM-friendly**: Models easily understand the mapping
|
|
1110
|
+
- **Configurable**: Can be disabled with `enableAbbreviations: false`
|
|
1111
|
+
|
|
1112
|
+
**When Abbreviations Help Most:**
|
|
1113
|
+
- Deeply nested JSON structures
|
|
1114
|
+
- Arrays of objects with repeated field names
|
|
1115
|
+
- API responses with consistent schemas
|
|
1116
|
+
- Data with many repeated prefixes (e.g., `user.profile.settings.theme`)
|
|
1117
|
+
|
|
1118
|
+
**Disable Abbreviations:**
|
|
1119
|
+
```javascript
|
|
1120
|
+
const config = new BrevitConfig({
|
|
1121
|
+
enableAbbreviations: false // Disable abbreviation feature
|
|
1122
|
+
});
|
|
1123
|
+
```
|
|
1124
|
+
|
|
1011
1125
|
## API
|
|
1012
1126
|
|
|
1013
1127
|
### BrevitClient
|
|
@@ -1061,6 +1175,8 @@ class BrevitConfig {
|
|
|
1061
1175
|
imageMode: ImageOptimizationModeType;
|
|
1062
1176
|
jsonPathsToKeep: string[];
|
|
1063
1177
|
longTextThreshold: number;
|
|
1178
|
+
enableAbbreviations: boolean; // Default: true
|
|
1179
|
+
abbreviationThreshold: number; // Default: 2
|
|
1064
1180
|
}
|
|
1065
1181
|
```
|
|
1066
1182
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "brevit",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "A high-performance JavaScript library for semantically compressing and optimizing data before sending it to a Large Language Model (LLM).",
|
|
5
5
|
"main": "src/brevit.js",
|
|
6
6
|
"types": "src/brevit.d.ts",
|