| 1 | package Langertha::Role::ImageGeneration; | ||||||||||
| 2 | # ABSTRACT: Role for engines that support image generation | ||||||||||
| 3 | 104 | our $VERSION = '0.503'; | |||||||||
| 4 | 104 | ▶ | use Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 104 | ▶ | use Future::AsyncAwait; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 104 | ▶ | use Carp qw( croak ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 104 | ▶ | use Time::HiRes qw( gettimeofday tv_interval ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 104 | ▶ | use Langertha::CallResult; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | # simple_image_f sends through the engine's async backend (k292): injected | ||||||||||
| 11 | # client > Net::Async::HTTP > the sync LWP shim (ADR 0027). | ||||||||||
| 12 | 104 | with 'Langertha::Role::AsyncHTTP'; | |||||||||
| 13 | |||||||||||
| 14 | |||||||||||
| 15 | 104 | requires 'image_request'; | |||||||||
| 16 | 104 | requires 'simple_image'; | |||||||||
| 17 | |||||||||||
| 18 | 104 | has image_model => ( | |||||||||
| 19 | is => 'ro', | ||||||||||
| 20 | isa => 'Maybe[Str]', | ||||||||||
| 21 | lazy_build => 1, | ||||||||||
| 22 | ); | ||||||||||
| 23 | 23 | ▶ | sub _build_image_model { | ||||||||
|
Subroutine
_build_image_model: called CC=3 SCAR=12.5
| |||||||||||
| 24 | 23 | my ( $self ) = @_; | |||||||||
| 25 | 23 | ▶ | croak "".(ref $self)." can't handle models!" unless $self->does('Langertha::Role::Models'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | 23 | ▶ | return $self->default_image_model if $self->can('default_image_model'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | 0 | return $self->model; | |||||||||
| 28 | } | ||||||||||
| 29 | |||||||||||
| 30 | |||||||||||
| 31 | 9 | ▶ | async sub simple_image_f { | ||||||||
|
Subroutine
simple_image_f: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 32 | 9 | my ( $self, $prompt, %extra ) = @_; | |||||||||
| 33 | 9 | my $request = $self->image_request($prompt, %extra); | |||||||||
| 34 | 9 | my $response = await $self->_async_do_request_f( request => $request ); | |||||||||
| 35 | 8 | return $request->response_call->($response); | |||||||||
| 36 | 104 | } | |||||||||
| 37 | |||||||||||
| 38 | |||||||||||
| 39 | 2 | ▶ | sub simple_image_result { | ||||||||
|
Subroutine
simple_image_result: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 40 | 2 | my ( $self, $prompt, %extra ) = @_; | |||||||||
| 41 | 2 | ▶ | my $model = exists $extra{model} ? $extra{model} : $self->image_model; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | 2 | my $request = $self->image_request($prompt, %extra); | |||||||||
| 43 | 2 | my $t0 = [gettimeofday]; | |||||||||
| 44 | 2 | my $response = $self->user_agent->request($request); | |||||||||
| 45 | 2 | my $elapsed = tv_interval($t0); | |||||||||
| 46 | 2 | my $value = $request->response_call->($response); | |||||||||
| 47 | 2 | return Langertha::CallResult->from_http_response( $self, $response, | |||||||||
| 48 | value => $value, model => $model, total_seconds => $elapsed ); | ||||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | |||||||||||
| 52 | 7 | ▶ | async sub simple_image_result_f { | ||||||||
|
Subroutine
simple_image_result_f: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 53 | 7 | my ( $self, $prompt, %extra ) = @_; | |||||||||
| 54 | 7 | ▶ | my $model = exists $extra{model} ? $extra{model} : $self->image_model; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | 7 | my $request = $self->image_request($prompt, %extra); | |||||||||
| 56 | 7 | my $t0 = [gettimeofday]; | |||||||||
| 57 | 7 | my $response = await $self->_async_do_request_f( request => $request ); | |||||||||
| 58 | 7 | my $elapsed = tv_interval($t0); | |||||||||
| 59 | 7 | my $value = $request->response_call->($response); | |||||||||
| 60 | 6 | return Langertha::CallResult->from_http_response( $self, $response, | |||||||||
| 61 | value => $value, model => $model, total_seconds => $elapsed ); | ||||||||||
| 62 | 104 | } | |||||||||
| 63 | |||||||||||
| 64 | |||||||||||
| 65 | |||||||||||
| 66 | 104 | 1; | |||||||||
| 67 | |||||||||||
| 68 | __END__ | ||||||||||
| 69 | |||||||||||
| 70 | =pod | ||||||||||
| 71 | |||||||||||
| 72 | =encoding UTF-8 | ||||||||||
| 73 | |||||||||||
| 74 | =head1 NAME | ||||||||||
| 75 | |||||||||||
| 76 | Langertha::Role::ImageGeneration - Role for engines that support image generation | ||||||||||
| 77 | |||||||||||
| 78 | =head1 VERSION | ||||||||||
| 79 | |||||||||||
| 80 | version 0.503 | ||||||||||
| 81 | |||||||||||
| 82 | =head1 DESCRIPTION | ||||||||||
| 83 | |||||||||||
| 84 | Engines that can generate images consume this role. It requires | ||||||||||
| 85 | C<image_request> and C<simple_image> methods, and provides an | ||||||||||
| 86 | C<image_model> attribute, the async L</simple_image_f> and | ||||||||||
| 87 | L</simple_image_result> / L</simple_image_result_f>. | ||||||||||
| 88 | |||||||||||
| 89 | =head2 image_model | ||||||||||
| 90 | |||||||||||
| 91 | The model name to use for image generation requests. Lazily defaults to | ||||||||||
| 92 | C<default_image_model> if the engine provides it, otherwise falls back | ||||||||||
| 93 | to the general C<model> attribute from L<Langertha::Role::Models>. | ||||||||||
| 94 | |||||||||||
| 95 | =head2 simple_image_f | ||||||||||
| 96 | |||||||||||
| 97 | my $images = await $engine->simple_image_f('A cat in space', size => '1024x1024'); | ||||||||||
| 98 | |||||||||||
| 99 | Async variant of C<simple_image>: same arguments, returns a L<Future> that | ||||||||||
| 100 | resolves to the same value (for the OpenAI dialect an ArrayRef of image | ||||||||||
| 101 | objects, see L<Langertha::Role::OpenAICompatible/image_response>) and fails | ||||||||||
| 102 | with the same error text. The request goes through the engine's async | ||||||||||
| 103 | backend (L<Langertha::Role::AsyncHTTP>), so | ||||||||||
| 104 | L<Langertha::Role::HTTP/user_agent_timeout> bounds it on | ||||||||||
| 105 | L<Net::Async::HTTP> too; without that module it runs synchronously over LWP. | ||||||||||
| 106 | |||||||||||
| 107 | =head2 simple_image_result | ||||||||||
| 108 | |||||||||||
| 109 | my $result = $engine->simple_image_result('A cat in space'); | ||||||||||
| 110 | my $images = $result->value; | ||||||||||
| 111 | say $result->usage->output_tokens if $result->has_usage; | ||||||||||
| 112 | |||||||||||
| 113 | Like C<simple_image>, but returns a L<Langertha::CallResult>: the same image | ||||||||||
| 114 | objects as C<value>, plus the provider's C<usage> (GPT image models report | ||||||||||
| 115 | tokens), this response's C<rate_limit>, the model and the measured | ||||||||||
| 116 | C<total_seconds>. Croaks like C<simple_image>. | ||||||||||
| 117 | |||||||||||
| 118 | =head2 simple_image_result_f | ||||||||||
| 119 | |||||||||||
| 120 | my $result = await $engine->simple_image_result_f('A cat', size => '1024x1024'); | ||||||||||
| 121 | |||||||||||
| 122 | Async variant of L</simple_image_result>, sent like L</simple_image_f>: | ||||||||||
| 123 | resolves to the L<Langertha::CallResult> and fails with the same error text. | ||||||||||
| 124 | |||||||||||
| 125 | =head1 SEE ALSO | ||||||||||
| 126 | |||||||||||
| 127 | =over | ||||||||||
| 128 | |||||||||||
| 129 | =item * L<Langertha::ImageGen> - Wrapper class for image generation with plugin support | ||||||||||
| 130 | |||||||||||
| 131 | =item * L<Langertha::Role::Models> - Model selection role | ||||||||||
| 132 | |||||||||||
| 133 | =item * L<Langertha::Plugin::Langfuse> - Observability plugin (hooks into image gen) | ||||||||||
| 134 | |||||||||||
| 135 | =back | ||||||||||
| 136 | |||||||||||
| 137 | =head1 SUPPORT | ||||||||||
| 138 | |||||||||||
| 139 | =head2 Issues | ||||||||||
| 140 | |||||||||||
| 141 | Please report bugs and feature requests on GitHub at | ||||||||||
| 142 | L<https://github.com/Getty/langertha/issues>. | ||||||||||
| 143 | |||||||||||
| 144 | =head2 IRC | ||||||||||
| 145 | |||||||||||
| 146 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||
| 147 | |||||||||||
| 148 | =head1 CONTRIBUTING | ||||||||||
| 149 | |||||||||||
| 150 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||
| 151 | |||||||||||
| 152 | =head1 AUTHOR | ||||||||||
| 153 | |||||||||||
| 154 | Torsten Raudssus <getty@cpan.org> | ||||||||||
| 155 | |||||||||||
| 156 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 157 | |||||||||||
| 158 | This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>. | ||||||||||
| 159 | |||||||||||
| 160 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 161 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 162 | |||||||||||
| 163 | =cut | ||||||||||