API.pm

filter: Statementstmt 76.7% 172 / 224 Branchbran 52.4% 43 / 82 Conditioncond 48.9% 48 / 98 MC/DCmcdc 18.1% 8 / 44 Subroutinesub 83.3% 35 / 42 Podpod 100% 16 / 16 totaltotal 63.6% 322 / 506 CC 83 SCAR 27.2
CC
83
Cov
65%
send_messages
32.5
query_string
30.0
usage_report
30.0
CRAP 15.2
« API/Bedrock.pm ↑ Summary API/Anthropic.pm »
1 package LLM::API;
2 # Provider-independent LLM client
3  
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 8 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=10.5
8 8 ▶ if ( $ENV{DEBUG} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"DEBUG"}) 0 8 8
9 0 require Data::Dumper;
10 0 Data::Dumper->import('Dumper');
11 }
12 }
13  
14 8 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 8 ▶use File::Basename qw(dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 8 ▶use File::ShareDir qw(dist_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 8 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 8 ▶use CLI::Simple::Utils qw(slurp choose);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 8 ▶use CLI::Simple::Constants qw(:booleans);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 8 ▶use JSON::PP qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 8 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 8 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 8 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 8 ▶use URI::Encode qw(uri_encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 8 ▶use YAML::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 8 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 8 Readonly::Scalar our $DEFAULT_LLM_MODEL => 'claude-sonnet-4-6'; # default model
30  
31 # endpoints
32 8 Readonly::Scalar our $LLM_URL_BASE => 'https://api.anthropic.com/v1/';
33 8 Readonly::Scalar our $LLM_MESSAGES_EP => 'messages';
34 8 Readonly::Scalar our $LLM_MODELS_EP => 'models';
35 8 Readonly::Scalar our $LLM_USAGE_EP => 'organizations/usage_report/messages';
36 8 Readonly::Scalar our $LLM_COUNT_TOKENS_EP => 'messages/count_tokens';
37 8 Readonly::Scalar our $LLM_COST_EP => 'organizations/cost_report';
38 8 Readonly::Scalar our $LLM_KEYS_EP => 'organizations/api_keys';
39  
40 8 Readonly::Scalar our $LLM_MAX_TOKENS => 8192;
41 8 Readonly::Scalar our $LLM_VERSION => '2023-06-01';
42 8 Readonly::Scalar our $LLM_ROLE => 'user';
43 8 Readonly::Scalar our $LLM_TIMEOUT => 300;
44  
45 # Curated provider metadata is installed with the distribution share files.
46 8 my $PROVIDERS;
47  
48 8 Readonly::Hash our %LLM_MEDIA_TYPES => (
49 text => 'text/plain',
50 pdf => 'application/pdf',
51 );
52  
53 8 __PACKAGE__->follow_best_practice;
54 8 __PACKAGE__->mk_accessors(
55 qw(
56 api_key_getter
57 provider
58 region
59 max_tokens
60 model
61 version
62 role
63 timeout
64 )
65 );
66  
67 8 our $VERSION = '2.0.0';
68  
69 8 ▶use parent qw(Class::Accessor::Fast);
Subroutine BEGIN: called CC=1 SCAR=0.0
70  
71 ########################################################################
72 18 ▶sub new {
Subroutine new: called CC=15 SCAR=28.2
Pod documented
73 ########################################################################
74 18 my ( $class, @args ) = @_;
75  
76 18 ▶ my $options = ref $args[0] ? $args[0] : {@args};
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] ? : 0 18 18
77  
78 # Anthropic remains the default for existing callers.
79 18 ▶ $options->{provider} //= q{anthropic};
Condition100%2/2
$options->{"provider"} //= "anthropic"
l!l
144
MC/DC100%1/1
$options->{"provider"} //= "anthropic"
$options->{"provider"}
Truth table100%2/2
A
A= $options->{"provider"}
Aresult
11
00
80 die sprintf "ERROR: unsupported LLM provider: %s\n", $options->{provider}
81 18 ▶ if !exists _providers()->{ $options->{provider} };
Branch: 1 branch
BranchTrueFalseTotal
unless exists _providers()->{$options->{"provider"}} 2 16 18
82  
83 # API key from the option list takes precedence
84 16 my $api_key = $options->{api_key};
85 16 delete $options->{api_key};
86  
87 16 ▶ $api_key //= $ENV{LLM_API_KEY};
Condition66%2/3
$api_key //= $ENV{"LLM_API_KEY"}
l!l&&r!l&&!r
6010
MC/DC50%1/2
$api_key //= $ENV{"LLM_API_KEY"}
$api_key $ENV{"LLM_API_KEY"}
Truth table66%2/3
A //= B
A= $api_keyB= $ENV{"LLM_API_KEY"}
ABresult
1X1
011
000
88 16 delete $ENV{LLM_API_KEY};
89  
90 die "ERROR: api_key is a required argument\n"
91 16 ▶ if $options->{provider} eq q{anthropic} && !$api_key;
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"provider"} eq "anthropic" and not $api_key 0 16 16
Condition66%2/3
$options->{"provider"} eq "anthropic" and not $api_key
!ll&&!rl&&r
1060
MC/DC0%0/2
$options->{"provider"} eq "anthropic" and not $api_key
$options->{"provider"} eq "anthropic" not $api_key
Truth table66%2/3
A and not(B)
A= $options->{"provider"} eq "anthropic"B= not $api_key
ABresult
0X0
100
111
92  
93 16 ▶ $options->{model} //= $options->{provider} eq q{bedrock} ? q{amazon.nova-micro-v1:0} : $DEFAULT_LLM_MODEL;
Branch: 1 branch
BranchTrueFalseTotal
$options->{'provider'} eq 'bedrock' ? : 3 5 8
Condition66%2/3
$options->{"model"} //= $options->{"provider"} eq "bedrock" ? "amazon.nova-micro-v1:0" : $LLM::API::DEFAULT_LLM_MODEL
l!l&&r!l&&!r
880
MC/DC0%0/2
$options->{"model"} //= $options->{"provider"} eq "bedrock" ? "amazon.nova-micro-v1:0" : $LLM::API::DEFAULT_LLM_MODEL
$options->{"model"} $options->{"provider"} eq "bedrock" ? "amazon.nova-micro-v1:0" : $LLM::API::DEFAULT_LLM_MODEL
Truth table66%2/3
A //= B
A= $options->{"model"}B= $options->{"provider"} eq "bedrock" ? "amazon.nova-micro-v1:0" : $LLM::API::DEFAULT_LLM_MODEL
ABresult
1X1
011
000
94  
95 $options->{region} //= $ENV{AWS_REGION} // $ENV{AWS_DEFAULT_REGION} // q{us-east-1}
96 16 ▶ if $options->{provider} eq q{bedrock};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"provider"} eq "bedrock" 10 6 16
Condition57%4/7
$options->{"region"} //= ($ENV{"AWS_REGION"} // $ENV{"AWS_DEFAULT_REGION"}) // "us-east-1"
l!l
28
($ENV{"AWS_REGION"} // $ENV{"AWS_DEFAULT_REGION"}) // "us-east-1"
l!l
08
$ENV{"AWS_REGION"} // $ENV{"AWS_DEFAULT_REGION"}
l!l&&r!l&&!r
008
MC/DC100%1/1
$options->{"region"} //= ($ENV{"AWS_REGION"} // $ENV{"AWS_DEFAULT_REGION"}) // "us-east-1"
$options->{"region"}
Truth table100%2/2
A
A= $options->{"region"}
Aresult
11
00
97  
98 16 ▶ $options->{timeout} //= $LLM_TIMEOUT;
Condition33%1/3
$options->{"timeout"} //= $LLM::API::LLM_TIMEOUT
l!l&&r!l&&!r
0160
MC/DC0%0/2
$options->{"timeout"} //= $LLM::API::LLM_TIMEOUT
$options->{"timeout"} $LLM::API::LLM_TIMEOUT
Truth table33%1/3
A //= B
A= $options->{"timeout"}B= $LLM::API::LLM_TIMEOUT
ABresult
1X1
011
000
99  
100 16 my $self = $class->SUPER::new($options);
101  
102 6 ▶ $self->set_api_key_getter( sub {$api_key} )
Subroutine __ANON__: called CC=1 SCAR=0.0
103 16 ▶ if $options->{provider} eq q{anthropic};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"provider"} eq "anthropic" 6 10 16
104  
105 16 $self->_init;
106  
107 16 return $self;
108 }
109  
110 ########################################################################
111 12 ▶sub count_input_tokens {
Subroutine count_input_tokens: called CC=1 SCAR=0.0
Pod documented
112 ########################################################################
113 12 my ( $self, @args ) = @_;
114  
115 12 return $self->_provider_class->count_input_tokens( $self, @args );
116 }
117  
118 ########################################################################
119 12 ▶sub send_prompt {
Subroutine send_prompt: called CC=1 SCAR=0.0
Pod documented
120 ########################################################################
121 12 my ( $self, @args ) = @_;
122  
123 12 return $self->_provider_class->send_prompt( $self, @args );
124 }
125  
126 ########################################################################
127 9 ▶sub send_messages {
Subroutine send_messages: called CC=23 SCAR=34.8
Pod documented
128 ########################################################################
129 9 my ( $self, $messages ) = @_;
130  
131 die "ERROR: usage: send_messages(array-ref)\n"
132 9 ▶ if !ref $messages || reftype($messages) ne q{ARRAY} || !@{$messages};
Branch: 1 branch
BranchTrueFalseTotal
if not ref $messages or builtin::reftype($messages) ne "ARRAY" or not @{$messages;} 2 7 9
Condition83%5/6
not ref $messages or builtin::reftype($messages) ne "ARRAY" or not @{$messages;}
l!l&&r!l&&!r
117
not ref $messages or builtin::reftype($messages) ne "ARRAY"
l!l&&r!l&&!r
108
MC/DC66%2/3
not ref $messages or builtin::reftype($messages) ne "ARRAY" or not @{$messages;}
not ref $messages builtin::reftype($messages) ne "ARRAY" not @{$messages;}
Truth table75%3/4
not(A) or B or not(C)
A= not ref $messagesB= builtin::reftype($messages) ne "ARRAY"C= not @{$messages;}
ABCresult
1XX1
01X1
0011
0000
133  
134 7 for my $message ( @{$messages} ) {
135 die "ERROR: each message must contain a user or assistant role and text content\n"
136 if !ref $message
137 || reftype($message) ne q{HASH}
138 || ( $message->{role} // q{} ) !~ m{\A(?:user|assistant)\z}
139 || !ref $message->{content}
140 || reftype( $message->{content} ) ne q{ARRAY}
141 13 ▶ || !@{ $message->{content} };
Branch: 1 branch
BranchTrueFalseTotal
if not ref $message or builtin::reftype($message) ne "HASH" or not +($message->{"role"} // "") =~ /\A(?:user|assistant)\z/ or not ref $message->{"content"} or builtin::reftype($message->{"content"}) ne "ARRAY" or not @{$message->{"content"};} 2 11 13
Condition64%11/17
not ref $message or builtin::reftype($message) ne "HASH" or not +($message->{"role"} // "") =~ /\A(?:user|assistant)\z/ or not ref $message->{"content"} or builtin::reftype($message->{"content"}) ne "ARRAY" or not @{$message->{"content"};}
l!l&&r!l&&!r
1111
not ref $message or builtin::reftype($message) ne "HASH" or not +($message->{"role"} // "") =~ /\A(?:user|assistant)\z/ or not ref $message->{"content"} or builtin::reftype($message->{"content"}) ne "ARRAY"
l!l&&r!l&&!r
1012
not ref $message or builtin::reftype($message) ne "HASH" or not +($message->{"role"} // "") =~ /\A(?:user|assistant)\z/ or not ref $message->{"content"}
l!l&&r!l&&!r
1012
not ref $message or builtin::reftype($message) ne "HASH" or not +($message->{"role"} // "") =~ /\A(?:user|assistant)\z/
l!l&&r!l&&!r
0112
not ref $message or builtin::reftype($message) ne "HASH"
l!l&&r!l&&!r
0013
$message->{"role"} // ""
l!l
130
MC/DC16%1/6
not ref $message or builtin::reftype($message) ne "HASH" or not +($message->{"role"} // "") =~ /\A(?:user|assistant)\z/ or not ref $message->{"content"} or builtin::reftype($message->{"content"}) ne "ARRAY" or not @{$message->{"content"};}
not ref $message builtin::reftype($message) ne "HASH" $message->{"role"} not ref $message->{"content"} builtin::reftype($message->{"content"}) ne "ARRAY" not @{$message->{"content"};}
Truth table28%2/7
not(A) or B or not(C) or not(D) or E or not(F)
A= not ref $messageB= builtin::reftype($message) ne "HASH"C= $message->{"role"}D= not ref $message->{"content"}E= builtin::reftype($message->{"content"}) ne "ARRAY"F= not @{$message->{"content"};}
ABCDEFresult
1XXXXX1
01XXXX1
000XXX1
0011XX1
00101X1
0010011
0010000
142  
143 11 for my $block ( @{ $message->{content} } ) {
144 die "ERROR: send_messages requires text or document blocks\n"
145 if !ref $block
146 || reftype($block) ne q{HASH}
147 12 ▶ || ( $block->{type} // q{} ) ne q{text} && ( $block->{type} // q{} ) ne q{document};
Branch: 1 branch
BranchTrueFalseTotal
if not ref $block or builtin::reftype($block) ne "HASH" or ($block->{"type"} // "") ne "text" and ($block->{"type"} // "") ne "document" 0 12 12
Condition46%6/13
not ref $block or builtin::reftype($block) ne "HASH" or ($block->{"type"} // "") ne "text" and ($block->{"type"} // "") ne "document"
l!l&&r!l&&!r
0012
not ref $block or builtin::reftype($block) ne "HASH"
l!l&&r!l&&!r
0012
($block->{"type"} // "") ne "text" and ($block->{"type"} // "") ne "document"
!ll&&!rl&&r
930
$block->{"type"} // ""
l!l
120
$block->{"type"} // ""
l!l
30
MC/DC0%0/4
not ref $block or builtin::reftype($block) ne "HASH" or ($block->{"type"} // "") ne "text" and ($block->{"type"} // "") ne "document"
not ref $block builtin::reftype($block) ne "HASH" $block->{"type"} $block->{"type"}
Truth table20%1/5
not(A) or B or C and D
A= not ref $blockB= builtin::reftype($block) ne "HASH"C= $block->{"type"}D= $block->{"type"}
ABCDresult
1XXX1
01XX1
00111
000X0
00100
148  
149 12 ▶ if ( $block->{type} eq q{text} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block->{'type'} eq 'text') { } 9 3 12
150 die "ERROR: text content is missing\n"
151 9 ▶ if !defined $block->{text};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $block->{"text"} 0 9 9
152 }
153 else {
154 die "ERROR: document content must be a user message with a source\n"
155 3 ▶ if $message->{role} ne q{user} || ref $block->{source} ne q{HASH};
Branch: 1 branch
BranchTrueFalseTotal
if $message->{"role"} ne "user" or ref $block->{"source"} ne "HASH" 2 1 3
Condition66%2/3
$message->{"role"} ne "user" or ref $block->{"source"} ne "HASH"
l!l&&r!l&&!r
201
MC/DC50%1/2
$message->{"role"} ne "user" or ref $block->{"source"} ne "HASH"
$message->{"role"} ne "user" ref $block->{"source"} ne "HASH"
Truth table66%2/3
A or B
A= $message->{"role"} ne "user"B= ref $block->{"source"} ne "HASH"
ABresult
1X1
011
000
156 }
157 }
158 }
159  
160 3 return $self->_provider_class->send_messages( $self, $messages );
161 }
162  
163 ########################################################################
164 5 ▶sub document {
Subroutine document: called CC=4 SCAR=14.5
Pod documented
165 ########################################################################
166 5 my ( $self, %args ) = @_;
167  
168 5 my ( $media_type, $data, $title ) = @args{qw(media_type data title)};
169  
170 5 ▶ $media_type //= 'text';
Condition50%1/2
$media_type //= "text"
l!l
50
MC/DC0%0/1
$media_type //= "text"
$media_type
Truth table50%1/2
A
A= $media_type
Aresult
11
00
171  
172 die "ERROR: unknown media type: $media_type. Only pdf and text are currently supported.\n"
173 5 ▶ if !$LLM_MEDIA_TYPES{$media_type};
Branch: 1 branch
BranchTrueFalseTotal
unless $LLM::API::LLM_MEDIA_TYPES{$media_type} 0 5 5
174  
175 5 ▶ my ( $type, $document_data ) = choose {
Subroutine __ANON__: called CC=4 SCAR=14.6
176 5 ▶ return ( 'base64', encode_base64( ${$data} ) )
Branch: 1 branch
BranchTrueFalseTotal
if ref $data and builtin::reftype($data) eq "SCALAR" 4 1 5
Condition66%2/3
ref $data and builtin::reftype($data) eq "SCALAR"
!ll&&!rl&&r
104
MC/DC50%1/2
ref $data and builtin::reftype($data) eq "SCALAR"
ref $data builtin::reftype($data) eq "SCALAR"
Truth table66%2/3
A and B
A= ref $dataB= builtin::reftype($data) eq "SCALAR"
ABresult
0X0
100
111
177 if ref $data && reftype($data) eq 'SCALAR';
178  
179 1 ▶ return ( 'text', $data )
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data 1 0 1
180 if !ref $data;
181  
182 0 die "ERROR: only scalars and scalar references supported.\n"
183 5 };
184  
185 my $document = {
186 type => 'document',
187 source => {
188 type => $type,
189 5 ▶ media_type => $LLM_MEDIA_TYPES{$media_type},
Branch: 1 branch
BranchTrueFalseTotal
$title ? : 5 0 5
190 data => $document_data,
191 },
192 $title ? ( title => $title ) : (),
193 };
194  
195 5 return $document;
196 }
197  
198 ########################################################################
199 2 ▶sub models {
Subroutine models: called CC=4 SCAR=13.9
Pod documented
200 ########################################################################
201 2 my ($self) = @_;
202  
203 2 ▶ die "ERROR: models is not supported by this provider\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_provider ne "anthropic" 1 1 2
204 if $self->get_provider ne q{anthropic};
205  
206 1 my @model_list;
207  
208 1 ▶ while ($TRUE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($LLM::API::TRUE) 1 0 1
209 1 my $rsp = $self->_do_api( $LLM_URL_BASE . $LLM_MODELS_EP );
210 1 push @model_list, @{ $rsp->{data} };
211 1 ▶ last if !$rsp->{has_more};
Branch: 1 branch
BranchTrueFalseTotal
unless $rsp->{"has_more"} 1 0 1
212 }
213  
214 1 my %models = map { $_->{id} => $_ } @model_list;
215  
216 1 return \%models;
217 }
218  
219 ########################################################################
220 0 ▶sub api_keys {
Subroutine api_keys: not called CC=2 SCAR=17.9
Pod documented
221 ########################################################################
222 0 my ($self) = @_;
223  
224 0 ▶ die "ERROR: api_keys is not supported by this provider\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_provider ne "anthropic" 0 0 0
225 if $self->get_provider ne q{anthropic};
226  
227 0 return $self->_do_api( $LLM_URL_BASE . $LLM_KEYS_EP );
228 }
229  
230 ########################################################################
231 0 ▶sub rfc_3339 {
Subroutine rfc_3339: not called CC=1 SCAR=6.9
Pod documented
232 ########################################################################
233 0 my ( $self, $date ) = @_;
234  
235 0 ▶ my $fmt = choose {
Subroutine __ANON__: not called CC=3 SCAR=24.8
236 0 ▶ return '%Y-%m-%d'
Branch: 1 branch
BranchTrueFalseTotal
if $date =~ /\A\d{4}-\d{2}-\d{2}\z/msx 0 0 0
237 if $date =~ /\A\d{4}-\d{2}-\d{2}\z/xsm;
238  
239 0 ▶ return '%Y-%m-%d %H:%M'
Branch: 1 branch
BranchTrueFalseTotal
if $date =~ /\A\d{4}-\d{2}-\d{2}[ ]\d{2}:\d{2}\z/msx 0 0 0
240 if $date =~ /\A\d{4}-\d{2}-\d{2}[ ]\d{2}:\d{2}\z/xsm;
241  
242 0 die "ERROR: invalid date format (use YYYY-MM-DD or YYYY-MM-DD hh:mm)\n"
243 0 };
244  
245 0 my $t = Time::Piece->strptime( $date, $fmt );
246  
247 # Format to RFC 3339 standard
248 0 return $t->strftime('%Y-%m-%dT%H:%M:%SZ');
249 }
250  
251 ########################################################################
252 0 ▶sub query_string {
Subroutine query_string: not called CC=5 SCAR=34.0
Pod documented
253 ########################################################################
254 0 my ( $self, %params ) = @_;
255  
256 0 my @vars;
257  
258 0 foreach my $k ( keys %params ) {
259  
260 0 ▶ if ( !ref $params{$k} ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $params{$k}) { } 0 0 0
elsif (builtin::reftype($params{$k}) eq 'ARRAY') { } 0 0 0
elsif (builtin::reftype($params{$k}) eq 'HASH') { } 0 0 0
261 0 push @vars, sprintf '%s=%s', $k, uri_encode( $params{$k} );
262 0 next;
263 }
264 elsif ( reftype( $params{$k} ) eq 'ARRAY' ) {
265 0 push @vars, map { sprintf '%s=%s', $k, uri_encode($_) } @{ $params{$k} };
266 0 next;
267 }
268 elsif ( reftype( $params{$k} ) eq 'HASH' ) {
269 0 push @vars, map { sprintf '%s=%s', $_, uri_encode( $params{$k}->{$_} ) } keys %{ $params{$k} };
270 0 next;
271 }
272  
273 0 die "ERROR: unsupport ref type\n";
274 }
275  
276 0 return join q{&}, @vars;
277 }
278  
279 ########################################################################
280 0 ▶sub now {
Subroutine now: not called CC=1 SCAR=6.9
Pod documented
281 ########################################################################
282 0 my ($self) = @_;
283  
284 0 my @t = localtime;
285 0 $t[5] += 1900;
286 0 $t[4]++;
287  
288 0 return sprintf '%04d-%02d-%02d', @t[ ( 5, 4, 3 ) ];
289 }
290  
291 ########################################################################
292 0 ▶sub usage_report {
Subroutine usage_report: not called CC=5 SCAR=34.0
Pod documented
293 ########################################################################
294 0 my ( $self, %args ) = @_;
295  
296 0 ▶ die "ERROR: usage_report is not supported by this provider\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_provider ne "anthropic" 0 0 0
297 if $self->get_provider ne q{anthropic};
298  
299 0 my ( $from, $to, $api_key_ids, $bucket_width ) = @args{qw(from to api_key_ids bucket_width)};
300 0 ▶ $from //= $self->now();
Condition0%0/3
$from //= $self->now
l!l&&r!l&&!r
000
MC/DC0%0/2
$from //= $self->now
$from $self->now
Truth table0%0/3
A //= B
A= $fromB= $self->now
ABresult
1X1
011
000
301  
302 0 my $starting_at = $self->rfc_3339($from);
303  
304 0 ▶ my %params = (
Branch: 1 branch
BranchTrueFalseTotal
$to ? : 0 0 0
Condition0%0/2
$bucket_width // "1h"
l!l
00
MC/DC0%0/1
$bucket_width // "1h"
$bucket_width
Truth table0%0/2
A
A= $bucket_width
Aresult
11
00
305 bucket_width => $bucket_width // '1h',
306 starting_at => $starting_at,
307 'api_key_ids[]' => $api_key_ids,
308 $to ? ( ending_at => $self->rfc_3339($to) ) : (),
309 );
310  
311 0 my $url = sprintf '%s?%s', $LLM_URL_BASE . $LLM_USAGE_EP, $self->query_string(%params);
312  
313 0 return $self->_do_admin_api($url);
314 }
315  
316 ########################################################################
317 0 ▶sub cost_report {
Subroutine cost_report: not called CC=5 SCAR=34.0
Pod documented
318 ########################################################################
319 0 my ( $self, %args ) = @_;
320  
321 0 ▶ die "ERROR: cost_report is not supported by this provider\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_provider ne "anthropic" 0 0 0
322 if $self->get_provider ne q{anthropic};
323  
324 0 my ( $from, $to, $bucket_width ) = @args{qw(from to bucket_width)};
325 0 ▶ $from //= $self->now();
Condition0%0/3
$from //= $self->now
l!l&&r!l&&!r
000
MC/DC0%0/2
$from //= $self->now
$from $self->now
Truth table0%0/3
A //= B
A= $fromB= $self->now
ABresult
1X1
011
000
326  
327 0 my $starting_at = $self->rfc_3339($from);
328  
329 0 ▶ my %params = (
Branch: 1 branch
BranchTrueFalseTotal
$to ? : 0 0 0
Condition0%0/2
$bucket_width // "1d"
l!l
00
MC/DC0%0/1
$bucket_width // "1d"
$bucket_width
Truth table0%0/2
A
A= $bucket_width
Aresult
11
00
330 bucket_width => $bucket_width // '1d',
331 starting_at => $starting_at,
332 $to ? ( ending_at => $self->rfc_3339($to) ) : (),
333 );
334  
335 0 my $url = sprintf '%s?%s', $LLM_URL_BASE . $LLM_COST_EP, $self->query_string(%params);
336  
337 0 return $self->_do_admin_api($url);
338 }
339  
340 ########################################################################
341 6 ▶sub headers {
Subroutine headers: called CC=1 SCAR=0.0
Pod documented
342 ########################################################################
343 6 my ( $self, @args ) = @_;
344  
345 6 return $self->_provider_class->headers( $self, @args );
346 }
347  
348 ########################################################################
349 25 ▶sub text {
Subroutine text: called CC=1 SCAR=0.0
Pod documented
350 ########################################################################
351 25 my ( $self, $text ) = @_;
352  
353 return {
354 25 type => 'text',
355 text => $text,
356 };
357 }
358  
359 ########################################################################
360 4 ▶sub prompt {
Subroutine prompt: called CC=1 SCAR=0.0
Pod documented
361 ########################################################################
362 4 my ( $self, $prompt ) = @_;
363  
364 4 my $llm_rsp = $self->send_prompt( [ $self->text($prompt) ] );
365  
366 4 return $llm_rsp;
367 }
368  
369 ########################################################################
370 13 ▶sub pricing {
Subroutine pricing: called CC=11 SCAR=28.7
Pod documented
371 ########################################################################
372 13 my ( $self, $model ) = @_;
373  
374 13 ▶ $model //= $self->get_model;
Condition66%2/3
$model //= $self->get_model
l!l&&r!l&&!r
1030
MC/DC0%0/2
$model //= $self->get_model
$model $self->get_model
Truth table66%2/3
A //= B
A= $modelB= $self->get_model
ABresult
1X1
011
000
375 13 my $provider = $self->get_provider;
376 13 my $details = _providers()->{$provider}->{models}->{$model};
377  
378 13 ▶ die sprintf "ERROR: no pricing for provider %s, model %s\n", $provider, $model
Branch: 1 branch
BranchTrueFalseTotal
unless $details 2 11 13
379 if !$details;
380  
381 11 my @rates;
382 11 for my $name (qw(input_tokens output_tokens)) {
383 22 my $amount = $details->{$name};
384 die sprintf "ERROR: invalid %s pricing for provider %s, model %s\n", $name, $provider, $model
385 if ref $amount ne q{ARRAY}
386 22 ▶ || @{$amount} != 2
Branch: 1 branch
BranchTrueFalseTotal
if ref $amount ne "ARRAY" or @{$amount;} != 2 or not defined $amount->[0] or not defined $amount->[1] or not $amount->[0] =~ /\A\d+(?:\.\d+)?\z/ or not $amount->[1] =~ /\A\d+(?:\.\d+)?\z/ or $amount->[1] == 0 0 22 22
Condition33%6/18
ref $amount ne "ARRAY" or @{$amount;} != 2 or not defined $amount->[0] or not defined $amount->[1] or not $amount->[0] =~ /\A\d+(?:\.\d+)?\z/ or not $amount->[1] =~ /\A\d+(?:\.\d+)?\z/ or $amount->[1] == 0
l!l&&r!l&&!r
0022
ref $amount ne "ARRAY" or @{$amount;} != 2 or not defined $amount->[0] or not defined $amount->[1] or not $amount->[0] =~ /\A\d+(?:\.\d+)?\z/ or not $amount->[1] =~ /\A\d+(?:\.\d+)?\z/
l!l&&r!l&&!r
0022
ref $amount ne "ARRAY" or @{$amount;} != 2 or not defined $amount->[0] or not defined $amount->[1] or not $amount->[0] =~ /\A\d+(?:\.\d+)?\z/
l!l&&r!l&&!r
0022
ref $amount ne "ARRAY" or @{$amount;} != 2 or not defined $amount->[0] or not defined $amount->[1]
l!l&&r!l&&!r
0022
ref $amount ne "ARRAY" or @{$amount;} != 2 or not defined $amount->[0]
l!l&&r!l&&!r
0022
ref $amount ne "ARRAY" or @{$amount;} != 2
l!l&&r!l&&!r
0022
MC/DC0%0/7
ref $amount ne "ARRAY" or @{$amount;} != 2 or not defined $amount->[0] or not defined $amount->[1] or not $amount->[0] =~ /\A\d+(?:\.\d+)?\z/ or not $amount->[1] =~ /\A\d+(?:\.\d+)?\z/ or $amount->[1] == 0
ref $amount ne "ARRAY" @{$amount;} != 2 not defined $amount->[0] not defined $amount->[1] not $amount->[0] =~ /\A\d+(?:\.\d+)?\z/ not $amount->[1] =~ /\A\d+(?:\.\d+)?\z/ $amount->[1] == 0
Truth table12%1/8
A or B or not(C) or not(D) or not(E) or not(F) or G
A= ref $amount ne "ARRAY"B= @{$amount;} != 2C= not defined $amount->[0]D= not defined $amount->[1]E= not $amount->[0] =~ /\A\d+(?:\.\d+)?\z/F= not $amount->[1] =~ /\A\d+(?:\.\d+)?\z/G= $amount->[1] == 0
ABCDEFGresult
1XXXXXX1
01XXXXX1
001XXXX1
0001XXX1
00001XX1
000001X1
00000011
00000000
387 || !defined $amount->[0]
388 || !defined $amount->[1]
389 || $amount->[0] !~ m{\A\d+(?:\.\d+)?\z}
390 || $amount->[1] !~ m{\A\d+(?:\.\d+)?\z}
391 || $amount->[1] == 0;
392 22 push @rates, $amount->[0] / $amount->[1];
393 }
394  
395 11 return @rates;
396 }
397  
398 ########################################################################
399 # Private Methods
400 ########################################################################
401  
402 ########################################################################
403 65 ▶sub _providers {
Subroutine _providers: called CC=8 SCAR=24.2
404 ########################################################################
405 65 ▶ return $PROVIDERS if defined $PROVIDERS;
Branch: 1 branch
BranchTrueFalseTotal
if defined $PROVIDERS 58 7 65
406  
407 # Installed distributions use their share directory. Source checkouts
408 # use the same catalog from the project root for pre-install tests.
409 7 my $path = eval { dist_file( q{LLM-API}, q{providers.yml} ) };
410 7 ▶ if ( !$path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path) 0 7 7
411 0 my $source = File::Spec->catfile( dirname(__FILE__), qw(.. .. providers.yml) );
412 0 ▶ $path = $source if -f $source;
Branch: 1 branch
BranchTrueFalseTotal
if -f $source 0 0 0
413 }
414 7 ▶ die "ERROR: cannot locate LLM provider catalog providers.yml\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $path 0 7 7
415 if !$path;
416  
417 7 my $yaml = YAML::Tiny->read($path);
418 7 ▶ die sprintf "ERROR: cannot read LLM provider catalog %s: %s\n", $path, YAML::Tiny->errstr // q{unknown error}
Branch: 1 branch
BranchTrueFalseTotal
unless $yaml 0 7 7
Condition0%0/2
"YAML::Tiny"->errstr // "unknown error"
l!l
00
MC/DC0%0/1
"YAML::Tiny"->errstr // "unknown error"
"YAML::Tiny"->errstr
Truth table0%0/2
A
A= "YAML::Tiny"->errstr
Aresult
11
00
419 if !$yaml;
420  
421 7 my $providers = $yaml->[0]->{providers};
422 7 ▶ die sprintf "ERROR: invalid LLM provider catalog %s\n", $path
Branch: 1 branch
BranchTrueFalseTotal
if ref $providers ne "HASH" 0 7 7
423 if ref $providers ne q{HASH};
424  
425 7 $PROVIDERS = $providers;
426 7 return $PROVIDERS;
427 }
428  
429 ########################################################################
430 34 ▶sub _provider_class {
Subroutine _provider_class: called CC=3 SCAR=11.2
431 ########################################################################
432 34 my ($self) = @_;
433  
434 34 my $class = _providers()->{ $self->get_provider }->{class};
435 34 ▶ die sprintf "ERROR: missing class for LLM provider %s\n", $self->get_provider
Branch: 1 branch
BranchTrueFalseTotal
unless $class 0 34 34
436 if !$class;
437  
438 34 eval "require $class"; ## no critic
439 34 ▶ die sprintf "ERROR: cannot load LLM provider %s: %s\n", $self->get_provider, $@
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 34 34
440 if $@;
441 34 return $class;
442 }
443  
444 ########################################################################
445 16 ▶sub _init {
Subroutine _init: called CC=3 SCAR=11.0
446 ########################################################################
447 16 my ($self) = @_;
448  
449 8 ▶ no strict 'refs'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
450 16 foreach my $key (qw(max_tokens version role)) {
451 48 my $val = ${ 'LLM::API::LLM_' . uc $key };
452  
453 48 ▶ next if defined $self->get($key);
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->get($key) 4 44 48
454  
455 44 $self->set( $key, $val );
456 }
457  
458 16 return;
459 }
460  
461 ########################################################################
462 1 ▶sub _do_api { goto &_do_admin_api; }
Subroutine _do_api: called CC=1 SCAR=0.0
463 ########################################################################
464  
465 ########################################################################
466 1 ▶sub _do_admin_api {
Subroutine _do_admin_api: called CC=1 SCAR=0.0
467 ########################################################################
468 1 my ( $self, @args ) = @_;
469  
470 1 return $self->_provider_class->_do_admin_api( $self, @args );
471 }
472  
473 8 1;
474  
475 __END__
« API/Bedrock.pm ↑ Summary API/Anthropic.pm »