| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | if ref $block ne q{HASH}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 28 | ▶ | if ( ( $block->{type} // q{} ) eq q{text} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$block->{"type"} // ""
MC/DC0%0/1
$block->{"type"} // ""
$block->{"type"}
Truth table50%1/2
A
A= $block->{"type"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | die "ERROR: Bedrock text block must contain text\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 20 | ▶ | if !defined $block->{text}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 20 | push @converted, { text => $block->{text} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 20 | ▶ | $has_prompt = 1 if $block->{text} =~ m{\S}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition50%1/2
$block->{"type"} // ""
MC/DC0%0/1
$block->{"type"} // ""
$block->{"type"}
Truth table50%1/2
A
A= $block->{"type"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
ref $source ne "HASH" or not defined $source->{"data"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 7 | ▶ | if ( ( $source->{media_type} // q{} ) eq q{text/plain} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$source->{"media_type"} // ""
MC/DC0%0/1
$source->{"media_type"} // ""
$source->{"media_type"}
Truth table50%1/2
A
A= $source->{"media_type"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 1 | my $data = $source->{data}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | $data = decode_base64($data) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 1 | ▶ | if ( $source->{type} // q{} ) eq q{base64}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$source->{"type"} // ""
MC/DC0%0/1
$source->{"type"} // ""
$source->{"type"}
Truth table50%1/2
A
A= $source->{"type"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition28%2/7
($source->{"type"} // "") ne "text" and ($source->{"type"} // "") ne "base64"
$source->{"type"} // ""
$source->{"type"} // ""
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition50%1/2
$source->{"media_type"} // ""
MC/DC0%0/1
$source->{"media_type"} // ""
$source->{"media_type"}
Truth table50%1/2
A
A= $source->{"media_type"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition42%3/7
($source->{"type"} // "") ne "text" and ($source->{"type"} // "") ne "base64"
$source->{"type"} // ""
$source->{"type"} // ""
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 5 | ▶ | die "ERROR: PDF document is empty\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | if !length $bytes; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 5 | ▶ | my $name = $block->{title} // q{Document}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$block->{"title"} // "Document"
MC/DC0%0/1
$block->{"title"} // "Document"
$block->{"title"}
Truth table50%1/2
A
A= $block->{"title"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$has_document and not $has_prompt
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
not ref $content or builtin::reftype($content) ne "ARRAY"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition50%1/2
$block->{"type"} // ""
MC/DC0%0/1
$block->{"type"} // ""
$block->{"type"}
Truth table50%1/2
A
A= $block->{"type"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 1 | ▶ | my $error = $@ || q{Unknown Bedrock error}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$@ || 'Unknown Bedrock error'
MC/DC0%0/1
$@ || 'Unknown Bedrock error'
$@
Truth table50%1/2
A
A= $@
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
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"
ref $result ne "HASH" or ref $result->{"output"} ne "HASH" or ref $result->{"output"}{"message"} ne "HASH"
ref $result ne "HASH" or ref $result->{"output"} ne "HASH"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | push @content, { type => q{text}, text => $block->{text} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 7 | ▶ | if defined $block->{text}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 6 | ▶ | my $usage = $result->{usage} // {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$result->{"usage"} // {}
MC/DC0%0/1
$result->{"usage"} // {}
$result->{"usage"}
Truth table50%1/2
A
A= $result->{"usage"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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"} // ""
MC/DC0%0/1
$result->{"stopReason"} // ""
$result->{"stopReason"}
Truth table50%1/2
A
A= $result->{"stopReason"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
$usage->{"inputTokens"} // 0
$usage->{"outputTokens"} // 0
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
Truth table50%1/2
A
A= $usage->{"inputTokens"}
Truth table50%1/2
A
A= $usage->{"outputTokens"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
not ref $content or builtin::reftype($content) ne "ARRAY"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%6/6
ref $result ne "HASH" or not defined $result->{"inputTokens"} or not $result->{"inputTokens"} =~ /\A[0-9]+\z/
ref $result ne "HASH" or not defined $result->{"inputTokens"}
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/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||