ak-gemini 1.0.3 → 1.0.5

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.
Files changed (4) hide show
  1. package/README.md +40 -20
  2. package/index.cjs +53 -41351
  3. package/index.js +45 -2
  4. package/package.json +3 -3
package/README.md CHANGED
@@ -7,12 +7,13 @@ Use this to power LLM-driven data pipelines, JSON mapping, or any automated AI t
7
7
 
8
8
  ## Features
9
9
 
10
- * **Model-Agnostic**: Configure for any Gemini model (`gemini-2.0-flash` by default)
11
- * **Declarative Examples**: Seed transformations using example mappings, with support for custom keys (`PROMPT`, `ANSWER`, `CONTEXT`, or your own)
12
- * **Automatic Validation & Repair**: Validate outputs with your own async function; auto-repair failed payloads with LLM feedback loop (exponential backoff, fully configurable)
13
- * **Strong TypeScript/JSDoc Typings**: All public APIs fully typed (see `/types`)
14
- * **Minimal API Surface**: Dead simple, no ceremony—init, seed, transform, validate.
15
- * **Robust Logging**: Pluggable logger for all steps, easy debugging
10
+ * **Model-Agnostic:** Use any Gemini model (`gemini-2.0-flash` by default)
11
+ * **Declarative Few-shot Examples:** Seed transformations using example mappings, with support for custom keys (`PROMPT`, `ANSWER`, `CONTEXT`, or your own)
12
+ * **Automatic Validation & Repair:** Validate outputs with your own async function; auto-repair failed payloads with LLM feedback loop (exponential backoff, fully configurable)
13
+ * **Token Counting & Safety:** Preview the *exact* Gemini token consumption for any operation—including all examples, instructions, and your input—before sending, so you can avoid window errors and manage costs.
14
+ * **Strong TypeScript/JSDoc Typings:** All public APIs fully typed (see `/types`)
15
+ * **Minimal API Surface:** Dead simple, no ceremony—init, seed, transform, validate.
16
+ * **Robust Logging:** Pluggable logger for all steps, easy debugging
16
17
 
17
18
  ---
18
19
 
@@ -43,7 +44,7 @@ or pass it directly in the constructor options.
43
44
  ### 2. **Basic Example**
44
45
 
45
46
  ```js
46
- import AITransformer from 'ai-transformer';
47
+ import AITransformer from 'ak-gemini';
47
48
 
48
49
  const transformer = new AITransformer({
49
50
  modelName: 'gemini-2.0-flash', // or your preferred Gemini model
@@ -72,7 +73,22 @@ console.log(result);
72
73
 
73
74
  ---
74
75
 
75
- ### 3. **Automatic Validation & Self-Healing**
76
+ ### 3. **Token Window Safety/Preview**
77
+
78
+ Before calling `.message()` or `.seed()`, you can preview the exact token usage that will be sent to Gemini—*including* your system instructions, examples, and user input. This is vital for avoiding window errors and managing context size:
79
+
80
+ ```js
81
+ const { totalTokens, breakdown } = await transformer.estimateTokenUsage({ name: "Bob" });
82
+ console.log(`Total tokens: ${totalTokens}`);
83
+ console.log(breakdown); // See per-section token counts
84
+
85
+ // Optional: abort or trim if over limit
86
+ if (totalTokens > 32000) throw new Error("Request too large for selected Gemini model");
87
+ ```
88
+
89
+ ---
90
+
91
+ ### 4. **Automatic Validation & Self-Healing**
76
92
 
77
93
  You can pass a custom async validator—if it fails, the transformer will attempt to self-correct using LLM feedback, retrying up to `maxRetries` times:
78
94
 
@@ -127,7 +143,12 @@ You can omit `examples` to use the `examplesFile` (if provided).
127
143
 
128
144
  #### `await transformer.message(sourcePayload)`
129
145
 
130
- Transforms input JSON to output JSON using the seeded examples and system instructions.
146
+ Transforms input JSON to output JSON using the seeded examples and system instructions. Throws if estimated token window would be exceeded.
147
+
148
+ #### `await transformer.estimateTokenUsage(sourcePayload)`
149
+
150
+ Returns `{ totalTokens, breakdown }` for the *full request* that would be sent to Gemini (system instructions + all examples + your sourcePayload as the new prompt).
151
+ Lets you preview token window safety and abort/trim as needed.
131
152
 
132
153
  #### `await transformer.transformWithValidation(sourcePayload, validatorFn, options?)`
133
154
 
@@ -187,10 +208,19 @@ const result = await transformer.transformWithValidation(
187
208
 
188
209
  ---
189
210
 
211
+ ## Token Window Management & Error Handling
212
+
213
+ * Throws on missing `GEMINI_API_KEY`
214
+ * `.message()` and `.seed()` will *estimate* and prevent calls that would exceed Gemini's model window
215
+ * All API and parsing errors surfaced as `Error` with context
216
+ * Validator and retry failures include the number of attempts and last error
217
+
218
+ ---
219
+
190
220
  ## Testing
191
221
 
192
222
  * **Jest test suite included**
193
- * Mocks Google Gemini, logger, ak-tools
223
+ * Real API integration tests as well as local unit tests
194
224
  * 100% coverage for all error cases, configuration options, edge cases
195
225
 
196
226
  Run tests with:
@@ -200,13 +230,3 @@ npm test
200
230
  ```
201
231
 
202
232
  ---
203
-
204
- ## Error Handling
205
-
206
- * Throws on missing `GEMINI_API_KEY`
207
- * All API and parsing errors surfaced as `Error` with context
208
- * Validator and retry failures include the number of attempts and last error
209
-
210
- ---
211
-
212
-