ak-gemini 1.0.4 → 1.0.6

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 (6) hide show
  1. package/README.md +43 -22
  2. package/index.cjs +288 -69
  3. package/index.js +424 -89
  4. package/package.json +21 -16
  5. package/types.d.ts +125 -0
  6. package/types.ts +0 -65
package/package.json CHANGED
@@ -2,19 +2,25 @@
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.4",
5
+ "version": "1.0.6",
6
6
  "main": "index.js",
7
7
  "files": [
8
8
  "index.js",
9
9
  "index.cjs",
10
- "types.ts",
10
+ "types.d.ts",
11
11
  "logger.js"
12
12
  ],
13
- "types": "types.ts",
13
+ "types": "types.d.ts",
14
14
  "exports": {
15
15
  ".": {
16
- "import": "./index.js",
17
- "require": "./index.cjs"
16
+ "import": {
17
+ "types": "./types.d.ts",
18
+ "default": "./index.js"
19
+ },
20
+ "require": {
21
+ "types": "./types.d.ts",
22
+ "default": "./index.cjs"
23
+ }
18
24
  }
19
25
  },
20
26
  "repository": {
@@ -26,17 +32,16 @@
26
32
  },
27
33
  "homepage": "https://github.com/ak--47/ak-gemini#readme",
28
34
  "scripts": {
29
- "prepublishOnly": "npm run build:cjs",
35
+ "prepublishOnly": "npm run typecheck && npm run build:cjs",
30
36
  "post": "npm publish --access public",
31
37
  "release": "npm version patch && npm publish --access public",
32
- "local": "./scripts/local.sh",
33
- "fire": "./scripts/fire.sh",
34
- "deploy": "./scripts/deploy.sh",
35
- "perms": "chmod +x ./scripts/*.sh",
36
38
  "update-deps": "npx npm-check-updates -u && npm install",
37
39
  "prune": "rm -rf tmp/*",
38
40
  "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 --external:@google/genai --external:ak-tools --external:dotenv --external:pino-pretty --external:pino"
41
+ "test:unit": "npm test -- tests/module.test.js",
42
+ "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",
43
+ "coverage": "node --no-warnings --experimental-vm-modules node_modules/jest/bin/jest.js --coverage",
44
+ "typecheck": "tsc --noEmit"
40
45
  },
41
46
  "type": "module",
42
47
  "keywords": [
@@ -46,17 +51,17 @@
46
51
  ],
47
52
  "license": "ISC",
48
53
  "dependencies": {
49
- "@google-cloud/functions-framework": "^4.0.0",
50
- "@google/genai": "^1.3.0",
51
- "ak-tools": "^1.0.64",
54
+ "@google/genai": "^1.25.0",
55
+ "ak-tools": "^1.1.12",
52
56
  "dotenv": "^16.5.0",
53
57
  "pino": "^9.7.0",
54
58
  "pino-pretty": "^13.0.0"
55
59
  },
56
60
  "devDependencies": {
57
61
  "@types/jest": "^29.5.14",
58
- "esbuild": "^0.25.5",
62
+ "esbuild": "^0.25.11",
59
63
  "jest": "^29.7.0",
60
- "nodemon": "^3.1.10"
64
+ "nodemon": "^3.1.10",
65
+ "typescript": "^5.9.2"
61
66
  }
62
67
  }
package/types.d.ts ADDED
@@ -0,0 +1,125 @@
1
+ import type { GoogleGenAI } from '@google/genai';
2
+
3
+ export interface SafetySetting {
4
+ category: string; // The harm category
5
+ threshold: string; // The blocking threshold
6
+ }
7
+
8
+ export interface ChatConfig {
9
+ responseMimeType?: string; // MIME type for responses
10
+ temperature?: number; // Controls randomness (0.0 to 1.0)
11
+ topP?: number; // Controls diversity via nucleus sampling
12
+ topK?: number; // Controls diversity by limiting top-k tokens
13
+ systemInstruction?: string; // System instruction for the model
14
+ safetySettings?: SafetySetting[]; // Safety settings array
15
+ responseSchema?: Object; // Schema for validating model responses
16
+ [key: string]: any; // Additional properties for flexibility
17
+ }
18
+
19
+ export interface AITransformerContext {
20
+ modelName?: string;
21
+ systemInstructions?: string;
22
+ chatConfig?: ChatConfig;
23
+ genAI?: any;
24
+ chat?: any;
25
+ examplesFile?: string | null;
26
+ exampleData?: TransformationExample[] | null;
27
+ promptKey?: string;
28
+ answerKey?: string;
29
+ contextKey?: string;
30
+ explanationKey?: string;
31
+ systemInstructionsKey?: string;
32
+ maxRetries?: number;
33
+ retryDelay?: number;
34
+ init?: (force?: boolean) => Promise<void>; // Initialization function
35
+ seed?: () => Promise<void>; // Function to seed the transformer with examples
36
+ message?: (payload: Record<string, unknown>) => Promise<Record<string, unknown>>; // Function to send messages to the model
37
+ rebuild?: (lastPayload: Record<string, unknown>, serverError: string) => Promise<Record<string, unknown>>; // Function to rebuild the transformer
38
+ rawMessage?: (payload: Record<string, unknown> | string) => Promise<Record<string, unknown>>; // Function to send raw messages to the model
39
+ genAIClient?: GoogleGenAI; // Google GenAI client instance
40
+ onlyJSON?: boolean; // If true, only JSON responses are allowed
41
+
42
+ }
43
+
44
+ export interface TransformationExample {
45
+ CONTEXT?: Record<string, unknown> | string; // optional context for the transformation
46
+ PROMPT?: Record<string, unknown>; // what the user provides as input
47
+ ANSWER?: Record<string, unknown>; // what the model should return as output
48
+ INPUT?: Record<string, unknown>; // alias for PROMPT
49
+ OUTPUT?: Record<string, unknown>; // alias for ANSWER
50
+ SYSTEM?: string; // system instructions for this example
51
+ EXPLANATION?: string; // explanation for this example
52
+ [key: string]: any; // allow additional properties for flexible key mapping
53
+ }
54
+
55
+ export interface ExampleFileContent {
56
+ examples: TransformationExample[];
57
+ }
58
+
59
+ export interface AITransformerOptions {
60
+ // ? https://ai.google.dev/gemini-api/docs/models
61
+ modelName?: string; // The Gemini model to use
62
+ systemInstructions?: string; // Custom system instructions for the model
63
+ chatConfig?: ChatConfig; // Configuration object for the chat session
64
+ examplesFile?: string; // Path to JSON file containing transformation examples
65
+ exampleData?: TransformationExample[]; // Inline examples to seed the transformer
66
+ sourceKey?: string; // Key name for source data in examples (alias for promptKey)
67
+ targetKey?: string; // Key name for target data in examples (alias for answerKey)
68
+ promptKey?: string; // Key for the prompt in examples
69
+ answerKey?: string; // Key for the answer in examples
70
+ contextKey?: string; // Key name for context data in examples
71
+ explanationKey?: string; // Key name for explanation data in examples
72
+ systemInstructionsKey?: string; // Key for system instructions in examples
73
+ maxRetries?: number; // Maximum retry attempts for auto-retry functionality
74
+ retryDelay?: number; // Initial retry delay in milliseconds
75
+ // ? https://ai.google.dev/gemini-api/docs/structured-output
76
+ responseSchema?: Object; // Schema for validating model responses
77
+ apiKey?: string; // API key for Google GenAI
78
+ onlyJSON?: boolean; // If true, only JSON responses are allowed
79
+ asyncValidator?: AsyncValidatorFunction; // Optional async validator function for response validation
80
+ logLevel?: 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal' | 'none'; // Log level for the logger (defaults to 'info', 'none' disables logging)
81
+ }
82
+
83
+ // Async validator function type
84
+ export type AsyncValidatorFunction = (payload: Record<string, unknown>) => Promise<unknown>;
85
+
86
+
87
+ export declare class AITransformer {
88
+ // Constructor
89
+ constructor(options?: AITransformerOptions);
90
+
91
+ // Properties
92
+ modelName: string;
93
+ promptKey: string;
94
+ answerKey: string;
95
+ contextKey: string;
96
+ explanationKey: string;
97
+ systemInstructionKey: string;
98
+ maxRetries: number;
99
+ retryDelay: number;
100
+ systemInstructions: string;
101
+ chatConfig: ChatConfig;
102
+ apiKey: string;
103
+ onlyJSON: boolean;
104
+ asyncValidator: AsyncValidatorFunction | null;
105
+ genAIClient: any;
106
+ chat: any;
107
+ logLevel: 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal' | 'none';
108
+
109
+ // Methods
110
+ init(force?: boolean): Promise<void>;
111
+ seed(examples?: TransformationExample[]): Promise<any>;
112
+ message(payload: Record<string, unknown>, opts?: object, validatorFn?: AsyncValidatorFunction | null): Promise<Record<string, unknown>>;
113
+ rawMessage(sourcePayload: Record<string, unknown> | string): Promise<Record<string, unknown> | any>;
114
+ transformWithValidation(sourcePayload: Record<string, unknown>, validatorFn: AsyncValidatorFunction, options?: object): Promise<Record<string, unknown>>;
115
+ messageAndValidate(sourcePayload: Record<string, unknown>, validatorFn: AsyncValidatorFunction, options?: object): Promise<Record<string, unknown>>;
116
+ rebuild(lastPayload: Record<string, unknown>, serverError: string): Promise<Record<string, unknown>>;
117
+ reset(): Promise<void>;
118
+ getHistory(): Array<any>;
119
+ estimateTokenUsage(nextPayload: Record<string, unknown> | string): Promise<{ totalTokens: number; breakdown?: any }>;
120
+ estimate(nextPayload: Record<string, unknown> | string): Promise<{ totalTokens: number; breakdown?: any }>;
121
+ }
122
+
123
+ // Default export
124
+ declare const _default: typeof AITransformer;
125
+ export default _default;
package/types.ts DELETED
@@ -1,65 +0,0 @@
1
- import type { GoogleGenAI } from '@google/genai';
2
-
3
- export interface SafetySetting {
4
- category: string; // The harm category
5
- threshold: string; // The blocking threshold
6
- }
7
-
8
- export interface ChatConfig {
9
- responseMimeType?: string; // MIME type for responses
10
- temperature?: number; // Controls randomness (0.0 to 1.0)
11
- topP?: number; // Controls diversity via nucleus sampling
12
- topK?: number; // Controls diversity by limiting top-k tokens
13
- systemInstruction?: string; // System instruction for the model
14
- safetySettings?: SafetySetting[]; // Safety settings array
15
- responseSchema?: Object; // Schema for validating model responses
16
- }
17
-
18
- export interface AITransformerContext {
19
- modelName?: string;
20
- systemInstructions?: string;
21
- chatConfig?: ChatConfig;
22
- genAI?: any;
23
- chat?: any;
24
- examplesFile?: string | null;
25
- exampleData?: TransformationExample[] | null;
26
- promptKey?: string;
27
- answerKey?: string;
28
- contextKey?: string;
29
- maxRetries?: number;
30
- retryDelay?: number;
31
- init: () => Promise<void>; // Initialization function
32
- seed: () => Promise<void>; // Function to seed the transformer with examples
33
- message: (payload: Record<string, unknown>) => Promise<Record<string, unknown>>; // Function to send messages to the model
34
- genAIClient?: GoogleGenAI; // Google GenAI client instance
35
-
36
- }
37
-
38
- export interface TransformationExample {
39
- CONTEXT?: Record<string, unknown>; // optional context for the transformation
40
- PROMPT?: Record<string, unknown>; // what the user provides as input
41
- ANSWER?: Record<string, unknown>; // what the model should return as output
42
- }
43
-
44
- export interface ExampleFileContent {
45
- examples: TransformationExample[];
46
- }
47
-
48
- export interface AITransformerOptions {
49
- modelName?: string; // The Gemini model to use
50
- systemInstructions?: string; // Custom system instructions for the model
51
- chatConfig?: ChatConfig; // Configuration object for the chat session
52
- examplesFile?: string; // Path to JSON file containing transformation examples
53
- exampleData?: TransformationExample[]; // Inline examples to seed the transformer
54
- sourceKey?: string; // Key name for source data in examples
55
- targetKey?: string; // Key name for target data in examples
56
- contextKey?: string; // Key name for context data in examples
57
- maxRetries?: number; // Maximum retry attempts for auto-retry functionality
58
- retryDelay?: number; // Initial retry delay in milliseconds
59
- // ? https://ai.google.dev/gemini-api/docs/structured-output
60
- responseSchema?: Object; // Schema for validating model responses
61
- apiKey?: string; // API key for Google GenAI
62
- }
63
-
64
- // Async validator function type
65
- export type AsyncValidatorFunction = (payload: Record<string, unknown>) => Promise<Record<string, unknown>>;