@elizaos/plugin-google-genai 2.0.0-alpha.4 → 2.0.0-alpha.537
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 +122 -0
- package/dist/browser/index.browser.js +232 -32
- package/dist/browser/index.browser.js.map +12 -11
- package/dist/build.d.ts +3 -0
- package/dist/build.d.ts.map +1 -0
- package/dist/build.js +117 -0
- package/dist/cjs/index.node.cjs +300 -66
- package/dist/cjs/index.node.js.map +12 -11
- package/dist/generated/specs/specs.d.ts +55 -0
- package/dist/generated/specs/specs.d.ts.map +1 -0
- package/dist/generated/specs/specs.js +34 -0
- package/dist/index.browser.d.ts +5 -0
- package/dist/index.browser.d.ts.map +1 -0
- package/dist/index.browser.js +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +252 -0
- package/dist/index.node.d.ts +5 -0
- package/dist/index.node.d.ts.map +1 -0
- package/dist/index.node.js +4 -0
- package/dist/init.d.ts +16 -0
- package/dist/init.d.ts.map +1 -0
- package/dist/init.js +27 -0
- package/dist/models/embedding.d.ts +6 -0
- package/dist/models/embedding.d.ts.map +1 -0
- package/dist/models/embedding.js +57 -0
- package/dist/models/image.d.ts +7 -0
- package/dist/models/image.d.ts.map +1 -0
- package/dist/models/image.js +91 -0
- package/dist/models/index.d.ts +13 -0
- package/dist/models/index.d.ts.map +1 -0
- package/dist/models/index.js +12 -0
- package/dist/models/object.d.ts +10 -0
- package/dist/models/object.d.ts.map +1 -0
- package/dist/models/object.js +84 -0
- package/dist/models/text.d.ts +51 -0
- package/dist/models/text.d.ts.map +1 -0
- package/dist/models/text.js +257 -0
- package/dist/node/index.node.js +232 -32
- package/dist/node/index.node.js.map +12 -11
- package/dist/types/index.d.ts +47 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +1 -0
- package/dist/utils/config.d.ts +25 -0
- package/dist/utils/config.d.ts.map +1 -0
- package/dist/utils/config.js +115 -0
- package/dist/utils/events.d.ts +12 -0
- package/dist/utils/events.d.ts.map +1 -0
- package/dist/utils/events.js +14 -0
- package/dist/utils/index.d.ts +4 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +3 -0
- package/dist/utils/tokenization.d.ts +2 -0
- package/dist/utils/tokenization.d.ts.map +1 -0
- package/dist/utils/tokenization.js +3 -0
- package/dist/vitest.config.d.ts +3 -0
- package/dist/vitest.config.d.ts.map +1 -0
- package/dist/vitest.config.js +8 -0
- package/package.json +17 -13
package/README.md
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# @elizaos/plugin-google-genai
|
|
2
|
+
|
|
3
|
+
Google Generative AI (Gemini) plugin for elizaOS with native support for TypeScript, Python, and Rust.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Text Generation**: Generate text using Gemini 2.0 Flash and 2.5 Pro models
|
|
8
|
+
- **Embeddings**: Generate text embeddings with text-embedding-004
|
|
9
|
+
- **Image Analysis**: Analyze and describe images with multimodal capabilities
|
|
10
|
+
- **JSON Object Generation**: Generate structured JSON with schema validation
|
|
11
|
+
|
|
12
|
+
## Available Models
|
|
13
|
+
|
|
14
|
+
| Model Type | Default Model | Description |
|
|
15
|
+
| ----------------- | ---------------------------- | ---------------------------------- |
|
|
16
|
+
| TEXT_SMALL | gemini-2.0-flash-001 | Fast, efficient for everyday tasks |
|
|
17
|
+
| TEXT_LARGE | gemini-2.5-pro-preview-03-25 | Most capable for complex tasks |
|
|
18
|
+
| TEXT_EMBEDDING | text-embedding-004 | Text embeddings (768 dimensions) |
|
|
19
|
+
| IMAGE_DESCRIPTION | gemini-2.5-pro-preview-03-25 | Multimodal image analysis |
|
|
20
|
+
| OBJECT_SMALL | gemini-2.0-flash-001 | Fast JSON generation |
|
|
21
|
+
| OBJECT_LARGE | gemini-2.5-pro-preview-03-25 | Complex JSON generation |
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
### TypeScript/JavaScript (npm)
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
npm install @elizaos/plugin-google-genai
|
|
29
|
+
# or
|
|
30
|
+
bun add @elizaos/plugin-google-genai
|
|
31
|
+
```
|
|
32
|
+
## Configuration
|
|
33
|
+
|
|
34
|
+
Set the following environment variables:
|
|
35
|
+
|
|
36
|
+
| Variable | Required | Description |
|
|
37
|
+
| ------------------------------ | -------- | ---------------------------------------------------------------------------- |
|
|
38
|
+
| `GOOGLE_GENERATIVE_AI_API_KEY` | Yes | Your Google AI API key from [Google AI Studio](https://aistudio.google.com/) |
|
|
39
|
+
| `GOOGLE_SMALL_MODEL` | No | Override small model (default: gemini-2.0-flash-001) |
|
|
40
|
+
| `GOOGLE_LARGE_MODEL` | No | Override large model (default: gemini-2.5-pro-preview-03-25) |
|
|
41
|
+
| `GOOGLE_EMBEDDING_MODEL` | No | Override embedding model (default: text-embedding-004) |
|
|
42
|
+
| `GOOGLE_IMAGE_MODEL` | No | Override image analysis model |
|
|
43
|
+
| `GOOGLE_TIMEOUT_SECONDS` | No | Request timeout (default: 60) |
|
|
44
|
+
|
|
45
|
+
## Usage
|
|
46
|
+
|
|
47
|
+
### TypeScript (elizaOS Plugin)
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
import { googleGenAIPlugin } from "@elizaos/plugin-google-genai";
|
|
51
|
+
|
|
52
|
+
// Register the plugin with your elizaOS agent
|
|
53
|
+
const agent = new Agent({
|
|
54
|
+
plugins: [googleGenAIPlugin],
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// Use via runtime
|
|
58
|
+
const text = await runtime.useModel(ModelType.TEXT_LARGE, {
|
|
59
|
+
prompt: "Explain quantum mechanics in simple terms.",
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
const embedding = await runtime.useModel(ModelType.TEXT_EMBEDDING, {
|
|
63
|
+
text: "Hello, world!",
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
const object = await runtime.useModel(ModelType.OBJECT_SMALL, {
|
|
67
|
+
prompt: "Generate a person profile with name and age.",
|
|
68
|
+
schema: {
|
|
69
|
+
type: "object",
|
|
70
|
+
properties: {
|
|
71
|
+
name: { type: "string" },
|
|
72
|
+
age: { type: "number" },
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
```
|
|
77
|
+
## Project Structure
|
|
78
|
+
|
|
79
|
+
```
|
|
80
|
+
plugin-google-genai/
|
|
81
|
+
├── typescript/ # TypeScript implementation
|
|
82
|
+
│ ├── index.ts # Main plugin entry
|
|
83
|
+
│ ├── models/ # Model handlers
|
|
84
|
+
│ ├── utils/ # Utility functions
|
|
85
|
+
│ └── __tests__/ # Unit and integration tests
|
|
86
|
+
├── package.json # npm publishing config
|
|
87
|
+
└── README.md # This file
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Development
|
|
91
|
+
|
|
92
|
+
### TypeScript
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
# Install dependencies
|
|
96
|
+
bun install
|
|
97
|
+
|
|
98
|
+
# Build
|
|
99
|
+
bun run build
|
|
100
|
+
|
|
101
|
+
# Run tests
|
|
102
|
+
bun run test
|
|
103
|
+
|
|
104
|
+
# Type checking
|
|
105
|
+
bun run typecheck
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## Publishing
|
|
109
|
+
|
|
110
|
+
### npm (TypeScript)
|
|
111
|
+
|
|
112
|
+
```bash
|
|
113
|
+
npm publish --access public
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## License
|
|
117
|
+
|
|
118
|
+
MIT
|
|
119
|
+
|
|
120
|
+
## Contributing
|
|
121
|
+
|
|
122
|
+
See the main [elizaOS contribution guidelines](https://github.com/elizaos/eliza/blob/main/CONTRIBUTING.md).
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// index.ts
|
|
2
|
-
import
|
|
2
|
+
import * as ElizaCore3 from "@elizaos/core";
|
|
3
|
+
import { logger as logger7, ModelType as ModelType4 } from "@elizaos/core";
|
|
3
4
|
import { GoogleGenAI as GoogleGenAI3 } from "@google/genai";
|
|
4
5
|
|
|
5
6
|
// init.ts
|
|
@@ -29,9 +30,24 @@ function getApiKey(runtime) {
|
|
|
29
30
|
function getSmallModel(runtime) {
|
|
30
31
|
return getSetting(runtime, "GOOGLE_SMALL_MODEL") ?? getSetting(runtime, "SMALL_MODEL", "gemini-2.0-flash-001") ?? "gemini-2.0-flash-001";
|
|
31
32
|
}
|
|
33
|
+
function getNanoModel(runtime) {
|
|
34
|
+
return getSetting(runtime, "GOOGLE_NANO_MODEL") ?? getSetting(runtime, "NANO_MODEL") ?? getSmallModel(runtime);
|
|
35
|
+
}
|
|
36
|
+
function getMediumModel(runtime) {
|
|
37
|
+
return getSetting(runtime, "GOOGLE_MEDIUM_MODEL") ?? getSetting(runtime, "MEDIUM_MODEL") ?? getSmallModel(runtime);
|
|
38
|
+
}
|
|
32
39
|
function getLargeModel(runtime) {
|
|
33
40
|
return getSetting(runtime, "GOOGLE_LARGE_MODEL") ?? getSetting(runtime, "LARGE_MODEL", "gemini-2.5-pro-preview-03-25") ?? "gemini-2.5-pro-preview-03-25";
|
|
34
41
|
}
|
|
42
|
+
function getMegaModel(runtime) {
|
|
43
|
+
return getSetting(runtime, "GOOGLE_MEGA_MODEL") ?? getSetting(runtime, "MEGA_MODEL") ?? getLargeModel(runtime);
|
|
44
|
+
}
|
|
45
|
+
function getResponseHandlerModel(runtime) {
|
|
46
|
+
return getSetting(runtime, "GOOGLE_RESPONSE_HANDLER_MODEL") ?? getSetting(runtime, "GOOGLE_SHOULD_RESPOND_MODEL") ?? getSetting(runtime, "RESPONSE_HANDLER_MODEL") ?? getSetting(runtime, "SHOULD_RESPOND_MODEL") ?? getNanoModel(runtime);
|
|
47
|
+
}
|
|
48
|
+
function getActionPlannerModel(runtime) {
|
|
49
|
+
return getSetting(runtime, "GOOGLE_ACTION_PLANNER_MODEL") ?? getSetting(runtime, "GOOGLE_PLANNER_MODEL") ?? getSetting(runtime, "ACTION_PLANNER_MODEL") ?? getSetting(runtime, "PLANNER_MODEL") ?? getMediumModel(runtime);
|
|
50
|
+
}
|
|
35
51
|
function getImageModel(runtime) {
|
|
36
52
|
return getSetting(runtime, "GOOGLE_IMAGE_MODEL") ?? getSetting(runtime, "IMAGE_MODEL", "gemini-2.5-pro-preview-03-25") ?? "gemini-2.5-pro-preview-03-25";
|
|
37
53
|
}
|
|
@@ -90,12 +106,13 @@ function initializeGoogleGenAI(_config, runtime) {
|
|
|
90
106
|
}
|
|
91
107
|
|
|
92
108
|
// models/embedding.ts
|
|
93
|
-
import
|
|
109
|
+
import * as ElizaCore from "@elizaos/core";
|
|
110
|
+
import { logger as logger3 } from "@elizaos/core";
|
|
94
111
|
|
|
95
112
|
// utils/events.ts
|
|
96
|
-
|
|
113
|
+
var MODEL_USED_EVENT = "MODEL_USED";
|
|
97
114
|
function emitModelUsageEvent(runtime, type, _prompt, usage) {
|
|
98
|
-
runtime.emitEvent(
|
|
115
|
+
runtime.emitEvent(MODEL_USED_EVENT, {
|
|
99
116
|
runtime,
|
|
100
117
|
source: "plugin-google-genai",
|
|
101
118
|
type,
|
|
@@ -113,6 +130,7 @@ async function countTokens(text) {
|
|
|
113
130
|
}
|
|
114
131
|
|
|
115
132
|
// models/embedding.ts
|
|
133
|
+
var TEXT_EMBEDDING_MODEL_TYPE = ElizaCore.ModelType?.TEXT_EMBEDDING ?? "TEXT_EMBEDDING";
|
|
116
134
|
async function handleTextEmbedding(runtime, params) {
|
|
117
135
|
const genAI = createGoogleGenAI(runtime);
|
|
118
136
|
if (!genAI) {
|
|
@@ -128,9 +146,9 @@ async function handleTextEmbedding(runtime, params) {
|
|
|
128
146
|
logger3.warn("Empty text for embedding");
|
|
129
147
|
return Array(768).fill(0);
|
|
130
148
|
}
|
|
131
|
-
const maxChars =
|
|
149
|
+
const maxChars = 8192 * 4;
|
|
132
150
|
if (text.length > maxChars) {
|
|
133
|
-
logger3.warn(`[Google GenAI] Embedding input too long (~${Math.ceil(text.length / 4)} tokens), truncating to
|
|
151
|
+
logger3.warn(`[Google GenAI] Embedding input too long (~${Math.ceil(text.length / 4)} tokens), truncating to ~8192 tokens`);
|
|
134
152
|
text = text.slice(0, maxChars);
|
|
135
153
|
}
|
|
136
154
|
try {
|
|
@@ -140,7 +158,7 @@ async function handleTextEmbedding(runtime, params) {
|
|
|
140
158
|
});
|
|
141
159
|
const embedding = response.embeddings?.[0]?.values || [];
|
|
142
160
|
const promptTokens = await countTokens(text);
|
|
143
|
-
emitModelUsageEvent(runtime,
|
|
161
|
+
emitModelUsageEvent(runtime, TEXT_EMBEDDING_MODEL_TYPE, text, {
|
|
144
162
|
promptTokens,
|
|
145
163
|
completionTokens: 0,
|
|
146
164
|
totalTokens: promptTokens
|
|
@@ -289,19 +307,94 @@ async function handleObjectLarge(runtime, params) {
|
|
|
289
307
|
return generateObjectByModelType(runtime, params, "OBJECT_LARGE", getLargeModel);
|
|
290
308
|
}
|
|
291
309
|
// models/text.ts
|
|
292
|
-
import
|
|
293
|
-
|
|
310
|
+
import * as ElizaCore2 from "@elizaos/core";
|
|
311
|
+
import { logger as logger6 } from "@elizaos/core";
|
|
312
|
+
var CORE_MODEL_TYPES = ElizaCore2.ModelType ?? {};
|
|
313
|
+
var TEXT_NANO_MODEL_TYPE = CORE_MODEL_TYPES.TEXT_NANO ?? "TEXT_NANO";
|
|
314
|
+
var TEXT_MEDIUM_MODEL_TYPE = CORE_MODEL_TYPES.TEXT_MEDIUM ?? "TEXT_MEDIUM";
|
|
315
|
+
var TEXT_SMALL_MODEL_TYPE = CORE_MODEL_TYPES.TEXT_SMALL ?? "TEXT_SMALL";
|
|
316
|
+
var TEXT_LARGE_MODEL_TYPE = CORE_MODEL_TYPES.TEXT_LARGE ?? "TEXT_LARGE";
|
|
317
|
+
var TEXT_MEGA_MODEL_TYPE = CORE_MODEL_TYPES.TEXT_MEGA ?? "TEXT_MEGA";
|
|
318
|
+
var RESPONSE_HANDLER_MODEL_TYPE = CORE_MODEL_TYPES.RESPONSE_HANDLER ?? "RESPONSE_HANDLER";
|
|
319
|
+
var ACTION_PLANNER_MODEL_TYPE = CORE_MODEL_TYPES.ACTION_PLANNER ?? "ACTION_PLANNER";
|
|
320
|
+
function buildPromptParts(prompt, attachments) {
|
|
321
|
+
const parts = [{ text: prompt }];
|
|
322
|
+
for (const attachment of attachments ?? []) {
|
|
323
|
+
if (attachment.data instanceof URL) {
|
|
324
|
+
parts.push({
|
|
325
|
+
fileData: {
|
|
326
|
+
mimeType: attachment.mediaType,
|
|
327
|
+
fileUri: attachment.data.toString()
|
|
328
|
+
}
|
|
329
|
+
});
|
|
330
|
+
continue;
|
|
331
|
+
}
|
|
332
|
+
if (typeof attachment.data === "string" && /^https?:\/\//i.test(attachment.data)) {
|
|
333
|
+
parts.push({
|
|
334
|
+
fileData: {
|
|
335
|
+
mimeType: attachment.mediaType,
|
|
336
|
+
fileUri: attachment.data
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
continue;
|
|
340
|
+
}
|
|
341
|
+
if (typeof attachment.data === "string") {
|
|
342
|
+
const dataUrlMatch = attachment.data.match(/^data:([^;,]+);base64,(.+)$/i);
|
|
343
|
+
parts.push({
|
|
344
|
+
inlineData: {
|
|
345
|
+
mimeType: dataUrlMatch?.[1] ?? attachment.mediaType,
|
|
346
|
+
data: dataUrlMatch?.[2] ?? attachment.data
|
|
347
|
+
}
|
|
348
|
+
});
|
|
349
|
+
continue;
|
|
350
|
+
}
|
|
351
|
+
parts.push({
|
|
352
|
+
inlineData: {
|
|
353
|
+
mimeType: attachment.mediaType,
|
|
354
|
+
data: Buffer.from(attachment.data).toString("base64")
|
|
355
|
+
}
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
return parts;
|
|
359
|
+
}
|
|
360
|
+
function getModelNameForType(runtime, modelType) {
|
|
361
|
+
switch (modelType) {
|
|
362
|
+
case TEXT_NANO_MODEL_TYPE:
|
|
363
|
+
return getNanoModel(runtime);
|
|
364
|
+
case TEXT_MEDIUM_MODEL_TYPE:
|
|
365
|
+
return getMediumModel(runtime);
|
|
366
|
+
case TEXT_SMALL_MODEL_TYPE:
|
|
367
|
+
return getSmallModel(runtime);
|
|
368
|
+
case TEXT_LARGE_MODEL_TYPE:
|
|
369
|
+
return getLargeModel(runtime);
|
|
370
|
+
case TEXT_MEGA_MODEL_TYPE:
|
|
371
|
+
return getMegaModel(runtime);
|
|
372
|
+
case RESPONSE_HANDLER_MODEL_TYPE:
|
|
373
|
+
return getResponseHandlerModel(runtime);
|
|
374
|
+
case ACTION_PLANNER_MODEL_TYPE:
|
|
375
|
+
return getActionPlannerModel(runtime);
|
|
376
|
+
default:
|
|
377
|
+
return getLargeModel(runtime);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
async function handleTextSmall(runtime, {
|
|
381
|
+
prompt,
|
|
382
|
+
stopSequences = [],
|
|
383
|
+
maxTokens = 8192,
|
|
384
|
+
temperature = 0.7,
|
|
385
|
+
attachments
|
|
386
|
+
}) {
|
|
294
387
|
const genAI = createGoogleGenAI(runtime);
|
|
295
388
|
if (!genAI) {
|
|
296
389
|
throw new Error("Google Generative AI client not initialized");
|
|
297
390
|
}
|
|
298
|
-
const modelName =
|
|
391
|
+
const modelName = getModelNameForType(runtime, TEXT_SMALL_MODEL_TYPE);
|
|
299
392
|
logger6.log(`[TEXT_SMALL] Using model: ${modelName}`);
|
|
300
393
|
try {
|
|
301
394
|
const systemInstruction = runtime.character.system || undefined;
|
|
302
395
|
const response = await genAI.models.generateContent({
|
|
303
396
|
model: modelName,
|
|
304
|
-
contents: prompt,
|
|
397
|
+
contents: (attachments?.length ?? 0) > 0 ? [{ role: "user", parts: buildPromptParts(prompt, attachments) }] : prompt,
|
|
305
398
|
config: {
|
|
306
399
|
temperature,
|
|
307
400
|
topK: 40,
|
|
@@ -315,7 +408,7 @@ async function handleTextSmall(runtime, { prompt, stopSequences = [], maxTokens
|
|
|
315
408
|
const text = response.text || "";
|
|
316
409
|
const promptTokens = await countTokens(prompt);
|
|
317
410
|
const completionTokens = await countTokens(text);
|
|
318
|
-
emitModelUsageEvent(runtime,
|
|
411
|
+
emitModelUsageEvent(runtime, TEXT_SMALL_MODEL_TYPE, prompt, {
|
|
319
412
|
promptTokens,
|
|
320
413
|
completionTokens,
|
|
321
414
|
totalTokens: promptTokens + completionTokens
|
|
@@ -326,18 +419,24 @@ async function handleTextSmall(runtime, { prompt, stopSequences = [], maxTokens
|
|
|
326
419
|
throw error;
|
|
327
420
|
}
|
|
328
421
|
}
|
|
329
|
-
async function handleTextLarge(runtime, {
|
|
422
|
+
async function handleTextLarge(runtime, {
|
|
423
|
+
prompt,
|
|
424
|
+
stopSequences = [],
|
|
425
|
+
maxTokens = 8192,
|
|
426
|
+
temperature = 0.7,
|
|
427
|
+
attachments
|
|
428
|
+
}) {
|
|
330
429
|
const genAI = createGoogleGenAI(runtime);
|
|
331
430
|
if (!genAI) {
|
|
332
431
|
throw new Error("Google Generative AI client not initialized");
|
|
333
432
|
}
|
|
334
|
-
const modelName =
|
|
433
|
+
const modelName = getModelNameForType(runtime, TEXT_LARGE_MODEL_TYPE);
|
|
335
434
|
logger6.log(`[TEXT_LARGE] Using model: ${modelName}`);
|
|
336
435
|
try {
|
|
337
436
|
const systemInstruction = runtime.character.system || undefined;
|
|
338
437
|
const response = await genAI.models.generateContent({
|
|
339
438
|
model: modelName,
|
|
340
|
-
contents: prompt,
|
|
439
|
+
contents: (attachments?.length ?? 0) > 0 ? [{ role: "user", parts: buildPromptParts(prompt, attachments) }] : prompt,
|
|
341
440
|
config: {
|
|
342
441
|
temperature,
|
|
343
442
|
topK: 40,
|
|
@@ -351,7 +450,7 @@ async function handleTextLarge(runtime, { prompt, stopSequences = [], maxTokens
|
|
|
351
450
|
const text = response.text || "";
|
|
352
451
|
const promptTokens = await countTokens(prompt);
|
|
353
452
|
const completionTokens = await countTokens(text);
|
|
354
|
-
emitModelUsageEvent(runtime,
|
|
453
|
+
emitModelUsageEvent(runtime, TEXT_LARGE_MODEL_TYPE, prompt, {
|
|
355
454
|
promptTokens,
|
|
356
455
|
completionTokens,
|
|
357
456
|
totalTokens: promptTokens + completionTokens
|
|
@@ -362,7 +461,76 @@ async function handleTextLarge(runtime, { prompt, stopSequences = [], maxTokens
|
|
|
362
461
|
throw error;
|
|
363
462
|
}
|
|
364
463
|
}
|
|
464
|
+
async function handleTextNano(runtime, params) {
|
|
465
|
+
return handleTextWithType(runtime, TEXT_NANO_MODEL_TYPE, params);
|
|
466
|
+
}
|
|
467
|
+
async function handleTextMedium(runtime, params) {
|
|
468
|
+
return handleTextWithType(runtime, TEXT_MEDIUM_MODEL_TYPE, params);
|
|
469
|
+
}
|
|
470
|
+
async function handleTextMega(runtime, params) {
|
|
471
|
+
return handleTextWithType(runtime, TEXT_MEGA_MODEL_TYPE, params);
|
|
472
|
+
}
|
|
473
|
+
async function handleResponseHandler(runtime, params) {
|
|
474
|
+
return handleTextWithType(runtime, RESPONSE_HANDLER_MODEL_TYPE, params);
|
|
475
|
+
}
|
|
476
|
+
async function handleActionPlanner(runtime, params) {
|
|
477
|
+
return handleTextWithType(runtime, ACTION_PLANNER_MODEL_TYPE, params);
|
|
478
|
+
}
|
|
479
|
+
async function handleTextWithType(runtime, modelType, {
|
|
480
|
+
prompt,
|
|
481
|
+
stopSequences = [],
|
|
482
|
+
maxTokens = 8192,
|
|
483
|
+
temperature = 0.7,
|
|
484
|
+
attachments
|
|
485
|
+
}) {
|
|
486
|
+
const genAI = createGoogleGenAI(runtime);
|
|
487
|
+
if (!genAI) {
|
|
488
|
+
throw new Error("Google Generative AI client not initialized");
|
|
489
|
+
}
|
|
490
|
+
const modelName = getModelNameForType(runtime, modelType);
|
|
491
|
+
logger6.log(`[${modelType}] Using model: ${modelName}`);
|
|
492
|
+
try {
|
|
493
|
+
const systemInstruction = runtime.character.system || undefined;
|
|
494
|
+
const response = await genAI.models.generateContent({
|
|
495
|
+
model: modelName,
|
|
496
|
+
contents: (attachments?.length ?? 0) > 0 ? [{ role: "user", parts: buildPromptParts(prompt, attachments) }] : prompt,
|
|
497
|
+
config: {
|
|
498
|
+
temperature,
|
|
499
|
+
topK: 40,
|
|
500
|
+
topP: 0.95,
|
|
501
|
+
maxOutputTokens: maxTokens,
|
|
502
|
+
stopSequences,
|
|
503
|
+
safetySettings: getSafetySettings(),
|
|
504
|
+
...systemInstruction && { systemInstruction }
|
|
505
|
+
}
|
|
506
|
+
});
|
|
507
|
+
const text = response.text || "";
|
|
508
|
+
const promptTokens = await countTokens(prompt);
|
|
509
|
+
const completionTokens = await countTokens(text);
|
|
510
|
+
emitModelUsageEvent(runtime, modelType, prompt, {
|
|
511
|
+
promptTokens,
|
|
512
|
+
completionTokens,
|
|
513
|
+
totalTokens: promptTokens + completionTokens
|
|
514
|
+
});
|
|
515
|
+
return text;
|
|
516
|
+
} catch (error) {
|
|
517
|
+
logger6.error(`[${modelType}] Error: ${error instanceof Error ? error.message : String(error)}`);
|
|
518
|
+
throw error;
|
|
519
|
+
}
|
|
520
|
+
}
|
|
365
521
|
// index.ts
|
|
522
|
+
var CORE_MODEL_TYPES2 = ElizaCore3.ModelType ?? {};
|
|
523
|
+
var TEXT_NANO_MODEL_TYPE2 = CORE_MODEL_TYPES2.TEXT_NANO ?? "TEXT_NANO";
|
|
524
|
+
var TEXT_MEDIUM_MODEL_TYPE2 = CORE_MODEL_TYPES2.TEXT_MEDIUM ?? "TEXT_MEDIUM";
|
|
525
|
+
var TEXT_SMALL_MODEL_TYPE2 = CORE_MODEL_TYPES2.TEXT_SMALL ?? "TEXT_SMALL";
|
|
526
|
+
var TEXT_LARGE_MODEL_TYPE2 = CORE_MODEL_TYPES2.TEXT_LARGE ?? "TEXT_LARGE";
|
|
527
|
+
var TEXT_EMBEDDING_MODEL_TYPE2 = CORE_MODEL_TYPES2.TEXT_EMBEDDING ?? "TEXT_EMBEDDING";
|
|
528
|
+
var IMAGE_DESCRIPTION_MODEL_TYPE = CORE_MODEL_TYPES2.IMAGE_DESCRIPTION ?? "IMAGE_DESCRIPTION";
|
|
529
|
+
var OBJECT_SMALL_MODEL_TYPE = CORE_MODEL_TYPES2.OBJECT_SMALL ?? "OBJECT_SMALL";
|
|
530
|
+
var OBJECT_LARGE_MODEL_TYPE = CORE_MODEL_TYPES2.OBJECT_LARGE ?? "OBJECT_LARGE";
|
|
531
|
+
var TEXT_MEGA_MODEL_TYPE2 = CORE_MODEL_TYPES2.TEXT_MEGA ?? "TEXT_MEGA";
|
|
532
|
+
var RESPONSE_HANDLER_MODEL_TYPE2 = CORE_MODEL_TYPES2.RESPONSE_HANDLER ?? "RESPONSE_HANDLER";
|
|
533
|
+
var ACTION_PLANNER_MODEL_TYPE2 = CORE_MODEL_TYPES2.ACTION_PLANNER ?? "ACTION_PLANNER";
|
|
366
534
|
var pluginTests = [
|
|
367
535
|
{
|
|
368
536
|
name: "google_genai_plugin_tests",
|
|
@@ -387,7 +555,7 @@ var pluginTests = [
|
|
|
387
555
|
name: "google_test_text_embedding",
|
|
388
556
|
fn: async (runtime) => {
|
|
389
557
|
try {
|
|
390
|
-
const embedding = await runtime.useModel(
|
|
558
|
+
const embedding = await runtime.useModel(ModelType4.TEXT_EMBEDDING, {
|
|
391
559
|
text: "Hello, world!"
|
|
392
560
|
});
|
|
393
561
|
logger7.log(`Embedding dimension: ${embedding.length}`);
|
|
@@ -404,7 +572,7 @@ var pluginTests = [
|
|
|
404
572
|
name: "google_test_text_small",
|
|
405
573
|
fn: async (runtime) => {
|
|
406
574
|
try {
|
|
407
|
-
const text = await runtime.useModel(
|
|
575
|
+
const text = await runtime.useModel(ModelType4.TEXT_SMALL, {
|
|
408
576
|
prompt: "What is the nature of reality in 10 words?"
|
|
409
577
|
});
|
|
410
578
|
if (text.length === 0) {
|
|
@@ -421,7 +589,7 @@ var pluginTests = [
|
|
|
421
589
|
name: "google_test_text_large",
|
|
422
590
|
fn: async (runtime) => {
|
|
423
591
|
try {
|
|
424
|
-
const text = await runtime.useModel(
|
|
592
|
+
const text = await runtime.useModel(ModelType4.TEXT_LARGE, {
|
|
425
593
|
prompt: "Explain quantum mechanics in simple terms."
|
|
426
594
|
});
|
|
427
595
|
if (text.length === 0) {
|
|
@@ -438,8 +606,8 @@ var pluginTests = [
|
|
|
438
606
|
name: "google_test_image_description",
|
|
439
607
|
fn: async (runtime) => {
|
|
440
608
|
try {
|
|
441
|
-
const result = await runtime.useModel(
|
|
442
|
-
if (result && typeof result === "object" && "title" in result && "description" in result) {
|
|
609
|
+
const result = await runtime.useModel(ModelType4.IMAGE_DESCRIPTION, "https://upload.wikimedia.org/wikipedia/commons/thumb/1/1c/Vitalik_Buterin_TechCrunch_London_2015_%28cropped%29.jpg/537px-Vitalik_Buterin_TechCrunch_London_2015_%28cropped%29.jpg");
|
|
610
|
+
if (result != null && typeof result === "object" && "title" in result && "description" in result) {
|
|
443
611
|
logger7.log("Image description:", JSON.stringify(result));
|
|
444
612
|
} else {
|
|
445
613
|
logger7.error(`Invalid image description result format: ${JSON.stringify(result)}`);
|
|
@@ -463,12 +631,12 @@ var pluginTests = [
|
|
|
463
631
|
},
|
|
464
632
|
required: ["name", "age", "hobbies"]
|
|
465
633
|
};
|
|
466
|
-
const result = await runtime.useModel(
|
|
634
|
+
const result = await runtime.useModel(ModelType4.OBJECT_SMALL, {
|
|
467
635
|
prompt: "Generate a person profile with name, age, and hobbies.",
|
|
468
636
|
schema
|
|
469
637
|
});
|
|
470
638
|
logger7.log("Generated object:", JSON.stringify(result));
|
|
471
|
-
if (!result
|
|
639
|
+
if (!result?.name || !result?.age || !result?.hobbies) {
|
|
472
640
|
throw new Error("Generated object missing required fields");
|
|
473
641
|
}
|
|
474
642
|
} catch (error) {
|
|
@@ -492,43 +660,75 @@ var googleGenAIPlugin = {
|
|
|
492
660
|
description: "Google Generative AI plugin for Gemini models",
|
|
493
661
|
config: {
|
|
494
662
|
GOOGLE_GENERATIVE_AI_API_KEY: env.GOOGLE_GENERATIVE_AI_API_KEY ?? null,
|
|
663
|
+
GOOGLE_NANO_MODEL: env.GOOGLE_NANO_MODEL ?? null,
|
|
664
|
+
GOOGLE_MEDIUM_MODEL: env.GOOGLE_MEDIUM_MODEL ?? null,
|
|
495
665
|
GOOGLE_SMALL_MODEL: env.GOOGLE_SMALL_MODEL ?? null,
|
|
496
666
|
GOOGLE_LARGE_MODEL: env.GOOGLE_LARGE_MODEL ?? null,
|
|
667
|
+
GOOGLE_MEGA_MODEL: env.GOOGLE_MEGA_MODEL ?? null,
|
|
668
|
+
GOOGLE_RESPONSE_HANDLER_MODEL: env.GOOGLE_RESPONSE_HANDLER_MODEL ?? null,
|
|
669
|
+
GOOGLE_SHOULD_RESPOND_MODEL: env.GOOGLE_SHOULD_RESPOND_MODEL ?? null,
|
|
670
|
+
GOOGLE_ACTION_PLANNER_MODEL: env.GOOGLE_ACTION_PLANNER_MODEL ?? null,
|
|
671
|
+
GOOGLE_PLANNER_MODEL: env.GOOGLE_PLANNER_MODEL ?? null,
|
|
497
672
|
GOOGLE_IMAGE_MODEL: env.GOOGLE_IMAGE_MODEL ?? null,
|
|
498
673
|
GOOGLE_EMBEDDING_MODEL: env.GOOGLE_EMBEDDING_MODEL ?? null,
|
|
674
|
+
NANO_MODEL: env.NANO_MODEL ?? null,
|
|
675
|
+
MEDIUM_MODEL: env.MEDIUM_MODEL ?? null,
|
|
499
676
|
SMALL_MODEL: env.SMALL_MODEL ?? null,
|
|
500
677
|
LARGE_MODEL: env.LARGE_MODEL ?? null,
|
|
678
|
+
MEGA_MODEL: env.MEGA_MODEL ?? null,
|
|
679
|
+
RESPONSE_HANDLER_MODEL: env.RESPONSE_HANDLER_MODEL ?? null,
|
|
680
|
+
SHOULD_RESPOND_MODEL: env.SHOULD_RESPOND_MODEL ?? null,
|
|
681
|
+
ACTION_PLANNER_MODEL: env.ACTION_PLANNER_MODEL ?? null,
|
|
682
|
+
PLANNER_MODEL: env.PLANNER_MODEL ?? null,
|
|
501
683
|
IMAGE_MODEL: env.IMAGE_MODEL ?? null
|
|
502
684
|
},
|
|
503
685
|
async init(config, runtime) {
|
|
504
686
|
initializeGoogleGenAI(config, runtime);
|
|
505
687
|
},
|
|
506
688
|
models: {
|
|
507
|
-
[
|
|
689
|
+
[TEXT_NANO_MODEL_TYPE2]: async (runtime, params) => {
|
|
690
|
+
return handleTextNano(runtime, params);
|
|
691
|
+
},
|
|
692
|
+
[TEXT_MEDIUM_MODEL_TYPE2]: async (runtime, params) => {
|
|
693
|
+
return handleTextMedium(runtime, params);
|
|
694
|
+
},
|
|
695
|
+
[TEXT_SMALL_MODEL_TYPE2]: async (runtime, params) => {
|
|
508
696
|
return handleTextSmall(runtime, params);
|
|
509
697
|
},
|
|
510
|
-
[
|
|
698
|
+
[TEXT_LARGE_MODEL_TYPE2]: async (runtime, params) => {
|
|
511
699
|
return handleTextLarge(runtime, params);
|
|
512
700
|
},
|
|
513
|
-
[
|
|
701
|
+
[TEXT_MEGA_MODEL_TYPE2]: async (runtime, params) => {
|
|
702
|
+
return handleTextMega(runtime, params);
|
|
703
|
+
},
|
|
704
|
+
[RESPONSE_HANDLER_MODEL_TYPE2]: async (runtime, params) => {
|
|
705
|
+
return handleResponseHandler(runtime, params);
|
|
706
|
+
},
|
|
707
|
+
[ACTION_PLANNER_MODEL_TYPE2]: async (runtime, params) => {
|
|
708
|
+
return handleActionPlanner(runtime, params);
|
|
709
|
+
},
|
|
710
|
+
[TEXT_EMBEDDING_MODEL_TYPE2]: async (runtime, params) => {
|
|
514
711
|
return handleTextEmbedding(runtime, params);
|
|
515
712
|
},
|
|
516
|
-
[
|
|
713
|
+
[IMAGE_DESCRIPTION_MODEL_TYPE]: async (runtime, params) => {
|
|
517
714
|
return handleImageDescription(runtime, params);
|
|
518
715
|
},
|
|
519
|
-
[
|
|
716
|
+
[OBJECT_SMALL_MODEL_TYPE]: async (runtime, params) => {
|
|
520
717
|
return handleObjectSmall(runtime, params);
|
|
521
718
|
},
|
|
522
|
-
[
|
|
719
|
+
[OBJECT_LARGE_MODEL_TYPE]: async (runtime, params) => {
|
|
523
720
|
return handleObjectLarge(runtime, params);
|
|
524
721
|
}
|
|
525
722
|
},
|
|
526
723
|
tests: pluginTests
|
|
527
724
|
};
|
|
528
|
-
var
|
|
725
|
+
var plugin_google_genai_default = googleGenAIPlugin;
|
|
726
|
+
|
|
727
|
+
// index.browser.ts
|
|
728
|
+
var index_browser_default = plugin_google_genai_default;
|
|
529
729
|
export {
|
|
530
730
|
googleGenAIPlugin,
|
|
531
|
-
|
|
731
|
+
index_browser_default as default
|
|
532
732
|
};
|
|
533
733
|
|
|
534
|
-
//# debugId=
|
|
734
|
+
//# debugId=DB1428E48B045C3E64756E2164756E21
|