Skip to main content

Crate agentai_image

Crate agentai_image 

Source
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 URL

With 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

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: GenerateImageToolBox and ReadMediaToolBox. Without it the crate has only ImageGenerator and does not depend on agentai.
  • 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 the aws-lc-sys C library).
  • native-tls — TLS of the operating system (OpenSSL, SChannel or Security Framework). It cannot be combined with a rustls-* feature: set default-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§

provider
Image providers
storage
Storage for media: images, audio and other files, kept under an ID.

Structs§

GenerateImageToolBox
ToolBox that lets an agent generate and edit images with an ImageGenerator.
ImageGenerator
Generates and edits images with an ImageProvider.
ReadMediaToolBox
ToolBox with the tool read_media, which gives the model a stored file (an image, a document, …) by its ID, read from any MediaRead.

Enums§

ImageToolOutput
What GenerateImageToolBox sends back to the model.