| 1 | package LLM::API::Response; | ||||||||||||
| 2 | |||||||||||||
| 3 | 6 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 6 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | 6 | ▶ | use LLM::API::Usage; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 6 | ▶ | use LLM::API::Content; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 6 | ▶ | use JSON::PP; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 6 | ▶ | use English qw(-no_match_vars); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 6 | ▶ | use Data::Dumper; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | |||||||||||||
| 13 | 6 | our @ACCESSORS = qw( | |||||||||||
| 14 | content | ||||||||||||
| 15 | decoded_content | ||||||||||||
| 16 | id | ||||||||||||
| 17 | model | ||||||||||||
| 18 | provider_response | ||||||||||||
| 19 | raw_response | ||||||||||||
| 20 | role | ||||||||||||
| 21 | stop_reason | ||||||||||||
| 22 | stop_sequence | ||||||||||||
| 23 | type | ||||||||||||
| 24 | usage | ||||||||||||
| 25 | ); | ||||||||||||
| 26 | |||||||||||||
| 27 | 6 | __PACKAGE__->mk_accessors(@ACCESSORS); | |||||||||||
| 28 | |||||||||||||
| 29 | 6 | ▶ | use parent qw(Class::Accessor::Fast); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 30 | |||||||||||||
| 31 | 6 | our $VERSION = '1.0.0'; | |||||||||||
| 32 | |||||||||||||
| 33 | ######################################################################## | ||||||||||||
| 34 | 5 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 35 | ######################################################################## | ||||||||||||
| 36 | 5 | my ( $class, $raw_response ) = @_; | |||||||||||
| 37 | |||||||||||||
| 38 | 5 | my $self = $class->SUPER::new( { raw_response => $raw_response } ); | |||||||||||
| 39 | |||||||||||||
| 40 | 5 | $self->init; | |||||||||||
| 41 | |||||||||||||
| 42 | 5 | return $self; | |||||||||||
| 43 | } | ||||||||||||
| 44 | |||||||||||||
| 45 | ######################################################################## | ||||||||||||
| 46 | 9 | ▶ | sub from_result { | ||||||||||
|
Subroutine
from_result: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 47 | ######################################################################## | ||||||||||||
| 48 | 9 | my ( $class, %args ) = @_; | |||||||||||
| 49 | 9 | my $result = $args{result}; | |||||||||||
| 50 | |||||||||||||
| 51 | 9 | ▶ | die "ERROR: normalized LLM response must be a hash reference\n" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 52 | if ref $result ne q{HASH}; | ||||||||||||
| 53 | |||||||||||||
| 54 | 8 | my $raw_response = { | |||||||||||
| 55 | success => 1, | ||||||||||||
| 56 | status => 200, | ||||||||||||
| 57 | code => 200, | ||||||||||||
| 58 | reason => q{OK}, | ||||||||||||
| 59 | }; | ||||||||||||
| 60 | |||||||||||||
| 61 | my $self = $class->SUPER::new( | ||||||||||||
| 62 | { raw_response => $raw_response, | ||||||||||||
| 63 | provider_response => $args{provider_response}, | ||||||||||||
| 64 | } | ||||||||||||
| 65 | 8 | ); | |||||||||||
| 66 | |||||||||||||
| 67 | 8 | $self->_load_result($result); | |||||||||||
| 68 | |||||||||||||
| 69 | 8 | return $self; | |||||||||||
| 70 | } | ||||||||||||
| 71 | |||||||||||||
| 72 | ######################################################################## | ||||||||||||
| 73 | 2 | ▶ | sub from_error { | ||||||||||
|
Subroutine
from_error: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||
| 74 | ######################################################################## | ||||||||||||
| 75 | 2 | my ( $class, %args ) = @_; | |||||||||||
| 76 | 2 | ▶ | my $code = $args{code} // 500; | ||||||||||
|
Condition100%2/2
$args{"code"} // 500
MC/DC100%1/1
$args{"code"} // 500
$args{"code"}
Truth table100%2/2
A
A= $args{"code"}
| |||||||||||||
| 77 | 2 | ▶ | my $message = $args{message} // q{Provider request failed}; | ||||||||||
|
Condition50%1/2
$args{"message"} // "Provider request failed"
MC/DC0%0/1
$args{"message"} // "Provider request failed"
$args{"message"}
Truth table50%1/2
A
A= $args{"message"}
| |||||||||||||
| 78 | |||||||||||||
| 79 | my $self = $class->SUPER::new( | ||||||||||||
| 80 | { raw_response => { | ||||||||||||
| 81 | success => 0, | ||||||||||||
| 82 | code => $code, | ||||||||||||
| 83 | status => $code, | ||||||||||||
| 84 | reason => $message, | ||||||||||||
| 85 | }, | ||||||||||||
| 86 | provider_response => $args{provider_response}, | ||||||||||||
| 87 | } | ||||||||||||
| 88 | 2 | ); | |||||||||||
| 89 | |||||||||||||
| 90 | 2 | $self->{provider_error_message} = $message; | |||||||||||
| 91 | |||||||||||||
| 92 | 2 | return $self; | |||||||||||
| 93 | } | ||||||||||||
| 94 | |||||||||||||
| 95 | ######################################################################## | ||||||||||||
| 96 | 17 | ▶ | sub is_success { | ||||||||||
|
Subroutine
is_success: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 97 | ######################################################################## | ||||||||||||
| 98 | 17 | my ($self) = @_; | |||||||||||
| 99 | |||||||||||||
| 100 | 17 | return $self->raw_response->{success}; | |||||||||||
| 101 | } | ||||||||||||
| 102 | |||||||||||||
| 103 | ######################################################################## | ||||||||||||
| 104 | 0 | ▶ | sub status { | ||||||||||
|
Subroutine
status: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 105 | ######################################################################## | ||||||||||||
| 106 | 0 | my ($self) = @_; | |||||||||||
| 107 | |||||||||||||
| 108 | 0 | return $self->raw_response->{status}; | |||||||||||
| 109 | } | ||||||||||||
| 110 | |||||||||||||
| 111 | ######################################################################## | ||||||||||||
| 112 | 0 | ▶ | sub reason { | ||||||||||
|
Subroutine
reason: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 113 | ######################################################################## | ||||||||||||
| 114 | 0 | my ($self) = @_; | |||||||||||
| 115 | |||||||||||||
| 116 | 0 | return $self->raw_response->{reason}; | |||||||||||
| 117 | } | ||||||||||||
| 118 | |||||||||||||
| 119 | ######################################################################## | ||||||||||||
| 120 | 3 | ▶ | sub code { | ||||||||||
|
Subroutine
code: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 121 | ######################################################################## | ||||||||||||
| 122 | 3 | my ($self) = @_; | |||||||||||
| 123 | |||||||||||||
| 124 | 3 | return $self->raw_response->{code}; | |||||||||||
| 125 | } | ||||||||||||
| 126 | |||||||||||||
| 127 | ######################################################################## | ||||||||||||
| 128 | 0 | ▶ | sub raw_content { | ||||||||||
|
Subroutine
raw_content: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 129 | ######################################################################## | ||||||||||||
| 130 | 0 | my ($self) = @_; | |||||||||||
| 131 | |||||||||||||
| 132 | 0 | return $self->raw_response->{content}; | |||||||||||
| 133 | } | ||||||||||||
| 134 | |||||||||||||
| 135 | ######################################################################## | ||||||||||||
| 136 | 2 | ▶ | sub error { | ||||||||||
|
Subroutine
error: called CC=3 SCAR=12.0
Pod
undocumented
| |||||||||||||
| 137 | ######################################################################## | ||||||||||||
| 138 | 2 | my ($self) = @_; | |||||||||||
| 139 | |||||||||||||
| 140 | 2 | my $rsp = $self->raw_response; | |||||||||||
| 141 | |||||||||||||
| 142 | return q{} | ||||||||||||
| 143 | 2 | ▶ | if $rsp->{success}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 144 | |||||||||||||
| 145 | return sprintf '%s: %s', $rsp->{code}, $self->{provider_error_message} | ||||||||||||
| 146 | 2 | ▶ | if exists $self->{provider_error_message}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 147 | |||||||||||||
| 148 | 0 | return sprintf '%s: %s', $rsp->{code}, decode_json( $rsp->{content} ); | |||||||||||
| 149 | } | ||||||||||||
| 150 | |||||||||||||
| 151 | ######################################################################## | ||||||||||||
| 152 | 5 | ▶ | sub init { | ||||||||||
|
Subroutine
init: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 153 | ######################################################################## | ||||||||||||
| 154 | 5 | my ($self) = @_; | |||||||||||
| 155 | |||||||||||||
| 156 | 5 | my $rsp = $self->raw_response; | |||||||||||
| 157 | |||||||||||||
| 158 | return | ||||||||||||
| 159 | 5 | ▶ | if !$self->is_success; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 160 | |||||||||||||
| 161 | 4 | my $decoded_content = decode_json( $rsp->{content} ); | |||||||||||
| 162 | 4 | $self->_load_result($decoded_content); | |||||||||||
| 163 | |||||||||||||
| 164 | 4 | return $self; | |||||||||||
| 165 | } | ||||||||||||
| 166 | |||||||||||||
| 167 | ######################################################################## | ||||||||||||
| 168 | 12 | ▶ | sub _load_result { | ||||||||||
|
Subroutine
_load_result: called CC=4 SCAR=13.9
| |||||||||||||
| 169 | ######################################################################## | ||||||||||||
| 170 | 12 | my ( $self, $result ) = @_; | |||||||||||
| 171 | |||||||||||||
| 172 | 12 | $self->decoded_content($result); | |||||||||||
| 173 | 12 | ▶ | $self->usage( LLM::API::Usage->new( $result->{usage} // {} ) ); | ||||||||||
|
Condition50%1/2
$result->{"usage"} // {}
MC/DC0%0/1
$result->{"usage"} // {}
$result->{"usage"}
Truth table50%1/2
A
A= $result->{"usage"}
| |||||||||||||
| 174 | 12 | ▶ | $self->content( LLM::API::Content->new( $result->{content} // [] ) ); | ||||||||||
|
Condition100%2/2
$result->{"content"} // []
MC/DC100%1/1
$result->{"content"} // []
$result->{"content"}
Truth table100%2/2
A
A= $result->{"content"}
| |||||||||||||
| 175 | |||||||||||||
| 176 | 12 | foreach my $attr (qw(type role model id stop_reason stop_sequence)) { | |||||||||||
| 177 | 72 | $self->set( $attr, $result->{$attr} ); | |||||||||||
| 178 | } | ||||||||||||
| 179 | |||||||||||||
| 180 | 12 | return $self; | |||||||||||
| 181 | } | ||||||||||||
| 182 | |||||||||||||
| 183 | ######################################################################## | ||||||||||||
| 184 | 5 | ▶ | sub was_cutoff { | ||||||||||
|
Subroutine
was_cutoff: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 185 | ######################################################################## | ||||||||||||
| 186 | 5 | my ($self) = @_; | |||||||||||
| 187 | |||||||||||||
| 188 | 5 | return $self->stop_reason eq 'max_tokens'; | |||||||||||
| 189 | } | ||||||||||||
| 190 | |||||||||||||
| 191 | 6 | 1; | |||||||||||
| 192 | |||||||||||||
| 193 | __END__ | ||||||||||||