CPAN/Maker/Bootstrapper/Role/LLM/Utils.pm

filter: Statementstmt 21.0% 28 / 133 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 56.2% 9 / 16 Podpod n/a totaltotal 16.5% 37 / 224 CC 31 SCAR 44.1
CC
31
Cov
15%
_print_token_usage
210.0
_check_llm
72.0
_pre_submission_report
30.0
CRAP 82.0
« CPAN/Maker/Bootstrapper/Role/LLM/Reviewer.pm ↑ Summary CPAN/Maker/Bootstrapper/Role/LLM/Models.pm »
1 package CPAN::Maker::Bootstrapper::Role::LLM::Utils;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use open ':std', ':encoding(UTF-8)';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use CLI::Simple::Constants qw(:booleans);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use CLI::Simple::Utils qw(slurp);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use CPAN::Maker::Bootstrapper::Constants qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Cwd qw(abs_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use Role::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 ########################################################################
17 0 ▶sub _check_llm {
Subroutine _check_llm: not called CC=8 SCAR=42.8
18 ########################################################################
19 0 my ( $self, $api_key ) = @_;
20  
21 0 ▶ $api_key //= $ENV{LLM_API_KEY};
Condition0%0/3
$api_key //= $ENV{"LLM_API_KEY"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$api_key //= $ENV{"LLM_API_KEY"}
$api_key $ENV{"LLM_API_KEY"}
Truth table0%0/3
A //= B
A= $api_keyB= $ENV{"LLM_API_KEY"}
ABresult
1X1
011
000
22  
23 # Remove from environment so it is not inherited by child processes
24 # such as 'make'. This does not protect against memory inspection
25 # of the current process - see LLM::API for how the key is stored
26 # using a closure to prevent accidental serialization via Dumper.
27 0 delete $ENV{LLM_API_KEY};
28  
29 0 ▶ if ( !$api_key && ( my $helper = $self->get_llm_api_key_helper ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $api_key and my $helper = $self->get_llm_api_key_helper) 0 0 0
Condition0%0/3
not $api_key and my $helper = $self->get_llm_api_key_helper
!ll&&!rl&&r
000
MC/DC0%0/2
not $api_key and my $helper = $self->get_llm_api_key_helper
not $api_key my $helper = $self->get_llm_api_key_helper
Truth table0%0/3
not(A) and B
A= not $api_keyB= my $helper = $self->get_llm_api_key_helper
ABresult
0X0
100
111
30 0 chomp( $api_key = qx{$helper} );
31 0 ▶ die "ERROR: llm-api-key-helper returned nothing\n" if !$api_key;
Branch: 1 branch
BranchTrueFalseTotal
unless $api_key 0 0 0
32 }
33  
34 0 ▶ die "ERROR: you must pass an API key or set LLM_API_KEY in the environment\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $api_key 0 0 0
35 if !$api_key;
36  
37 eval {
38 0 require LLM::API;
39 0 LLM::API->import(qw($LLM_MODEL));
40 0 1;
41 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require LLM::API; "LLM::API"->import("\$LLM_MODEL"); 1 } } or do { die "ERROR: LLM::API is required to use AI assisted commands\n" } 0 0 0
42 0 die "ERROR: LLM::API is required to use AI assisted commands\n";
43 };
44  
45 0 my $llm = LLM::API->new(
46 api_key => $api_key,
47 max_tokens => $self->get_max_tokens,
48 model => $self->get_model,
49 );
50  
51 0 ▶ die "ERROR: could not create an LLM::API instance\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $llm 0 0 0
52 if !$llm;
53  
54 0 $self->set_llm($llm);
55  
56 0 return $llm;
57 }
58  
59 ########################################################################
60 0 ▶sub _submit_prompt {
Subroutine _submit_prompt: not called CC=2 SCAR=17.9
61 ########################################################################
62 0 my ( $self, $llm, $prompt ) = @_;
63  
64 0 my $llm_rsp = $llm->send_prompt($prompt);
65  
66 0 ▶ die sprintf "ERROR: API request failed: %s %s\n", $llm_rsp->status, $llm_rsp->reason
Branch: 1 branch
BranchTrueFalseTotal
unless $llm_rsp->is_success 0 0 0
67 if !$llm_rsp->is_success;
68  
69 0 return $llm_rsp;
70 }
71  
72 ########################################################################
73 0 ▶sub _strip_pod {
Subroutine _strip_pod: not called CC=3 SCAR=24.8
74 ########################################################################
75 0 my ( $self, $file ) = @_;
76  
77 0 ▶ return slurp( abs_path($file) )
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ /[.]p[ml](?:[.]in)?\z/msx 0 0 0
78 if $file !~ /[.]p[ml](?:[.]in)?\z/xsm;
79  
80 0 require Pod::Extract;
81 0 Pod::Extract->import('extract_pod');
82  
83 0 ▶ open my $fh, '<', abs_path($file)
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", abs_path($file) or die "ERROR: could not open $file for reading\n" 0 0 0
84 or die "ERROR: could not open $file for reading\n";
85  
86 0 my ( $pod, $code, $sections ) = extract_pod($fh);
87 0 close $fh;
88  
89 0 return $code;
90 }
91  
92 ########################################################################
93 0 ▶sub _estimate_tokens {
Subroutine _estimate_tokens: not called CC=2 SCAR=17.9
94 ########################################################################
95 0 my ( $self, $text ) = @_;
96  
97 0 ▶ return 0 if !$text;
Branch: 1 branch
BranchTrueFalseTotal
unless $text 0 0 0
98 0 return int( length($text) / 4 );
99 }
100  
101 ########################################################################
102 0 ▶sub _pre_submission_report {
Subroutine _pre_submission_report: not called CC=5 SCAR=34.0
103 ########################################################################
104 0 my ( $self, %args ) = @_;
105  
106 my ( $review_type, $prompt_str, $text, $annotations, $context_files, $max_tokens, $input_tokens, $input_cost ) = @args{
107 0 qw(type prompt text annotations context_files
108 max_tokens input_tokens input_cost)
109 };
110  
111 0 my ( $input_token_cost, $output_token_cost ) = $self->get_llm->pricing( $self->get_model );
112  
113 0 $self->_load_ASCIITable;
114  
115 0 my $title = sprintf "%s Review: Pre-Submission Estimate\nModel: %s", ucfirst($review_type), $self->get_model;
116  
117 0 my $t = Text::ASCIITable->new( { headingText => $title } );
118 0 $t->setCols( 'Component', 'Tokens', 'Cost (USD)' );
119 0 $t->setColWidth( 'Component', 24 );
120 0 $t->setColWidth( 'Tokens', 16 );
121 0 $t->setColWidth( 'Cost (USD)', 20 );
122  
123 0 my $prompt_tokens = $self->_estimate_tokens($prompt_str);
124 0 my $file_tokens = $self->_estimate_tokens($text);
125 0 my $annotation_tokens = $self->_estimate_tokens($annotations);
126  
127 0 my $context_tokens = 0;
128 0 ▶ for my $f ( @{ $context_files // [] } ) {
Condition0%0/2
$context_files // []
l!l
00
MC/DC0%0/1
$context_files // []
$context_files
Truth table0%0/2
A
A= $context_files
Aresult
11
00
129 0 $context_tokens += $self->_estimate_tokens( slurp( abs_path($f) ) );
130 }
131  
132 0 $t->addRow( 'Prompt (est)', $prompt_tokens, sprintf '$%.4f', $prompt_tokens * $input_token_cost );
133 0 $t->addRow( 'Primary file (est)', $file_tokens, sprintf '$%.4f', $file_tokens * $input_token_cost );
134 0 ▶ $t->addRow( 'Annotations (est)', $annotation_tokens, sprintf '$%.4f', $annotation_tokens * $input_token_cost )
Branch: 1 branch
BranchTrueFalseTotal
if $annotation_tokens 0 0 0
135 if $annotation_tokens;
136 0 ▶ $t->addRow( 'Context (est)', $context_tokens, sprintf '$%.4f', $context_tokens * $input_token_cost )
Branch: 1 branch
BranchTrueFalseTotal
if $context_tokens 0 0 0
137 if $context_tokens;
138 0 $t->addRowLine;
139 0 $t->addRow( 'Input total', $input_tokens, sprintf '$%.4f', $input_cost );
140 0 $t->addRowLine;
141  
142 0 my $output_low = int( $max_tokens / 2 );
143 0 my $output_high = $max_tokens;
144 0 my $cost_low = $output_low * $output_token_cost;
145 0 my $cost_high = $output_high * $output_token_cost;
146  
147 0 $t->addRow(
148 'Output (est)',
149 sprintf( '%d - %d', $output_low, $output_high ),
150 sprintf( '$%.4f - $%.4f', $cost_low, $cost_high )
151 );
152 0 $t->addRow( 'Max tokens', $max_tokens, q{-} );
153 0 $t->addRowLine;
154 0 $t->addRow( 'Total (est)', q{-}, sprintf( '$%.4f - $%.4f', $input_cost + $cost_low, $input_cost + $cost_high ) );
155  
156 0 print {*STDERR} $t;
157  
158 0 return;
159 }
160  
161 ########################################################################
162 0 ▶sub _fmt_tokens {
Subroutine _fmt_tokens: not called CC=3 SCAR=24.8
163 ########################################################################
164 0 my ($n) = @_;
165  
166 0 ▶ return q{-}
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 0 0
167 if !defined $n;
168  
169 0 ▶ return sprintf '%dK', $n / 1_000
Branch: 1 branch
BranchTrueFalseTotal
if $n < 1000000 0 0 0
170 if $n < 1_000_000;
171  
172 0 return sprintf '%.1fM', $n / 1_000_000;
173 }
174  
175 ########################################################################
176 0 ▶sub _print_token_usage {
Subroutine _print_token_usage: not called CC=14 SCAR=53.5
177 ########################################################################
178 0 my ( $self, $llm_rsp, $label ) = @_;
179  
180 0 $self->_load_ASCIITable;
181  
182 0 ▶ $label //= 'Token Usage';
Condition0%0/2
$label //= "Token Usage"
l!l
00
MC/DC0%0/1
$label //= "Token Usage"
$label
Truth table0%0/2
A
A= $label
Aresult
11
00
183  
184 0 my $usage = eval { $llm_rsp->usage };
185  
186 0 ▶ if ( !$usage ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($usage) 0 0 0
187 0 my $raw = JSON::PP->new->decode( $llm_rsp->raw_content );
188  
189 0 $usage = $raw->{usage};
190 }
191  
192 0 ▶ return if !$usage;
Branch: 1 branch
BranchTrueFalseTotal
unless $usage 0 0 0
193  
194 # pricing per token
195 0 my ( $input_token_cost, $output_token_cost ) = eval { $self->get_llm->pricing(); };
196  
197 # ...this should never happen since we are being called from methods
198 # that must have instantiated a valid LLM::API instance
199 0 ▶ if ($EVAL_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CPAN::Maker::Bootstrapper::Role::LLM::Utils::EVAL_ERROR) 0 0 0
200 0 warn "Pricing estimates cannot be calculated.\n$EVAL_ERROR";
201 0 return;
202 }
203  
204 0 ▶ my $input_cost = ( $usage->{input_tokens} // 0 ) * $input_token_cost;
Condition0%0/2
$usage->{"input_tokens"} // 0
l!l
00
MC/DC0%0/1
$usage->{"input_tokens"} // 0
$usage->{"input_tokens"}
Truth table0%0/2
A
A= $usage->{"input_tokens"}
Aresult
11
00
205 0 ▶ my $output_cost = ( $usage->{output_tokens} // 0 ) * $output_token_cost;
Condition0%0/2
$usage->{"output_tokens"} // 0
l!l
00
MC/DC0%0/1
$usage->{"output_tokens"} // 0
$usage->{"output_tokens"}
Truth table0%0/2
A
A= $usage->{"output_tokens"}
Aresult
11
00
206 0 ▶ my $cache_read = ( $usage->{cache_read_input_tokens} // 0 );
Condition0%0/2
$usage->{"cache_read_input_tokens"} // 0
l!l
00
MC/DC0%0/1
$usage->{"cache_read_input_tokens"} // 0
$usage->{"cache_read_input_tokens"}
Truth table0%0/2
A
A= $usage->{"cache_read_input_tokens"}
Aresult
11
00
207 0 ▶ my $cache_write = ( $usage->{cache_creation_input_tokens} // 0 );
Condition0%0/2
$usage->{"cache_creation_input_tokens"} // 0
l!l
00
MC/DC0%0/1
$usage->{"cache_creation_input_tokens"} // 0
$usage->{"cache_creation_input_tokens"}
Truth table0%0/2
A
A= $usage->{"cache_creation_input_tokens"}
Aresult
11
00
208 0 my $total_cost = $input_cost + $output_cost;
209  
210 0 ▶ my $model_name = $self->get_model // 'unknown model';
Condition0%0/2
$self->get_model // "unknown model"
l!l
00
MC/DC0%0/1
$self->get_model // "unknown model"
$self->get_model
Truth table0%0/2
A
A= $self->get_model
Aresult
11
00
211  
212 0 my $heading = sprintf "%s\n(%s Estimates)", $label, $model_name;
213  
214 0 my $t = Text::ASCIITable->new( { headingText => $heading } );
215  
216 0 $t->setCols( 'Metric', 'Tokens', 'Cost (USD)' );
217 0 $t->setColWidth( 'Metric', 30 );
218 0 $t->setColWidth( 'Tokens', 12 );
219 0 $t->setColWidth( 'Cost (USD)', 12 );
220  
221 0 ▶ $t->addRow( 'Input tokens', $usage->{input_tokens} // 0, sprintf '$%.4f', $input_cost );
Condition0%0/2
$usage->{"input_tokens"} // 0
l!l
00
MC/DC0%0/1
$usage->{"input_tokens"} // 0
$usage->{"input_tokens"}
Truth table0%0/2
A
A= $usage->{"input_tokens"}
Aresult
11
00
222 0 ▶ $t->addRow( 'Output tokens', $usage->{output_tokens} // 0, sprintf '$%.4f', $output_cost );
Condition0%0/2
$usage->{"output_tokens"} // 0
l!l
00
MC/DC0%0/1
$usage->{"output_tokens"} // 0
$usage->{"output_tokens"}
Truth table0%0/2
A
A= $usage->{"output_tokens"}
Aresult
11
00
223 0 $t->addRow( 'Cache write tokens', $cache_write, q{-} );
224 0 $t->addRow( 'Cache read tokens', $cache_read, q{-} );
225 0 $t->addRowLine;
226 0 ▶ $t->addRow( 'Total', ( $usage->{input_tokens} // 0 ) + ( $usage->{output_tokens} // 0 ), sprintf '$%.4f', $total_cost );
Condition0%0/4
$usage->{"input_tokens"} // 0
l!l
00
$usage->{"output_tokens"} // 0
l!l
00
MC/DC0%0/1
$usage->{"input_tokens"} // 0
$usage->{"input_tokens"}
MC/DC0%0/1
$usage->{"output_tokens"} // 0
$usage->{"output_tokens"}
Truth table0%0/2
A
A= $usage->{"input_tokens"}
Aresult
11
00
Truth table0%0/2
A
A= $usage->{"output_tokens"}
Aresult
11
00
227  
228 0 print {*STDERR} $t;
229  
230 0 return;
231 }
232  
233 1 1;
« CPAN/Maker/Bootstrapper/Role/LLM/Reviewer.pm ↑ Summary CPAN/Maker/Bootstrapper/Role/LLM/Models.pm »