| 1 | package Langertha::Embedder; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Embedding abstraction wrapping an engine with optional model override | ||||||||||||||||||||||||||||||||||||||||||
| 3 | 5 | our $VERSION = '0.505'; | |||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 10 | ▶ | $vector = await $plugin->plugin_after_embedding($text, $vector, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition50%3/6
ref $value and ref $old and builtin::refaddr($value) == builtin::refaddr($old)
ref $value and ref $old
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)
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||