NAME

AI::Image - Generate images using OpenAI's DALL-E

VERSION

Version 0.1

SYNOPSIS

use AI::Image;

my $ai = AI::Image->new(
    'key'   => 'sk-......',
);

my $image_url = $ai->image("A photorealistic image of a cat wearing a top hat and monocle.");

print $image_url;

DESCRIPTION

This module provides a simple interface to generate images using OpenAI's DALL-E API.

API KEYS

A free OpenAI API can be obtained from https://platform.openai.com/account/api-keys

MODELS

Although the API Key is free, each use incurs a cost. This is dependent on the model chosen and the size. The 'dall-e-3' model produces better images but at a higher cost. Likewise, bigger images cost more. The default model dall-e-2 with the default size of 512x512 produces resonable results at a low cost and is a good place to start using this module.

See also https://platform.openai.com/docs/models/overview

METHODS

new

my $ai = AI::Image->new(%params);

Creates a new AI::Image object.

Parameters

key

required Your OpenAI API key.

api

The API to use (currently only 'OpenAI' is supported).

model

The language model to use (default: 'dall-e-2').

See https://platform.openai.com/docs/models/overview

size

The size for the generated image (default: '512x512').

debug

Used for testing. If set to any true value, the image method will return details of the error encountered instead of undef

image

my $url = $ai->image($prompt);

Generates an image based on the provided prompt and returns the URL of the generated image. The URL is valid for 1 hour.

Parameters

prompt

The textual description of the desired image.

success

my $success = $ai->success();

Returns true if the last operation was successful.

error

my $error = $ai->error();

Returns the error message if the last operation failed.

EXAMPLE

It is common that the generated image will want to be saved as a file. This can be easily acheived using the getstore method of LWP::Simple.

use strict;
use warnings;

use LWP::Simple;
use AI::Image;

my $ai = AI::Image->new(
    'key'   => 'sk-......',
);

my $image_url = $ai->image("A dog reading a newspaper");

getstore( $image_url, 'my_ai_image.png' );

SEE ALSO

https://openai.com - OpenAI official website

AUTHOR

Ian Boddison <ian at boddison.com>

BUGS

Please report any bugs or feature requests to bug-ai-image at rt.cpan.org, or through the web interface at https://rt.cpan.org/NoAuth/ReportBug.html?Queue=bug-ai-image. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc AI::Image

You can also look for information at:

COPYRIGHT AND LICENSE

Copyright (C) 2024 by Ian Boddison

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.