Langertha/Embedder.pm

filter: Statementstmt 100% 47 / 47 Branchbran 90.0% 9 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 98.4% 64 / 65 CC 8 SCAR 10.0
CC
8
Cov
98%
simple_embedding
4.0
_run_plugin_before_embedding
3.0
_run_plugin_after_embedding
3.0
CRAP 2.7
« Langertha/Role/Capabilities.pm ↑ Summary Langertha/Engine/Anthropic.pm »
1 package Langertha::Embedder;
2 # ABSTRACT: Embedding abstraction wrapping an engine with optional model override
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  
24 7 ▶async sub _run_plugin_before_embedding {
Subroutine _run_plugin_before_embedding: called CC=3 SCAR=11.0
25 7 my ( $self, $text ) = @_;
26 7 for my $plugin (@{$self->_plugin_instances}) {
27 3 ▶ if ($plugin->can('plugin_before_embedding')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plugin->can("plugin_before_embedding")) 3 0 3
28 3 $text = await $plugin->plugin_before_embedding($text);
29 }
30 }
31 7 return $text;
32 2 }
33  
34 7 ▶async sub _run_plugin_after_embedding {
Subroutine _run_plugin_after_embedding: called CC=3 SCAR=11.0
35 7 my ( $self, $text, $vector ) = @_;
36 7 for my $plugin (@{$self->_plugin_instances}) {
37 3 ▶ if ($plugin->can('plugin_after_embedding')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plugin->can("plugin_after_embedding")) 2 1 3
38 2 $vector = await $plugin->plugin_after_embedding($text, $vector);
39 }
40 }
41 7 return $vector;
42 2 }
43  
44 sub simple_embedding {
45 8 ▶ my ( $self, $text ) = @_;
Subroutine simple_embedding: called CC=4 SCAR=13.9
Pod documented
46 8 ▶ $log->debugf("[Embedder] simple_embedding via %s, model=%s",
Branch: 1 branch
BranchTrueFalseTotal
$self->has_model ? : 3 5 8
47 ref $self->engine, $self->has_model ? $self->model : 'default');
48 8 my $engine = $self->engine;
49 8 ▶ croak ref($engine) . " does not support embeddings"
Branch: 1 branch
BranchTrueFalseTotal
unless $engine->does("Langertha::Role::Embedding") 1 7 8
50 unless $engine->does('Langertha::Role::Embedding');
51  
52 7 $text = $self->_run_plugin_before_embedding($text)->get;
53  
54 7 my $vector;
55 7 ▶ if ($self->has_model) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_model) { } 3 4 7
56 3 my $request = $engine->embedding_request($text, model => $self->model);
57 3 my $response = $engine->user_agent->request($request);
58 3 $vector = $request->response_call->($response);
59 } else {
60 4 $vector = $engine->simple_embedding($text);
61 }
62  
63 7 $vector = $self->_run_plugin_after_embedding($text, $vector)->get;
64  
65 7 return $vector;
66 }
67  
68  
69  
70 2 __PACKAGE__->meta->make_immutable;
71  
72 2 1;
73  
74 __END__
75  
76 =pod
77  
78 =encoding UTF-8
79  
80 =head1 NAME
81  
82 Langertha::Embedder - Embedding abstraction wrapping an engine with optional model override
83  
84 =head1 VERSION
85  
86 version 0.502
87  
88 =head1 SYNOPSIS
89  
90 use Langertha::Engine::OpenAI;
91 use Langertha::Embedder;
92  
93 my $engine = Langertha::Engine::OpenAI->new(
94 api_key => $ENV{OPENAI_API_KEY},
95 model => 'text-embedding-3-small',
96 );
97  
98 my $embedder = Langertha::Embedder->new(
99 engine => $engine,
100 plugins => ['Langfuse'],
101 );
102  
103 my $vector = $embedder->simple_embedding('Hello world');
104  
105 # Override model per-embedder
106 my $large = Langertha::Embedder->new(
107 engine => $engine,
108 model => 'text-embedding-3-large',
109 );
110  
111 =head1 DESCRIPTION
112  
113 C<Langertha::Embedder> wraps any engine that consumes
114 L<Langertha::Role::Embedding> and adds an optional model override plus
115 plugin lifecycle hooks via L<Langertha::Role::PluginHost>.
116  
117 Use this class when you need multiple embedding configurations from the
118 same engine instance, or when you want plugin observability (e.g.
119 L<Langertha::Plugin::Langfuse>) without modifying the engine.
120  
121 =head2 engine
122  
123 The LLM engine to delegate embedding requests to. Must consume
124 L<Langertha::Role::Embedding>.
125  
126 =head2 model
127  
128 Optional model name override. When set, overrides the engine's
129 C<embedding_model> for requests made through this Embedder.
130  
131 =head2 simple_embedding
132  
133 my $vector = $embedder->simple_embedding($text);
134  
135 Returns the embedding vector for C<$text>. If C<model> is set, uses it
136 as an override; otherwise delegates directly to the engine's
137 C<simple_embedding>. Plugin hooks C<plugin_before_embedding> and
138 C<plugin_after_embedding> are fired around the request.
139  
140 =head1 SEE ALSO
141  
142 =over
143  
144 =item * L<Langertha::Role::PluginHost> - Plugin system consumed by this class
145  
146 =item * L<Langertha::Role::Embedding> - Embedding role required by the engine
147  
148 =item * L<Langertha::Plugin::Langfuse> - Observability plugin for embedding calls
149  
150 =item * L<Langertha::Chat> - Chat counterpart to this class
151  
152 =item * L<Langertha::ImageGen> - Image generation counterpart to this class
153  
154 =back
155  
156 =head1 SUPPORT
157  
158 =head2 Issues
159  
160 Please report bugs and feature requests on GitHub at
161 L<https://github.com/Getty/langertha/issues>.
162  
163 =head2 IRC
164  
165 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
166  
167 =head1 CONTRIBUTING
168  
169 Contributions are welcome! Please fork the repository and submit a pull request.
170  
171 =head1 AUTHOR
172  
173 Torsten Raudssus <getty@cpan.org>
174  
175 =head1 COPYRIGHT AND LICENSE
176  
177 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
178  
179 This is free software; you can redistribute it and/or modify it under
180 the same terms as the Perl 5 programming language system itself.
181  
182 =cut
« Langertha/Role/Capabilities.pm ↑ Summary Langertha/Engine/Anthropic.pm »