ak-gemini 1.0.59 → 1.1.1

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 CHANGED
@@ -7,10 +7,12 @@ 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:** Use any Gemini model (`gemini-2.0-flash` by default)
10
+ * **Model-Agnostic:** Use any Gemini model (`gemini-2.5-flash` by default)
11
11
  * **Declarative Few-shot Examples:** Seed transformations using example mappings, with support for custom keys (`PROMPT`, `ANSWER`, `CONTEXT`, or your own)
12
12
  * **Automatic Validation & Repair:** Validate outputs with your own async function; auto-repair failed payloads with LLM feedback loop (exponential backoff, fully configurable)
13
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
+ * **Conversation Management:** Clear conversation history while preserving examples, or send stateless one-off messages that don't affect history
15
+ * **Response Metadata:** Access actual model version and token counts from API responses for billing verification and debugging
14
16
  * **Strong TypeScript/JSDoc Typings:** All public APIs fully typed (see `/types`)
15
17
  * **Minimal API Surface:** Dead simple, no ceremony—init, seed, transform, validate.
16
18
  * **Robust Logging:** Pluggable logger for all steps, easy debugging
@@ -47,7 +49,7 @@ or pass it directly in the constructor options.
47
49
  import AITransformer from 'ak-gemini';
48
50
 
49
51
  const transformer = new AITransformer({
50
- modelName: 'gemini-2.0-flash', // or your preferred Gemini model
52
+ modelName: 'gemini-2.5-flash', // or your preferred Gemini model
51
53
  sourceKey: 'INPUT', // Custom prompt key (default: 'PROMPT')
52
54
  targetKey: 'OUTPUT', // Custom answer key (default: 'ANSWER')
53
55
  contextKey: 'CONTEXT', // Optional, for per-example context
@@ -75,15 +77,19 @@ console.log(result);
75
77
 
76
78
  ### 3. **Token Window Safety/Preview**
77
79
 
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:
80
+ Before calling `.message()` or `.seed()`, you can preview the INPUT 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
81
 
80
82
  ```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
83
+ const { inputTokens } = await transformer.estimate({ name: "Bob" });
84
+ console.log(`Input tokens: ${inputTokens}`);
84
85
 
85
86
  // Optional: abort or trim if over limit
86
- if (totalTokens > 32000) throw new Error("Request too large for selected Gemini model");
87
+ if (inputTokens > 32000) throw new Error("Request too large for selected Gemini model");
88
+
89
+ // After the call, check actual usage (input + output)
90
+ await transformer.message({ name: "Bob" });
91
+ const usage = transformer.getLastUsage();
92
+ console.log(`Actual usage: ${usage.promptTokens} in, ${usage.responseTokens} out`);
87
93
  ```
88
94
 
89
95
  ---
@@ -106,6 +112,25 @@ console.log(validPayload);
106
112
 
107
113
  ---
108
114
 
115
+ ### 5. **Conversation Management**
116
+
117
+ Manage chat history to control costs and isolate requests:
118
+
119
+ ```js
120
+ // Clear conversation history while preserving seeded examples
121
+ await transformer.clearConversation();
122
+
123
+ // Send a stateless message that doesn't affect chat history
124
+ const result = await transformer.message({ query: "one-off question" }, { stateless: true });
125
+
126
+ // Check actual model and token usage from last API call
127
+ console.log(transformer.lastResponseMetadata);
128
+ // → { modelVersion: 'gemini-2.5-flash-001', requestedModel: 'gemini-2.5-flash',
129
+ // promptTokens: 150, responseTokens: 42, totalTokens: 192, timestamp: 1703... }
130
+ ```
131
+
132
+ ---
133
+
109
134
  ## API
110
135
 
111
136
  ### Constructor
@@ -116,7 +141,7 @@ new AITransformer(options)
116
141
 
117
142
  | Option | Type | Default | Description |
118
143
  | ------------------ | ------ | ------------------ | ------------------------------------------------- |
119
- | modelName | string | 'gemini-2.0-flash' | Gemini model to use |
144
+ | modelName | string | 'gemini-2.5-flash' | Gemini model to use |
120
145
  | sourceKey | string | 'PROMPT' | Key for prompt/example input |
121
146
  | targetKey | string | 'ANSWER' | Key for expected output in examples |
122
147
  | contextKey | string | 'CONTEXT' | Key for per-example context (optional) |
@@ -125,6 +150,7 @@ new AITransformer(options)
125
150
  | responseSchema | object | null | Optional JSON schema for strict output validation |
126
151
  | maxRetries | number | 3 | Retries for validation+rebuild loop |
127
152
  | retryDelay | number | 1000 | Initial retry delay in ms (exponential backoff) |
153
+ | logLevel | string | 'info' | Log level: 'trace', 'debug', 'info', 'warn', 'error', 'fatal', or 'none' |
128
154
  | chatConfig | object | ... | Gemini chat config overrides |
129
155
  | systemInstructions | string | ... | System prompt for Gemini |
130
156
 
@@ -141,14 +167,20 @@ Initializes Gemini chat session (idempotent).
141
167
  Seeds the model with example transformations (uses keys from constructor).
142
168
  You can omit `examples` to use the `examplesFile` (if provided).
143
169
 
144
- #### `await transformer.message(sourcePayload)`
170
+ #### `await transformer.message(sourcePayload, options?)`
145
171
 
146
172
  Transforms input JSON to output JSON using the seeded examples and system instructions. Throws if estimated token window would be exceeded.
147
173
 
148
- #### `await transformer.estimateTokenUsage(sourcePayload)`
174
+ **Options:**
175
+ - `stateless: true` — Send a one-off message without affecting chat history (uses `generateContent` instead of chat)
176
+ - `labels: {}` — Per-message billing labels
177
+
178
+ #### `await transformer.estimate(sourcePayload)`
149
179
 
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.
180
+ Returns `{ inputTokens }` the estimated INPUT tokens for the request (system instructions + all examples + your sourcePayload).
181
+ Use this to preview token window safety and manage costs before sending.
182
+
183
+ **Note:** This only estimates input tokens. Output tokens cannot be predicted before the API call. Use `getLastUsage()` after `message()` to see actual consumption.
152
184
 
153
185
  #### `await transformer.transformWithValidation(sourcePayload, validatorFn, options?)`
154
186
 
@@ -167,6 +199,48 @@ Resets the Gemini chat session, clearing all history/examples.
167
199
 
168
200
  Returns the current chat history (for debugging).
169
201
 
202
+ #### `await transformer.clearConversation()`
203
+
204
+ Clears conversation history while preserving seeded examples. Useful for starting fresh user sessions without re-seeding.
205
+
206
+ #### `transformer.getLastUsage()`
207
+
208
+ Returns structured usage data for billing verification. Token counts are **cumulative across all retry attempts** - if validation failed and a retry was needed, you see the total tokens consumed, not just the final successful call. Returns `null` if no API call has been made yet.
209
+
210
+ ```js
211
+ const usage = transformer.getLastUsage();
212
+ // {
213
+ // promptTokens: 300, // CUMULATIVE input tokens across all attempts
214
+ // responseTokens: 84, // CUMULATIVE output tokens across all attempts
215
+ // totalTokens: 384, // CUMULATIVE total tokens
216
+ // attempts: 2, // Number of attempts (1 = first try success, 2+ = retries needed)
217
+ // modelVersion: 'gemini-2.5-flash-001', // Actual model that responded
218
+ // requestedModel: 'gemini-2.5-flash', // Model you requested
219
+ // timestamp: 1703... // When response was received
220
+ // }
221
+ ```
222
+
223
+ ---
224
+
225
+ ### Properties
226
+
227
+ #### `transformer.lastResponseMetadata`
228
+
229
+ After each API call, contains metadata from the response:
230
+
231
+ ```js
232
+ {
233
+ modelVersion: string | null, // Actual model version that responded (e.g., 'gemini-2.5-flash-001')
234
+ requestedModel: string, // Model you requested (e.g., 'gemini-2.5-flash')
235
+ promptTokens: number, // Tokens in the prompt
236
+ responseTokens: number, // Tokens in the response
237
+ totalTokens: number, // Total tokens used
238
+ timestamp: number // When response was received
239
+ }
240
+ ```
241
+
242
+ Useful for verifying billing, debugging model behavior, and tracking token usage.
243
+
170
244
  ---
171
245
 
172
246
  ## Examples