Expand description
§AgentAI Image
Image generation and editing for AgentAI: from your
code with ImageGenerator, or by an agent with the toolboxes (feature toolbox).
§Generating images
use agentai_image::ImageGenerator;
// The provider comes from the model name, the API key from `OPENAI_API_KEY`
let generator = ImageGenerator::new("openai::gpt-image-1")?.with_size("1024x1024");
let response = generator.generate("A lighthouse at dawn").await?;
// response.image is an agentai `Media`, base64 data or a URLWith with_storage every image is saved in a
MediaSave and gets an ID. The storage module (from
agentai-core) has the storage traits and MemoryStorage.
§Tools for agents
GenerateImageToolBox:generate_imageandedit_image(the latter needs a storage). The model gets the image, or only its metadata as JSON (ImageToolOutput).ReadMediaToolBox:read_mediagives the model a stored file by ID.
use agentai::Agent;
use agentai::tool::ToolBoxSet;
use agentai_image::storage::MemoryStorage;
use agentai_image::{GenerateImageToolBox, ImageGenerator, ReadMediaToolBox};
let storage = MemoryStorage::new();
let mut tools = ToolBoxSet::new();
tools.add_tool(
GenerateImageToolBox::new(ImageGenerator::new("openai::gpt-image-1")?)
.with_storage(storage.clone()),
);
tools.add_tool(ReadMediaToolBox::new(storage.clone()));
let agent = Agent::new("claude-haiku-5-5", "You are an illustrator.")?.with_toolbox(tools);
let result = agent.run::<String>("Draw a lighthouse at dawn").await?;§Providers
The image APIs live in provider: openai (OpenAI and compatible
servers) and openrouter. ImageGenerator::new picks one from the
model name (openai::gpt-image-1, openrouter::bytedance-seed/seedream-4.5) with its
settings from environment variables. provider::list_models lists the image models of a
provider, with names ready for ImageGenerator::new:
for model in agentai_image::provider::list_models("openrouter").await? {
println!("{}", model.model);
}To set the API key
or base URL in code, build the provider and pass it to ImageGenerator::from_provider:
use agentai_image::ImageGenerator;
use agentai_image::provider::openai::OpenAiImageProvider;
let provider = OpenAiImageProvider::new("gpt-image-1", "sk-...", "https://api.openai.com/v1");
let generator = ImageGenerator::from_provider(provider);Implement provider::ImageProvider to use another API.
§Features
Available features for agentai-image crate.
Features list:
toolbox(enabled by default) — Enables the agent tools:GenerateImageToolBoxandReadMediaToolBox. Without it the crate has onlyImageGeneratorand does not depend onagentai.rustls-ring(enabled by default) — TLS with rustls and the ring crypto provider, checking certificates against the OS trust store.rustls-aws-lc— TLS with rustls and the aws-lc-rs crypto provider (builds theaws-lc-sysC library).native-tls— TLS of the operating system (OpenSSL, SChannel or Security Framework). It cannot be combined with arustls-*feature: setdefault-features = false.system-proxy(enabled by default) — Reads the macOS and Windows system proxy settings.
Re-exports§
pub use provider::ImageError;pub use provider::ImageModel;pub use provider::ImageResponse;
Modules§
Structs§
- Generate
Image Tool Box ToolBoxthat lets an agent generate and edit images with anImageGenerator.- Image
Generator - Generates and edits images with an
ImageProvider. - Read
Media Tool Box ToolBoxwith the toolread_media, which gives the model a stored file (an image, a document, …) by its ID, read from anyMediaRead.
Enums§
- Image
Tool Output - What
GenerateImageToolBoxsends back to the model.