API/Bedrock.pm

filter: Statementstmt 92.7% 89 / 96 Branchbran 70.8% 34 / 48 Conditioncond 51.4% 35 / 68 MC/DCmcdc 21.2% 7 / 33 Subroutinesub 75.0% 9 / 12 Podpod 0.0% 0 / 4 totaltotal 66.6% 174 / 261 CC 57 SCAR 34.4
CC
57
Cov
75%
_content
47.5
send_messages
29.8
count_input_tokens
6.0
CRAP 31.2
↑ Summary API.pm »
1 package LLM::API::Bedrock;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use LLM::API::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use MIME::Base64 qw(decode_base64 encode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 our $VERSION = '2.0.0';
11  
12 ########################################################################
13 0 ▶sub _runtime {
Subroutine _runtime: not called CC=1 SCAR=6.9
14 ########################################################################
15 0 my ( $class, $self ) = @_;
16  
17 # Neither AWS dependency is needed when the Anthropic provider is used.
18 0 require Amazon::Credentials;
19 0 require Amazon::API::BedrockRuntime;
20  
21 0 my $credentials = Amazon::Credentials->new;
22 0 return Amazon::API::BedrockRuntime->new(
23 credentials => $credentials,
24 region => $self->get_region,
25 );
26 }
27  
28 ########################################################################
29 23 ▶sub _content {
Subroutine _content: called CC=31 SCAR=38.6
30 ########################################################################
31 23 my ( $class, $blocks ) = @_;
32  
33 23 my @converted;
34 23 my $has_document = 0;
35 23 my $has_prompt = 0;
36  
37 23 for my $block ( @{$blocks} ) {
38 28 ▶ die "ERROR: invalid Bedrock content block\n"
Branch: 1 branch
BranchTrueFalseTotal
if ref $block ne "HASH" 0 28 28
39 if ref $block ne q{HASH};
40  
41 28 ▶ if ( ( $block->{type} // q{} ) eq q{text} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($block->{"type"} // "") eq "text") 20 8 28
Condition50%1/2
$block->{"type"} // ""
l!l
280
MC/DC0%0/1
$block->{"type"} // ""
$block->{"type"}
Truth table50%1/2
A
A= $block->{"type"}
Aresult
11
00
42 die "ERROR: Bedrock text block must contain text\n"
43 20 ▶ if !defined $block->{text};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $block->{"text"} 0 20 20
44 20 push @converted, { text => $block->{text} };
45 20 ▶ $has_prompt = 1 if $block->{text} =~ m{\S};
Branch: 1 branch
BranchTrueFalseTotal
if $block->{"text"} =~ /\S/ 18 2 20
46 20 next;
47 }
48  
49 die "ERROR: unsupported Bedrock content block\n"
50 8 ▶ if ( $block->{type} // q{} ) ne q{document};
Branch: 1 branch
BranchTrueFalseTotal
if ($block->{"type"} // "") ne "document" 0 8 8
Condition50%1/2
$block->{"type"} // ""
l!l
80
MC/DC0%0/1
$block->{"type"} // ""
$block->{"type"}
Truth table50%1/2
A
A= $block->{"type"}
Aresult
11
00
51  
52 8 my $source = $block->{source};
53 die "ERROR: invalid Bedrock document source\n"
54 8 ▶ if ref $source ne q{HASH} || !defined $source->{data};
Branch: 1 branch
BranchTrueFalseTotal
if ref $source ne "HASH" or not defined $source->{"data"} 1 7 8
Condition66%2/3
ref $source ne "HASH" or not defined $source->{"data"}
l!l&&r!l&&!r
017
MC/DC50%1/2
ref $source ne "HASH" or not defined $source->{"data"}
ref $source ne "HASH" not defined $source->{"data"}
Truth table66%2/3
A or not(B)
A= ref $source ne "HASH"B= not defined $source->{"data"}
ABresult
1X1
011
000
55  
56 7 ▶ if ( ( $source->{media_type} // q{} ) eq q{text/plain} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($source->{"media_type"} // "") eq "text/plain") 1 6 7
Condition50%1/2
$source->{"media_type"} // ""
l!l
70
MC/DC0%0/1
$source->{"media_type"} // ""
$source->{"media_type"}
Truth table50%1/2
A
A= $source->{"media_type"}
Aresult
11
00
57 1 my $data = $source->{data};
58 $data = decode_base64($data)
59 1 ▶ if ( $source->{type} // q{} ) eq q{base64};
Branch: 1 branch
BranchTrueFalseTotal
if ($source->{"type"} // "") eq "base64" 0 1 1
Condition50%1/2
$source->{"type"} // ""
l!l
10
MC/DC0%0/1
$source->{"type"} // ""
$source->{"type"}
Truth table50%1/2
A
A= $source->{"type"}
Aresult
11
00
60 die "ERROR: unsupported document source encoding\n"
61 1 ▶ if ( $source->{type} // q{} ) ne q{text} && ( $source->{type} // q{} ) ne q{base64};
Branch: 1 branch
BranchTrueFalseTotal
if ($source->{"type"} // "") ne "text" and ($source->{"type"} // "") ne "base64" 0 1 1
Condition28%2/7
($source->{"type"} // "") ne "text" and ($source->{"type"} // "") ne "base64"
!ll&&!rl&&r
100
$source->{"type"} // ""
l!l
10
$source->{"type"} // ""
l!l
00
MC/DC0%0/2
($source->{"type"} // "") ne "text" and ($source->{"type"} // "") ne "base64"
$source->{"type"} $source->{"type"}
Truth table0%0/3
A and B
A= $source->{"type"}B= $source->{"type"}
ABresult
0X0
100
111
62 1 push @converted, { text => $data };
63 1 next;
64 }
65  
66 die "ERROR: unsupported Bedrock document media type\n"
67 6 ▶ if ( $source->{media_type} // q{} ) ne q{application/pdf};
Branch: 1 branch
BranchTrueFalseTotal
if ($source->{"media_type"} // "") ne "application/pdf" 1 5 6
Condition50%1/2
$source->{"media_type"} // ""
l!l
60
MC/DC0%0/1
$source->{"media_type"} // ""
$source->{"media_type"}
Truth table50%1/2
A
A= $source->{"media_type"}
Aresult
11
00
68  
69 die "ERROR: unsupported document source encoding\n"
70 5 ▶ if ( $source->{type} // q{} ) ne q{text} && ( $source->{type} // q{} ) ne q{base64};
Branch: 1 branch
BranchTrueFalseTotal
if ($source->{"type"} // "") ne "text" and ($source->{"type"} // "") ne "base64" 0 5 5
Condition42%3/7
($source->{"type"} // "") ne "text" and ($source->{"type"} // "") ne "base64"
!ll&&!rl&&r
050
$source->{"type"} // ""
l!l
50
$source->{"type"} // ""
l!l
50
MC/DC0%0/2
($source->{"type"} // "") ne "text" and ($source->{"type"} // "") ne "base64"
$source->{"type"} $source->{"type"}
Truth table33%1/3
A and B
A= $source->{"type"}B= $source->{"type"}
ABresult
0X0
100
111
71  
72 # LLM::API::document() retains its Anthropic-compatible source structure.
73 # The Bedrock JSON request requires base64 in document.source.bytes.
74 5 ▶ my $bytes = $source->{type} eq q{base64} ? decode_base64( $source->{data} ) : $source->{data};
Branch: 1 branch
BranchTrueFalseTotal
$source->{'type'} eq 'base64' ? : 5 0 5
75 5 ▶ die "ERROR: PDF document is empty\n"
Branch: 1 branch
BranchTrueFalseTotal
unless length $bytes 0 5 5
76 if !length $bytes;
77  
78 5 ▶ my $name = $block->{title} // q{Document};
Condition50%1/2
$block->{"title"} // "Document"
l!l
50
MC/DC0%0/1
$block->{"title"} // "Document"
$block->{"title"}
Truth table50%1/2
A
A= $block->{"title"}
Aresult
11
00
79 5 $name =~ s{[^a-zA-Z0-9 ()\[\]-]}{ }g;
80 5 $name =~ s{\s+}{ }g;
81 5 $name =~ s{\A\s+|\s+\z}{}g;
82 5 ▶ $name = q{Document} if !length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless length $name 0 5 5
83 5 $name = substr $name, 0, 200;
84  
85 5 push @converted,
86 {
87 document => {
88 format => q{pdf},
89 name => $name,
90 source => { bytes => $bytes },
91 },
92 };
93 5 $has_document = 1;
94 }
95  
96 21 ▶ die "ERROR: Bedrock PDF documents require an accompanying text prompt\n"
Branch: 1 branch
BranchTrueFalseTotal
if $has_document and not $has_prompt 2 19 21
Condition100%3/3
$has_document and not $has_prompt
!ll&&!rl&&r
1632
MC/DC100%2/2
$has_document and not $has_prompt
$has_document not $has_prompt
Truth table100%3/3
A and not(B)
A= $has_documentB= not $has_prompt
ABresult
0X0
100
111
97 if $has_document && !$has_prompt;
98  
99 19 return \@converted;
100 }
101  
102 ########################################################################
103 8 ▶sub send_prompt {
Subroutine send_prompt: called CC=3 SCAR=12.5
Pod undocumented
104 ########################################################################
105 8 my ( $class, $self, $content ) = @_;
106  
107 8 ▶ die "ERROR: usage: send_prompt(array-ref)\n"
Branch: 1 branch
BranchTrueFalseTotal
if not ref $content or builtin::reftype($content) ne "ARRAY" 0 8 8
Condition33%1/3
not ref $content or builtin::reftype($content) ne "ARRAY"
l!l&&r!l&&!r
008
MC/DC0%0/2
not ref $content or builtin::reftype($content) ne "ARRAY"
not ref $content builtin::reftype($content) ne "ARRAY"
Truth table33%1/3
not(A) or B
A= not ref $contentB= builtin::reftype($content) ne "ARRAY"
ABresult
1X1
011
000
108 if !ref $content || reftype($content) ne q{ARRAY};
109  
110 8 return $class->send_messages( $self, [ { role => $self->get_role, content => $content } ] );
111 }
112  
113 ########################################################################
114 10 ▶sub send_messages {
Subroutine send_messages: called CC=20 SCAR=33.9
Pod undocumented
115 ########################################################################
116 10 my ( $class, $self, $messages ) = @_;
117  
118 10 my @converted;
119 10 for my $message ( @{$messages} ) {
120 14 ▶ if ( $message->{role} ne q{user} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($message->{"role"} ne "user") 2 12 14
121 2 for my $block ( @{ $message->{content} } ) {
122 die "ERROR: Bedrock documents are supported only in user messages\n"
123 2 ▶ if ( $block->{type} // q{} ) eq q{document};
Branch: 1 branch
BranchTrueFalseTotal
if ($block->{"type"} // "") eq "document" 0 2 2
Condition50%1/2
$block->{"type"} // ""
l!l
20
MC/DC0%0/1
$block->{"type"} // ""
$block->{"type"}
Truth table50%1/2
A
A= $block->{"type"}
Aresult
11
00
124 }
125 }
126  
127 push @converted,
128 {
129 role => $message->{role},
130 14 content => $class->_content( $message->{content} ),
131 };
132 }
133  
134 7 my $result;
135 7 my $ok = eval {
136 7 $result = $class->_runtime($self)->Converse(
137 { modelId => $self->get_model,
138 inferenceConfig => { maxTokens => $self->get_max_tokens },
139 messages => \@converted,
140 }
141 );
142 6 return 1;
143 };
144  
145 7 ▶ if ( !$ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 1 6 7
146 1 ▶ my $error = $@ || q{Unknown Bedrock error};
Condition50%1/2
$@ || 'Unknown Bedrock error'
l!l
10
MC/DC0%0/1
$@ || 'Unknown Bedrock error'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
147 1 return LLM::API::Response->from_error( message => $error );
148 }
149  
150 return LLM::API::Response->from_error( message => q{Invalid Bedrock response}, provider_response => $result )
151 if ref $result ne q{HASH}
152 || ref $result->{output} ne q{HASH}
153 || ref $result->{output}->{message} ne q{HASH}
154 6 ▶ || ref $result->{output}->{message}->{content} ne q{ARRAY};
Branch: 1 branch
BranchTrueFalseTotal
if ref $result ne "HASH" or ref $result->{"output"} ne "HASH" or ref $result->{"output"}{"message"} ne "HASH" or ref $result->{"output"}{"message"}{"content"} ne "ARRAY" 0 6 6
Condition33%3/9
ref $result ne "HASH" or ref $result->{"output"} ne "HASH" or ref $result->{"output"}{"message"} ne "HASH" or ref $result->{"output"}{"message"}{"content"} ne "ARRAY"
l!l&&r!l&&!r
006
ref $result ne "HASH" or ref $result->{"output"} ne "HASH" or ref $result->{"output"}{"message"} ne "HASH"
l!l&&r!l&&!r
006
ref $result ne "HASH" or ref $result->{"output"} ne "HASH"
l!l&&r!l&&!r
006
MC/DC0%0/4
ref $result ne "HASH" or ref $result->{"output"} ne "HASH" or ref $result->{"output"}{"message"} ne "HASH" or ref $result->{"output"}{"message"}{"content"} ne "ARRAY"
ref $result ne "HASH" ref $result->{"output"} ne "HASH" ref $result->{"output"}{"message"} ne "HASH" ref $result->{"output"}{"message"}{"content"} ne "ARRAY"
Truth table20%1/5
A or B or C or D
A= ref $result ne "HASH"B= ref $result->{"output"} ne "HASH"C= ref $result->{"output"}{"message"} ne "HASH"D= ref $result->{"output"}{"message"}{"content"} ne "ARRAY"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
155  
156 6 my @content;
157 6 for my $block ( @{ $result->{output}->{message}->{content} } ) {
158 7 ▶ next if ref $block ne q{HASH};
Branch: 1 branch
BranchTrueFalseTotal
if ref $block ne "HASH" 0 7 7
159 push @content, { type => q{text}, text => $block->{text} }
160 7 ▶ if defined $block->{text};
Branch: 1 branch
BranchTrueFalseTotal
if defined $block->{"text"} 7 0 7
161 }
162  
163 6 ▶ my $usage = $result->{usage} // {};
Condition50%1/2
$result->{"usage"} // {}
l!l
60
MC/DC0%0/1
$result->{"usage"} // {}
$result->{"usage"}
Truth table50%1/2
A
A= $result->{"usage"}
Aresult
11
00
164 6 my %stops = ( end_turn => q{end_turn}, max_tokens => q{max_tokens} );
165 6 ▶ my $stop = $result->{stopReason} // q{};
Condition50%1/2
$result->{"stopReason"} // ""
l!l
60
MC/DC0%0/1
$result->{"stopReason"} // ""
$result->{"stopReason"}
Truth table50%1/2
A
A= $result->{"stopReason"}
Aresult
11
00
166  
167 return LLM::API::Response->from_result(
168 result => {
169 type => q{message},
170 role => q{assistant},
171 model => $self->get_model,
172 stop_reason => $stops{$stop} // $stop,
173 content => \@content,
174 usage => {
175 input_tokens => $usage->{inputTokens} // 0,
176 6 ▶ output_tokens => $usage->{outputTokens} // 0,
Condition42%3/7
$stops{$stop} // $stop
l!l&&r!l&&!r
600
$usage->{"inputTokens"} // 0
l!l
60
$usage->{"outputTokens"} // 0
l!l
60
MC/DC0%0/2
$stops{$stop} // $stop
$stops{$stop} $stop
MC/DC0%0/1
$usage->{"inputTokens"} // 0
$usage->{"inputTokens"}
MC/DC0%0/1
$usage->{"outputTokens"} // 0
$usage->{"outputTokens"}
Truth table33%1/3
A // B
A= $stops{$stop}B= $stop
ABresult
1X1
011
000
Truth table50%1/2
A
A= $usage->{"inputTokens"}
Aresult
11
00
Truth table50%1/2
A
A= $usage->{"outputTokens"}
Aresult
11
00
177 },
178 },
179 provider_response => $result,
180 );
181 }
182  
183 ########################################################################
184 11 ▶sub count_input_tokens {
Subroutine count_input_tokens: called CC=6 SCAR=17.9
Pod undocumented
185 ########################################################################
186 11 my ( $class, $self, $content ) = @_;
187  
188 11 ▶ die "ERROR: usage: count_input_tokens(array-ref)\n"
Branch: 1 branch
BranchTrueFalseTotal
if not ref $content or builtin::reftype($content) ne "ARRAY" 2 9 11
Condition66%2/3
not ref $content or builtin::reftype($content) ne "ARRAY"
l!l&&r!l&&!r
209
MC/DC50%1/2
not ref $content or builtin::reftype($content) ne "ARRAY"
not ref $content builtin::reftype($content) ne "ARRAY"
Truth table66%2/3
not(A) or B
A= not ref $contentB= builtin::reftype($content) ne "ARRAY"
ABresult
1X1
011
000
189 if !ref $content || reftype($content) ne q{ARRAY};
190  
191 9 my $blocks = $class->_content($content);
192 8 my $result = $class->_runtime($self)->CountTokens(
193 { modelId => $self->get_model,
194 input => { converse => { messages => [ { role => $self->get_role, content => $blocks } ] }, },
195 }
196 );
197  
198 die "ERROR: invalid Bedrock CountTokens response\n"
199 if ref $result ne q{HASH}
200 || !defined $result->{inputTokens}
201 7 ▶ || $result->{inputTokens} !~ m{\A[0-9]+\z};
Branch: 1 branch
BranchTrueFalseTotal
if ref $result ne "HASH" or not defined $result->{"inputTokens"} or not $result->{"inputTokens"} =~ /\A[0-9]+\z/ 3 4 7
Condition100%6/6
ref $result ne "HASH" or not defined $result->{"inputTokens"} or not $result->{"inputTokens"} =~ /\A[0-9]+\z/
l!l&&r!l&&!r
214
ref $result ne "HASH" or not defined $result->{"inputTokens"}
l!l&&r!l&&!r
115
MC/DC100%3/3
ref $result ne "HASH" or not defined $result->{"inputTokens"} or not $result->{"inputTokens"} =~ /\A[0-9]+\z/
ref $result ne "HASH" not defined $result->{"inputTokens"} not $result->{"inputTokens"} =~ /\A[0-9]+\z/
Truth table100%4/4
A or not(B) or not(C)
A= ref $result ne "HASH"B= not defined $result->{"inputTokens"}C= not $result->{"inputTokens"} =~ /\A[0-9]+\z/
ABCresult
1XX1
01X1
0011
0000
202  
203 4 my ($input_rate) = $self->pricing( $self->get_model );
204 3 my $input_tokens = 0 + $result->{inputTokens};
205  
206 3 return ( $input_tokens, $input_tokens * $input_rate );
207 }
208  
209 ########################################################################
210 0 ▶sub headers {
Subroutine headers: not called CC=1 SCAR=6.9
Pod undocumented
211 ########################################################################
212 0 die "ERROR: headers is not supported by the Bedrock provider\n";
213 }
214  
215 ########################################################################
216 0 ▶sub _do_admin_api {
Subroutine _do_admin_api: not called CC=1 SCAR=6.9
217 ########################################################################
218 0 die "ERROR: administrative API operations are not supported by the Bedrock provider\n";
219 }
220  
221 4 1;
↑ Summary API.pm »