| 1 | package PDL::ML::Gateway; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use 5.016; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use warnings; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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 JSON::PP qw(encode_json decode_json); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 2 | our $VERSION = '0.012'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 2 | ▶ | sub new { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.2
Pod
undocumented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 2 | my ($class, %args) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | return bless { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 2 | ▶ | nodes => $args{nodes} || [], | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args{'nodes'} || []
MC/DC0%0/1
$args{'nodes'} || []
$args{'nodes'}
Truth table50%1/2
A
A= $args{'nodes'}
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | }, $class; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 4 | ▶ | sub add_node { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_node: called CC=7 SCAR=21.5
Pod
undocumented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 4 | my ($self, %node) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 4 | ▶ | croak 'node name and url required' unless $node{name} && $node{url}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$node{"name"} and $node{"url"}
MC/DC0%0/2
$node{"name"} and $node{"url"}
$node{"name"}
$node{"url"}
Truth table33%1/3
A and B
A= $node{"name"}B= $node{"url"}
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 4 | ▶ | $node{healthy} //= 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$node{"healthy"} //= 1
MC/DC0%0/1
$node{"healthy"} //= 1
$node{"healthy"}
Truth table50%1/2
A
A= $node{"healthy"}
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 4 | ▶ | $node{priority} //= 10; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$node{"priority"} //= 10
MC/DC0%0/1
$node{"priority"} //= 10
$node{"priority"}
Truth table50%1/2
A
A= $node{"priority"}
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 4 | ▶ | $node{models} //= []; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$node{"models"} //= []
MC/DC0%0/1
$node{"models"} //= []
$node{"models"}
Truth table50%1/2
A
A= $node{"models"}
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 4 | ▶ | $node{dtypes} //= []; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$node{"dtypes"} //= []
MC/DC0%0/1
$node{"dtypes"} //= []
$node{"dtypes"}
Truth table50%1/2
A
A= $node{"dtypes"}
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 4 | push @{ $self->{nodes} }, \%node; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 4 | return \%node; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | ▶ | sub nodes { @{ $_[0]->{nodes} } } | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
nodes: not called CC=1 SCAR=6.9
Pod
undocumented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 1 | ▶ | sub list_models { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
list_models: called CC=5 SCAR=16.1
Pod
undocumented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 1 | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 1 | my %seen; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | my @out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 1 | for my $n (@{ $self->{nodes} }) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 4 | ▶ | next unless $n->{healthy}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 4 | for my $m (@{ $n->{models} }) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 5 | ▶ | next if $seen{$m}++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 3 | push @out, { id => $m, object => 'model', owned_by => $n->{name} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 1 | return { object => 'list', data => \@out }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 4 | ▶ | sub select_node { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
select_node: called CC=10 SCAR=24.4
Pod
undocumented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 4 | my ($self, %req) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 4 | ▶ | my $model = $req{model} // ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$req{"model"} // ""
MC/DC0%0/1
$req{"model"} // ""
$req{"model"}
Truth table50%1/2
A
A= $req{"model"}
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 4 | my $dtype = $req{dtype}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | my @cands = grep { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | $_->{healthy} | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 20 | ▶ | && ( !length($model) || grep { $_ eq $model } @{ $_->{models} } ) | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
not $dtype or grep {$_ eq $dtype;} @{$$_{"dtypes"};}
MC/DC100%2/2
not $dtype or grep {$_ eq $dtype;} @{$$_{"dtypes"};}
not $dtype
grep {$_ eq $dtype;} @{$$_{"dtypes"};}
Truth table100%3/3
not(A) or B
A= not $dtypeB= grep {$_ eq $dtype;} @{$$_{"dtypes"};}
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 16 | ▶ | && ( !$dtype || grep { $_ eq $dtype } @{ $_->{dtypes} } ) | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition77%7/9
$_->{'healthy'} && (!length($model) || grep({$_ eq $model;} @{$$_{'models'};})) && (!$dtype || grep({$_ eq $dtype;} @{$$_{'dtypes'};}))
$_->{"healthy"} and !length($model) || grep({$_ eq $model;} @{$$_{"models"};})
not length $model or grep {$_ eq $model;} @{$$_{"models"};}
MC/DC50%2/4
$_->{'healthy'} && (!length($model) || grep({$_ eq $model;} @{$$_{'models'};})) && (!$dtype || grep({$_ eq $dtype;} @{$$_{'dtypes'};}))
$_->{"healthy"}
not length $model
grep {$_ eq $model;} @{$$_{"models"};}
(!$dtype || grep({$_ eq $dtype;} @{$$_{'dtypes'};}))
Truth table50%3/6
A and not(B) or C && D
A= $_->{"healthy"}B= not length $modelC= grep {$_ eq $model;} @{$$_{"models"};}D= (!$dtype || grep({$_ eq $dtype;} @{$$_{'dtypes'};}))
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 4 | } @{ $self->{nodes} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 4 | ▶ | return undef unless @cands; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | @cands = sort { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 3 | ($a->{priority} <=> $b->{priority}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 1 | ▶ | || (($b->{free_vram_gb} || 0) <=> ($a->{free_vram_gb} || 0)) | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition14%1/7
$PDL::ML::Gateway::a->{'priority'} <=> $PDL::ML::Gateway::b->{'priority'} || ($PDL::ML::Gateway::b->{'free_vram_gb'} || 0) <=> ($PDL::ML::Gateway::a->{'free_vram_gb'} || 0)
$PDL::ML::Gateway::b->{'free_vram_gb'} || 0
$PDL::ML::Gateway::a->{'free_vram_gb'} || 0
MC/DC0%0/2
$PDL::ML::Gateway::a->{'priority'} <=> $PDL::ML::Gateway::b->{'priority'} || ($PDL::ML::Gateway::b->{'free_vram_gb'} || 0) <=> ($PDL::ML::Gateway::a->{'free_vram_gb'} || 0)
$PDL::ML::Gateway::a->{'priority'} <=> $PDL::ML::Gateway::b->{'priority'}
$PDL::ML::Gateway::b->{'free_vram_gb'}
MC/DC0%0/1
$PDL::ML::Gateway::a->{'free_vram_gb'} || 0
$PDL::ML::Gateway::a->{'free_vram_gb'}
Truth table33%1/3
A || B
A= $PDL::ML::Gateway::a->{'priority'} <=> $PDL::ML::Gateway::b->{'priority'}B= $PDL::ML::Gateway::b->{'free_vram_gb'}
Truth table0%0/2
A
A= $PDL::ML::Gateway::a->{'free_vram_gb'}
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | } @cands; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 3 | return $cands[0]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | sub build_mojo_app { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
build_mojo_app: not called CC=2 SCAR=17.9
Pod
undocumented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | require Mojolicious; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | my $app = Mojolicious->new; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | my $r = $app->routes; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | my $gw = $self; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | $r->get('/health' => sub { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | my ($c) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | $c->render(json => { status => 'ok', nodes => scalar($gw->nodes) }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | $r->get('/v1/models' => sub { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | my ($c) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | $c->render(json => $gw->list_models); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | for my $ep ('/v1/chat/completions', '/v1/completions', '/v1/embeddings') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | $r->post($ep => sub { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | my ($c) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | my $body = $c->req->json || {}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$c->req->json || {}
MC/DC0%0/1
$c->req->json || {}
$c->req->json
Truth table0%0/2
A
A= $c->req->json
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | my $node = $gw->select_node(model => $body->{model}, dtype => $body->{dtype}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | return $c->render(status => 503, json => { error => { message => "No healthy backend for model '$body->{model}'" } }) | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | unless $node; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | $c->render_later; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | my $target = $node->{url} . $ep; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | $c->ua->post_p($target => json => $body)->then(sub { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | my ($tx) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | $c->res->headers->content_type($tx->result->headers->content_type || 'application/json'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$tx->result->headers->content_type || 'application/json'
MC/DC0%0/1
$tx->result->headers->content_type || 'application/json'
$tx->result->headers->content_type
Truth table0%0/2
A
A= $tx->result->headers->content_type
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | $c->render(status => $tx->result->code, data => $tx->result->body); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | })->catch(sub { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | my ($err) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | $c->render(status => 502, json => { error => { message => "Backend $node->{name} error: $err" } }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | return $app; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 2 | 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | __END__ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||