| 1 | package Praxo::Adapters::Anthropic; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use HTTP::Tiny (); | ||
| 6 | use Praxo::Errors qw(praxo_error); | ||
| 7 | use Praxo::Json (); | ||
| 8 | use Praxo::Models (); | ||
| 9 | |||
| 10 | sub new { | ||
| 11 | my ($class, %args) = @_; | ||
| 12 | my $base_url = $args{base_url} // 'https://api.anthropic.com'; | ||
| 13 | $base_url =~ s{/$}{}; | ||
| 14 | return bless { | ||
| 15 | base_url => $base_url, | ||
| 16 | model => $args{model} // '', | ||
| 17 | api_key => $args{api_key}, | ||
| 18 | version => $args{version} // '2023-06-01', | ||
| 19 | max_tokens => $args{max_tokens} // 1024, | ||
| 20 | transport => $args{transport}, | ||
| 21 | timeout => $args{timeout} // 60, | ||
| 22 | }, $class; | ||
| 23 | } | ||
| 24 | |||
| 25 | sub generate { | ||
| 26 | my ($self, $request) = @_; | ||
| 27 | praxo_error('MODEL_CONFIG', 'anthropic: API key is required') | ||
| 28 | unless defined $self->{api_key} && $self->{api_key} ne ''; | ||
| 29 | praxo_error('MODEL_CONFIG', 'anthropic: model is required') | ||
| 30 | unless $self->{model} ne ''; | ||
| 31 | my $payload = _payload($self, $request); | ||
| 32 | my $data = $self->{transport} | ||
| 33 | ? $self->{transport}->('/v1/messages', $payload) | ||
| 34 | : $self->_post('/v1/messages', $payload); | ||
| 35 | return _parse_response($data); | ||
| 36 | } | ||
| 37 | |||
| 38 | sub _payload { | ||
| 39 | my ($self, $request) = @_; | ||
| 40 | my ($system, $messages) = _build_messages($request->{messages}); | ||
| 41 | my %payload = ( | ||
| 42 | model => $self->{model}, | ||
| 43 | max_tokens => $self->{max_tokens}, | ||
| 44 | messages => $messages, | ||
| 45 | ); | ||
| 46 | $payload{system} = $system if $system ne ''; | ||
| 47 | my @tools = map { | ||
| 48 | { | ||
| 49 | name => $_->{name}, | ||
| 50 | description => $_->{description} // '', | ||
| 51 | input_schema => $_->{parameters} // { type => 'object', properties => {} }, | ||
| 52 | } | ||
| 53 | } @{ $request->{tools} // [] }; | ||
| 54 | if (@tools) { | ||
| 55 | $payload{tools} = \@tools; | ||
| 56 | my $choice = _tool_choice($request->{toolChoice}); | ||
| 57 | $payload{tool_choice} = $choice if $choice; | ||
| 58 | } | ||
| 59 | $payload{temperature} = $request->{temperature} | ||
| 60 | if defined $request->{temperature} && $request->{temperature} != 0; | ||
| 61 | return \%payload; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub _build_messages { | ||
| 65 | my ($messages) = @_; | ||
| 66 | my @system; | ||
| 67 | my @out; | ||
| 68 | my @pending; | ||
| 69 | my $flush = sub { | ||
| 70 | return unless @pending; | ||
| 71 | push @out, { role => 'user', content => [@pending] }; | ||
| 72 | @pending = (); | ||
| 73 | }; | ||
| 74 | for my $message (@{ $messages // [] }) { | ||
| 75 | if ($message->{role} eq Praxo::Models::SYSTEM()) { | ||
| 76 | push @system, $message->{content} | ||
| 77 | if defined $message->{content} && $message->{content} ne ''; | ||
| 78 | next; | ||
| 79 | } | ||
| 80 | if ($message->{role} eq Praxo::Models::TOOL()) { | ||
| 81 | push @pending, { | ||
| 82 | type => 'tool_result', | ||
| 83 | tool_use_id => $message->{toolId} // '', | ||
| 84 | content => $message->{content} // '', | ||
| 85 | }; | ||
| 86 | next; | ||
| 87 | } | ||
| 88 | $flush->(); | ||
| 89 | my $content = _message_content($message); | ||
| 90 | push @out, { role => $message->{role}, content => $content } if @$content; | ||
| 91 | } | ||
| 92 | $flush->(); | ||
| 93 | return (join("\n\n", @system), \@out); | ||
| 94 | } | ||
| 95 | |||
| 96 | sub _message_content { | ||
| 97 | my ($message) = @_; | ||
| 98 | my @content; | ||
| 99 | push @content, { type => 'text', text => $message->{content} } | ||
| 100 | if defined $message->{content} && $message->{content} ne ''; | ||
| 101 | for my $block (@{ $message->{contentBlocks} // [] }) { | ||
| 102 | push @content, { type => 'text', text => $block->{text} } | ||
| 103 | if ($block->{type} // '') eq 'text' | ||
| 104 | && defined $block->{text} && $block->{text} ne ''; | ||
| 105 | } | ||
| 106 | for my $call (@{ $message->{toolCalls} // [] }) { | ||
| 107 | push @content, { | ||
| 108 | type => 'tool_use', | ||
| 109 | id => $call->{id} // '', | ||
| 110 | name => $call->{name} // '', | ||
| 111 | input => $call->{arguments} // {}, | ||
| 112 | }; | ||
| 113 | } | ||
| 114 | return \@content; | ||
| 115 | } | ||
| 116 | |||
| 117 | sub _tool_choice { | ||
| 118 | my ($choice) = @_; | ||
| 119 | return { type => 'auto' } if defined $choice && $choice eq 'auto'; | ||
| 120 | return { type => 'any' } if defined $choice && $choice eq 'required'; | ||
| 121 | return { type => 'none' } if defined $choice && $choice eq 'none'; | ||
| 122 | return undef; | ||
| 123 | } | ||
| 124 | |||
| 125 | sub _parse_response { | ||
| 126 | my ($data) = @_; | ||
| 127 | praxo_error('MODEL_HTTP', 'anthropic: ' . ($data->{error}{message} // 'error')) | ||
| 128 | if ref($data->{error}) eq 'HASH'; | ||
| 129 | my $text = ''; | ||
| 130 | my @calls; | ||
| 131 | for my $block (@{ $data->{content} // [] }) { | ||
| 132 | if (($block->{type} // '') eq 'text') { | ||
| 133 | $text .= $block->{text} // ''; | ||
| 134 | } elsif (($block->{type} // '') eq 'tool_use') { | ||
| 135 | push @calls, Praxo::Models::tool_call( | ||
| 136 | id => $block->{id} // '', | ||
| 137 | name => $block->{name} // '', | ||
| 138 | arguments => $block->{input} // {}, | ||
| 139 | ); | ||
| 140 | } | ||
| 141 | } | ||
| 142 | my $usage = ref($data->{usage}) eq 'HASH' ? $data->{usage} : {}; | ||
| 143 | my $response = Praxo::Models::response( | ||
| 144 | message => Praxo::Models::message_assistant($text, \@calls), | ||
| 145 | usage => Praxo::Models::usage( | ||
| 146 | input_tokens => $usage->{input_tokens} // 0, | ||
| 147 | output_tokens => $usage->{output_tokens} // 0, | ||
| 148 | cached_tokens => $usage->{cache_read_input_tokens} // 0, | ||
| 149 | ), | ||
| 150 | finish_reason => $data->{stop_reason}, | ||
| 151 | ); | ||
| 152 | return $response; | ||
| 153 | } | ||
| 154 | |||
| 155 | sub _post { | ||
| 156 | my ($self, $path, $payload) = @_; | ||
| 157 | my %headers = ( | ||
| 158 | 'content-type' => 'application/json', | ||
| 159 | 'x-api-key' => $self->{api_key}, | ||
| 160 | 'anthropic-version' => $self->{version}, | ||
| 161 | ); | ||
| 162 | my $http = HTTP::Tiny->new(timeout => $self->{timeout}); | ||
| 163 | my $response = $http->post( | ||
| 164 | $self->{base_url} . $path, | ||
| 165 | { headers => \%headers, content => Praxo::Json::stringify($payload) }, | ||
| 166 | ); | ||
| 167 | praxo_error( | ||
| 168 | 'PROVIDER_HTTP', | ||
| 169 | 'provider returned HTTP ' . ($response->{status} // 0) . ': ' . ($response->{content} // ''), | ||
| 170 | ) unless $response->{success}; | ||
| 171 | return Praxo::Json::parse($response->{content}); | ||
| 172 | } | ||
| 173 | |||
| 174 | 1; |