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.
- package/README.md +40 -20
- package/index.cjs +53 -41351
- package/index.js +45 -2
- package/package.json +3 -3
package/index.js
CHANGED
|
@@ -96,6 +96,7 @@ export default class AITransformer {
|
|
|
96
96
|
this.reset = resetChat.bind(this);
|
|
97
97
|
this.getHistory = getChatHistory.bind(this);
|
|
98
98
|
this.transformWithValidation = transformWithValidation.bind(this);
|
|
99
|
+
this.estimate = estimateTokenUsage.bind(this);
|
|
99
100
|
}
|
|
100
101
|
}
|
|
101
102
|
|
|
@@ -143,7 +144,8 @@ function AITransformFactory(options = {}) {
|
|
|
143
144
|
log.debug(`Creating AI Transformer with model: ${this.modelName}`);
|
|
144
145
|
log.debug(`Using keys - Source: "${this.promptKey}", Target: "${this.answerKey}", Context: "${this.contextKey}"`);
|
|
145
146
|
|
|
146
|
-
|
|
147
|
+
const ai = new GoogleGenAI({ apiKey: this.apiKey });
|
|
148
|
+
this.genAIClient = ai;
|
|
147
149
|
this.chat = null;
|
|
148
150
|
}
|
|
149
151
|
|
|
@@ -221,7 +223,7 @@ async function seedWithExamples(examples) {
|
|
|
221
223
|
}
|
|
222
224
|
}
|
|
223
225
|
|
|
224
|
-
const currentHistory = this
|
|
226
|
+
const currentHistory = this?.chat?.getHistory() || [];
|
|
225
227
|
|
|
226
228
|
this.chat = await this.genAIClient.chats.create({
|
|
227
229
|
model: this.modelName,
|
|
@@ -317,6 +319,47 @@ async function transformWithValidation(sourcePayload, validatorFn, options = {})
|
|
|
317
319
|
}
|
|
318
320
|
}
|
|
319
321
|
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Estimate total token usage if you were to send a new payload as the next message.
|
|
325
|
+
* Considers system instructions, current chat history (including examples), and the new message.
|
|
326
|
+
* @param {object|string} nextPayload - The next user message to be sent (object or string)
|
|
327
|
+
* @returns {Promise<{ totalTokens: number, ... }>} - The result of Gemini's countTokens API
|
|
328
|
+
*/
|
|
329
|
+
async function estimateTokenUsage(nextPayload) {
|
|
330
|
+
// Compose the conversation contents, Gemini-style
|
|
331
|
+
const contents = [];
|
|
332
|
+
|
|
333
|
+
// (1) System instructions (if applicable)
|
|
334
|
+
if (this.systemInstructions) {
|
|
335
|
+
// Add as a 'system' part; adjust role if Gemini supports
|
|
336
|
+
contents.push({ parts: [{ text: this.systemInstructions }] });
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
// (2) All current chat history (seeded examples + real user/model turns)
|
|
340
|
+
if (this.chat && typeof this.chat.getHistory === "function") {
|
|
341
|
+
const history = this.chat.getHistory();
|
|
342
|
+
if (Array.isArray(history) && history.length > 0) {
|
|
343
|
+
contents.push(...history);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// (3) The next user message
|
|
348
|
+
const nextMessage = typeof nextPayload === "string"
|
|
349
|
+
? nextPayload
|
|
350
|
+
: JSON.stringify(nextPayload, null, 2);
|
|
351
|
+
|
|
352
|
+
contents.push({ parts: [{ text: nextMessage }] });
|
|
353
|
+
|
|
354
|
+
// Call Gemini's token estimator
|
|
355
|
+
const resp = await this.genAIClient.models.countTokens({
|
|
356
|
+
model: this.modelName,
|
|
357
|
+
contents,
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
return resp; // includes totalTokens, possibly breakdown
|
|
361
|
+
}
|
|
362
|
+
|
|
320
363
|
/**
|
|
321
364
|
* Rebuilds a payload based on server error feedback
|
|
322
365
|
* @param {Object} lastPayload - The payload that failed validation
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "ak-gemini",
|
|
3
3
|
"author": "ak@mixpanel.com",
|
|
4
4
|
"description": "AK's Generative AI Helper for doing... transforms",
|
|
5
|
-
"version": "1.0.
|
|
5
|
+
"version": "1.0.5",
|
|
6
6
|
"main": "index.js",
|
|
7
7
|
"files": [
|
|
8
8
|
"index.js",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"update-deps": "npx npm-check-updates -u && npm install",
|
|
37
37
|
"prune": "rm -rf tmp/*",
|
|
38
38
|
"test": "node --no-warnings --experimental-vm-modules node_modules/jest/bin/jest.js",
|
|
39
|
-
"build:cjs": "esbuild index.js --bundle --platform=node --format=cjs --outfile=index.cjs"
|
|
39
|
+
"build:cjs": "esbuild index.js --bundle --platform=node --format=cjs --outfile=index.cjs --external:@google/genai --external:ak-tools --external:dotenv --external:pino-pretty --external:pino"
|
|
40
40
|
},
|
|
41
41
|
"type": "module",
|
|
42
42
|
"keywords": [
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"license": "ISC",
|
|
48
48
|
"dependencies": {
|
|
49
49
|
"@google-cloud/functions-framework": "^4.0.0",
|
|
50
|
-
"@google/genai": "^1.
|
|
50
|
+
"@google/genai": "^1.4.0",
|
|
51
51
|
"ak-tools": "^1.0.64",
|
|
52
52
|
"dotenv": "^16.5.0",
|
|
53
53
|
"pino": "^9.7.0",
|