compress-lightreach 1.0.1 → 1.0.2
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 +330 -80
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,25 +1,26 @@
|
|
|
1
1
|
# Compress Light Reach
|
|
2
2
|
|
|
3
|
-
**
|
|
3
|
+
**AI cost management SDK with intelligent model routing, prompt compression, and real-time token tracking**
|
|
4
4
|
|
|
5
5
|
[](https://badge.fury.io/js/compress-lightreach)
|
|
6
6
|
[](https://nodejs.org/)
|
|
7
7
|
[](https://opensource.org/licenses/MIT)
|
|
8
8
|
|
|
9
|
-
Compress Light Reach is a Node.js/TypeScript
|
|
9
|
+
Compress Light Reach is a Node.js/TypeScript SDK that provides intelligent model routing and prompt compression for LLM applications, reducing token usage and costs while maintaining quality.
|
|
10
10
|
|
|
11
11
|
## Features
|
|
12
12
|
|
|
13
|
-
- **
|
|
14
|
-
- **
|
|
13
|
+
- **Intelligent Model Routing**: Automatically selects optimal model based on quality requirements (HLE) and available provider keys
|
|
14
|
+
- **Token-aware Compression**: Replaces repeated substrings with shorter placeholders
|
|
15
|
+
- **Dual Algorithms**:
|
|
15
16
|
- Fast greedy (~99% optimal) for daily use
|
|
16
17
|
- Optimal DP (O(n²)) for critical prompts
|
|
17
18
|
- **Lossless**: Perfect decompression guaranteed
|
|
18
|
-
- **Output
|
|
19
|
-
- **Cloud API**: Uses Light Reach's cloud service for compression
|
|
20
|
-
- **
|
|
19
|
+
- **Output Compression**: Optional model output compression support
|
|
20
|
+
- **Cloud API**: Uses Light Reach's cloud service for compression and routing
|
|
21
|
+
- **Multi-provider Support**: OpenAI, Anthropic, Google, DeepSeek, Moonshot
|
|
21
22
|
- **TypeScript**: Full TypeScript support with type definitions
|
|
22
|
-
- **
|
|
23
|
+
- **BYOK**: Provider API keys managed securely in dashboard (never passed through SDK)
|
|
23
24
|
|
|
24
25
|
## Installation
|
|
25
26
|
|
|
@@ -33,12 +34,12 @@ or
|
|
|
33
34
|
yarn add compress-lightreach
|
|
34
35
|
```
|
|
35
36
|
|
|
36
|
-
## Quick Start
|
|
37
|
+
## Quick Start
|
|
37
38
|
|
|
38
39
|
The SDK uses **intelligent model routing** and targets `POST /api/v2/complete`.
|
|
39
40
|
|
|
40
|
-
- Authenticate with your **LightReach API key** (env var `PCOMPRESLR_API_KEY`)
|
|
41
|
-
- Manage **provider keys** (OpenAI/Anthropic/Google) in the dashboard (BYOK)
|
|
41
|
+
- Authenticate with your **LightReach API key** (env var `PCOMPRESLR_API_KEY` or `LIGHTREACH_API_KEY`)
|
|
42
|
+
- Manage **provider keys** (OpenAI/Anthropic/Google/etc.) in the dashboard (BYOK)
|
|
42
43
|
- System automatically selects optimal model based on your requirements
|
|
43
44
|
|
|
44
45
|
```typescript
|
|
@@ -51,7 +52,7 @@ const result = await client.complete({
|
|
|
51
52
|
{ role: 'system', content: 'You are a helpful assistant.' },
|
|
52
53
|
{ role: 'user', content: 'Explain quantum computing in simple terms.' },
|
|
53
54
|
],
|
|
54
|
-
|
|
55
|
+
desired_hle: 30, // Quality preference (0-40, where 40 is SOTA)
|
|
55
56
|
});
|
|
56
57
|
|
|
57
58
|
console.log(result.decompressed_response);
|
|
@@ -64,14 +65,14 @@ console.log(`Token savings: ${result.compression_stats.token_savings}`);
|
|
|
64
65
|
```typescript
|
|
65
66
|
const result = await client.complete({
|
|
66
67
|
messages: [{ role: 'user', content: 'Generate a long report...' }],
|
|
67
|
-
|
|
68
|
-
|
|
68
|
+
desired_hle: 25,
|
|
69
|
+
compress_output: true,
|
|
69
70
|
});
|
|
70
71
|
|
|
71
72
|
console.log(result.decompressed_response);
|
|
72
73
|
```
|
|
73
74
|
|
|
74
|
-
### Intelligent Model Routing
|
|
75
|
+
### Intelligent Model Routing
|
|
75
76
|
|
|
76
77
|
The system automatically selects the optimal model based on quality requirements and your available provider keys:
|
|
77
78
|
|
|
@@ -82,14 +83,14 @@ const client = new PcompresslrAPIClient("your-lightreach-api-key");
|
|
|
82
83
|
|
|
83
84
|
// Cross-provider optimization: system picks cheapest model meeting your quality bar
|
|
84
85
|
const result = await client.complete({
|
|
85
|
-
messages: [{role: 'user', content: 'Explain quantum computing'}],
|
|
86
|
-
|
|
86
|
+
messages: [{ role: 'user', content: 'Explain quantum computing' }],
|
|
87
|
+
desired_hle: 30, // Quality preference (0-40, where 40 is SOTA)
|
|
87
88
|
});
|
|
88
89
|
|
|
89
90
|
// Check what was selected
|
|
90
|
-
console.log(result.routing_info?.selected_model);
|
|
91
|
-
console.log(result.routing_info?.selected_provider);
|
|
92
|
-
console.log(result.routing_info?.model_hle);
|
|
91
|
+
console.log(result.routing_info?.selected_model); // e.g., "gpt-4o-mini"
|
|
92
|
+
console.log(result.routing_info?.selected_provider); // e.g., "openai"
|
|
93
|
+
console.log(result.routing_info?.model_hle); // e.g., 32.5
|
|
93
94
|
console.log(result.routing_info?.model_price_per_million); // e.g., 0.15
|
|
94
95
|
```
|
|
95
96
|
|
|
@@ -100,24 +101,24 @@ Optionally constrain to a specific provider:
|
|
|
100
101
|
```typescript
|
|
101
102
|
// Only use OpenAI models, but pick the cheapest one meeting HLE 35
|
|
102
103
|
const result = await client.complete({
|
|
103
|
-
messages: [{role: 'user', content: 'Write a poem'}],
|
|
104
|
-
|
|
105
|
-
|
|
104
|
+
messages: [{ role: 'user', content: 'Write a poem' }],
|
|
105
|
+
llm_provider: 'openai', // Optional: constrain to one provider
|
|
106
|
+
desired_hle: 35,
|
|
106
107
|
});
|
|
107
108
|
```
|
|
108
109
|
|
|
109
110
|
### HLE Cascading with Admin Controls
|
|
110
111
|
|
|
111
|
-
Admins can set quality **ceilings** via the dashboard (global or per-tag) to control costs. Your `
|
|
112
|
+
Admins can set quality **ceilings** via the dashboard (global or per-tag) to control costs. Your `desired_hle` is a preference, but requests will error if they exceed the admin-set ceiling:
|
|
112
113
|
|
|
113
114
|
```typescript
|
|
114
115
|
// Admin set global HLE ceiling to 30%
|
|
115
116
|
// Requesting above the ceiling will error
|
|
116
117
|
try {
|
|
117
118
|
const result = await client.complete({
|
|
118
|
-
messages: [{role: 'user', content: 'Process payment'}],
|
|
119
|
-
|
|
120
|
-
tags: {env: 'production'},
|
|
119
|
+
messages: [{ role: 'user', content: 'Process payment' }],
|
|
120
|
+
desired_hle: 35, // ERROR: exceeds ceiling of 30
|
|
121
|
+
tags: { env: 'production' },
|
|
121
122
|
});
|
|
122
123
|
} catch (e) {
|
|
123
124
|
console.error(e.message); // "Requested HLE 35% exceeds workspace maximum of 30%"
|
|
@@ -125,9 +126,9 @@ try {
|
|
|
125
126
|
|
|
126
127
|
// Correct usage: request within ceiling
|
|
127
128
|
const result = await client.complete({
|
|
128
|
-
messages: [{role: 'user', content: 'Process payment'}],
|
|
129
|
-
|
|
130
|
-
tags: {env: 'production'},
|
|
129
|
+
messages: [{ role: 'user', content: 'Process payment' }],
|
|
130
|
+
desired_hle: 25, // OK: below ceiling of 30
|
|
131
|
+
tags: { env: 'production' },
|
|
131
132
|
});
|
|
132
133
|
|
|
133
134
|
// Check if your HLE was lowered by admin ceiling
|
|
@@ -138,6 +139,61 @@ if (result.routing_info?.hle_clamped) {
|
|
|
138
139
|
}
|
|
139
140
|
```
|
|
140
141
|
|
|
142
|
+
### Using the LightReach Wrapper Class
|
|
143
|
+
|
|
144
|
+
For a more ergonomic API with camelCase parameters, use the `LightReach` class:
|
|
145
|
+
|
|
146
|
+
```typescript
|
|
147
|
+
import { LightReach } from 'compress-lightreach';
|
|
148
|
+
|
|
149
|
+
const client = new LightReach({
|
|
150
|
+
apiKey: 'your-lightreach-api-key',
|
|
151
|
+
defaultModel: 'gpt-4',
|
|
152
|
+
defaultProvider: 'openai',
|
|
153
|
+
useOptimal: false, // Use greedy algorithm by default
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
const result = await client.complete({
|
|
157
|
+
messages: [{ role: 'user', content: 'Hello!' }],
|
|
158
|
+
compress: true,
|
|
159
|
+
compressOutput: false,
|
|
160
|
+
compressionConfig: {
|
|
161
|
+
compressSystem: false,
|
|
162
|
+
compressUser: true,
|
|
163
|
+
compressAssistant: false,
|
|
164
|
+
compressOnlyLastNUser: 1,
|
|
165
|
+
},
|
|
166
|
+
temperature: 0.7,
|
|
167
|
+
maxTokens: 1000,
|
|
168
|
+
tags: { env: 'production' },
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
console.log(result.decompressed_response);
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### Compression Only (No LLM Call)
|
|
175
|
+
|
|
176
|
+
```typescript
|
|
177
|
+
import { PcompresslrAPIClient } from 'compress-lightreach';
|
|
178
|
+
|
|
179
|
+
const client = new PcompresslrAPIClient("your-lightreach-api-key");
|
|
180
|
+
|
|
181
|
+
// Compress text without making an LLM call
|
|
182
|
+
const compressed = await client.compress(
|
|
183
|
+
"Your text with repeated content here...",
|
|
184
|
+
"gpt-4", // Model for tokenization
|
|
185
|
+
"greedy", // Algorithm: 'greedy' or 'optimal'
|
|
186
|
+
{ env: 'dev' } // Optional tags
|
|
187
|
+
);
|
|
188
|
+
|
|
189
|
+
console.log(compressed.llm_format);
|
|
190
|
+
console.log(`Compression ratio: ${compressed.compression_ratio}`);
|
|
191
|
+
|
|
192
|
+
// Decompress later
|
|
193
|
+
const decompressed = await client.decompress(compressed.llm_format);
|
|
194
|
+
console.log(decompressed.decompressed);
|
|
195
|
+
```
|
|
196
|
+
|
|
141
197
|
### Command Line Interface
|
|
142
198
|
|
|
143
199
|
```bash
|
|
@@ -145,7 +201,6 @@ if (result.routing_info?.hle_clamped) {
|
|
|
145
201
|
export PCOMPRESLR_API_KEY=your-api-key
|
|
146
202
|
|
|
147
203
|
# Compress a prompt
|
|
148
|
-
# Run the CLI directly (the published binary name is `pcompresslr`)
|
|
149
204
|
npx pcompresslr "Your prompt with repeated text here..."
|
|
150
205
|
|
|
151
206
|
# Use optimal algorithm only
|
|
@@ -161,67 +216,239 @@ npx pcompresslr "Your prompt here" --greedy-only
|
|
|
161
216
|
|
|
162
217
|
Main API client for intelligent model routing and compression.
|
|
163
218
|
|
|
164
|
-
#### Constructor
|
|
219
|
+
#### Constructor
|
|
220
|
+
|
|
221
|
+
```typescript
|
|
222
|
+
new PcompresslrAPIClient(apiKey?: string, apiUrl?: string, timeout?: number)
|
|
223
|
+
```
|
|
165
224
|
|
|
166
|
-
|
|
167
|
-
- `
|
|
168
|
-
- `
|
|
225
|
+
**Parameters:**
|
|
226
|
+
- `apiKey` (string, optional): LightReach API key. Falls back to `LIGHTREACH_API_KEY` or `PCOMPRESLR_API_KEY` env vars.
|
|
227
|
+
- `apiUrl` (string, optional): Override base API URL. Falls back to `PCOMPRESLR_API_URL` env var. Default: `https://api.compress.lightreach.io`
|
|
228
|
+
- `timeout` (number, optional): Request timeout in milliseconds. Default: `120000` (2 minutes)
|
|
169
229
|
|
|
170
230
|
#### Methods
|
|
171
231
|
|
|
172
|
-
##### `complete(request)
|
|
232
|
+
##### `complete(request: CompleteV2Request): Promise<CompleteResponse>`
|
|
173
233
|
|
|
174
234
|
Messages-first completion with intelligent routing (POST `/api/v2/complete`).
|
|
175
235
|
|
|
176
|
-
**Parameters (CompleteV2Request):**
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
236
|
+
**Request Parameters (`CompleteV2Request`):**
|
|
237
|
+
|
|
238
|
+
| Parameter | Type | Default | Description |
|
|
239
|
+
|-----------|------|---------|-------------|
|
|
240
|
+
| `messages` | `Message[]` | required | Conversation history with `role` and `content` |
|
|
241
|
+
| `llm_provider` | `'openai' \| 'anthropic' \| 'google' \| 'deepseek' \| 'moonshot'` | — | Optional provider constraint. Omit for cross-provider optimization |
|
|
242
|
+
| `desired_hle` | `number` | — | Quality preference (0-40, where 40 is SOTA). Must not exceed admin ceilings |
|
|
243
|
+
| `compress` | `boolean` | `true` | Whether to compress messages |
|
|
244
|
+
| `compress_output` | `boolean` | `false` | Whether to request compressed output from LLM |
|
|
245
|
+
| `algorithm` | `'greedy' \| 'optimal'` | `'greedy'` | Compression algorithm |
|
|
246
|
+
| `compression_config` | `object` | — | Per-role compression settings (see below) |
|
|
247
|
+
| `temperature` | `number` | — | LLM temperature parameter |
|
|
248
|
+
| `max_tokens` | `number` | — | Maximum tokens to generate |
|
|
249
|
+
| `tags` | `Record<string, string>` | — | Tags for cost attribution and tag-level HLE ceilings |
|
|
250
|
+
| `max_history_messages` | `number` | — | Limit conversation history length |
|
|
251
|
+
|
|
252
|
+
**`compression_config` options:**
|
|
253
|
+
|
|
254
|
+
```typescript
|
|
255
|
+
{
|
|
256
|
+
compress_system?: boolean; // default: false
|
|
257
|
+
compress_user?: boolean; // default: true
|
|
258
|
+
compress_assistant?: boolean; // default: false
|
|
259
|
+
compress_only_last_n_user?: number | null; // default: 1
|
|
260
|
+
}
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
**Response (`CompleteResponse`):**
|
|
264
|
+
|
|
265
|
+
```typescript
|
|
266
|
+
{
|
|
267
|
+
decompressed_response: string; // Final decompressed LLM response
|
|
268
|
+
compression_stats: {
|
|
269
|
+
original_size_chars: number;
|
|
270
|
+
compressed_size_chars: number;
|
|
271
|
+
original_tokens: number;
|
|
272
|
+
compressed_tokens: number;
|
|
273
|
+
compression_ratio: number;
|
|
274
|
+
token_savings: number;
|
|
275
|
+
token_savings_percent: number;
|
|
276
|
+
processing_time_ms?: number;
|
|
277
|
+
};
|
|
278
|
+
llm_stats: {
|
|
279
|
+
prompt_tokens: number;
|
|
280
|
+
completion_tokens: number;
|
|
281
|
+
total_tokens: number;
|
|
282
|
+
};
|
|
283
|
+
routing_info?: {
|
|
284
|
+
selected_model: string; // Model chosen by system
|
|
285
|
+
selected_provider: string; // Provider chosen by system
|
|
286
|
+
selected_model_id: string;
|
|
287
|
+
model_hle: number; // HLE score of selected model
|
|
288
|
+
model_price_per_million: number;
|
|
289
|
+
requested_hle: number | null;
|
|
290
|
+
effective_hle: number | null; // Effective HLE after admin ceilings
|
|
291
|
+
hle_source: 'request' | 'tag' | 'global' | 'none';
|
|
292
|
+
hle_clamped: boolean; // true if admin ceiling lowered your desired_hle
|
|
293
|
+
};
|
|
294
|
+
warnings?: string[];
|
|
295
|
+
|
|
296
|
+
// Convenience aliases
|
|
297
|
+
text?: string; // Alias for decompressed_response
|
|
298
|
+
tokens_saved?: number; // Alias for compression_stats.token_savings
|
|
299
|
+
tokens_used?: number; // Alias for llm_stats.total_tokens
|
|
300
|
+
compression_ratio?: number; // Alias for compression_stats.compression_ratio
|
|
301
|
+
}
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
##### `compress(prompt, model?, algorithm?, tags?): Promise<CompressResponse>`
|
|
203
305
|
|
|
204
306
|
Compression-only (POST `/api/v1/compress`).
|
|
205
307
|
|
|
206
|
-
|
|
308
|
+
**Parameters:**
|
|
309
|
+
- `prompt` (string, required): Text to compress
|
|
310
|
+
- `model` (string, optional): Model for tokenization. Default: `'gpt-4'`
|
|
311
|
+
- `algorithm` (`'greedy' | 'optimal'`, optional): Compression algorithm. Default: `'greedy'`
|
|
312
|
+
- `tags` (`Record<string, string>`, optional): Tags for attribution
|
|
313
|
+
|
|
314
|
+
**Response (`CompressResponse`):**
|
|
315
|
+
|
|
316
|
+
```typescript
|
|
317
|
+
{
|
|
318
|
+
compressed: string;
|
|
319
|
+
dictionary: Record<string, string>;
|
|
320
|
+
llm_format: string;
|
|
321
|
+
compression_ratio: number;
|
|
322
|
+
original_size: number;
|
|
323
|
+
compressed_size: number;
|
|
324
|
+
processing_time_ms: number;
|
|
325
|
+
algorithm: string;
|
|
326
|
+
}
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
##### `decompress(llmFormat): Promise<DecompressResponse>`
|
|
207
330
|
|
|
208
331
|
Decompress an LLM-formatted compressed prompt (POST `/api/v1/decompress`).
|
|
209
332
|
|
|
210
|
-
|
|
333
|
+
**Parameters:**
|
|
334
|
+
- `llmFormat` (string, required): The `llm_format` string from a compress response
|
|
335
|
+
|
|
336
|
+
**Response (`DecompressResponse`):**
|
|
337
|
+
|
|
338
|
+
```typescript
|
|
339
|
+
{
|
|
340
|
+
decompressed: string;
|
|
341
|
+
processing_time_ms: number;
|
|
342
|
+
}
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
##### `healthCheck(): Promise<HealthCheckResponse>`
|
|
211
346
|
|
|
212
347
|
Check API health status (GET `/health`).
|
|
213
348
|
|
|
349
|
+
**Response:**
|
|
350
|
+
|
|
351
|
+
```typescript
|
|
352
|
+
{
|
|
353
|
+
status: string;
|
|
354
|
+
version?: string;
|
|
355
|
+
}
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
### `LightReach` Class
|
|
359
|
+
|
|
360
|
+
Convenience wrapper with camelCase parameters.
|
|
361
|
+
|
|
362
|
+
#### Constructor
|
|
363
|
+
|
|
364
|
+
```typescript
|
|
365
|
+
new LightReach(options?: {
|
|
366
|
+
apiKey?: string;
|
|
367
|
+
apiUrl?: string;
|
|
368
|
+
defaultModel?: string; // Default: 'gpt-4'
|
|
369
|
+
defaultProvider?: 'openai' | 'anthropic' | 'google'; // Default: 'openai'
|
|
370
|
+
useOptimal?: boolean; // Default: false (use greedy)
|
|
371
|
+
})
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
#### Methods
|
|
375
|
+
|
|
376
|
+
##### `complete(options: CompleteOptions): Promise<CompleteResponse>`
|
|
377
|
+
|
|
378
|
+
```typescript
|
|
379
|
+
interface CompleteOptions {
|
|
380
|
+
messages: Message[];
|
|
381
|
+
model?: string;
|
|
382
|
+
provider?: 'openai' | 'anthropic' | 'google';
|
|
383
|
+
compress?: boolean;
|
|
384
|
+
compressionConfig?: {
|
|
385
|
+
compressSystem?: boolean;
|
|
386
|
+
compressUser?: boolean;
|
|
387
|
+
compressAssistant?: boolean;
|
|
388
|
+
compressOnlyLastNUser?: number | null;
|
|
389
|
+
};
|
|
390
|
+
compressOutput?: boolean;
|
|
391
|
+
useOptimal?: boolean;
|
|
392
|
+
temperature?: number;
|
|
393
|
+
maxTokens?: number;
|
|
394
|
+
tags?: Record<string, string>;
|
|
395
|
+
maxHistoryMessages?: number;
|
|
396
|
+
}
|
|
397
|
+
```
|
|
398
|
+
|
|
399
|
+
##### `compress(text, options?): Promise<CompressResponse>`
|
|
400
|
+
|
|
401
|
+
```typescript
|
|
402
|
+
await client.compress(text, {
|
|
403
|
+
model?: string;
|
|
404
|
+
algorithm?: 'greedy' | 'optimal';
|
|
405
|
+
tags?: Record<string, string>;
|
|
406
|
+
});
|
|
407
|
+
```
|
|
408
|
+
|
|
409
|
+
### Message Types
|
|
410
|
+
|
|
411
|
+
```typescript
|
|
412
|
+
type MessageRole = 'system' | 'developer' | 'user' | 'assistant';
|
|
413
|
+
|
|
414
|
+
interface Message {
|
|
415
|
+
role: MessageRole;
|
|
416
|
+
content: string;
|
|
417
|
+
}
|
|
418
|
+
```
|
|
419
|
+
|
|
214
420
|
### Environment Variables
|
|
215
421
|
|
|
216
|
-
|
|
217
|
-
|
|
422
|
+
| Variable | Description |
|
|
423
|
+
|----------|-------------|
|
|
424
|
+
| `PCOMPRESLR_API_KEY` | Your LightReach API key (primary) |
|
|
425
|
+
| `LIGHTREACH_API_KEY` | Your LightReach API key (alternative) |
|
|
426
|
+
| `PCOMPRESLR_API_URL` | Override the API base URL (advanced/testing) |
|
|
218
427
|
|
|
219
428
|
### Exceptions
|
|
220
429
|
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
430
|
+
| Exception | Description |
|
|
431
|
+
|-----------|-------------|
|
|
432
|
+
| `PcompresslrAPIError` | Base exception class |
|
|
433
|
+
| `APIKeyError` | Invalid or missing API key |
|
|
434
|
+
| `RateLimitError` | Rate limit exceeded |
|
|
435
|
+
| `APIRequestError` | General API errors (including routing failures) |
|
|
436
|
+
|
|
437
|
+
```typescript
|
|
438
|
+
import { APIKeyError, RateLimitError, APIRequestError } from 'compress-lightreach';
|
|
439
|
+
|
|
440
|
+
try {
|
|
441
|
+
const result = await client.complete({ messages: [...] });
|
|
442
|
+
} catch (error) {
|
|
443
|
+
if (error instanceof APIKeyError) {
|
|
444
|
+
console.error('Invalid API key');
|
|
445
|
+
} else if (error instanceof RateLimitError) {
|
|
446
|
+
console.error('Rate limited, please retry later');
|
|
447
|
+
} else if (error instanceof APIRequestError) {
|
|
448
|
+
console.error('API error:', error.message);
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
```
|
|
225
452
|
|
|
226
453
|
## How It Works
|
|
227
454
|
|
|
@@ -237,7 +464,7 @@ The library:
|
|
|
237
464
|
|
|
238
465
|
## Examples
|
|
239
466
|
|
|
240
|
-
### Example 1:
|
|
467
|
+
### Example 1: Complete with Compression
|
|
241
468
|
|
|
242
469
|
```typescript
|
|
243
470
|
import { PcompresslrAPIClient } from 'compress-lightreach';
|
|
@@ -250,10 +477,9 @@ Write a story about a dog. The dog is very friendly.
|
|
|
250
477
|
Write a story about a bird. The bird is very friendly.
|
|
251
478
|
`;
|
|
252
479
|
|
|
253
|
-
// One call handles compression, LLM request, and decompression
|
|
254
480
|
const result = await client.complete({
|
|
255
481
|
messages: [{ role: "user", content: prompt }],
|
|
256
|
-
|
|
482
|
+
desired_hle: 30,
|
|
257
483
|
});
|
|
258
484
|
|
|
259
485
|
console.log(result.decompressed_response);
|
|
@@ -262,22 +488,46 @@ console.log(`Token savings: ${result.compression_stats.token_savings} tokens`);
|
|
|
262
488
|
console.log(`Compression ratio: ${(result.compression_stats.compression_ratio * 100).toFixed(2)}%`);
|
|
263
489
|
```
|
|
264
490
|
|
|
265
|
-
### Example 2:
|
|
491
|
+
### Example 2: Output Compression
|
|
266
492
|
|
|
267
493
|
```typescript
|
|
268
494
|
import { PcompresslrAPIClient } from 'compress-lightreach';
|
|
269
495
|
|
|
270
496
|
const client = new PcompresslrAPIClient("your-lightreach-api-key");
|
|
271
497
|
|
|
272
|
-
// Complete with output compression - response is automatically decompressed
|
|
273
498
|
const result = await client.complete({
|
|
274
499
|
messages: [{ role: "user", content: "Generate a long report with repeated sections..." }],
|
|
275
|
-
|
|
276
|
-
|
|
500
|
+
desired_hle: 35,
|
|
501
|
+
compress_output: true,
|
|
277
502
|
});
|
|
503
|
+
|
|
278
504
|
console.log(result.decompressed_response);
|
|
279
505
|
```
|
|
280
506
|
|
|
507
|
+
### Example 3: Multi-turn Conversation
|
|
508
|
+
|
|
509
|
+
```typescript
|
|
510
|
+
import { PcompresslrAPIClient } from 'compress-lightreach';
|
|
511
|
+
|
|
512
|
+
const client = new PcompresslrAPIClient("your-lightreach-api-key");
|
|
513
|
+
|
|
514
|
+
const result = await client.complete({
|
|
515
|
+
messages: [
|
|
516
|
+
{ role: "system", content: "You are a helpful coding assistant." },
|
|
517
|
+
{ role: "user", content: "How do I read a file in Python?" },
|
|
518
|
+
{ role: "assistant", content: "You can use open() with a context manager..." },
|
|
519
|
+
{ role: "user", content: "How about writing to a file?" },
|
|
520
|
+
],
|
|
521
|
+
desired_hle: 30,
|
|
522
|
+
compression_config: {
|
|
523
|
+
compress_system: false,
|
|
524
|
+
compress_user: true,
|
|
525
|
+
compress_assistant: false,
|
|
526
|
+
compress_only_last_n_user: 2, // Only compress last 2 user messages
|
|
527
|
+
},
|
|
528
|
+
});
|
|
529
|
+
```
|
|
530
|
+
|
|
281
531
|
## Getting an API Key
|
|
282
532
|
|
|
283
533
|
To use Compress Light Reach, you need an API key from [compress.lightreach.io](https://compress.lightreach.io).
|
|
@@ -289,7 +539,7 @@ To use Compress Light Reach, you need an API key from [compress.lightreach.io](h
|
|
|
289
539
|
|
|
290
540
|
## Security & Privacy
|
|
291
541
|
|
|
292
|
-
**BYOK model:** Provider keys (OpenAI/Anthropic/Google) are managed in the dashboard and **never passed through this SDK**. The SDK only uses your LightReach API key for authentication with the service.
|
|
542
|
+
**BYOK model:** Provider keys (OpenAI/Anthropic/Google/etc.) are managed in the dashboard and **never passed through this SDK**. The SDK only uses your LightReach API key for authentication with the service.
|
|
293
543
|
|
|
294
544
|
## Requirements
|
|
295
545
|
|
|
@@ -298,7 +548,7 @@ To use Compress Light Reach, you need an API key from [compress.lightreach.io](h
|
|
|
298
548
|
|
|
299
549
|
## License
|
|
300
550
|
|
|
301
|
-
MIT License - see [LICENSE](
|
|
551
|
+
MIT License - see [LICENSE](./LICENSE) file for details.
|
|
302
552
|
|
|
303
553
|
## Support
|
|
304
554
|
|
package/package.json
CHANGED