blib/lib/Langertha/Embedder.pm

filter: Statementstmt 100% 88 / 88 Branchbran 90.0% 27 / 30 Conditioncond 50.0% 3 / 6 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 13 / 13 Podpod 100% 4 / 4 totaltotal 94.4% 136 / 144 CC 20 SCAR 11.5
CC
20
Cov
95%
_with_hooked_value
4.2
_run_plugin_after_embedding
4.0
simple_embedding
4.0
CRAP 3.2
« blib/lib/Langertha/ImageGen.pm ↑ Summary blib/lib/Langertha/Engine/VLLMHook.pm »
1 package Langertha::Embedder;
2 # ABSTRACT: Embedding abstraction wrapping an engine with optional model override
3 5 our $VERSION = '0.503';
4 5 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Log::Any qw( $log );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Scalar::Util qw( refaddr );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 with 'Langertha::Role::PluginHost';
11  
12  
13 5 has engine => (
14 is => 'ro',
15 required => 1,
16 );
17  
18 5 has model => (
19 is => 'ro',
20 isa => 'Str',
21 predicate => 'has_model',
22 );
23  
24  
25 15 ▶async sub _run_plugin_before_embedding {
Subroutine _run_plugin_before_embedding: called CC=3 SCAR=11.0
26 15 my ( $self, $text ) = @_;
27 15 for my $plugin (@{$self->_plugin_instances}) {
28 11 ▶ if ($plugin->can('plugin_before_embedding')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plugin->can("plugin_before_embedding")) 7 4 11
29 7 $text = await $plugin->plugin_before_embedding($text);
30 }
31 }
32 15 return $text;
33 5 }
34  
35 # $call_result (optional) is the Langertha::CallResult of the *_result path;
36 # every hook gets the same one, while the vector is piped hook to hook.
37 15 ▶async sub _run_plugin_after_embedding {
Subroutine _run_plugin_after_embedding: called CC=4 SCAR=13.9
38 15 my ( $self, $text, $vector, $call_result ) = @_;
39 15 for my $plugin (@{$self->_plugin_instances}) {
40 11 ▶ if ($plugin->can('plugin_after_embedding')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plugin->can("plugin_after_embedding")) 10 1 11
41 10 ▶ $vector = await $plugin->plugin_after_embedding($text, $vector,
Branch: 1 branch
BranchTrueFalseTotal
defined $call_result ? : 4 6 10
42 defined $call_result ? ($call_result) : ());
43 }
44 }
45 15 return $vector;
46 5 }
47  
48 4 ▶sub _assert_embedding_engine {
Subroutine _assert_embedding_engine: called CC=2 SCAR=7.0
49 4 my ( $self ) = @_;
50 4 my $engine = $self->engine;
51 4 ▶ croak ref($engine) . " does not support embeddings"
Branch: 1 branch
BranchTrueFalseTotal
unless $engine->does("Langertha::Role::Embedding") 0 4 4
52 unless $engine->does('Langertha::Role::Embedding');
53 4 return $engine;
54 }
55  
56 # The engine's CallResult when the hooks returned its own vector, else a new
57 # one carrying the hooks' vector (CallResult is immutable).
58 4 ▶sub _with_hooked_value {
Subroutine _with_hooked_value: called CC=4 SCAR=14.5
59 4 my ( $call_result, $value ) = @_;
60 4 my $old = $call_result->value;
61 4 ▶ return $call_result if ref $value && ref $old && refaddr($value) == refaddr($old);
Branch: 1 branch
BranchTrueFalseTotal
if ref $value and ref $old and builtin::refaddr($value) == builtin::refaddr($old) 3 1 4
Condition50%3/6
ref $value and ref $old and builtin::refaddr($value) == builtin::refaddr($old)
!ll&&!rl&&r
013
ref $value and ref $old
!ll&&!rl&&r
004
MC/DC33%1/3
ref $value and ref $old and builtin::refaddr($value) == builtin::refaddr($old)
ref $value ref $old builtin::refaddr($value) == builtin::refaddr($old)
Truth table50%2/4
A and B and C
A= ref $valueB= ref $oldC= builtin::refaddr($value) == builtin::refaddr($old)
ABCresult
0XX0
10X0
1100
1111
62 1 return $call_result->with_value($value);
63 }
64  
65 8 ▶sub simple_embedding {
Subroutine simple_embedding: called CC=4 SCAR=13.9
Pod documented
66 8 my ( $self, $text ) = @_;
67 8 ▶ $log->debugf("[Embedder] simple_embedding via %s, model=%s",
Branch: 1 branch
BranchTrueFalseTotal
$self->has_model ? : 3 5 8
68 ref $self->engine, $self->has_model ? $self->model : 'default');
69 8 my $engine = $self->engine;
70 8 ▶ croak ref($engine) . " does not support embeddings"
Branch: 1 branch
BranchTrueFalseTotal
unless $engine->does("Langertha::Role::Embedding") 1 7 8
71 unless $engine->does('Langertha::Role::Embedding');
72  
73 7 $text = $self->_run_plugin_before_embedding($text)->get;
74  
75 7 my $vector;
76 7 ▶ if ($self->has_model) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_model) { } 3 4 7
77 3 my $request = $engine->embedding_request($text, model => $self->model);
78 3 my $response = $engine->user_agent->request($request);
79 3 $vector = $request->response_call->($response);
80 } else {
81 4 $vector = $engine->simple_embedding($text);
82 }
83  
84 7 $vector = $self->_run_plugin_after_embedding($text, $vector)->get;
85  
86 7 return $vector;
87 }
88  
89  
90 5 ▶async sub simple_embedding_f {
Subroutine simple_embedding_f: called CC=4 SCAR=13.9
Pod documented
91 5 my ( $self, $text ) = @_;
92 5 ▶ $log->debugf("[Embedder] simple_embedding_f via %s, model=%s",
Branch: 1 branch
BranchTrueFalseTotal
$self->has_model ? : 1 4 5
93 ref $self->engine, $self->has_model ? $self->model : 'default');
94 5 my $engine = $self->engine;
95 5 ▶ croak ref($engine) . " does not support embeddings"
Branch: 1 branch
BranchTrueFalseTotal
unless $engine->does("Langertha::Role::Embedding") 1 4 5
96 unless $engine->does('Langertha::Role::Embedding');
97 4 $text = await $self->_run_plugin_before_embedding($text);
98 4 my $vector;
99 4 ▶ if ($self->has_model) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_model) { } 1 3 4
100 1 my $request = $engine->embedding_request($text, model => $self->model);
101 1 my $response = await $engine->_async_do_request_f( request => $request );
102 1 $vector = $request->response_call->($response);
103 } else {
104 3 $vector = await $engine->simple_embedding_f($text);
105 }
106 4 return await $self->_run_plugin_after_embedding($text, $vector);
107 5 }
108  
109  
110 1 ▶sub simple_embedding_result {
Subroutine simple_embedding_result: called CC=3 SCAR=11.2
Pod documented
111 1 my ( $self, $text ) = @_;
112 1 ▶ $log->debugf("[Embedder] simple_embedding_result via %s, model=%s",
Branch: 1 branch
BranchTrueFalseTotal
$self->has_model ? : 1 0 1
113 ref $self->engine, $self->has_model ? $self->model : 'default');
114 1 my $engine = $self->_assert_embedding_engine;
115 1 $text = $self->_run_plugin_before_embedding($text)->get;
116 1 ▶ my $result = $engine->simple_embedding_result($text,
Branch: 1 branch
BranchTrueFalseTotal
$self->has_model ? : 1 0 1
117 $self->has_model ? ( model => $self->model ) : ());
118 1 my $vector = $self->_run_plugin_after_embedding($text, $result->value, $result)->get;
119 1 return _with_hooked_value($result, $vector);
120 }
121  
122  
123 3 ▶async sub simple_embedding_result_f {
Subroutine simple_embedding_result_f: called CC=3 SCAR=11.0
Pod documented
124 3 my ( $self, $text ) = @_;
125 3 ▶ $log->debugf("[Embedder] simple_embedding_result_f via %s, model=%s",
Branch: 1 branch
BranchTrueFalseTotal
$self->has_model ? : 1 2 3
126 ref $self->engine, $self->has_model ? $self->model : 'default');
127 3 my $engine = $self->_assert_embedding_engine;
128 3 $text = await $self->_run_plugin_before_embedding($text);
129 3 ▶ my $result = await $engine->simple_embedding_result_f($text,
Branch: 1 branch
BranchTrueFalseTotal
$self->has_model ? : 1 2 3
130 $self->has_model ? ( model => $self->model ) : ());
131 3 my $vector = await $self->_run_plugin_after_embedding($text, $result->value, $result);
132 3 return _with_hooked_value($result, $vector);
133 5 }
134  
135  
136  
137 5 __PACKAGE__->meta->make_immutable;
138  
139 5 1;
140  
141 __END__
142  
143 =pod
144  
145 =encoding UTF-8
146  
147 =head1 NAME
148  
149 Langertha::Embedder - Embedding abstraction wrapping an engine with optional model override
150  
151 =head1 VERSION
152  
153 version 0.503
154  
155 =head1 SYNOPSIS
156  
157 use Langertha::Engine::OpenAI;
158 use Langertha::Embedder;
159  
160 my $engine = Langertha::Engine::OpenAI->new(
161 api_key => $ENV{OPENAI_API_KEY},
162 model => 'text-embedding-3-small',
163 );
164  
165 my $embedder = Langertha::Embedder->new(
166 engine => $engine,
167 plugins => ['Langfuse'],
168 );
169  
170 my $vector = $embedder->simple_embedding('Hello world');
171  
172 # Override model per-embedder
173 my $large = Langertha::Embedder->new(
174 engine => $engine,
175 model => 'text-embedding-3-large',
176 );
177  
178 =head1 DESCRIPTION
179  
180 C<Langertha::Embedder> wraps any engine that consumes
181 L<Langertha::Role::Embedding> and adds an optional model override plus
182 plugin lifecycle hooks via L<Langertha::Role::PluginHost>.
183  
184 Use this class when you need multiple embedding configurations from the
185 same engine instance, or when you want plugin observability (e.g.
186 L<Langertha::Plugin::Langfuse>) without modifying the engine.
187  
188 =head2 engine
189  
190 The LLM engine to delegate embedding requests to. Must consume
191 L<Langertha::Role::Embedding>.
192  
193 =head2 model
194  
195 Optional model name override. When set, overrides the engine's
196 C<embedding_model> for requests made through this Embedder.
197  
198 =head2 simple_embedding
199  
200 my $vector = $embedder->simple_embedding($text);
201  
202 Returns the embedding vector for C<$text>; an ArrayRef of strings is one
203 batch request and returns an ArrayRef of vectors in input order (the
204 plugin hooks see the whole batch). If C<model> is set, uses it
205 as an override; otherwise delegates directly to the engine's
206 C<simple_embedding>. Plugin hooks C<plugin_before_embedding> and
207 C<plugin_after_embedding> are fired around the request.
208  
209 =head2 simple_embedding_f
210  
211 my $vector = await $embedder->simple_embedding_f($text);
212  
213 Async variant of L</simple_embedding>: the same result and plugin hooks, with
214 the hooks awaited and the request sent through the engine's async backend
215 (see L<Langertha::Role::Embedding/simple_embedding_f>).
216  
217 =head2 simple_embedding_result
218  
219 my $result = $embedder->simple_embedding_result($text);
220 my $vector = $result->value;
221 say $result->usage->input_tokens if $result->has_usage;
222  
223 Like L</simple_embedding>, with the same model override and plugin hooks,
224 but returns the L<Langertha::CallResult> of
225 L<Langertha::Role::Embedding/simple_embedding_result>: C<value> is the vector
226 after the after-hooks, and C<usage>, C<rate_limit>, C<model> and
227 C<total_seconds> are the call's. C<plugin_after_embedding> gets that
228 C<CallResult> as an extra third argument (see L<Langertha::Plugin>). When a
229 hook returns a different vector, the result is a new C<CallResult> with that
230 value and the other attributes copied.
231  
232 =head2 simple_embedding_result_f
233  
234 my $result = await $embedder->simple_embedding_result_f($text);
235  
236 Async variant of L</simple_embedding_result>: the same result and hooks, sent
237 through the engine's async backend like L</simple_embedding_f>.
238  
239 =head1 SEE ALSO
240  
241 =over
242  
243 =item * L<Langertha::Role::PluginHost> - Plugin system consumed by this class
244  
245 =item * L<Langertha::Role::Embedding> - Embedding role required by the engine
246  
247 =item * L<Langertha::Plugin::Langfuse> - Observability plugin for embedding calls
248  
249 =item * L<Langertha::Chat> - Chat counterpart to this class
250  
251 =item * L<Langertha::ImageGen> - Image generation counterpart to this class
252  
253 =back
254  
255 =head1 SUPPORT
256  
257 =head2 Issues
258  
259 Please report bugs and feature requests on GitHub at
260 L<https://github.com/Getty/langertha/issues>.
261  
262 =head2 IRC
263  
264 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
265  
266 =head1 CONTRIBUTING
267  
268 Contributions are welcome! Please fork the repository and submit a pull request.
269  
270 =head1 AUTHOR
271  
272 Torsten Raudssus <getty@cpan.org>
273  
274 =head1 COPYRIGHT AND LICENSE
275  
276 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
277  
278 This is free software; you can redistribute it and/or modify it under
279 the same terms as the Perl 5 programming language system itself.
280  
281 =cut
« blib/lib/Langertha/ImageGen.pm ↑ Summary blib/lib/Langertha/Engine/VLLMHook.pm »