compress-lightreach 1.0.1 → 1.0.3
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 +278 -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,59 @@ if (result.routing_info?.hle_clamped) {
|
|
|
138
139
|
}
|
|
139
140
|
```
|
|
140
141
|
|
|
142
|
+
### With Compression Config
|
|
143
|
+
|
|
144
|
+
Configure per-role compression settings:
|
|
145
|
+
|
|
146
|
+
```typescript
|
|
147
|
+
import { PcompresslrAPIClient } from 'compress-lightreach';
|
|
148
|
+
|
|
149
|
+
const client = new PcompresslrAPIClient("your-lightreach-api-key");
|
|
150
|
+
|
|
151
|
+
const result = await client.complete({
|
|
152
|
+
messages: [{ role: 'user', content: 'Hello!' }],
|
|
153
|
+
desired_hle: 30,
|
|
154
|
+
compress: true,
|
|
155
|
+
compress_output: false,
|
|
156
|
+
compression_config: {
|
|
157
|
+
compress_system: false,
|
|
158
|
+
compress_user: true,
|
|
159
|
+
compress_assistant: false,
|
|
160
|
+
compress_only_last_n_user: 1,
|
|
161
|
+
},
|
|
162
|
+
temperature: 0.7,
|
|
163
|
+
max_tokens: 1000,
|
|
164
|
+
tags: { env: 'production' },
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
console.log(result.decompressed_response);
|
|
168
|
+
console.log(`Model used: ${result.routing_info?.selected_model}`);
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
### Compression Only (No LLM Call)
|
|
173
|
+
|
|
174
|
+
```typescript
|
|
175
|
+
import { PcompresslrAPIClient } from 'compress-lightreach';
|
|
176
|
+
|
|
177
|
+
const client = new PcompresslrAPIClient("your-lightreach-api-key");
|
|
178
|
+
|
|
179
|
+
// Compress text without making an LLM call
|
|
180
|
+
const compressed = await client.compress(
|
|
181
|
+
"Your text with repeated content here...",
|
|
182
|
+
"gpt-4", // Model for tokenization
|
|
183
|
+
"greedy", // Algorithm: 'greedy' or 'optimal'
|
|
184
|
+
{ env: 'dev' } // Optional tags
|
|
185
|
+
);
|
|
186
|
+
|
|
187
|
+
console.log(compressed.llm_format);
|
|
188
|
+
console.log(`Compression ratio: ${compressed.compression_ratio}`);
|
|
189
|
+
|
|
190
|
+
// Decompress later
|
|
191
|
+
const decompressed = await client.decompress(compressed.llm_format);
|
|
192
|
+
console.log(decompressed.decompressed);
|
|
193
|
+
```
|
|
194
|
+
|
|
141
195
|
### Command Line Interface
|
|
142
196
|
|
|
143
197
|
```bash
|
|
@@ -145,7 +199,6 @@ if (result.routing_info?.hle_clamped) {
|
|
|
145
199
|
export PCOMPRESLR_API_KEY=your-api-key
|
|
146
200
|
|
|
147
201
|
# Compress a prompt
|
|
148
|
-
# Run the CLI directly (the published binary name is `pcompresslr`)
|
|
149
202
|
npx pcompresslr "Your prompt with repeated text here..."
|
|
150
203
|
|
|
151
204
|
# Use optimal algorithm only
|
|
@@ -161,67 +214,189 @@ npx pcompresslr "Your prompt here" --greedy-only
|
|
|
161
214
|
|
|
162
215
|
Main API client for intelligent model routing and compression.
|
|
163
216
|
|
|
164
|
-
#### Constructor
|
|
217
|
+
#### Constructor
|
|
165
218
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
219
|
+
```typescript
|
|
220
|
+
new PcompresslrAPIClient(apiKey?: string, apiUrl?: string, timeout?: number)
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
**Parameters:**
|
|
224
|
+
- `apiKey` (string, optional): LightReach API key. Falls back to `LIGHTREACH_API_KEY` or `PCOMPRESLR_API_KEY` env vars.
|
|
225
|
+
- `apiUrl` (string, optional): Override base API URL. Falls back to `PCOMPRESLR_API_URL` env var. Default: `https://api.compress.lightreach.io`
|
|
226
|
+
- `timeout` (number, optional): Request timeout in milliseconds. Default: `120000` (2 minutes)
|
|
169
227
|
|
|
170
228
|
#### Methods
|
|
171
229
|
|
|
172
|
-
##### `complete(request)
|
|
230
|
+
##### `complete(request: CompleteV2Request): Promise<CompleteResponse>`
|
|
173
231
|
|
|
174
232
|
Messages-first completion with intelligent routing (POST `/api/v2/complete`).
|
|
175
233
|
|
|
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
|
-
|
|
234
|
+
**Request Parameters (`CompleteV2Request`):**
|
|
235
|
+
|
|
236
|
+
| Parameter | Type | Default | Description |
|
|
237
|
+
|-----------|------|---------|-------------|
|
|
238
|
+
| `messages` | `Message[]` | required | Conversation history with `role` and `content` |
|
|
239
|
+
| `llm_provider` | `'openai' \| 'anthropic' \| 'google' \| 'deepseek' \| 'moonshot'` | — | Optional provider constraint. Omit for cross-provider optimization |
|
|
240
|
+
| `desired_hle` | `number` | — | Quality preference (0-40, where 40 is SOTA). Must not exceed admin ceilings |
|
|
241
|
+
| `compress` | `boolean` | `true` | Whether to compress messages |
|
|
242
|
+
| `compress_output` | `boolean` | `false` | Whether to request compressed output from LLM |
|
|
243
|
+
| `algorithm` | `'greedy' \| 'optimal'` | `'greedy'` | Compression algorithm |
|
|
244
|
+
| `compression_config` | `object` | — | Per-role compression settings (see below) |
|
|
245
|
+
| `temperature` | `number` | — | LLM temperature parameter |
|
|
246
|
+
| `max_tokens` | `number` | — | Maximum tokens to generate |
|
|
247
|
+
| `tags` | `Record<string, string>` | — | Tags for cost attribution and tag-level HLE ceilings |
|
|
248
|
+
| `max_history_messages` | `number` | — | Limit conversation history length |
|
|
249
|
+
|
|
250
|
+
**`compression_config` options:**
|
|
251
|
+
|
|
252
|
+
```typescript
|
|
253
|
+
{
|
|
254
|
+
compress_system?: boolean; // default: false
|
|
255
|
+
compress_user?: boolean; // default: true
|
|
256
|
+
compress_assistant?: boolean; // default: false
|
|
257
|
+
compress_only_last_n_user?: number | null; // default: 1
|
|
258
|
+
}
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
**Response (`CompleteResponse`):**
|
|
262
|
+
|
|
263
|
+
```typescript
|
|
264
|
+
{
|
|
265
|
+
decompressed_response: string; // Final decompressed LLM response
|
|
266
|
+
compression_stats: {
|
|
267
|
+
original_size_chars: number;
|
|
268
|
+
compressed_size_chars: number;
|
|
269
|
+
original_tokens: number;
|
|
270
|
+
compressed_tokens: number;
|
|
271
|
+
compression_ratio: number;
|
|
272
|
+
token_savings: number;
|
|
273
|
+
token_savings_percent: number;
|
|
274
|
+
processing_time_ms?: number;
|
|
275
|
+
};
|
|
276
|
+
llm_stats: {
|
|
277
|
+
prompt_tokens: number;
|
|
278
|
+
completion_tokens: number;
|
|
279
|
+
total_tokens: number;
|
|
280
|
+
};
|
|
281
|
+
routing_info?: {
|
|
282
|
+
selected_model: string; // Model chosen by system
|
|
283
|
+
selected_provider: string; // Provider chosen by system
|
|
284
|
+
selected_model_id: string;
|
|
285
|
+
model_hle: number; // HLE score of selected model
|
|
286
|
+
model_price_per_million: number;
|
|
287
|
+
requested_hle: number | null;
|
|
288
|
+
effective_hle: number | null; // Effective HLE after admin ceilings
|
|
289
|
+
hle_source: 'request' | 'tag' | 'global' | 'none';
|
|
290
|
+
hle_clamped: boolean; // true if admin ceiling lowered your desired_hle
|
|
291
|
+
};
|
|
292
|
+
warnings?: string[];
|
|
293
|
+
|
|
294
|
+
// Convenience aliases
|
|
295
|
+
text?: string; // Alias for decompressed_response
|
|
296
|
+
tokens_saved?: number; // Alias for compression_stats.token_savings
|
|
297
|
+
tokens_used?: number; // Alias for llm_stats.total_tokens
|
|
298
|
+
compression_ratio?: number; // Alias for compression_stats.compression_ratio
|
|
299
|
+
}
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
##### `compress(prompt, model?, algorithm?, tags?): Promise<CompressResponse>`
|
|
203
303
|
|
|
204
304
|
Compression-only (POST `/api/v1/compress`).
|
|
205
305
|
|
|
206
|
-
|
|
306
|
+
**Parameters:**
|
|
307
|
+
- `prompt` (string, required): Text to compress
|
|
308
|
+
- `model` (string, optional): Model for tokenization. Default: `'gpt-4'`
|
|
309
|
+
- `algorithm` (`'greedy' | 'optimal'`, optional): Compression algorithm. Default: `'greedy'`
|
|
310
|
+
- `tags` (`Record<string, string>`, optional): Tags for attribution
|
|
311
|
+
|
|
312
|
+
**Response (`CompressResponse`):**
|
|
313
|
+
|
|
314
|
+
```typescript
|
|
315
|
+
{
|
|
316
|
+
compressed: string;
|
|
317
|
+
dictionary: Record<string, string>;
|
|
318
|
+
llm_format: string;
|
|
319
|
+
compression_ratio: number;
|
|
320
|
+
original_size: number;
|
|
321
|
+
compressed_size: number;
|
|
322
|
+
processing_time_ms: number;
|
|
323
|
+
algorithm: string;
|
|
324
|
+
}
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
##### `decompress(llmFormat): Promise<DecompressResponse>`
|
|
207
328
|
|
|
208
329
|
Decompress an LLM-formatted compressed prompt (POST `/api/v1/decompress`).
|
|
209
330
|
|
|
210
|
-
|
|
331
|
+
**Parameters:**
|
|
332
|
+
- `llmFormat` (string, required): The `llm_format` string from a compress response
|
|
333
|
+
|
|
334
|
+
**Response (`DecompressResponse`):**
|
|
335
|
+
|
|
336
|
+
```typescript
|
|
337
|
+
{
|
|
338
|
+
decompressed: string;
|
|
339
|
+
processing_time_ms: number;
|
|
340
|
+
}
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
##### `healthCheck(): Promise<HealthCheckResponse>`
|
|
211
344
|
|
|
212
345
|
Check API health status (GET `/health`).
|
|
213
346
|
|
|
347
|
+
**Response:**
|
|
348
|
+
|
|
349
|
+
```typescript
|
|
350
|
+
{
|
|
351
|
+
status: string;
|
|
352
|
+
version?: string;
|
|
353
|
+
}
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
### Message Types
|
|
358
|
+
|
|
359
|
+
```typescript
|
|
360
|
+
type MessageRole = 'system' | 'developer' | 'user' | 'assistant';
|
|
361
|
+
|
|
362
|
+
interface Message {
|
|
363
|
+
role: MessageRole;
|
|
364
|
+
content: string;
|
|
365
|
+
}
|
|
366
|
+
```
|
|
367
|
+
|
|
214
368
|
### Environment Variables
|
|
215
369
|
|
|
216
|
-
|
|
217
|
-
|
|
370
|
+
| Variable | Description |
|
|
371
|
+
|----------|-------------|
|
|
372
|
+
| `PCOMPRESLR_API_KEY` | Your LightReach API key (primary) |
|
|
373
|
+
| `LIGHTREACH_API_KEY` | Your LightReach API key (alternative) |
|
|
374
|
+
| `PCOMPRESLR_API_URL` | Override the API base URL (advanced/testing) |
|
|
218
375
|
|
|
219
376
|
### Exceptions
|
|
220
377
|
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
378
|
+
| Exception | Description |
|
|
379
|
+
|-----------|-------------|
|
|
380
|
+
| `PcompresslrAPIError` | Base exception class |
|
|
381
|
+
| `APIKeyError` | Invalid or missing API key |
|
|
382
|
+
| `RateLimitError` | Rate limit exceeded |
|
|
383
|
+
| `APIRequestError` | General API errors (including routing failures) |
|
|
384
|
+
|
|
385
|
+
```typescript
|
|
386
|
+
import { APIKeyError, RateLimitError, APIRequestError } from 'compress-lightreach';
|
|
387
|
+
|
|
388
|
+
try {
|
|
389
|
+
const result = await client.complete({ messages: [...] });
|
|
390
|
+
} catch (error) {
|
|
391
|
+
if (error instanceof APIKeyError) {
|
|
392
|
+
console.error('Invalid API key');
|
|
393
|
+
} else if (error instanceof RateLimitError) {
|
|
394
|
+
console.error('Rate limited, please retry later');
|
|
395
|
+
} else if (error instanceof APIRequestError) {
|
|
396
|
+
console.error('API error:', error.message);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
```
|
|
225
400
|
|
|
226
401
|
## How It Works
|
|
227
402
|
|
|
@@ -237,7 +412,7 @@ The library:
|
|
|
237
412
|
|
|
238
413
|
## Examples
|
|
239
414
|
|
|
240
|
-
### Example 1:
|
|
415
|
+
### Example 1: Complete with Compression
|
|
241
416
|
|
|
242
417
|
```typescript
|
|
243
418
|
import { PcompresslrAPIClient } from 'compress-lightreach';
|
|
@@ -250,10 +425,9 @@ Write a story about a dog. The dog is very friendly.
|
|
|
250
425
|
Write a story about a bird. The bird is very friendly.
|
|
251
426
|
`;
|
|
252
427
|
|
|
253
|
-
// One call handles compression, LLM request, and decompression
|
|
254
428
|
const result = await client.complete({
|
|
255
429
|
messages: [{ role: "user", content: prompt }],
|
|
256
|
-
|
|
430
|
+
desired_hle: 30,
|
|
257
431
|
});
|
|
258
432
|
|
|
259
433
|
console.log(result.decompressed_response);
|
|
@@ -262,22 +436,46 @@ console.log(`Token savings: ${result.compression_stats.token_savings} tokens`);
|
|
|
262
436
|
console.log(`Compression ratio: ${(result.compression_stats.compression_ratio * 100).toFixed(2)}%`);
|
|
263
437
|
```
|
|
264
438
|
|
|
265
|
-
### Example 2:
|
|
439
|
+
### Example 2: Output Compression
|
|
266
440
|
|
|
267
441
|
```typescript
|
|
268
442
|
import { PcompresslrAPIClient } from 'compress-lightreach';
|
|
269
443
|
|
|
270
444
|
const client = new PcompresslrAPIClient("your-lightreach-api-key");
|
|
271
445
|
|
|
272
|
-
// Complete with output compression - response is automatically decompressed
|
|
273
446
|
const result = await client.complete({
|
|
274
447
|
messages: [{ role: "user", content: "Generate a long report with repeated sections..." }],
|
|
275
|
-
|
|
276
|
-
|
|
448
|
+
desired_hle: 35,
|
|
449
|
+
compress_output: true,
|
|
277
450
|
});
|
|
451
|
+
|
|
278
452
|
console.log(result.decompressed_response);
|
|
279
453
|
```
|
|
280
454
|
|
|
455
|
+
### Example 3: Multi-turn Conversation
|
|
456
|
+
|
|
457
|
+
```typescript
|
|
458
|
+
import { PcompresslrAPIClient } from 'compress-lightreach';
|
|
459
|
+
|
|
460
|
+
const client = new PcompresslrAPIClient("your-lightreach-api-key");
|
|
461
|
+
|
|
462
|
+
const result = await client.complete({
|
|
463
|
+
messages: [
|
|
464
|
+
{ role: "system", content: "You are a helpful coding assistant." },
|
|
465
|
+
{ role: "user", content: "How do I read a file in Python?" },
|
|
466
|
+
{ role: "assistant", content: "You can use open() with a context manager..." },
|
|
467
|
+
{ role: "user", content: "How about writing to a file?" },
|
|
468
|
+
],
|
|
469
|
+
desired_hle: 30,
|
|
470
|
+
compression_config: {
|
|
471
|
+
compress_system: false,
|
|
472
|
+
compress_user: true,
|
|
473
|
+
compress_assistant: false,
|
|
474
|
+
compress_only_last_n_user: 2, // Only compress last 2 user messages
|
|
475
|
+
},
|
|
476
|
+
});
|
|
477
|
+
```
|
|
478
|
+
|
|
281
479
|
## Getting an API Key
|
|
282
480
|
|
|
283
481
|
To use Compress Light Reach, you need an API key from [compress.lightreach.io](https://compress.lightreach.io).
|
|
@@ -289,7 +487,7 @@ To use Compress Light Reach, you need an API key from [compress.lightreach.io](h
|
|
|
289
487
|
|
|
290
488
|
## Security & Privacy
|
|
291
489
|
|
|
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.
|
|
490
|
+
**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
491
|
|
|
294
492
|
## Requirements
|
|
295
493
|
|
|
@@ -298,7 +496,7 @@ To use Compress Light Reach, you need an API key from [compress.lightreach.io](h
|
|
|
298
496
|
|
|
299
497
|
## License
|
|
300
498
|
|
|
301
|
-
MIT License - see [LICENSE](
|
|
499
|
+
MIT License - see [LICENSE](./LICENSE) file for details.
|
|
302
500
|
|
|
303
501
|
## Support
|
|
304
502
|
|
package/package.json
CHANGED