# Image Generation Example

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`)

```python
import os
import asyncio

from agentipy.tools.create_image import ImageGenerator
from agentipy.agent import SolanaAgentKit

async def main():
    api_key = "sk-proj----"
    #api_key = os.getenv("OPENAI_API_KEY")
    if not api_key:
        raise ValueError("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 = 1

    try:
        response = await ImageGenerator.create_image(agent, prompt, size=size, n=num_images)
        images = response.get("images", [])

        for i, url in enumerate(images, start=1):
            print(f"Image {i}: {url}")
    except Exception as err:
        print(f"Failed to generate images: {err}")

if __name__ == "__main__":
    asyncio.run(main())
```

## Source Files

You can find the source files for this example (including `readme.md` with more details on setup, configuration, and best practices) on GitHub: <https://github.com/niceberginc/agentipy/blob/main/examples/Image_Generation/>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.agentipy.fun/examples/image-generation-example.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
