mirror of
https://github.com/d0zingcat/solana-agent-kit.git
synced 2026-05-23 23:26:45 +00:00
feat: add image support
This commit is contained in:
40
src/tools/create_image.ts
Normal file
40
src/tools/create_image.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { SolanaAgentKit } from "../index";
|
||||
import OpenAI from "openai";
|
||||
|
||||
/**
|
||||
* Generate an image using OpenAI's DALL-E
|
||||
* @param agent SolanaAgentKit instance
|
||||
* @param prompt Text description of the image to generate
|
||||
* @param size Image size ('256x256', '512x512', or '1024x1024') (default: '1024x1024')
|
||||
* @param n Number of images to generate (default: 1)
|
||||
* @returns Object containing the generated image URLs
|
||||
*/
|
||||
export async function create_image(
|
||||
agent: SolanaAgentKit,
|
||||
prompt: string,
|
||||
size: "256x256" | "512x512" | "1024x1024" = "1024x1024",
|
||||
n: number = 1
|
||||
) {
|
||||
try {
|
||||
if (!agent.openai_api_key) {
|
||||
throw new Error("OpenAI API key not found in agent configuration");
|
||||
}
|
||||
|
||||
const openai = new OpenAI({
|
||||
apiKey: agent.openai_api_key
|
||||
});
|
||||
|
||||
const response = await openai.images.generate({
|
||||
prompt,
|
||||
n,
|
||||
size,
|
||||
});
|
||||
|
||||
return {
|
||||
images: response.data.map((img) => img.url),
|
||||
};
|
||||
|
||||
} catch (error: any) {
|
||||
throw new Error(`Image generation failed: ${error.message}`);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user