API/Anthropic.pm

filter: Statementstmt 95.4% 42 / 44 Branchbran 62.5% 5 / 8 Conditioncond 44.4% 4 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 4 totaltotal 76.8% 63 / 82 CC 8 SCAR 10.5
CC
8
Cov
84%
count_input_tokens
6.1
send_prompt
3.0
_do_admin_api
2.1
CRAP 2.9
« API.pm ↑ Summary API/Content.pm »
1 package LLM::API::Anthropic;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use JSON::PP qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use LLM::API::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 our $VERSION = '2.0.0';
12  
13 # Anthropic transport for LLM::API. The public client object is passed
14 # explicitly so its existing accessors, options, and response behavior
15 # remain unchanged while provider implementations are separated.
16  
17 ########################################################################
18 1 ▶sub count_input_tokens {
Subroutine count_input_tokens: called CC=5 SCAR=18.0
Pod undocumented
19 ########################################################################
20 1 my ( $class, $self, $content ) = @_;
21  
22 1 ▶ die "ERROR: usage: send_prompt(array-ref)\n"
Branch: 1 branch
BranchTrueFalseTotal
if not ref $content or builtin::reftype($content) ne "ARRAY" 0 1 1
Condition33%1/3
not ref $content or builtin::reftype($content) ne "ARRAY"
l!l&&r!l&&!r
001
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
23 if !ref $content || reftype($content) ne 'ARRAY';
24  
25 1 my $req = {
26 headers => $self->headers( 'content-type' => 'application/json' ),
27 content => encode_json(
28 { model => $self->get_model,
29 messages => [
30 { role => $self->get_role,
31 content => $content,
32 }
33 ]
34 }
35 )
36 };
37  
38 1 my $rsp
39 = HTTP::Tiny->new( timeout => $self->get_timeout )->post( $LLM::API::LLM_URL_BASE . $LLM::API::LLM_COUNT_TOKENS_EP, $req );
40  
41 0 die sprintf "ERROR %s:%s\n", @{$rsp}{qw(code content)}
42 1 ▶ if !$rsp->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $rsp->{"success"} 0 1 1
43  
44 1 ▶ my ($input_token_cost) = $self->pricing( $self->get_model // $LLM::API::DEFAULT_LLM_MODEL );
Condition33%1/3
$self->get_model // $LLM::API::DEFAULT_LLM_MODEL
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->get_model // $LLM::API::DEFAULT_LLM_MODEL
$self->get_model $LLM::API::DEFAULT_LLM_MODEL
Truth table33%1/3
A // B
A= $self->get_modelB= $LLM::API::DEFAULT_LLM_MODEL
ABresult
1X1
011
000
45  
46 1 my $decoded = decode_json( $rsp->{content} );
47  
48 1 my $input_tokens = $decoded->{input_tokens};
49  
50 1 return ( $input_tokens, $input_tokens * $input_token_cost );
51 }
52  
53 ########################################################################
54 4 ▶sub send_prompt {
Subroutine send_prompt: called CC=3 SCAR=11.0
Pod undocumented
55 ########################################################################
56 4 my ( $class, $self, $content ) = @_;
57  
58 4 ▶ die "ERROR: usage: send_prompt(array-ref)\n"
Branch: 1 branch
BranchTrueFalseTotal
if not ref $content or builtin::reftype($content) ne "ARRAY" 1 3 4
Condition66%2/3
not ref $content or builtin::reftype($content) ne "ARRAY"
l!l&&r!l&&!r
103
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
59 if !ref $content || reftype($content) ne 'ARRAY';
60  
61 3 return $class->send_messages( $self, [ { role => $self->get_role, content => $content } ] );
62 }
63  
64 ########################################################################
65 4 ▶sub send_messages {
Subroutine send_messages: called CC=1 SCAR=0.0
Pod undocumented
66 ########################################################################
67 4 my ( $class, $self, $messages ) = @_;
68  
69 4 my $req = {
70 headers => $self->headers( 'content-type' => 'application/json' ),
71 content => encode_json(
72 { model => $self->get_model,
73 max_tokens => $self->get_max_tokens,
74 messages => $messages,
75 }
76 )
77 };
78  
79 4 my $rsp
80 = HTTP::Tiny->new( timeout => $self->get_timeout )->post( $LLM::API::LLM_URL_BASE . $LLM::API::LLM_MESSAGES_EP, $req );
81  
82 4 return LLM::API::Response->new($rsp);
83 }
84  
85 ########################################################################
86 6 ▶sub headers {
Subroutine headers: called CC=1 SCAR=0.0
Pod undocumented
87 ########################################################################
88 6 my ( $class, $self, @headers ) = @_;
89  
90 return {
91 6 @headers,
92 'x-api-key' => $self->get_api_key_getter->(),
93 'anthropic-version' => $self->get_version,
94 };
95 }
96  
97 ########################################################################
98 1 ▶sub _do_admin_api {
Subroutine _do_admin_api: called CC=2 SCAR=7.4
99 ########################################################################
100 1 my ( $class, $self, $url ) = @_;
101  
102 1 my $rsp = HTTP::Tiny->new( timeout => $self->get_timeout )->get( $url, { headers => $self->headers } );
103  
104 0 die sprintf "%s:%s\n", @{$rsp}{qw(code content)}
105 1 ▶ if !$rsp->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $rsp->{"success"} 0 1 1
106  
107 1 return decode_json( $rsp->{content} );
108 }
109  
110 3 1;
« API.pm ↑ Summary API/Content.pm »