ak-gemini 2.0.2 → 2.0.4
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/GUIDE.md +328 -8
- package/README.md +75 -6
- package/base.js +132 -6
- package/embedding.js +181 -0
- package/index.cjs +252 -19
- package/index.js +3 -1
- package/package.json +2 -1
- package/transformer.js +0 -18
- package/types.d.ts +117 -2
package/index.cjs
CHANGED
|
@@ -32,6 +32,7 @@ __export(index_exports, {
|
|
|
32
32
|
BaseGemini: () => base_default,
|
|
33
33
|
Chat: () => chat_default,
|
|
34
34
|
CodeAgent: () => code_agent_default,
|
|
35
|
+
Embedding: () => Embedding,
|
|
35
36
|
HarmBlockThreshold: () => import_genai2.HarmBlockThreshold,
|
|
36
37
|
HarmCategory: () => import_genai2.HarmCategory,
|
|
37
38
|
Message: () => message_default,
|
|
@@ -310,7 +311,7 @@ function extractJSON(text) {
|
|
|
310
311
|
}
|
|
311
312
|
|
|
312
313
|
// base.js
|
|
313
|
-
import_dotenv.default.config();
|
|
314
|
+
import_dotenv.default.config({ quiet: true });
|
|
314
315
|
var { NODE_ENV = "unknown", LOG_LEVEL = "" } = process.env;
|
|
315
316
|
var DEFAULT_SAFETY_SETTINGS = [
|
|
316
317
|
{ category: import_genai.HarmCategory.HARM_CATEGORY_HARASSMENT, threshold: import_genai.HarmBlockThreshold.BLOCK_NONE },
|
|
@@ -335,7 +336,8 @@ var MODEL_PRICING = {
|
|
|
335
336
|
"gemini-3-pro": { input: 2, output: 12 },
|
|
336
337
|
"gemini-3-pro-preview": { input: 2, output: 12 },
|
|
337
338
|
"gemini-2.0-flash": { input: 0.1, output: 0.4 },
|
|
338
|
-
"gemini-2.0-flash-lite": { input: 0.02, output: 0.1 }
|
|
339
|
+
"gemini-2.0-flash-lite": { input: 0.02, output: 0.1 },
|
|
340
|
+
"gemini-embedding-001": { input: 6e-3, output: 0 }
|
|
339
341
|
};
|
|
340
342
|
var BaseGemini = class {
|
|
341
343
|
/**
|
|
@@ -361,6 +363,9 @@ var BaseGemini = class {
|
|
|
361
363
|
}
|
|
362
364
|
this._configureLogLevel(options.logLevel);
|
|
363
365
|
this.labels = options.labels || {};
|
|
366
|
+
this.enableGrounding = options.enableGrounding || false;
|
|
367
|
+
this.groundingConfig = options.groundingConfig || {};
|
|
368
|
+
this.cachedContent = options.cachedContent || null;
|
|
364
369
|
this.chatConfig = {
|
|
365
370
|
temperature: 0.7,
|
|
366
371
|
topP: 0.95,
|
|
@@ -433,14 +438,21 @@ var BaseGemini = class {
|
|
|
433
438
|
* @protected
|
|
434
439
|
*/
|
|
435
440
|
_getChatCreateOptions() {
|
|
436
|
-
|
|
441
|
+
const opts = {
|
|
437
442
|
model: this.modelName,
|
|
438
443
|
config: {
|
|
439
444
|
...this.chatConfig,
|
|
440
|
-
...this.vertexai && Object.keys(this.labels).length > 0 && { labels: this.labels }
|
|
445
|
+
...this.vertexai && Object.keys(this.labels).length > 0 && { labels: this.labels },
|
|
446
|
+
...this.cachedContent && { cachedContent: this.cachedContent }
|
|
441
447
|
},
|
|
442
448
|
history: []
|
|
443
449
|
};
|
|
450
|
+
if (this.enableGrounding) {
|
|
451
|
+
const existingTools = opts.config.tools || [];
|
|
452
|
+
opts.config.tools = [...existingTools, { googleSearch: this.groundingConfig }];
|
|
453
|
+
logger_default.debug("Search grounding ENABLED (WARNING: costs $35/1k queries)");
|
|
454
|
+
}
|
|
455
|
+
return opts;
|
|
444
456
|
}
|
|
445
457
|
// ── Chat Session Management ──────────────────────────────────────────────
|
|
446
458
|
/**
|
|
@@ -562,7 +574,8 @@ ${contextText}
|
|
|
562
574
|
promptTokens: response.usageMetadata?.promptTokenCount || 0,
|
|
563
575
|
responseTokens: response.usageMetadata?.candidatesTokenCount || 0,
|
|
564
576
|
totalTokens: response.usageMetadata?.totalTokenCount || 0,
|
|
565
|
-
timestamp: Date.now()
|
|
577
|
+
timestamp: Date.now(),
|
|
578
|
+
groundingMetadata: response.candidates?.[0]?.groundingMetadata || null
|
|
566
579
|
};
|
|
567
580
|
}
|
|
568
581
|
/**
|
|
@@ -582,7 +595,8 @@ ${contextText}
|
|
|
582
595
|
attempts: useCumulative ? cumulative.attempts : 1,
|
|
583
596
|
modelVersion: meta.modelVersion,
|
|
584
597
|
requestedModel: meta.requestedModel,
|
|
585
|
-
timestamp: meta.timestamp
|
|
598
|
+
timestamp: meta.timestamp,
|
|
599
|
+
groundingMetadata: meta.groundingMetadata || null
|
|
586
600
|
};
|
|
587
601
|
}
|
|
588
602
|
// ── Token Estimation ─────────────────────────────────────────────────────
|
|
@@ -627,6 +641,99 @@ ${contextText}
|
|
|
627
641
|
note: "Cost is for input tokens only; output cost depends on response length"
|
|
628
642
|
};
|
|
629
643
|
}
|
|
644
|
+
// ── Context Caching ─────────────────────────────────────────────────────
|
|
645
|
+
/**
|
|
646
|
+
* Creates a cached content resource for cost reduction on repeated prompts.
|
|
647
|
+
* Auto-populates model and systemInstruction from this instance if not provided.
|
|
648
|
+
* @param {Object} [config={}] - Cache configuration
|
|
649
|
+
* @param {string} [config.model] - Model (defaults to this.modelName)
|
|
650
|
+
* @param {string} [config.ttl] - Time-to-live (e.g., '3600s')
|
|
651
|
+
* @param {string} [config.displayName] - Human-readable name
|
|
652
|
+
* @param {Array} [config.contents] - Content to cache
|
|
653
|
+
* @param {string} [config.systemInstruction] - System prompt to cache (defaults to this.systemPrompt)
|
|
654
|
+
* @param {Array} [config.tools] - Tools to cache
|
|
655
|
+
* @param {Object} [config.toolConfig] - Tool configuration to cache
|
|
656
|
+
* @returns {Promise<Object>} The created cache resource
|
|
657
|
+
*/
|
|
658
|
+
async createCache(config = {}) {
|
|
659
|
+
const cacheConfig = {};
|
|
660
|
+
if (config.ttl) cacheConfig.ttl = config.ttl;
|
|
661
|
+
if (config.displayName) cacheConfig.displayName = config.displayName;
|
|
662
|
+
if (config.contents) cacheConfig.contents = config.contents;
|
|
663
|
+
if (config.tools) cacheConfig.tools = config.tools;
|
|
664
|
+
if (config.toolConfig) cacheConfig.toolConfig = config.toolConfig;
|
|
665
|
+
const sysInstruction = config.systemInstruction !== void 0 ? config.systemInstruction : this.systemPrompt;
|
|
666
|
+
if (sysInstruction) cacheConfig.systemInstruction = sysInstruction;
|
|
667
|
+
const cached = await this.genAIClient.caches.create({
|
|
668
|
+
model: config.model || this.modelName,
|
|
669
|
+
config: cacheConfig
|
|
670
|
+
});
|
|
671
|
+
logger_default.debug(`Cache created: ${cached.name}`);
|
|
672
|
+
return cached;
|
|
673
|
+
}
|
|
674
|
+
/**
|
|
675
|
+
* Retrieves a cached content resource by name.
|
|
676
|
+
* @param {string} cacheName - Server-generated resource name
|
|
677
|
+
* @returns {Promise<Object>} The cached content resource
|
|
678
|
+
*/
|
|
679
|
+
async getCache(cacheName) {
|
|
680
|
+
return await this.genAIClient.caches.get({ name: cacheName });
|
|
681
|
+
}
|
|
682
|
+
/**
|
|
683
|
+
* Lists all cached content resources.
|
|
684
|
+
* @returns {Promise<Object>} Pager of cached content resources
|
|
685
|
+
*/
|
|
686
|
+
async listCaches() {
|
|
687
|
+
const pager = await this.genAIClient.caches.list();
|
|
688
|
+
const results = [];
|
|
689
|
+
for await (const cache of pager) {
|
|
690
|
+
results.push(cache);
|
|
691
|
+
}
|
|
692
|
+
return results;
|
|
693
|
+
}
|
|
694
|
+
/**
|
|
695
|
+
* Updates a cached content resource (TTL or expiration).
|
|
696
|
+
* @param {string} cacheName - Server-generated resource name
|
|
697
|
+
* @param {Object} [config={}] - Update config
|
|
698
|
+
* @param {string} [config.ttl] - New TTL (e.g., '7200s')
|
|
699
|
+
* @param {string} [config.expireTime] - New expiration (RFC 3339)
|
|
700
|
+
* @returns {Promise<Object>} The updated cache resource
|
|
701
|
+
*/
|
|
702
|
+
async updateCache(cacheName, config = {}) {
|
|
703
|
+
return await this.genAIClient.caches.update({
|
|
704
|
+
name: cacheName,
|
|
705
|
+
config: {
|
|
706
|
+
...config.ttl && { ttl: config.ttl },
|
|
707
|
+
...config.expireTime && { expireTime: config.expireTime }
|
|
708
|
+
}
|
|
709
|
+
});
|
|
710
|
+
}
|
|
711
|
+
/**
|
|
712
|
+
* Deletes a cached content resource.
|
|
713
|
+
* Clears this.cachedContent if it matches the deleted cache.
|
|
714
|
+
* @param {string} cacheName - Server-generated resource name
|
|
715
|
+
* @returns {Promise<void>}
|
|
716
|
+
*/
|
|
717
|
+
async deleteCache(cacheName) {
|
|
718
|
+
await this.genAIClient.caches.delete({ name: cacheName });
|
|
719
|
+
logger_default.debug(`Cache deleted: ${cacheName}`);
|
|
720
|
+
if (this.cachedContent === cacheName) {
|
|
721
|
+
this.cachedContent = null;
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
/**
|
|
725
|
+
* Sets the cached content for this instance and reinitializes the session.
|
|
726
|
+
* @param {string} cacheName - Server-generated cache resource name
|
|
727
|
+
* @returns {Promise<void>}
|
|
728
|
+
*/
|
|
729
|
+
async useCache(cacheName) {
|
|
730
|
+
this.cachedContent = cacheName;
|
|
731
|
+
delete this.chatConfig.systemInstruction;
|
|
732
|
+
if (this.chatSession) {
|
|
733
|
+
await this.init(true);
|
|
734
|
+
}
|
|
735
|
+
logger_default.debug(`Using cache: ${cacheName}`);
|
|
736
|
+
}
|
|
630
737
|
// ── Private Helpers ──────────────────────────────────────────────────────
|
|
631
738
|
/**
|
|
632
739
|
* Configures the log level based on options, env vars, or NODE_ENV.
|
|
@@ -722,20 +829,8 @@ var Transformer = class extends base_default {
|
|
|
722
829
|
this.asyncValidator = options.asyncValidator || null;
|
|
723
830
|
this.maxRetries = options.maxRetries || 3;
|
|
724
831
|
this.retryDelay = options.retryDelay || 1e3;
|
|
725
|
-
this.enableGrounding = options.enableGrounding || false;
|
|
726
|
-
this.groundingConfig = options.groundingConfig || {};
|
|
727
832
|
logger_default.debug(`Transformer keys \u2014 Source: "${this.promptKey}", Target: "${this.answerKey}", Context: "${this.contextKey}"`);
|
|
728
833
|
}
|
|
729
|
-
// ── Chat Create Options Override ──────────────────────────────────────────
|
|
730
|
-
/** @protected */
|
|
731
|
-
_getChatCreateOptions() {
|
|
732
|
-
const opts = super._getChatCreateOptions();
|
|
733
|
-
if (this.enableGrounding) {
|
|
734
|
-
opts.config.tools = [{ googleSearch: this.groundingConfig }];
|
|
735
|
-
logger_default.debug(`Search grounding ENABLED (WARNING: costs $35/1k queries)`);
|
|
736
|
-
}
|
|
737
|
-
return opts;
|
|
738
|
-
}
|
|
739
834
|
// ── Seeding ──────────────────────────────────────────────────────────────
|
|
740
835
|
/**
|
|
741
836
|
* Seeds the chat with transformation examples using the configured key mapping.
|
|
@@ -2221,14 +2316,152 @@ ${serialized}` });
|
|
|
2221
2316
|
};
|
|
2222
2317
|
var rag_agent_default = RagAgent;
|
|
2223
2318
|
|
|
2319
|
+
// embedding.js
|
|
2320
|
+
var Embedding = class extends base_default {
|
|
2321
|
+
/**
|
|
2322
|
+
* @param {import('./types.d.ts').EmbeddingOptions} [options={}]
|
|
2323
|
+
*/
|
|
2324
|
+
constructor(options = {}) {
|
|
2325
|
+
if (options.modelName === void 0) {
|
|
2326
|
+
options = { ...options, modelName: "gemini-embedding-001" };
|
|
2327
|
+
}
|
|
2328
|
+
if (options.systemPrompt === void 0) {
|
|
2329
|
+
options = { ...options, systemPrompt: null };
|
|
2330
|
+
}
|
|
2331
|
+
super(options);
|
|
2332
|
+
this.taskType = options.taskType || null;
|
|
2333
|
+
this.title = options.title || null;
|
|
2334
|
+
this.outputDimensionality = options.outputDimensionality || null;
|
|
2335
|
+
this.autoTruncate = options.autoTruncate ?? true;
|
|
2336
|
+
logger_default.debug(`Embedding created with model: ${this.modelName}`);
|
|
2337
|
+
}
|
|
2338
|
+
/**
|
|
2339
|
+
* Initialize the Embedding client.
|
|
2340
|
+
* Override: validates API connection only, NO chat session (stateless).
|
|
2341
|
+
* @param {boolean} [force=false]
|
|
2342
|
+
* @returns {Promise<void>}
|
|
2343
|
+
*/
|
|
2344
|
+
async init(force = false) {
|
|
2345
|
+
if (this._initialized && !force) return;
|
|
2346
|
+
logger_default.debug(`Initializing ${this.constructor.name} with model: ${this.modelName}...`);
|
|
2347
|
+
try {
|
|
2348
|
+
await this.genAIClient.models.list();
|
|
2349
|
+
logger_default.debug(`${this.constructor.name}: API connection successful.`);
|
|
2350
|
+
} catch (e) {
|
|
2351
|
+
throw new Error(`${this.constructor.name} initialization failed: ${e.message}`);
|
|
2352
|
+
}
|
|
2353
|
+
this._initialized = true;
|
|
2354
|
+
logger_default.debug(`${this.constructor.name}: Initialized (stateless mode).`);
|
|
2355
|
+
}
|
|
2356
|
+
/**
|
|
2357
|
+
* Builds the config object for embedContent calls.
|
|
2358
|
+
* @param {Object} [overrides={}] - Per-call config overrides
|
|
2359
|
+
* @returns {Object} The config object
|
|
2360
|
+
* @private
|
|
2361
|
+
*/
|
|
2362
|
+
_buildConfig(overrides = {}) {
|
|
2363
|
+
const config = {};
|
|
2364
|
+
const taskType = overrides.taskType || this.taskType;
|
|
2365
|
+
const title = overrides.title || this.title;
|
|
2366
|
+
const dims = overrides.outputDimensionality || this.outputDimensionality;
|
|
2367
|
+
if (taskType) config.taskType = taskType;
|
|
2368
|
+
if (title) config.title = title;
|
|
2369
|
+
if (dims) config.outputDimensionality = dims;
|
|
2370
|
+
return config;
|
|
2371
|
+
}
|
|
2372
|
+
/**
|
|
2373
|
+
* Embed a single text string.
|
|
2374
|
+
* @param {string} text - The text to embed
|
|
2375
|
+
* @param {Object} [config={}] - Per-call config overrides
|
|
2376
|
+
* @param {string} [config.taskType] - Override task type
|
|
2377
|
+
* @param {string} [config.title] - Override title
|
|
2378
|
+
* @param {number} [config.outputDimensionality] - Override dimensions
|
|
2379
|
+
|
|
2380
|
+
* @returns {Promise<import('./types.d.ts').EmbeddingResult>} The embedding result
|
|
2381
|
+
*/
|
|
2382
|
+
async embed(text, config = {}) {
|
|
2383
|
+
if (!this._initialized) await this.init();
|
|
2384
|
+
const result = await this.genAIClient.models.embedContent({
|
|
2385
|
+
model: this.modelName,
|
|
2386
|
+
contents: text,
|
|
2387
|
+
config: this._buildConfig(config)
|
|
2388
|
+
});
|
|
2389
|
+
return result.embeddings[0];
|
|
2390
|
+
}
|
|
2391
|
+
/**
|
|
2392
|
+
* Embed multiple text strings in a single API call.
|
|
2393
|
+
* @param {string[]} texts - Array of texts to embed
|
|
2394
|
+
* @param {Object} [config={}] - Per-call config overrides
|
|
2395
|
+
* @param {string} [config.taskType] - Override task type
|
|
2396
|
+
* @param {string} [config.title] - Override title
|
|
2397
|
+
* @param {number} [config.outputDimensionality] - Override dimensions
|
|
2398
|
+
|
|
2399
|
+
* @returns {Promise<import('./types.d.ts').EmbeddingResult[]>} Array of embedding results
|
|
2400
|
+
*/
|
|
2401
|
+
async embedBatch(texts, config = {}) {
|
|
2402
|
+
if (!this._initialized) await this.init();
|
|
2403
|
+
const result = await this.genAIClient.models.embedContent({
|
|
2404
|
+
model: this.modelName,
|
|
2405
|
+
contents: texts,
|
|
2406
|
+
config: this._buildConfig(config)
|
|
2407
|
+
});
|
|
2408
|
+
return result.embeddings;
|
|
2409
|
+
}
|
|
2410
|
+
/**
|
|
2411
|
+
* Compute cosine similarity between two embedding vectors.
|
|
2412
|
+
* Pure math — no API call.
|
|
2413
|
+
* @param {number[]} a - First embedding vector
|
|
2414
|
+
* @param {number[]} b - Second embedding vector
|
|
2415
|
+
* @returns {number} Cosine similarity between -1 and 1
|
|
2416
|
+
*/
|
|
2417
|
+
similarity(a, b) {
|
|
2418
|
+
if (!a || !b || a.length !== b.length) {
|
|
2419
|
+
throw new Error("Vectors must be non-null and have the same length");
|
|
2420
|
+
}
|
|
2421
|
+
let dot = 0;
|
|
2422
|
+
let magA = 0;
|
|
2423
|
+
let magB = 0;
|
|
2424
|
+
for (let i = 0; i < a.length; i++) {
|
|
2425
|
+
dot += a[i] * b[i];
|
|
2426
|
+
magA += a[i] * a[i];
|
|
2427
|
+
magB += b[i] * b[i];
|
|
2428
|
+
}
|
|
2429
|
+
const magnitude = Math.sqrt(magA) * Math.sqrt(magB);
|
|
2430
|
+
if (magnitude === 0) return 0;
|
|
2431
|
+
return dot / magnitude;
|
|
2432
|
+
}
|
|
2433
|
+
// ── No-ops (embeddings don't use chat sessions) ──
|
|
2434
|
+
/** @returns {any[]} Always returns empty array */
|
|
2435
|
+
getHistory() {
|
|
2436
|
+
return [];
|
|
2437
|
+
}
|
|
2438
|
+
/** No-op for Embedding */
|
|
2439
|
+
async clearHistory() {
|
|
2440
|
+
}
|
|
2441
|
+
/** No-op for Embedding */
|
|
2442
|
+
async seed() {
|
|
2443
|
+
logger_default.warn("Embedding.seed() is a no-op \u2014 embeddings do not support few-shot examples.");
|
|
2444
|
+
return [];
|
|
2445
|
+
}
|
|
2446
|
+
/**
|
|
2447
|
+
* @param {any} _nextPayload
|
|
2448
|
+
* @throws {Error} Embedding does not support token estimation
|
|
2449
|
+
* @returns {Promise<{inputTokens: number}>}
|
|
2450
|
+
*/
|
|
2451
|
+
async estimate(_nextPayload) {
|
|
2452
|
+
throw new Error("Embedding does not support token estimation. Use embed() directly.");
|
|
2453
|
+
}
|
|
2454
|
+
};
|
|
2455
|
+
|
|
2224
2456
|
// index.js
|
|
2225
2457
|
var import_genai2 = require("@google/genai");
|
|
2226
|
-
var index_default = { Transformer: transformer_default, Chat: chat_default, Message: message_default, ToolAgent: tool_agent_default, CodeAgent: code_agent_default, RagAgent: rag_agent_default };
|
|
2458
|
+
var index_default = { Transformer: transformer_default, Chat: chat_default, Message: message_default, ToolAgent: tool_agent_default, CodeAgent: code_agent_default, RagAgent: rag_agent_default, Embedding };
|
|
2227
2459
|
// Annotate the CommonJS export names for ESM import in node:
|
|
2228
2460
|
0 && (module.exports = {
|
|
2229
2461
|
BaseGemini,
|
|
2230
2462
|
Chat,
|
|
2231
2463
|
CodeAgent,
|
|
2464
|
+
Embedding,
|
|
2232
2465
|
HarmBlockThreshold,
|
|
2233
2466
|
HarmCategory,
|
|
2234
2467
|
Message,
|
package/index.js
CHANGED
|
@@ -26,6 +26,7 @@ export { default as Message } from './message.js';
|
|
|
26
26
|
export { default as ToolAgent } from './tool-agent.js';
|
|
27
27
|
export { default as CodeAgent } from './code-agent.js';
|
|
28
28
|
export { default as RagAgent } from './rag-agent.js';
|
|
29
|
+
export { default as Embedding } from './embedding.js';
|
|
29
30
|
export { default as BaseGemini } from './base.js';
|
|
30
31
|
export { default as log } from './logger.js';
|
|
31
32
|
export { ThinkingLevel, HarmCategory, HarmBlockThreshold } from '@google/genai';
|
|
@@ -39,5 +40,6 @@ import Message from './message.js';
|
|
|
39
40
|
import ToolAgent from './tool-agent.js';
|
|
40
41
|
import CodeAgent from './code-agent.js';
|
|
41
42
|
import RagAgent from './rag-agent.js';
|
|
43
|
+
import Embedding from './embedding.js';
|
|
42
44
|
|
|
43
|
-
export default { Transformer, Chat, Message, ToolAgent, CodeAgent, RagAgent };
|
|
45
|
+
export default { Transformer, Chat, Message, ToolAgent, CodeAgent, RagAgent, Embedding };
|
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... everything",
|
|
5
|
-
"version": "2.0.
|
|
5
|
+
"version": "2.0.4",
|
|
6
6
|
"main": "index.js",
|
|
7
7
|
"files": [
|
|
8
8
|
"index.js",
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
"tool-agent.js",
|
|
15
15
|
"code-agent.js",
|
|
16
16
|
"rag-agent.js",
|
|
17
|
+
"embedding.js",
|
|
17
18
|
"json-helpers.js",
|
|
18
19
|
"types.d.ts",
|
|
19
20
|
"logger.js",
|
package/transformer.js
CHANGED
|
@@ -96,27 +96,9 @@ class Transformer extends BaseGemini {
|
|
|
96
96
|
this.maxRetries = options.maxRetries || 3;
|
|
97
97
|
this.retryDelay = options.retryDelay || 1000;
|
|
98
98
|
|
|
99
|
-
// ── Grounding ──
|
|
100
|
-
this.enableGrounding = options.enableGrounding || false;
|
|
101
|
-
this.groundingConfig = options.groundingConfig || {};
|
|
102
|
-
|
|
103
99
|
log.debug(`Transformer keys — Source: "${this.promptKey}", Target: "${this.answerKey}", Context: "${this.contextKey}"`);
|
|
104
100
|
}
|
|
105
101
|
|
|
106
|
-
// ── Chat Create Options Override ──────────────────────────────────────────
|
|
107
|
-
|
|
108
|
-
/** @protected */
|
|
109
|
-
_getChatCreateOptions() {
|
|
110
|
-
const opts = super._getChatCreateOptions();
|
|
111
|
-
|
|
112
|
-
if (this.enableGrounding) {
|
|
113
|
-
opts.config.tools = [{ googleSearch: this.groundingConfig }];
|
|
114
|
-
log.debug(`Search grounding ENABLED (WARNING: costs $35/1k queries)`);
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
return opts;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
102
|
// ── Seeding ──────────────────────────────────────────────────────────────
|
|
121
103
|
|
|
122
104
|
/**
|
package/types.d.ts
CHANGED
|
@@ -32,6 +32,23 @@ export interface ChatConfig {
|
|
|
32
32
|
[key: string]: any;
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
+
export interface GroundingChunk {
|
|
36
|
+
web?: { uri?: string; title?: string; domain?: string };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface GroundingSupport {
|
|
40
|
+
segment?: any;
|
|
41
|
+
groundingChunkIndices?: number[];
|
|
42
|
+
confidenceScores?: number[];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface GroundingMetadata {
|
|
46
|
+
groundingChunks?: GroundingChunk[];
|
|
47
|
+
groundingSupports?: GroundingSupport[];
|
|
48
|
+
webSearchQueries?: string[];
|
|
49
|
+
searchEntryPoint?: { renderedContent?: string };
|
|
50
|
+
}
|
|
51
|
+
|
|
35
52
|
export interface ResponseMetadata {
|
|
36
53
|
modelVersion: string | null;
|
|
37
54
|
requestedModel: string;
|
|
@@ -39,6 +56,7 @@ export interface ResponseMetadata {
|
|
|
39
56
|
responseTokens: number;
|
|
40
57
|
totalTokens: number;
|
|
41
58
|
timestamp: number;
|
|
59
|
+
groundingMetadata?: GroundingMetadata | null;
|
|
42
60
|
}
|
|
43
61
|
|
|
44
62
|
export interface UsageData {
|
|
@@ -55,6 +73,7 @@ export interface UsageData {
|
|
|
55
73
|
/** Model you requested (e.g., 'gemini-2.5-flash') */
|
|
56
74
|
requestedModel: string;
|
|
57
75
|
timestamp: number;
|
|
76
|
+
groundingMetadata?: GroundingMetadata | null;
|
|
58
77
|
}
|
|
59
78
|
|
|
60
79
|
export interface TransformationExample {
|
|
@@ -77,6 +96,38 @@ export interface GoogleAuthOptions {
|
|
|
77
96
|
universeDomain?: string;
|
|
78
97
|
}
|
|
79
98
|
|
|
99
|
+
export interface CacheConfig {
|
|
100
|
+
/** Model to cache for (defaults to instance modelName) */
|
|
101
|
+
model?: string;
|
|
102
|
+
/** Time-to-live duration (e.g., '3600s') */
|
|
103
|
+
ttl?: string;
|
|
104
|
+
/** Human-readable display name */
|
|
105
|
+
displayName?: string;
|
|
106
|
+
/** Content to cache */
|
|
107
|
+
contents?: any[];
|
|
108
|
+
/** System instruction to cache (defaults to instance systemPrompt) */
|
|
109
|
+
systemInstruction?: string;
|
|
110
|
+
/** Tools to cache */
|
|
111
|
+
tools?: any[];
|
|
112
|
+
/** Tool configuration to cache */
|
|
113
|
+
toolConfig?: any;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export interface CachedContentInfo {
|
|
117
|
+
/** Server-generated resource name */
|
|
118
|
+
name: string;
|
|
119
|
+
/** User-provided display name */
|
|
120
|
+
displayName?: string;
|
|
121
|
+
/** Model this cache is for */
|
|
122
|
+
model: string;
|
|
123
|
+
/** Creation timestamp */
|
|
124
|
+
createTime: string;
|
|
125
|
+
/** Expiration timestamp */
|
|
126
|
+
expireTime: string;
|
|
127
|
+
/** Cache usage metadata */
|
|
128
|
+
usageMetadata?: { totalTokenCount?: number };
|
|
129
|
+
}
|
|
130
|
+
|
|
80
131
|
export type AsyncValidatorFunction = (payload: Record<string, unknown>) => Promise<unknown>;
|
|
81
132
|
export type LogLevel = 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal' | 'none';
|
|
82
133
|
|
|
@@ -110,6 +161,14 @@ export interface BaseGeminiOptions {
|
|
|
110
161
|
|
|
111
162
|
/** Billing labels for cost segmentation (Vertex AI only) */
|
|
112
163
|
labels?: Record<string, string>;
|
|
164
|
+
|
|
165
|
+
/** Enable Google Search grounding (WARNING: costs $35/1k queries) */
|
|
166
|
+
enableGrounding?: boolean;
|
|
167
|
+
/** Google Search grounding configuration (searchTypes, excludeDomains, timeRangeFilter) */
|
|
168
|
+
groundingConfig?: Record<string, any>;
|
|
169
|
+
|
|
170
|
+
/** Cached content resource name to use for this session */
|
|
171
|
+
cachedContent?: string;
|
|
113
172
|
}
|
|
114
173
|
|
|
115
174
|
export interface TransformerOptions extends BaseGeminiOptions {
|
|
@@ -158,6 +217,35 @@ export interface MessageOptions extends BaseGeminiOptions {
|
|
|
158
217
|
responseMimeType?: string;
|
|
159
218
|
}
|
|
160
219
|
|
|
220
|
+
export type EmbeddingTaskType = 'RETRIEVAL_DOCUMENT' | 'RETRIEVAL_QUERY' | 'SEMANTIC_SIMILARITY' | 'CLUSTERING' | 'CLASSIFICATION' | 'QUESTION_ANSWERING' | 'FACT_VERIFICATION';
|
|
221
|
+
|
|
222
|
+
export interface EmbeddingOptions extends BaseGeminiOptions {
|
|
223
|
+
/** Embedding task type (affects how embeddings are optimized) */
|
|
224
|
+
taskType?: EmbeddingTaskType;
|
|
225
|
+
/** Title for the document being embedded (only with RETRIEVAL_DOCUMENT) */
|
|
226
|
+
title?: string;
|
|
227
|
+
/** Output dimensionality for the embedding vector */
|
|
228
|
+
outputDimensionality?: number;
|
|
229
|
+
/** Whether to auto-truncate long inputs (default: true) */
|
|
230
|
+
autoTruncate?: boolean;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export interface EmbedConfig {
|
|
234
|
+
/** Override task type for this call */
|
|
235
|
+
taskType?: EmbeddingTaskType;
|
|
236
|
+
/** Override title for this call */
|
|
237
|
+
title?: string;
|
|
238
|
+
/** Override output dimensionality for this call */
|
|
239
|
+
outputDimensionality?: number;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
export interface EmbeddingResult {
|
|
243
|
+
/** The embedding vector */
|
|
244
|
+
values?: number[];
|
|
245
|
+
/** Embedding statistics (Vertex AI) */
|
|
246
|
+
statistics?: { tokenCount?: number; truncated?: boolean };
|
|
247
|
+
}
|
|
248
|
+
|
|
161
249
|
/** Tool declaration in @google/genai FunctionDeclaration format */
|
|
162
250
|
export interface ToolDeclaration {
|
|
163
251
|
name: string;
|
|
@@ -374,6 +462,9 @@ export declare class BaseGemini {
|
|
|
374
462
|
exampleCount: number;
|
|
375
463
|
labels: Record<string, string>;
|
|
376
464
|
vertexai: boolean;
|
|
465
|
+
enableGrounding: boolean;
|
|
466
|
+
groundingConfig: Record<string, any>;
|
|
467
|
+
cachedContent: string | null;
|
|
377
468
|
|
|
378
469
|
init(force?: boolean): Promise<void>;
|
|
379
470
|
seed(examples?: TransformationExample[], opts?: SeedOptions): Promise<any[]>;
|
|
@@ -388,6 +479,14 @@ export declare class BaseGemini {
|
|
|
388
479
|
estimatedInputCost: number;
|
|
389
480
|
note: string;
|
|
390
481
|
}>;
|
|
482
|
+
|
|
483
|
+
// Context Caching
|
|
484
|
+
createCache(config?: CacheConfig): Promise<CachedContentInfo>;
|
|
485
|
+
getCache(cacheName: string): Promise<CachedContentInfo>;
|
|
486
|
+
listCaches(): Promise<CachedContentInfo[]>;
|
|
487
|
+
updateCache(cacheName: string, config?: { ttl?: string; expireTime?: string }): Promise<CachedContentInfo>;
|
|
488
|
+
deleteCache(cacheName: string): Promise<void>;
|
|
489
|
+
useCache(cacheName: string): Promise<void>;
|
|
391
490
|
}
|
|
392
491
|
|
|
393
492
|
export declare class Transformer extends BaseGemini {
|
|
@@ -401,8 +500,6 @@ export declare class Transformer extends BaseGemini {
|
|
|
401
500
|
asyncValidator: AsyncValidatorFunction | null;
|
|
402
501
|
maxRetries: number;
|
|
403
502
|
retryDelay: number;
|
|
404
|
-
enableGrounding: boolean;
|
|
405
|
-
|
|
406
503
|
seed(examples?: TransformationExample[]): Promise<any[]>;
|
|
407
504
|
send(payload: Record<string, unknown> | string, opts?: SendOptions, validatorFn?: AsyncValidatorFunction | null): Promise<Record<string, unknown>>;
|
|
408
505
|
rawSend(payload: Record<string, unknown> | string, messageOptions?: { labels?: Record<string, string> }): Promise<Record<string, unknown>>;
|
|
@@ -496,6 +593,23 @@ export declare class CodeAgent extends BaseGemini {
|
|
|
496
593
|
stop(): void;
|
|
497
594
|
}
|
|
498
595
|
|
|
596
|
+
export declare class Embedding extends BaseGemini {
|
|
597
|
+
constructor(options?: EmbeddingOptions);
|
|
598
|
+
|
|
599
|
+
taskType: EmbeddingTaskType | null;
|
|
600
|
+
title: string | null;
|
|
601
|
+
outputDimensionality: number | null;
|
|
602
|
+
autoTruncate: boolean;
|
|
603
|
+
|
|
604
|
+
init(force?: boolean): Promise<void>;
|
|
605
|
+
/** Embed a single text string */
|
|
606
|
+
embed(text: string, config?: EmbedConfig): Promise<EmbeddingResult>;
|
|
607
|
+
/** Embed multiple text strings in a single API call */
|
|
608
|
+
embedBatch(texts: string[], config?: EmbedConfig): Promise<EmbeddingResult[]>;
|
|
609
|
+
/** Compute cosine similarity between two embedding vectors (-1 to 1) */
|
|
610
|
+
similarity(a: number[], b: number[]): number;
|
|
611
|
+
}
|
|
612
|
+
|
|
499
613
|
// ── Module Exports ───────────────────────────────────────────────────────────
|
|
500
614
|
|
|
501
615
|
export declare function extractJSON(text: string): any;
|
|
@@ -508,6 +622,7 @@ declare const _default: {
|
|
|
508
622
|
ToolAgent: typeof ToolAgent;
|
|
509
623
|
CodeAgent: typeof CodeAgent;
|
|
510
624
|
RagAgent: typeof RagAgent;
|
|
625
|
+
Embedding: typeof Embedding;
|
|
511
626
|
};
|
|
512
627
|
|
|
513
628
|
export default _default;
|