forked from elizaOS/eliza
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.ts
648 lines (606 loc) · 22.3 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
import { createOpenAI } from '@ai-sdk/openai';
import type {
ImageDescriptionParams,
ModelTypeName,
ObjectGenerationParams,
Plugin,
TextEmbeddingParams,
} from '@elizaos/core';
import {
type DetokenizeTextParams,
type GenerateTextParams,
ModelType,
type TokenizeTextParams,
logger,
} from '@elizaos/core';
import { generateObject, generateText } from 'ai';
import { type TiktokenModel, encodingForModel } from 'js-tiktoken';
import { z } from 'zod';
/**
* Asynchronously tokenizes the given text based on the specified model and prompt.
*
* @param {ModelTypeName} model - The type of model to use for tokenization.
* @param {string} prompt - The text prompt to tokenize.
* @returns {number[]} - An array of tokens representing the encoded prompt.
*/
async function tokenizeText(model: ModelTypeName, prompt: string) {
const modelName =
model === ModelType.TEXT_SMALL
? (process.env.OPENAI_SMALL_MODEL ?? process.env.SMALL_MODEL ?? 'gpt-4o-mini')
: (process.env.LARGE_MODEL ?? 'gpt-4o');
const encoding = encodingForModel(modelName as TiktokenModel);
const tokens = encoding.encode(prompt);
return tokens;
}
/**
* Detokenize a sequence of tokens back into text using the specified model.
*
* @param {ModelTypeName} model - The type of model to use for detokenization.
* @param {number[]} tokens - The sequence of tokens to detokenize.
* @returns {string} The detokenized text.
*/
async function detokenizeText(model: ModelTypeName, tokens: number[]) {
const modelName =
model === ModelType.TEXT_SMALL
? (process.env.OPENAI_SMALL_MODEL ?? process.env.SMALL_MODEL ?? 'gpt-4o-mini')
: (process.env.OPENAI_LARGE_MODEL ?? process.env.LARGE_MODEL ?? 'gpt-4o');
const encoding = encodingForModel(modelName as TiktokenModel);
return encoding.decode(tokens);
}
/**
* Defines the OpenAI plugin with its name, description, and configuration options.
* @type {Plugin}
*/
export const openaiPlugin: Plugin = {
name: 'openai',
description: 'OpenAI plugin',
config: {
OPENAI_API_KEY: process.env.OPENAI_API_KEY,
OPENAI_BASE_URL: process.env.OPENAI_BASE_URL,
OPENAI_SMALL_MODEL: process.env.OPENAI_SMALL_MODEL,
OPENAI_LARGE_MODEL: process.env.OPENAI_LARGE_MODEL,
SMALL_MODEL: process.env.SMALL_MODEL,
LARGE_MODEL: process.env.LARGE_MODEL,
},
async init(config: Record<string, string>) {
try {
// const validatedConfig = await configSchema.parseAsync(config);
// // Set all environment variables at once
// for (const [key, value] of Object.entries(validatedConfig)) {
// if (value) process.env[key] = value;
// }
// If API key is not set, we'll show a warning but continue
if (!process.env.OPENAI_API_KEY) {
logger.warn(
'OPENAI_API_KEY is not set in environment - OpenAI functionality will be limited'
);
// Return early without throwing an error
return;
}
// Verify API key only if we have one
try {
const baseURL = process.env.OPENAI_BASE_URL ?? 'https://api.openai.com/v1';
const response = await fetch(`${baseURL}/models`, {
headers: { Authorization: `Bearer ${process.env.OPENAI_API_KEY}` },
});
if (!response.ok) {
logger.warn(`OpenAI API key validation failed: ${response.statusText}`);
logger.warn('OpenAI functionality will be limited until a valid API key is provided');
// Continue execution instead of throwing
} else {
// logger.log("OpenAI API key validated successfully");
}
} catch (fetchError) {
logger.warn(`Error validating OpenAI API key: ${fetchError}`);
logger.warn('OpenAI functionality will be limited until a valid API key is provided');
// Continue execution instead of throwing
}
} catch (error) {
// Convert to warning instead of error
logger.warn(
`OpenAI plugin configuration issue: ${error.errors
.map((e) => e.message)
.join(', ')} - You need to configure the OPENAI_API_KEY in your environment variables`
);
}
},
models: {
[ModelType.TEXT_EMBEDDING]: async (
_runtime,
params: TextEmbeddingParams | string | null
): Promise<number[]> => {
// Handle null input (initialization case)
if (params === null) {
logger.debug('Creating test embedding for initialization');
// Return a consistent vector for null input
const testVector = Array(1536).fill(0);
testVector[0] = 0.1; // Make it non-zero
return testVector;
}
// Get the text from whatever format was provided
let text: string;
if (typeof params === 'string') {
text = params; // Direct string input
} else if (typeof params === 'object' && params.text) {
text = params.text; // Object with text property
} else {
logger.warn('Invalid input format for embedding');
// Return a fallback for invalid input
const fallbackVector = Array(1536).fill(0);
fallbackVector[0] = 0.2; // Different value for tracking
return fallbackVector;
}
// Skip API call for empty text
if (!text.trim()) {
logger.warn('Empty text for embedding');
const emptyVector = Array(1536).fill(0);
emptyVector[0] = 0.3; // Different value for tracking
return emptyVector;
}
try {
const baseURL = process.env.OPENAI_BASE_URL ?? 'https://api.openai.com/v1';
// Call the OpenAI API
const response = await fetch(`${baseURL}/embeddings`, {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'text-embedding-3-small',
input: text,
}),
});
if (!response.ok) {
logger.error(`OpenAI API error: ${response.status} - ${response.statusText}`);
const errorVector = Array(1536).fill(0);
errorVector[0] = 0.4; // Different value for tracking
return errorVector;
}
const data = (await response.json()) as {
data: [{ embedding: number[] }];
};
if (!data?.data?.[0]?.embedding) {
logger.error('API returned invalid structure');
const errorVector = Array(1536).fill(0);
errorVector[0] = 0.5; // Different value for tracking
return errorVector;
}
const embedding = data.data[0].embedding;
logger.log(`Got valid embedding with length ${embedding.length}`);
return embedding;
} catch (error) {
logger.error('Error generating embedding:', error);
const errorVector = Array(1536).fill(0);
errorVector[0] = 0.6; // Different value for tracking
return errorVector;
}
},
[ModelType.TEXT_TOKENIZER_ENCODE]: async (
_runtime,
{ prompt, modelType = ModelType.TEXT_LARGE }: TokenizeTextParams
) => {
return await tokenizeText(modelType ?? ModelType.TEXT_LARGE, prompt);
},
[ModelType.TEXT_TOKENIZER_DECODE]: async (
_runtime,
{ tokens, modelType = ModelType.TEXT_LARGE }: DetokenizeTextParams
) => {
return await detokenizeText(modelType ?? ModelType.TEXT_LARGE, tokens);
},
[ModelType.TEXT_SMALL]: async (runtime, { prompt, stopSequences = [] }: GenerateTextParams) => {
const temperature = 0.7;
const frequency_penalty = 0.7;
const presence_penalty = 0.7;
const max_response_length = 8192;
const baseURL = runtime.getSetting('OPENAI_BASE_URL') ?? 'https://api.openai.com/v1';
const openai = createOpenAI({
apiKey: runtime.getSetting('OPENAI_API_KEY'),
baseURL,
});
const model =
runtime.getSetting('OPENAI_SMALL_MODEL') ??
runtime.getSetting('SMALL_MODEL') ??
'gpt-4o-mini';
logger.log('generating text');
logger.log(prompt);
const { text: openaiResponse } = await generateText({
model: openai.languageModel(model),
prompt: prompt,
system: runtime.character.system ?? undefined,
temperature: temperature,
maxTokens: max_response_length,
frequencyPenalty: frequency_penalty,
presencePenalty: presence_penalty,
stopSequences: stopSequences,
});
return openaiResponse;
},
[ModelType.TEXT_LARGE]: async (
runtime,
{
prompt,
stopSequences = [],
maxTokens = 8192,
temperature = 0.7,
frequencyPenalty = 0.7,
presencePenalty = 0.7,
}: GenerateTextParams
) => {
const baseURL = runtime.getSetting('OPENAI_BASE_URL') ?? 'https://api.openai.com/v1';
const openai = createOpenAI({
apiKey: runtime.getSetting('OPENAI_API_KEY'),
baseURL,
});
const model =
runtime.getSetting('OPENAI_LARGE_MODEL') ?? runtime.getSetting('LARGE_MODEL') ?? 'gpt-4o';
const { text: openaiResponse } = await generateText({
model: openai.languageModel(model),
prompt: prompt,
system: runtime.character.system ?? undefined,
temperature: temperature,
maxTokens: maxTokens,
frequencyPenalty: frequencyPenalty,
presencePenalty: presencePenalty,
stopSequences: stopSequences,
});
return openaiResponse;
},
[ModelType.IMAGE]: async (
runtime,
params: {
prompt: string;
n?: number;
size?: string;
}
) => {
const baseURL = runtime.getSetting('OPENAI_BASE_URL') ?? 'https://api.openai.com/v1';
const response = await fetch(`${baseURL}/images/generations`, {
method: 'POST',
headers: {
Authorization: `Bearer ${runtime.getSetting('OPENAI_API_KEY')}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
prompt: params.prompt,
n: params.n || 1,
size: params.size || '1024x1024',
}),
});
if (!response.ok) {
throw new Error(`Failed to generate image: ${response.statusText}`);
}
const data = await response.json();
const typedData = data as { data: { url: string }[] };
return typedData.data;
},
[ModelType.IMAGE_DESCRIPTION]: async (runtime, params: ImageDescriptionParams | string) => {
// Handle string case (direct URL)
let imageUrl: string;
let prompt: string | undefined;
if (typeof params === 'string') {
imageUrl = params;
prompt = undefined;
} else {
// Object parameter case
imageUrl = params.imageUrl;
prompt = params.prompt;
}
try {
const baseURL = process.env.OPENAI_BASE_URL ?? 'https://api.openai.com/v1';
const apiKey = process.env.OPENAI_API_KEY;
if (!apiKey) {
logger.error('OpenAI API key not set');
return {
title: 'Failed to analyze image',
description: 'API key not configured',
};
}
// Call the GPT-4 Vision API
const response = await fetch(`${baseURL}/chat/completions`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${apiKey}`,
},
body: JSON.stringify({
model: 'gpt-4-vision-preview',
messages: [
{
role: 'user',
content: [
{
type: 'text',
text:
prompt ||
'Please analyze this image and provide a title and detailed description.',
},
{
type: 'image_url',
image_url: { url: imageUrl },
},
],
},
],
max_tokens: 300,
}),
});
if (!response.ok) {
throw new Error(`OpenAI API error: ${response.status}`);
}
const result: any = await response.json();
const content = result.choices?.[0]?.message?.content;
if (!content) {
return {
title: 'Failed to analyze image',
description: 'No response from API',
};
}
// Extract title and description
const titleMatch = content.match(/title[:\s]+(.+?)(?:\n|$)/i);
const title = titleMatch?.[1] || 'Image Analysis';
// Rest of content is the description
const description = content.replace(/title[:\s]+(.+?)(?:\n|$)/i, '').trim();
return { title, description };
} catch (error) {
logger.error('Error analyzing image:', error);
return {
title: 'Failed to analyze image',
description: `Error: ${error instanceof Error ? error.message : String(error)}`,
};
}
},
[ModelType.TRANSCRIPTION]: async (runtime, audioBuffer: Buffer) => {
logger.log('audioBuffer', audioBuffer);
const baseURL = runtime.getSetting('OPENAI_BASE_URL') ?? 'https://api.openai.com/v1';
const formData = new FormData();
formData.append('file', new File([audioBuffer], 'recording.mp3', { type: 'audio/mp3' }));
formData.append('model', 'whisper-1');
const response = await fetch(`${baseURL}/audio/transcriptions`, {
method: 'POST',
headers: {
Authorization: `Bearer ${runtime.getSetting('OPENAI_API_KEY')}`,
// Note: Do not set a Content-Type header—letting fetch set it for FormData is best
},
body: formData,
});
logger.log('response', response);
if (!response.ok) {
throw new Error(`Failed to transcribe audio: ${response.statusText}`);
}
const data = (await response.json()) as { text: string };
return data.text;
},
[ModelType.OBJECT_SMALL]: async (runtime, params: ObjectGenerationParams) => {
const baseURL = runtime.getSetting('OPENAI_BASE_URL') ?? 'https://api.openai.com/v1';
const openai = createOpenAI({
apiKey: runtime.getSetting('OPENAI_API_KEY'),
baseURL,
});
const model =
runtime.getSetting('OPENAI_SMALL_MODEL') ??
runtime.getSetting('SMALL_MODEL') ??
'gpt-4o-mini';
try {
if (params.schema) {
// Skip zod validation and just use the generateObject without schema
logger.info('Using OBJECT_SMALL without schema validation');
const { object } = await generateObject({
model: openai.languageModel(model),
output: 'no-schema',
prompt: params.prompt,
temperature: params.temperature,
});
return object;
}
const { object } = await generateObject({
model: openai.languageModel(model),
output: 'no-schema',
prompt: params.prompt,
temperature: params.temperature,
});
return object;
} catch (error) {
logger.error('Error generating object:', error);
throw error;
}
},
[ModelType.OBJECT_LARGE]: async (runtime, params: ObjectGenerationParams) => {
const baseURL = runtime.getSetting('OPENAI_BASE_URL') ?? 'https://api.openai.com/v1';
const openai = createOpenAI({
apiKey: runtime.getSetting('OPENAI_API_KEY'),
baseURL,
});
const model =
runtime.getSetting('OPENAI_LARGE_MODEL') ?? runtime.getSetting('LARGE_MODEL') ?? 'gpt-4o';
try {
if (params.schema) {
// Skip zod validation and just use the generateObject without schema
logger.info('Using OBJECT_LARGE without schema validation');
const { object } = await generateObject({
model: openai.languageModel(model),
output: 'no-schema',
prompt: params.prompt,
temperature: params.temperature,
});
return object;
}
const { object } = await generateObject({
model: openai.languageModel(model),
output: 'no-schema',
prompt: params.prompt,
temperature: params.temperature,
});
return object;
} catch (error) {
logger.error('Error generating object:', error);
throw error;
}
},
},
tests: [
{
name: 'openai_plugin_tests',
tests: [
{
name: 'openai_test_url_and_api_key_validation',
fn: async (runtime) => {
const baseURL = runtime.getSetting('OPENAI_BASE_URL') ?? 'https://api.openai.com/v1';
const response = await fetch(`${baseURL}/models`, {
headers: {
Authorization: `Bearer ${runtime.getSetting('OPENAI_API_KEY')}`,
},
});
const data = await response.json();
logger.log('Models Available:', (data as any)?.data.length);
if (!response.ok) {
throw new Error(`Failed to validate OpenAI API key: ${response.statusText}`);
}
},
},
{
name: 'openai_test_text_embedding',
fn: async (runtime) => {
try {
const embedding = await runtime.useModel(ModelType.TEXT_EMBEDDING, {
text: 'Hello, world!',
});
logger.log('embedding', embedding);
} catch (error) {
logger.error('Error in test_text_embedding:', error);
throw error;
}
},
},
{
name: 'openai_test_text_large',
fn: async (runtime) => {
try {
const text = await runtime.useModel(ModelType.TEXT_LARGE, {
prompt: 'What is the nature of reality in 10 words?',
});
if (text.length === 0) {
throw new Error('Failed to generate text');
}
logger.log('generated with test_text_large:', text);
} catch (error) {
logger.error('Error in test_text_large:', error);
throw error;
}
},
},
{
name: 'openai_test_text_small',
fn: async (runtime) => {
try {
const text = await runtime.useModel(ModelType.TEXT_SMALL, {
prompt: 'What is the nature of reality in 10 words?',
});
if (text.length === 0) {
throw new Error('Failed to generate text');
}
logger.log('generated with test_text_small:', text);
} catch (error) {
logger.error('Error in test_text_small:', error);
throw error;
}
},
},
{
name: 'openai_test_image_generation',
fn: async (runtime) => {
logger.log('openai_test_image_generation');
try {
const image = await runtime.useModel(ModelType.IMAGE, {
prompt: 'A beautiful sunset over a calm ocean',
n: 1,
size: '1024x1024',
});
logger.log('generated with test_image_generation:', image);
} catch (error) {
logger.error('Error in test_image_generation:', error);
throw error;
}
},
},
{
name: 'image-description',
fn: async (runtime) => {
try {
logger.log('openai_test_image_description');
try {
const result = await runtime.useModel(
ModelType.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'
);
// Check if result has the expected structure
if (
result &&
typeof result === 'object' &&
'title' in result &&
'description' in result
) {
logger.log('Image description:', result);
} else {
logger.error('Invalid image description result format:', result);
}
} catch (e) {
logger.error('Error in image description test:', e);
}
} catch (e) {
logger.error('Error in openai_test_image_description:', e);
}
},
},
{
name: 'openai_test_transcription',
fn: async (runtime) => {
logger.log('openai_test_transcription');
try {
const response = await fetch(
'https://upload.wikimedia.org/wikipedia/en/4/40/Chris_Benoit_Voice_Message.ogg'
);
const arrayBuffer = await response.arrayBuffer();
const transcription = await runtime.useModel(
ModelType.TRANSCRIPTION,
Buffer.from(new Uint8Array(arrayBuffer))
);
logger.log('generated with test_transcription:', transcription);
} catch (error) {
logger.error('Error in test_transcription:', error);
throw error;
}
},
},
{
name: 'openai_test_text_tokenizer_encode',
fn: async (runtime) => {
const prompt = 'Hello tokenizer encode!';
const tokens = await runtime.useModel(ModelType.TEXT_TOKENIZER_ENCODE, { prompt });
if (!Array.isArray(tokens) || tokens.length === 0) {
throw new Error('Failed to tokenize text: expected non-empty array of tokens');
}
logger.log('Tokenized output:', tokens);
},
},
{
name: 'openai_test_text_tokenizer_decode',
fn: async (runtime) => {
const prompt = 'Hello tokenizer decode!';
// Encode the string into tokens first
const tokens = await runtime.useModel(ModelType.TEXT_TOKENIZER_ENCODE, { prompt });
// Now decode tokens back into text
const decodedText = await runtime.useModel(ModelType.TEXT_TOKENIZER_DECODE, { tokens });
if (decodedText !== prompt) {
throw new Error(
`Decoded text does not match original. Expected "${prompt}", got "${decodedText}"`
);
}
logger.log('Decoded text:', decodedText);
},
},
],
},
],
};
export default openaiPlugin;