This example demonstrates how to use the ImageGenerator utility within AgentiPy to create images using DALL·E. It leverages Agentipy's SolanaAgentKit for authentication (by holding the OpenAI API key) and asynchronous handling of the image generation request.
Key Features of the ImageGenerator Tool
Secure Key Management: Automatically uses the openai_api_key stored in a SolanaAgentKit instance.
Async-First API: All operations use the OpenAI AsyncOpenAI client for non-blocking calls.
Flexible Models & Sizes: Defaults to DALL·E 2 with support for 256×256, 512×512, and 1024×1024; easily switch to DALL·E 3 by adjusting the model parameter.
Robust Error Handling: Throws descriptive errors if the API key is missing or if generation fails.
Code (gen_img.py)
import osimport asynciofrom agentipy.tools.create_image import ImageGeneratorfrom agentipy.agent import SolanaAgentKitasyncdefmain(): api_key ="sk-proj----"#api_key = os.getenv("OPENAI_API_KEY")ifnot api_key:raiseValueError("Please set the OPENAI_API_KEY environment variable.") agent =SolanaAgentKit(openai_api_key=api_key) prompt = "A cartoon green python snake wearing tiny glasses, typing Python code on a glowing laptop. The screen shows Solana blockchain code (e.g., 'async def transaction()') with Solana logo (purple S-shaped vortex) floating nearby. Snake’s tail forms a blockchain symbol (🔗) and a tiny SOL coin. Background: clean digital workspace with abstract crypto nodes. Style: friendly, minimalist 2D flat design with mint green and purple accents." size ="1024x1024" num_images =1try: response =await ImageGenerator.create_image(agent, prompt,size=size,n=num_images) images = response.get("images",[])for i, url inenumerate(images,start=1):print(f"Image {i}: {url}")exceptExceptionas err:print(f"Failed to generate images: {err}")if__name__=="__main__": asyncio.run(main())