lib/Langertha/Skeid/Protocol/Rerank.pm

filter: Statementstmt 100% 112 / 112 Branchbran 94.8% 74 / 78 Conditioncond 81.2% 52 / 64 MC/DCmcdc 63.8% 23 / 36 Subroutinesub 100% 24 / 24 Podpod 100% 11 / 11 totaltotal 91.0% 296 / 325 CC 66 SCAR 19.3
CC
66
Cov
94%
_tei_response
16.0
_body_tokens
8.0
request_problem
7.0
CRAP 6.9
« lib/Langertha/Skeid/Protocol/MCP.pm ↑ Summary lib/Langertha/Skeid/Protocol/Audio.pm »
1 package Langertha::Skeid::Protocol::Rerank;
2 113 our $VERSION = '0.004';
3 # ABSTRACT: The rerank routes -- relayed as the node speaks, translated for a TEI node
4 113 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 113 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 113 ▶use B ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 113 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 113 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 113 ▶use Langertha::Skeid::Protocol::Refusal;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 # The upstream dialects a node can be marked with (`rerank_format`), beside the default -- the
13 # shape the client speaks, relayed. Per dialect: whether its route sits at the server root
14 # rather than below /v1, and the methods that translate for it.
15 113 my %FORMAT = (
16 tei => {
17 at_server_root => 1,
18 request => '_tei_request',
19 response => '_tei_response',
20 tokens => '_tei_tokens',
21 },
22 );
23  
24  
25 66 ▶sub routes { return ('/rerank', '/v1/rerank') }
Subroutine routes: called CC=1 SCAR=0.0
Pod documented
26  
27  
28 67 ▶sub endpoint { '/v1/rerank' }
Subroutine endpoint: called CC=1 SCAR=0.0
Pod documented
29  
30  
31 60 ▶sub upstream_path { '/rerank' }
Subroutine upstream_path: called CC=1 SCAR=0.0
Pod documented
32  
33  
34 6 ▶sub formats { return sort keys %FORMAT }
Subroutine formats: called CC=1 SCAR=0.0
Pod documented
35  
36  
37 1351 ▶sub normalize_format {
Subroutine normalize_format: called CC=5 SCAR=16.1
Pod documented
38 1351 my ($class, $value) = @_;
39 1351 ▶ return '' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1330 21 1351
40 21 ▶ croak 'rerank_format must be a string' if ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 0 21 21
41 21 my $format = lc "$value";
42 21 $format =~ s/\A\s+//;
43 21 $format =~ s/\s+\z//;
44 21 ▶ return '' unless length $format;
Branch: 1 branch
BranchTrueFalseTotal
unless length $format 1 20 21
45 20 ▶ return $format if $FORMAT{$format};
Branch: 1 branch
BranchTrueFalseTotal
if $FORMAT{$format} 15 5 20
46 5 croak "unknown rerank_format '$value' (expected one of: " . join(', ', $class->formats) . ')';
47 }
48  
49  
50 62 ▶sub at_server_root {
Subroutine at_server_root: called CC=4 SCAR=13.9
Pod documented
51 62 my ($class, $format) = @_;
52 62 ▶ return ($FORMAT{$format // ''} || {})->{at_server_root} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
+($FORMAT{$format // ''} || {})->{'at_server_root'} ? : 25 37 62
Condition75%3/4
$FORMAT{$format // ''} || {}
l!l
2537
$format // ""
l!l
620
MC/DC100%1/1
$FORMAT{$format // ''} || {}
$FORMAT{$format // ''}
MC/DC0%0/1
$format // ""
$format
Truth table100%2/2
A
A= $FORMAT{$format // ''}
Aresult
11
00
Truth table50%1/2
A
A= $format
Aresult
11
00
53 }
54  
55  
56 103 ▶sub request_problem {
Subroutine request_problem: called CC=7 SCAR=19.5
Pod documented
57 103 my ($class, $body) = @_;
58 103 ▶ return 'Invalid JSON body' unless ref($body) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $body eq "HASH" 6 97 103
59 return "'model' must be a non-empty string"
60 97 ▶ unless $class->_is_string($body->{model}) && length $body->{model};
Branch: 1 branch
BranchTrueFalseTotal
unless $class->_is_string($body->{"model"}) and length $body->{"model"} 8 89 97
Condition100%3/3
$class->_is_string($body->{"model"}) and length $body->{"model"}
!ll&&!rl&&r
6289
MC/DC100%2/2
$class->_is_string($body->{"model"}) and length $body->{"model"}
$class->_is_string($body->{"model"}) length $body->{"model"}
Truth table100%3/3
A and B
A= $class->_is_string($body->{"model"})B= length $body->{"model"}
ABresult
0X0
100
111
61 89 ▶ return "'query' must be a string" unless $class->_is_string($body->{query});
Branch: 1 branch
BranchTrueFalseTotal
unless $class->_is_string($body->{"query"}) 10 79 89
62 return "'documents' must be a non-empty array"
63 79 ▶ unless ref($body->{documents}) eq 'ARRAY' && @{$body->{documents}};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $body->{"documents"} eq "ARRAY" and @{$body->{"documents"};} 9 70 79
Condition100%3/3
ref $body->{"documents"} eq "ARRAY" and @{$body->{"documents"};}
!ll&&!rl&&r
6370
MC/DC100%2/2
ref $body->{"documents"} eq "ARRAY" and @{$body->{"documents"};}
ref $body->{"documents"} eq "ARRAY" @{$body->{"documents"};}
Truth table100%3/3
A and B
A= ref $body->{"documents"} eq "ARRAY"B= @{$body->{"documents"};}
ABresult
0X0
100
111
64 70 return undef;
65 }
66  
67  
68 98 ▶sub document_count {
Subroutine document_count: called CC=3 SCAR=12.0
Pod documented
69 98 my ($class, $body) = @_;
70 98 ▶ return 0 unless ref($body) eq 'HASH' && ref($body->{documents}) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $body eq "HASH" and ref $body->{"documents"} eq "ARRAY" 0 98 98
Condition33%1/3
ref $body eq "HASH" and ref $body->{"documents"} eq "ARRAY"
!ll&&!rl&&r
0098
MC/DC0%0/2
ref $body eq "HASH" and ref $body->{"documents"} eq "ARRAY"
ref $body eq "HASH" ref $body->{"documents"} eq "ARRAY"
Truth table33%1/3
A and B
A= ref $body eq "HASH"B= ref $body->{"documents"} eq "ARRAY"
ABresult
0X0
100
111
71 98 return scalar @{$body->{documents}};
72 }
73  
74  
75 66 ▶sub request_to_upstream {
Subroutine request_to_upstream: called CC=3 SCAR=11.0
Pod documented
76 66 my ($class, $body, $served_model, $format) = @_;
77 66 ▶ my $spec = $FORMAT{$format // ''} or return { %$body, model => $served_model };
Branch: 1 branch
BranchTrueFalseTotal
my $spec = $FORMAT{$format // ""} or return {%$body, "model", $served_model} 30 36 66
Condition50%1/2
$format // ""
l!l
660
MC/DC0%0/1
$format // ""
$format
Truth table50%1/2
A
A= $format
Aresult
11
00
78 30 my $method = $spec->{request};
79 30 return $class->$method($body);
80 }
81  
82  
83 31 ▶sub response_from_upstream {
Subroutine response_from_upstream: called CC=3 SCAR=11.4
Pod documented
84 31 my ($class, $payload, $headers, $body, $served_model, $format) = @_;
85 31 ▶ my $spec = $FORMAT{$format // ''} or croak __PACKAGE__ . '->response_from_upstream needs a format';
Branch: 1 branch
BranchTrueFalseTotal
my $spec = $FORMAT{$format // ""} or croak("Langertha::Skeid::Protocol::Rerank->response_from_upstream needs a format") 31 0 31
Condition50%1/2
$format // ""
l!l
310
MC/DC0%0/1
$format // ""
$format
Truth table50%1/2
A
A= $format
Aresult
11
00
86 31 my $method = $spec->{response};
87 31 return $class->$method($payload, $headers, $body, $served_model);
88 }
89  
90  
91 72 ▶sub usage {
Subroutine usage: called CC=4 SCAR=13.9
Pod documented
92 72 my ($class, $payload, $headers, $format) = @_;
93 72 ▶ my $spec = $FORMAT{$format // ''};
Condition50%1/2
$format // ""
l!l
720
MC/DC0%0/1
$format // ""
$format
Truth table50%1/2
A
A= $format
Aresult
11
00
94 72 ▶ my $method = $spec ? $spec->{tokens} : '_body_tokens';
Branch: 1 branch
BranchTrueFalseTotal
$spec ? : 25 47 72
95 72 my $tokens = $class->$method($payload, $headers);
96 72 ▶ return undef unless defined $tokens;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tokens 17 55 72
97 55 return { prompt_tokens => $tokens, completion_tokens => 0, total_tokens => $tokens };
98 }
99  
100 # The token count in the body of an answer in the client's shape.
101 47 ▶sub _body_tokens {
Subroutine _body_tokens: called CC=8 SCAR=20.8
102 47 my ($class, $payload) = @_;
103 47 ▶ return undef unless ref($payload) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $payload eq "HASH" 2 45 47
104 45 ▶ if (ref(my $usage = $payload->{usage}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref(my $usage = $payload->{"usage"}) eq "HASH") 40 5 45
105 40 for my $key (qw( prompt_tokens total_tokens )) {
106 52 ▶ return 0 + $usage->{$key} if $class->_is_count($usage->{$key});
Branch: 1 branch
BranchTrueFalseTotal
if $class->_is_count($usage->{$key}) 32 20 52
107 }
108 }
109 13 ▶ my $tokens = ref($payload->{meta}) eq 'HASH' ? $payload->{meta}{tokens} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $payload->{'meta'} eq 'HASH' ? : 3 10 13
110 return 0 + $tokens->{input_tokens}
111 13 ▶ if ref($tokens) eq 'HASH' && $class->_is_count($tokens->{input_tokens});
Branch: 1 branch
BranchTrueFalseTotal
if ref $tokens eq "HASH" and $class->_is_count($tokens->{"input_tokens"}) 2 11 13
Condition66%2/3
ref $tokens eq "HASH" and $class->_is_count($tokens->{"input_tokens"})
!ll&&!rl&&r
1102
MC/DC50%1/2
ref $tokens eq "HASH" and $class->_is_count($tokens->{"input_tokens"})
ref $tokens eq "HASH" $class->_is_count($tokens->{"input_tokens"})
Truth table66%2/3
A and B
A= ref $tokens eq "HASH"B= $class->_is_count($tokens->{"input_tokens"})
ABresult
0X0
100
111
112 11 return undef;
113 }
114  
115 #### TEI
116  
117 30 ▶sub _tei_request {
Subroutine _tei_request: called CC=7 SCAR=19.5
118 30 my ($class, $body) = @_;
119 30 my $documents = $body->{documents};
120 30 my @texts;
121 30 for my $index (0 .. $#$documents) {
122 86 my $document = $documents->[$index];
123 86 ▶ my $text = ref($document) eq 'HASH' ? $document->{text} : $document;
Branch: 1 branch
BranchTrueFalseTotal
ref $document eq 'HASH' ? : 3 83 86
124 86 ▶ Langertha::Skeid::Protocol::Refusal->refuse(
Branch: 1 branch
BranchTrueFalseTotal
unless $class->_is_string($text) 4 82 86
125 "documents[$index] is not text: this model takes a string or an object with a string 'text'")
126 unless $class->_is_string($text);
127 82 push @texts, $text;
128 }
129 26 $class->_top_n($body);
130 return {
131 query => $body->{query},
132 texts => \@texts,
133 (exists $body->{return_documents} ? (return_text => ($body->{return_documents} ? \1 : \0)) : ()),
134 24 ▶ (exists $body->{truncate} ? (truncate => $body->{truncate}) : ()),
Branch: 3 branches
BranchTrueFalseTotal
exists $body->{'return_documents'} ? : 3 21 24
$body->{'return_documents'} ? : 2 1 3
exists $body->{'truncate'} ? : 1 23 24
135 };
136 }
137  
138 31 ▶sub _tei_response {
Subroutine _tei_response: called CC=16 SCAR=27.8
139 31 my ($class, $payload, $headers, $body, $served_model) = @_;
140 31 ▶ die "The node's answer is not an array\n" unless ref($payload) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $payload eq "ARRAY" 3 28 31
141 28 my $count = $class->document_count($body);
142 28 ▶ my $with_documents = $body->{return_documents} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$body->{'return_documents'} ? : 9 19 28
143  
144 28 my @results;
145 28 for my $row (@$payload) {
146 71 ▶ die "A result is not an object\n" unless ref($row) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $row eq "HASH" 1 70 71
147 70 my ($index, $score) = @{$row}{qw( index score )};
148 70 ▶ die "A result has no usable index or score\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $class->_is_count($index) and $index < $count and defined $score and not ref $score and &looks_like_number($score) 4 66 70
Condition91%11/12
$class->_is_count($index) and $index < $count and defined $score and not ref $score and &looks_like_number($score)
!ll&&!rl&&r
3166
$class->_is_count($index) and $index < $count and defined $score and not ref $score
!ll&&!rl&&r
3067
$class->_is_count($index) and $index < $count and defined $score
!ll&&!rl&&r
2167
$class->_is_count($index) and $index < $count
!ll&&!rl&&r
1168
MC/DC80%4/5
$class->_is_count($index) and $index < $count and defined $score and not ref $score and &looks_like_number($score)
$class->_is_count($index) $index < $count defined $score not ref $score &looks_like_number($score)
Truth table83%5/6
A and B and C and not(D) and E
A= $class->_is_count($index)B= $index < $countC= defined $scoreD= not ref $scoreE= &looks_like_number($score)
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
149 unless $class->_is_count($index) && $index < $count
150 && defined($score) && !ref($score) && looks_like_number($score);
151 push @results, {
152 index => 0 + $index,
153 relevance_score => 0 + $score,
154 ($with_documents && $class->_is_string($row->{text})
155 ? (document => { text => $row->{text} })
156 66 ▶ : ()),
Branch: 1 branch
BranchTrueFalseTotal
$with_documents && $class->_is_string($row->{'text'}) ? : 8 58 66
Condition66%2/3
$with_documents and $class->_is_string($row->{"text"})
!ll&&!rl&&r
5808
MC/DC50%1/2
$with_documents and $class->_is_string($row->{"text"})
$with_documents $class->_is_string($row->{"text"})
Truth table66%2/3
A and B
A= $with_documentsB= $class->_is_string($row->{"text"})
ABresult
0X0
100
111
157 };
158 }
159  
160 @results = sort {
161 23 $b->{relevance_score} <=> $a->{relevance_score} || $a->{index} <=> $b->{index}
162 66 ▶ } @results;
Condition66%2/3
$Langertha::Skeid::Protocol::Rerank::b->{'relevance_score'} <=> $Langertha::Skeid::Protocol::Rerank::a->{'relevance_score'} || $Langertha::Skeid::Protocol::Rerank::a->{'index'} <=> $Langertha::Skeid::Protocol::Rerank::b->{'index'}
l!l&&r!l&&!r
6510
MC/DC0%0/2
$Langertha::Skeid::Protocol::Rerank::b->{'relevance_score'} <=> $Langertha::Skeid::Protocol::Rerank::a->{'relevance_score'} || $Langertha::Skeid::Protocol::Rerank::a->{'index'} <=> $Langertha::Skeid::Protocol::Rerank::b->{'index'}
$Langertha::Skeid::Protocol::Rerank::b->{'relevance_score'} <=> $Langertha::Skeid::Protocol::Rerank::a->{'relevance_score'} $Langertha::Skeid::Protocol::Rerank::a->{'index'} <=> $Langertha::Skeid::Protocol::Rerank::b->{'index'}
Truth table66%2/3
A || B
A= $Langertha::Skeid::Protocol::Rerank::b->{'relevance_score'} <=> $Langertha::Skeid::Protocol::Rerank::a->{'relevance_score'}B= $Langertha::Skeid::Protocol::Rerank::a->{'index'} <=> $Langertha::Skeid::Protocol::Rerank::b->{'index'}
ABresult
1X1
011
000
163 23 my $top_n = $class->_top_n($body);
164 23 ▶ splice(@results, $top_n) if $top_n && $top_n < @results;
Branch: 1 branch
BranchTrueFalseTotal
if $top_n and $top_n < @results 1 22 23
Condition100%3/3
$top_n and $top_n < @results
!ll&&!rl&&r
2021
MC/DC100%2/2
$top_n and $top_n < @results
$top_n $top_n < @results
Truth table100%3/3
A and B
A= $top_nB= $top_n < @results
ABresult
0X0
100
111
165  
166 23 my $tokens = $class->_tei_tokens($payload, $headers);
167 return {
168 23 ▶ model => $served_model,
Branch: 1 branch
BranchTrueFalseTotal
defined $tokens ? : 20 3 23
169 results => \@results,
170 (defined $tokens ? (usage => { total_tokens => $tokens }) : ()),
171 };
172 }
173  
174 # TEI reports what it computed in response headers, not in the body.
175 48 ▶sub _tei_tokens {
Subroutine _tei_tokens: called CC=3 SCAR=11.0
176 48 my ($class, $payload, $headers) = @_;
177 48 ▶ return undef unless $headers;
Branch: 1 branch
BranchTrueFalseTotal
unless $headers 0 48 48
178 48 my $tokens = $headers->header('x-compute-tokens');
179 48 ▶ return $class->_is_count($tokens) ? 0 + $tokens : undef;
Branch: 1 branch
BranchTrueFalseTotal
$class->_is_count($tokens) ? : 41 7 48
180 }
181  
182 # The client's top_n as the number of results to keep: 0 for all of them, which is what no
183 # top_n, null and 0 ask for. Anything that is not a whole number of at least zero is refused.
184 49 ▶sub _top_n {
Subroutine _top_n: called CC=4 SCAR=13.9
185 49 my ($class, $body) = @_;
186 49 my $top_n = $body->{top_n};
187 49 ▶ return 0 unless defined $top_n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $top_n 39 10 49
188 10 ▶ Langertha::Skeid::Protocol::Refusal->refuse("'top_n' must be a whole number of at least 0")
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $top_n and $top_n =~ /\A[0-9]+\z/ 2 8 10
Condition66%2/3
not ref $top_n and $top_n =~ /\A[0-9]+\z/
!ll&&!rl&&r
028
MC/DC50%1/2
not ref $top_n and $top_n =~ /\A[0-9]+\z/
not ref $top_n $top_n =~ /\A[0-9]+\z/
Truth table66%2/3
not(A) and B
A= not ref $top_nB= $top_n =~ /\A[0-9]+\z/
ABresult
0X0
100
111
189 unless !ref($top_n) && $top_n =~ /\A[0-9]+\z/;
190 8 return 0 + $top_n;
191 }
192  
193 #### Values off the wire
194  
195 # A JSON string, as opposed to a JSON number: a decoded number has a numeric value and no string
196 # value until something reads it as one, so this has to be asked before the value is used.
197 280 ▶sub _is_string {
Subroutine _is_string: called CC=5 SCAR=16.1
198 280 my ($class, $value) = @_;
199 280 ▶ return 0 unless defined($value) && !ref($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and not ref $value 18 262 280
Condition100%3/3
defined $value and not ref $value
!ll&&!rl&&r
108262
MC/DC100%2/2
defined $value and not ref $value
defined $value not ref $value
Truth table100%3/3
A and not(B)
A= defined $valueB= not ref $value
ABresult
0X0
100
111
200 262 my $flags = B::svref_2object(\$value)->FLAGS;
201 262 ▶ return (($flags & (B::SVp_IOK | B::SVp_NOK)) && !($flags & B::SVp_POK)) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$flags & 12288 && !($flags & 16384) ? : 2 260 262
Condition66%2/3
$flags & 12288 and not $flags & 16384
!ll&&!rl&&r
26002
MC/DC50%1/2
$flags & 12288 and not $flags & 16384
$flags & 12288 not $flags & 16384
Truth table66%2/3
A and not(B)
A= $flags & 12288B= not $flags & 16384
ABresult
0X0
100
111
202 }
203  
204 # A whole number of at least zero: what a count off the wire has to be.
205 172 ▶sub _is_count {
Subroutine _is_count: called CC=7 SCAR=19.5
206 172 my ($class, $value) = @_;
207 172 ▶ return 0 unless defined($value) && !ref($value) && looks_like_number($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and not ref $value and &looks_like_number($value) 24 148 172
Condition100%6/6
defined $value and not ref $value and &looks_like_number($value)
!ll&&!rl&&r
213148
defined $value and not ref $value
!ll&&!rl&&r
192151
MC/DC100%3/3
defined $value and not ref $value and &looks_like_number($value)
defined $value not ref $value &looks_like_number($value)
Truth table100%4/4
A and not(B) and C
A= defined $valueB= not ref $valueC= &looks_like_number($value)
ABCresult
0XX0
10X0
1100
1111
208 148 ▶ return ($value >= 0 && $value < 9**9**9 && $value == int($value)) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value >= 0 && $value < 9**9**9 && $value == int $value ? : 144 4 148
Condition100%6/6
$value >= 0 and $value < "Inf" and $value == int $value
!ll&&!rl&&r
31144
$value >= 0 and $value < "Inf"
!ll&&!rl&&r
21145
MC/DC100%3/3
$value >= 0 and $value < "Inf" and $value == int $value
$value >= 0 $value < "Inf" $value == int $value
Truth table100%4/4
A and B and C
A= $value >= 0B= $value < "Inf"C= $value == int $value
ABCresult
0XX0
10X0
1100
1111
209 }
210  
211  
212 113 1;
213  
214 __END__
« lib/Langertha/Skeid/Protocol/MCP.pm ↑ Summary lib/Langertha/Skeid/Protocol/Audio.pm »