blib/lib/Langertha/Role/Embedding.pm

filter: Statementstmt 100% 67 / 67 Branchbran 86.3% 19 / 22 Conditioncond 54.5% 6 / 11 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 100% 12 / 12 Podpod 100% 5 / 5 totaltotal 89.4% 110 / 123 CC 17 SCAR 13.6
CC
17
Cov
92%
_build_embedding_model
7.3
simple_embedding
4.1
simple_embedding_f
4.1
CRAP 3.9
« blib/lib/Langertha/Chat.pm ↑ Summary blib/lib/Langertha/Role/HermesTools.pm »
1 package Langertha::Role::Embedding;
2 # ABSTRACT: Role for APIs with embedding functionality
3 153 our $VERSION = '0.503';
4 153 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 153 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 153 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 153 ▶use Log::Any qw( $log );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 153 ▶use Time::HiRes qw( gettimeofday tv_interval );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 153 ▶use Langertha::CallResult;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # simple_embedding_f sends through the engine's async backend (k292): injected
12 # client > Net::Async::HTTP > the sync LWP shim (ADR 0027).
13 153 with 'Langertha::Role::AsyncHTTP';
14  
15 153 requires qw(
16 embedding_request
17 embedding_response
18 );
19  
20 153 has embedding_model => (
21 is => 'ro',
22 isa => 'Maybe[Str]',
23 lazy_build => 1,
24 );
25 79 ▶sub _build_embedding_model {
Subroutine _build_embedding_model: called CC=7 SCAR=19.9
26 79 my ( $self ) = @_;
27 79 ▶ croak "".(ref $self)." can't handle models!" unless $self->does('Langertha::Role::Models');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->does("Langertha::Role::Models") 0 79 79
28 79 ▶ return $self->model unless $self->can('default_embedding_model');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can("default_embedding_model") 0 79 79
29 79 my $default = $self->default_embedding_model;
30 79 ▶ return $default if defined $default;
Branch: 1 branch
BranchTrueFalseTotal
if defined $default 55 24 79
31 # No fixed embedding model (self-hosted servers, k297): the caller's model,
32 # never the engine's own placeholder default_model ('default' 404s on older
33 # vLLM); undef leaves the model field out and the server picks.
34 24 my $model = $self->model;
35 24 ▶ return undef unless defined $model;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $model 0 24 24
36 24 ▶ return undef if $self->can('default_model') && $model eq $self->default_model;
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("default_model") and $model eq $self->default_model 18 6 24
Condition66%2/3
$self->can("default_model") and $model eq $self->default_model
!ll&&!rl&&r
0618
MC/DC50%1/2
$self->can("default_model") and $model eq $self->default_model
$self->can("default_model") $model eq $self->default_model
Truth table66%2/3
A and B
A= $self->can("default_model")B= $model eq $self->default_model
ABresult
0X0
100
111
37 6 return $model;
38 }
39  
40  
41 153 has embedding_dimensions => (
42 is => 'ro',
43 isa => 'Maybe[Int]',
44 );
45  
46  
47 93 ▶sub embedding {
Subroutine embedding: called CC=1 SCAR=0.0
Pod documented
48 93 my ( $self, $text ) = @_;
49 93 return $self->embedding_request($text);
50 }
51  
52  
53 3 ▶sub simple_embedding {
Subroutine simple_embedding: called CC=4 SCAR=14.0
Pod documented
54 3 my ( $self, $text ) = @_;
55 $log->debugf("[%s] simple_embedding, model=%s, %s",
56 ref $self, $self->embedding_model // 'default',
57 3 ▶ ref $text eq 'ARRAY' ? 'inputs='.scalar(@{$text}) : 'input_length='.length($text // ''));
Branch: 1 branch
BranchTrueFalseTotal
ref $text eq 'ARRAY' ? : 1 2 3
Condition50%2/4
$self->embedding_model // "default"
l!l
30
$text // ""
l!l
20
MC/DC0%0/1
$self->embedding_model // "default"
$self->embedding_model
MC/DC0%0/1
$text // ""
$text
Truth table50%1/2
A
A= $self->embedding_model
Aresult
11
00
Truth table50%1/2
A
A= $text
Aresult
11
00
58 3 my $request = $self->embedding($text);
59 3 my $response = $self->user_agent->request($request);
60 3 return $request->response_call->($response);
61 }
62  
63  
64 15 ▶async sub simple_embedding_f {
Subroutine simple_embedding_f: called CC=4 SCAR=14.0
Pod documented
65 15 my ( $self, $text ) = @_;
66 $log->debugf("[%s] simple_embedding_f, model=%s, %s",
67 ref $self, $self->embedding_model // 'default',
68 15 ▶ ref $text eq 'ARRAY' ? 'inputs='.scalar(@{$text}) : 'input_length='.length($text // ''));
Branch: 1 branch
BranchTrueFalseTotal
ref $text eq 'ARRAY' ? : 4 11 15
Condition50%2/4
$self->embedding_model // "default"
l!l
150
$text // ""
l!l
110
MC/DC0%0/1
$self->embedding_model // "default"
$self->embedding_model
MC/DC0%0/1
$text // ""
$text
Truth table50%1/2
A
A= $self->embedding_model
Aresult
11
00
Truth table50%1/2
A
A= $text
Aresult
11
00
69 15 my $request = $self->embedding($text);
70 15 my $response = await $self->_async_do_request_f( request => $request );
71 14 return $request->response_call->($response);
72 153 }
73  
74  
75 3 ▶sub simple_embedding_result {
Subroutine simple_embedding_result: called CC=3 SCAR=11.0
Pod documented
76 3 my ( $self, $text, %extra ) = @_;
77 3 ▶ my $model = exists $extra{model} ? $extra{model} : $self->embedding_model;
Branch: 1 branch
BranchTrueFalseTotal
exists $extra{'model'} ? : 1 2 3
78 3 ▶ my $request = %extra ? $self->embedding_request($text, %extra) : $self->embedding($text);
Branch: 1 branch
BranchTrueFalseTotal
%extra ? : 1 2 3
79 3 my $t0 = [gettimeofday];
80 3 my $response = $self->user_agent->request($request);
81 3 my $elapsed = tv_interval($t0);
82 3 my $value = $request->response_call->($response);
83 2 return Langertha::CallResult->from_http_response( $self, $response,
84 value => $value, model => $model, total_seconds => $elapsed );
85 }
86  
87  
88 13 ▶async sub simple_embedding_result_f {
Subroutine simple_embedding_result_f: called CC=3 SCAR=11.0
Pod documented
89 13 my ( $self, $text, %extra ) = @_;
90 13 ▶ my $model = exists $extra{model} ? $extra{model} : $self->embedding_model;
Branch: 1 branch
BranchTrueFalseTotal
exists $extra{'model'} ? : 2 11 13
91 13 ▶ my $request = %extra ? $self->embedding_request($text, %extra) : $self->embedding($text);
Branch: 1 branch
BranchTrueFalseTotal
%extra ? : 2 11 13
92 13 my $t0 = [gettimeofday];
93 13 my $response = await $self->_async_do_request_f( request => $request );
94 13 my $elapsed = tv_interval($t0);
95 13 my $value = $request->response_call->($response);
96 10 return Langertha::CallResult->from_http_response( $self, $response,
97 value => $value, model => $model, total_seconds => $elapsed );
98 153 }
99  
100  
101  
102 153 1;
103  
104 __END__
105  
106 =pod
107  
108 =encoding UTF-8
109  
110 =head1 NAME
111  
112 Langertha::Role::Embedding - Role for APIs with embedding functionality
113  
114 =head1 VERSION
115  
116 version 0.503
117  
118 =head2 embedding_model
119  
120 The model name to use for embedding requests. Lazily defaults to
121 C<default_embedding_model> if the engine provides it, otherwise falls back
122 to the general C<model> attribute from L<Langertha::Role::Models>.
123  
124 An engine whose C<default_embedding_model> returns C<undef> (the self-hosted
125 vLLM, LlamaCpp and LM Studio servers) has no fixed embedding model: it uses
126 the C<model> you set, and without one sends no C<model> field, so the
127 server embeds with the model it serves.
128  
129 =head2 embedding_dimensions
130  
131 Optional size of the returned vectors, for models that can shorten them
132 (OpenAI C<text-embedding-3-*>, C<gemini-embedding-001>, Mistral
133 C<codestral-embed>, matryoshka models on vLLM / SGLang). How it reaches the
134 wire:
135  
136 =over 4
137  
138 =item * C<dimensions> — L<Langertha::Engine::OpenAI>,
139 L<Langertha::Engine::OllamaOpenAI>, L<Langertha::Engine::vLLM>,
140 L<Langertha::Engine::VLLMHook>, L<Langertha::Engine::SGLang>, and top-level
141 on the native C</api/embed> of L<Langertha::Engine::Ollama>.
142  
143 =item * C<output_dimension> — L<Langertha::Engine::Mistral>, for
144 C<codestral-embed*> models only.
145  
146 =item * C<embedContentConfig.outputDimensionality> —
147 L<Langertha::Engine::Gemini>.
148  
149 =item * not sent, with one C<carp> per engine instance — Mistral's other
150 embedding models (C<mistral-embed> included), L<Langertha::Engine::Scaleway>,
151 L<Langertha::Engine::LlamaCpp>, L<Langertha::Engine::LMStudioOpenAI> and
152 L<Langertha::Engine::TSystems>; each engine's POD says why.
153  
154 =back
155  
156 A matching extra passed to C<embedding_request> wins over it, and an
157 explicit C<dimensions> extra is always sent untouched. Unset (the default),
158 nothing is sent and the model answers in its native size.
159  
160 =head2 embedding
161  
162 my $request = $engine->embedding($text);
163  
164 Builds and returns an embedding HTTP request object for the given C<$text>.
165 Use L</simple_embedding> to execute the request and get the result directly.
166  
167 =head2 simple_embedding
168  
169 my $vector = $engine->simple_embedding($text);
170 my $vectors = $engine->simple_embedding([ $text_a, $text_b ]);
171  
172 Sends an embedding request for C<$text> and returns the embedding vector
173 (an ArrayRef of floats). An ArrayRef of strings is sent as one batch
174 request and returns an ArrayRef of vectors, one per input and in input
175 order. Blocks until the request completes. L</simple_embedding_f> is the
176 non-blocking variant.
177  
178 =head2 simple_embedding_f
179  
180 my $vector = await $engine->simple_embedding_f($text);
181 my $vectors = await $engine->simple_embedding_f([ $text_a, $text_b ]);
182  
183 Async variant of L</simple_embedding>: returns a L<Future> that resolves to
184 the same value (a vector, or an ArrayRef of vectors for an ArrayRef input)
185 and fails with the same error text. The request goes through the engine's
186 async backend (L<Langertha::Role::AsyncHTTP>), so
187 L<Langertha::Role::HTTP/user_agent_timeout> bounds it on
188 L<Net::Async::HTTP> too; without that module it runs synchronously over LWP.
189  
190 =head2 simple_embedding_result
191  
192 my $result = $engine->simple_embedding_result($text);
193 my $vector = $result->value;
194 say $result->usage->input_tokens if $result->has_usage;
195  
196 my $large = $engine->simple_embedding_result($text, model => 'text-embedding-3-large');
197  
198 Like L</simple_embedding>, but returns a L<Langertha::CallResult>: the same
199 vector (or ArrayRef of vectors for a batch) as C<value>, plus the provider's
200 C<usage>, this response's C<rate_limit>, the answering C<model> and the
201 measured C<total_seconds>. Croaks like L</simple_embedding>. Optional
202 C<%extra> goes to C<embedding_request> (e.g. a C<model> override, which is
203 then also the requested model of the result); L<Langertha::Embedder> uses it
204 for its model override.
205  
206 =head2 simple_embedding_result_f
207  
208 my $result = await $engine->simple_embedding_result_f(\@texts);
209  
210 Async variant of L</simple_embedding_result>, sent like
211 L</simple_embedding_f>: resolves to the L<Langertha::CallResult> and fails
212 with the same error text.
213  
214 =head1 SEE ALSO
215  
216 =over
217  
218 =item * L<Langertha::Role::HTTP> - HTTP transport layer
219  
220 =item * L<Langertha::Role::Models> - Model selection (provides C<embedding_model>)
221  
222 =back
223  
224 =head1 SUPPORT
225  
226 =head2 Issues
227  
228 Please report bugs and feature requests on GitHub at
229 L<https://github.com/Getty/langertha/issues>.
230  
231 =head2 IRC
232  
233 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
234  
235 =head1 CONTRIBUTING
236  
237 Contributions are welcome! Please fork the repository and submit a pull request.
238  
239 =head1 AUTHOR
240  
241 Torsten Raudssus <getty@cpan.org>
242  
243 =head1 COPYRIGHT AND LICENSE
244  
245 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
246  
247 This is free software; you can redistribute it and/or modify it under
248 the same terms as the Perl 5 programming language system itself.
249  
250 =cut
« blib/lib/Langertha/Chat.pm ↑ Summary blib/lib/Langertha/Role/HermesTools.pm »