| 1 | package Langertha::ImageGen; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Image generation abstraction wrapping an engine with optional overrides | ||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | our $VERSION = '0.502'; | |||||||||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Log::Any qw( $log ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 2 | with 'Langertha::Role::PluginHost'; | |||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 2 | has engine => ( | |||||||||||||||||||||||||||||||||||||||||
| 13 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 14 | required => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 15 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 2 | has model => ( | |||||||||||||||||||||||||||||||||||||||||
| 18 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 19 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||
| 20 | predicate => 'has_model', | ||||||||||||||||||||||||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 2 | has size => ( | |||||||||||||||||||||||||||||||||||||||||
| 24 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 25 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||
| 26 | predicate => 'has_size', | ||||||||||||||||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | 2 | has quality => ( | |||||||||||||||||||||||||||||||||||||||||
| 30 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 31 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||
| 32 | predicate => 'has_quality', | ||||||||||||||||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | sub _extra { | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 5 | ▶ | my ( $self ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_extra: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 39 | 5 | ▶ | ($self->has_model ? (model => $self->model) : ()), | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | ($self->has_size ? (size => $self->size) : ()), | ||||||||||||||||||||||||||||||||||||||||||
| 41 | ($self->has_quality ? (quality => $self->quality) : ()), | ||||||||||||||||||||||||||||||||||||||||||
| 42 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | sub _assert_image_engine { | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 9 | ▶ | my ( $self ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_assert_image_engine: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 9 | my $engine = $self->engine; | |||||||||||||||||||||||||||||||||||||||||
| 48 | 9 | ▶ | croak ref($engine) . " does not support image generation" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | unless $engine->does('Langertha::Role::ImageGeneration'); | ||||||||||||||||||||||||||||||||||||||||||
| 50 | 8 | return $engine; | |||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | # --- Plugin hook runners (async) --- | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 8 | ▶ | async sub _run_plugin_before_image_gen { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_run_plugin_before_image_gen: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 8 | my ( $self, $prompt ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 57 | 8 | for my $plugin (@{$self->_plugin_instances}) { | |||||||||||||||||||||||||||||||||||||||||
| 58 | 3 | ▶ | if ($plugin->can('plugin_before_image_gen')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 3 | $prompt = await $plugin->plugin_before_image_gen($prompt); | |||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||
| 62 | 8 | return $prompt; | |||||||||||||||||||||||||||||||||||||||||
| 63 | 2 | } | |||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 8 | ▶ | async sub _run_plugin_after_image_gen { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_run_plugin_after_image_gen: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 8 | my ( $self, $prompt, $result ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 67 | 8 | for my $plugin (@{$self->_plugin_instances}) { | |||||||||||||||||||||||||||||||||||||||||
| 68 | 3 | ▶ | if ($plugin->can('plugin_after_image_gen')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 2 | $result = await $plugin->plugin_after_image_gen($prompt, $result); | |||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 8 | return $result; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 2 | } | |||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | sub simple_image { | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 9 | ▶ | my ( $self, $prompt ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
simple_image: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 9 | ▶ | $log->debugf("[ImageGen] simple_image via %s, model=%s", | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | ref $self->engine, $self->has_model ? $self->model : 'default'); | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 9 | my $engine = $self->_assert_image_engine; | |||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 8 | $prompt = $self->_run_plugin_before_image_gen($prompt)->get; | |||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 8 | my $result; | |||||||||||||||||||||||||||||||||||||||||
| 84 | 8 | ▶ | if ($self->has_model || $self->has_size || $self->has_quality) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$self->has_model or $self->has_size or $self->has_quality
$self->has_model or $self->has_size
MC/DC66%2/3
$self->has_model or $self->has_size or $self->has_quality
$self->has_model
$self->has_size
$self->has_quality
Truth table75%3/4
A or B or C
A= $self->has_modelB= $self->has_sizeC= $self->has_quality
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 4 | my $request = $engine->image_request($prompt, $self->_extra); | |||||||||||||||||||||||||||||||||||||||||
| 86 | 4 | my $response = $engine->user_agent->request($request); | |||||||||||||||||||||||||||||||||||||||||
| 87 | 4 | $result = $request->response_call->($response); | |||||||||||||||||||||||||||||||||||||||||
| 88 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 4 | $result = $engine->simple_image($prompt); | |||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 8 | $result = $self->_run_plugin_after_image_gen($prompt, $result)->get; | |||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 8 | return $result; | |||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 2 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | __END__ | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | Langertha::ImageGen - Image generation abstraction wrapping an engine with optional overrides | ||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | version 0.502 | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | use Langertha::Engine::OpenAI; | ||||||||||||||||||||||||||||||||||||||||||
| 120 | use Langertha::ImageGen; | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | my $engine = Langertha::Engine::OpenAI->new( | ||||||||||||||||||||||||||||||||||||||||||
| 123 | api_key => $ENV{OPENAI_API_KEY}, | ||||||||||||||||||||||||||||||||||||||||||
| 124 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | my $image_gen = Langertha::ImageGen->new( | ||||||||||||||||||||||||||||||||||||||||||
| 127 | engine => $engine, | ||||||||||||||||||||||||||||||||||||||||||
| 128 | model => 'dall-e-3', | ||||||||||||||||||||||||||||||||||||||||||
| 129 | size => '1024x1024', | ||||||||||||||||||||||||||||||||||||||||||
| 130 | quality => 'hd', | ||||||||||||||||||||||||||||||||||||||||||
| 131 | plugins => ['Langfuse'], | ||||||||||||||||||||||||||||||||||||||||||
| 132 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | my $result = $image_gen->simple_image('A cat riding a bicycle through Paris'); | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | C<Langertha::ImageGen> wraps any engine that consumes | ||||||||||||||||||||||||||||||||||||||||||
| 139 | L<Langertha::Role::ImageGeneration> and adds optional overrides for | ||||||||||||||||||||||||||||||||||||||||||
| 140 | model, size, and quality, plus plugin lifecycle hooks via | ||||||||||||||||||||||||||||||||||||||||||
| 141 | L<Langertha::Role::PluginHost>. | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | Use this class when you need multiple image generation configurations | ||||||||||||||||||||||||||||||||||||||||||
| 144 | from the same engine instance, or when you want plugin observability | ||||||||||||||||||||||||||||||||||||||||||
| 145 | (e.g. L<Langertha::Plugin::Langfuse>) without modifying the engine. | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | =head2 engine | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | The LLM engine to delegate image generation requests to. Must consume | ||||||||||||||||||||||||||||||||||||||||||
| 150 | L<Langertha::Role::ImageGeneration>. | ||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | =head2 model | ||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | Optional model name override. When set, overrides the engine's | ||||||||||||||||||||||||||||||||||||||||||
| 155 | C<image_model> via C<%extra> pass-through. | ||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | =head2 size | ||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | Optional image size (e.g. C<'1024x1024'>, C<'1792x1024'>). | ||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | =head2 quality | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | Optional quality setting (e.g. C<'standard'>, C<'hd'>). | ||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | =head2 simple_image | ||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | my $result = $image_gen->simple_image('A cat in space'); | ||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | Returns the image generation result for C<$prompt>. If C<model>, | ||||||||||||||||||||||||||||||||||||||||||
| 170 | C<size>, or C<quality> overrides are set, uses them via C<%extra>; | ||||||||||||||||||||||||||||||||||||||||||
| 171 | otherwise delegates to the engine's C<simple_image>. Plugin hooks | ||||||||||||||||||||||||||||||||||||||||||
| 172 | C<plugin_before_image_gen> and C<plugin_after_image_gen> are fired. | ||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | =item * L<Langertha::Role::PluginHost> - Plugin system consumed by this class | ||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | =item * L<Langertha::Role::ImageGeneration> - Image generation role required by the engine | ||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | =item * L<Langertha::Plugin::Langfuse> - Observability plugin for image generation calls | ||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | =item * L<Langertha::Chat> - Chat counterpart to this class | ||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | =item * L<Langertha::Embedder> - Embedding counterpart to this class | ||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | =head2 Issues | ||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||
| 194 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||||||||||||||||
| 195 | L<https://github.com/Getty/langertha/issues>. | ||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | =head2 IRC | ||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||
| 201 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||
| 203 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||
| 205 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||
| 207 | Torsten Raudssus <getty@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||
| 209 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||
| 211 | This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>. | ||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||
| 213 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||
| 214 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||
| 216 | =cut | ||||||||||||||||||||||||||||||||||||||||||