-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
Copy pathindex.ts
358 lines (313 loc) · 13.9 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
import { elizaLogger, generateText } from "@elizaos/core";
import {
Action,
HandlerCallback,
IAgentRuntime,
Memory,
Plugin,
State,
ModelClass
} from "@elizaos/core";
import { generateImage } from "@elizaos/core";
import fs from "fs";
import path from "path";
import { validateImageGenConfig } from "./environment";
export function saveBase64Image(base64Data: string, filename: string): string {
// Create generatedImages directory if it doesn't exist
const imageDir = path.join(process.cwd(), "generatedImages");
if (!fs.existsSync(imageDir)) {
fs.mkdirSync(imageDir, { recursive: true });
}
// Remove the data:image/png;base64 prefix if it exists
const base64Image = base64Data.replace(/^data:image\/\w+;base64,/, "");
// Create a buffer from the base64 string
const imageBuffer = Buffer.from(base64Image, "base64");
// Create full file path
const filepath = path.join(imageDir, `${filename}.png`);
// Save the file
fs.writeFileSync(filepath, imageBuffer);
return filepath;
}
export async function saveHeuristImage(
imageUrl: string,
filename: string
): Promise<string> {
const imageDir = path.join(process.cwd(), "generatedImages");
if (!fs.existsSync(imageDir)) {
fs.mkdirSync(imageDir, { recursive: true });
}
// Fetch image from URL
const response = await fetch(imageUrl);
if (!response.ok) {
throw new Error(`Failed to fetch image: ${response.statusText}`);
}
const arrayBuffer = await response.arrayBuffer();
const imageBuffer = Buffer.from(arrayBuffer);
// Create full file path
const filepath = path.join(imageDir, `${filename}.png`);
// Save the file
fs.writeFileSync(filepath, imageBuffer);
return filepath;
}
const imageGeneration: Action = {
name: "GENERATE_IMAGE",
similes: [
"IMAGE_GENERATION",
"IMAGE_GEN",
"CREATE_IMAGE",
"MAKE_PICTURE",
"GENERATE_IMAGE",
"GENERATE_A",
"DRAW",
"DRAW_A",
"MAKE_A",
],
description: "Generate an image to go along with the message.",
suppressInitialMessage: true,
validate: async (runtime: IAgentRuntime, _message: Memory) => {
await validateImageGenConfig(runtime);
const anthropicApiKeyOk = !!runtime.getSetting("ANTHROPIC_API_KEY");
const togetherApiKeyOk = !!runtime.getSetting("TOGETHER_API_KEY");
const heuristApiKeyOk = !!runtime.getSetting("HEURIST_API_KEY");
const falApiKeyOk = !!runtime.getSetting("FAL_API_KEY");
const openAiApiKeyOk = !!runtime.getSetting("OPENAI_API_KEY");
const veniceApiKeyOk = !!runtime.getSetting("VENICE_API_KEY");
const livepeerGatewayUrlOk = !!runtime.getSetting("LIVEPEER_GATEWAY_URL");
return (
anthropicApiKeyOk ||
togetherApiKeyOk ||
heuristApiKeyOk ||
falApiKeyOk ||
openAiApiKeyOk ||
veniceApiKeyOk ||
livepeerGatewayUrlOk
);
},
handler: async (
runtime: IAgentRuntime,
message: Memory,
state: State,
options: {
width?: number;
height?: number;
count?: number;
negativePrompt?: string;
numIterations?: number;
guidanceScale?: number;
seed?: number;
modelId?: string;
jobId?: string;
stylePreset?: string;
hideWatermark?: boolean;
},
callback: HandlerCallback
) => {
elizaLogger.log("Composing state for message:", message);
state = (await runtime.composeState(message)) as State;
const userId = runtime.agentId;
elizaLogger.log("User ID:", userId);
const CONTENT = message.content.text;
const IMAGE_SYSTEM_PROMPT = `You are an expert in writing prompts for AI art generation. You excel at creating detailed and creative visual descriptions. Incorporating specific elements naturally. Always aim for clear, descriptive language that generates a creative picture. Your output should only contain the description of the image contents, but NOT an instruction like "create an image that..."`;
const STYLE = "futuristic with vibrant colors";
const IMAGE_PROMPT_INPUT = `You are tasked with generating an image prompt based on a content and a specified style.
Your goal is to create a detailed and vivid image prompt that captures the essence of the content while incorporating an appropriate subject based on your analysis of the content.\n\nYou will be given the following inputs:\n<content>\n${CONTENT}\n</content>\n\n<style>\n${STYLE}\n</style>\n\nA good image prompt consists of the following elements:\n\n
1. Main subject
2. Detailed description
3. Style
4. Lighting
5. Composition
6. Quality modifiers
To generate the image prompt, follow these steps:\n\n1. Analyze the content text carefully, identifying key themes, emotions, and visual elements mentioned or implied.
\n\n
2. Determine the most appropriate main subject by:
- Identifying concrete objects or persons mentioned in the content
- Analyzing the central theme or message
- Considering metaphorical representations of abstract concepts
- Selecting a subject that best captures the content's essence
3. Determine an appropriate environment or setting based on the content's context and your chosen subject.
4. Decide on suitable lighting that enhances the mood or atmosphere of the scene.
5. Choose a color palette that reflects the content's tone and complements the subject.
6. Identify the overall mood or emotion conveyed by the content.
7. Plan a composition that effectively showcases the subject and captures the content's essence.
8. Incorporate the specified style into your description, considering how it affects the overall look and feel of the image.
9. Use concrete nouns and avoid abstract concepts when describing the main subject and elements of the scene.
Construct your image prompt using the following structure:\n\n
1. Main subject: Describe the primary focus of the image based on your analysis
2. Environment: Detail the setting or background
3. Lighting: Specify the type and quality of light in the scene
4. Colors: Mention the key colors and their relationships
5. Mood: Convey the overall emotional tone
6. Composition: Describe how elements are arranged in the frame
7. Style: Incorporate the given style into the description
Ensure that your prompt is detailed, vivid, and incorporates all the elements mentioned above while staying true to the content and the specified style. LIMIT the image prompt 50 words or less. \n\nWrite a prompt. Only include the prompt and nothing else.`;
const imagePrompt = await generateText({
runtime,
context: IMAGE_PROMPT_INPUT,
modelClass: ModelClass.MEDIUM,
customSystemPrompt: IMAGE_SYSTEM_PROMPT,
});
elizaLogger.log("Image prompt received:", imagePrompt);
const imageSettings = runtime.character?.settings?.imageSettings || {};
elizaLogger.log("Image settings:", imageSettings);
const res: { image: string; caption: string }[] = [];
elizaLogger.log("Generating image with prompt:", imagePrompt);
const images = await generateImage(
{
prompt: imagePrompt,
width: options.width || imageSettings.width || 1024,
height: options.height || imageSettings.height || 1024,
...(options.count != null || imageSettings.count != null ? { count: options.count || imageSettings.count || 1 } : {}),
...(options.negativePrompt != null || imageSettings.negativePrompt != null ? { negativePrompt: options.negativePrompt || imageSettings.negativePrompt } : {}),
...(options.numIterations != null || imageSettings.numIterations != null ? { numIterations: options.numIterations || imageSettings.numIterations } : {}),
...(options.guidanceScale != null || imageSettings.guidanceScale != null ? { guidanceScale: options.guidanceScale || imageSettings.guidanceScale } : {}),
...(options.seed != null || imageSettings.seed != null ? { seed: options.seed || imageSettings.seed } : {}),
...(options.modelId != null || imageSettings.modelId != null ? { modelId: options.modelId || imageSettings.modelId } : {}),
...(options.jobId != null || imageSettings.jobId != null ? { jobId: options.jobId || imageSettings.jobId } : {}),
...(options.stylePreset != null || imageSettings.stylePreset != null ? { stylePreset: options.stylePreset || imageSettings.stylePreset } : {}),
...(options.hideWatermark != null || imageSettings.hideWatermark != null ? { hideWatermark: options.hideWatermark || imageSettings.hideWatermark } : {}),
},
runtime
);
if (images.success && images.data && images.data.length > 0) {
elizaLogger.log(
"Image generation successful, number of images:",
images.data.length
);
for (let i = 0; i < images.data.length; i++) {
const image = images.data[i];
// Save the image and get filepath
const filename = `generated_${Date.now()}_${i}`;
// Choose save function based on image data format
const filepath = image.startsWith("http")
? await saveHeuristImage(image, filename)
: saveBase64Image(image, filename);
elizaLogger.log(`Processing image ${i + 1}:`, filename);
//just dont even add a caption or a description just have it generate & send
/*
try {
const imageService = runtime.getService(ServiceType.IMAGE_DESCRIPTION);
if (imageService && typeof imageService.describeImage === 'function') {
const caption = await imageService.describeImage({ imageUrl: filepath });
captionText = caption.description;
captionTitle = caption.title;
}
} catch (error) {
elizaLogger.error("Caption generation failed, using default caption:", error);
}*/
const _caption = "...";
/*= await generateCaption(
{
imageUrl: image,
},
runtime
);*/
res.push({ image: filepath, caption: "..." }); //caption.title });
elizaLogger.log(
`Generated caption for image ${i + 1}:`,
"..." //caption.title
);
//res.push({ image: image, caption: caption.title });
callback(
{
text: "...", //caption.description,
attachments: [
{
id: crypto.randomUUID(),
url: filepath,
title: "Generated image",
source: "imageGeneration",
description: "...", //caption.title,
text: "...", //caption.description,
contentType: "image/png",
},
],
},
[
{
attachment: filepath,
name: `${filename}.png`,
},
]
);
}
} else {
elizaLogger.error("Image generation failed or returned no data.");
}
},
examples: [
// TODO: We want to generate images in more abstract ways, not just when asked to generate an image
[
{
user: "{{user1}}",
content: { text: "Generate an image of a cat" },
},
{
user: "{{agentName}}",
content: {
text: "Here's an image of a cat",
action: "GENERATE_IMAGE",
},
},
],
[
{
user: "{{user1}}",
content: { text: "Generate an image of a dog" },
},
{
user: "{{agentName}}",
content: {
text: "Here's an image of a dog",
action: "GENERATE_IMAGE",
},
},
],
[
{
user: "{{user1}}",
content: { text: "Create an image of a cat with a hat" },
},
{
user: "{{agentName}}",
content: {
text: "Here's an image of a cat with a hat",
action: "GENERATE_IMAGE",
},
},
],
[
{
user: "{{user1}}",
content: { text: "Make an image of a dog with a hat" },
},
{
user: "{{agentName}}",
content: {
text: "Here's an image of a dog with a hat",
action: "GENERATE_IMAGE",
},
},
],
[
{
user: "{{user1}}",
content: { text: "Paint an image of a cat with a hat" },
},
{
user: "{{agentName}}",
content: {
text: "Here's an image of a cat with a hat",
action: "GENERATE_IMAGE",
},
},
],
],
} as Action;
export const imageGenerationPlugin: Plugin = {
name: "imageGeneration",
description: "Generate images",
actions: [imageGeneration],
evaluators: [],
providers: [],
};
export default imageGenerationPlugin;