| 1 | package Praxo::Adapters::Gemini; | ||
| 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://generativelanguage.googleapis.com'; | ||
| 13 | $base_url =~ s{/$}{}; | ||
| 14 | return bless { | ||
| 15 | base_url => $base_url, | ||
| 16 | model => $args{model} // '', | ||
| 17 | api_key => $args{api_key}, | ||
| 18 | max_output_tokens => $args{max_output_tokens} // 0, | ||
| 19 | transport => $args{transport}, | ||
| 20 | timeout => $args{timeout} // 60, | ||
| 21 | }, $class; | ||
| 22 | } | ||
| 23 | |||
| 24 | sub generate { | ||
| 25 | my ($self, $request) = @_; | ||
| 26 | praxo_error('MODEL_CONFIG', 'gemini: API key is required') | ||
| 27 | unless defined $self->{api_key} && $self->{api_key} ne ''; | ||
| 28 | praxo_error('MODEL_CONFIG', 'gemini: model is required') | ||
| 29 | unless $self->{model} ne ''; | ||
| 30 | my $payload = _payload($self, $request); | ||
| 31 | my $path = '/v1beta/models/' . _escape($self->{model}) | ||
| 32 | . ':generateContent'; | ||
| 33 | my $data = $self->{transport} | ||
| 34 | ? $self->{transport}->($path, $payload) | ||
| 35 | : $self->_post($path, $payload); | ||
| 36 | return _parse_response($data); | ||
| 37 | } | ||
| 38 | |||
| 39 | sub _payload { | ||
| 40 | my ($self, $request) = @_; | ||
| 41 | my ($system, $contents) = _build_contents($request->{messages}); | ||
| 42 | my %payload = (contents => $contents); | ||
| 43 | $payload{system_instruction} = { parts => $system } if @$system; | ||
| 44 | my @declarations = map { | ||
| 45 | { | ||
| 46 | name => $_->{name}, | ||
| 47 | description => $_->{description} // '', | ||
| 48 | parameters => $_->{parameters} // { type => 'object', properties => {} }, | ||
| 49 | } | ||
| 50 | } @{ $request->{tools} // [] }; | ||
| 51 | if (@declarations) { | ||
| 52 | $payload{tools} = [{ functionDeclarations => \@declarations }]; | ||
| 53 | $payload{toolConfig} = { | ||
| 54 | functionCallingConfig => { | ||
| 55 | mode => _tool_mode($request->{toolChoice}), | ||
| 56 | }, | ||
| 57 | }; | ||
| 58 | } | ||
| 59 | if ((defined $request->{temperature} && $request->{temperature} != 0) | ||
| 60 | || $self->{max_output_tokens} > 0) { | ||
| 61 | $payload{generationConfig} = { | ||
| 62 | temperature => $request->{temperature} // 0, | ||
| 63 | maxOutputTokens => $self->{max_output_tokens}, | ||
| 64 | }; | ||
| 65 | } | ||
| 66 | return \%payload; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub _build_contents { | ||
| 70 | my ($messages) = @_; | ||
| 71 | my @system; | ||
| 72 | my @out; | ||
| 73 | my @pending; | ||
| 74 | my %names; | ||
| 75 | my $flush = sub { | ||
| 76 | return unless @pending; | ||
| 77 | push @out, { role => 'user', parts => [@pending] }; | ||
| 78 | @pending = (); | ||
| 79 | }; | ||
| 80 | for my $message (@{ $messages // [] }) { | ||
| 81 | if ($message->{role} eq Praxo::Models::SYSTEM()) { | ||
| 82 | push @system, { text => $message->{content} } | ||
| 83 | if defined $message->{content} && $message->{content} ne ''; | ||
| 84 | next; | ||
| 85 | } | ||
| 86 | if ($message->{role} eq Praxo::Models::TOOL()) { | ||
| 87 | my $name = $names{$message->{toolId} // ''} // $message->{toolId} // 'tool'; | ||
| 88 | push @pending, { | ||
| 89 | functionResponse => { | ||
| 90 | name => $name, | ||
| 91 | response => { result => $message->{content} // '' }, | ||
| 92 | }, | ||
| 93 | }; | ||
| 94 | next; | ||
| 95 | } | ||
| 96 | $flush->(); | ||
| 97 | my $parts = _message_parts($message); | ||
| 98 | if (@$parts) { | ||
| 99 | push @out, { | ||
| 100 | role => $message->{role} eq Praxo::Models::ASSISTANT() ? 'model' : 'user', | ||
| 101 | parts => $parts, | ||
| 102 | }; | ||
| 103 | } | ||
| 104 | for my $call (@{ $message->{toolCalls} // [] }) { | ||
| 105 | $names{$call->{id}} = $call->{name} | ||
| 106 | if defined $call->{id} && $call->{id} ne '' | ||
| 107 | && defined $call->{name} && $call->{name} ne ''; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | $flush->(); | ||
| 111 | return (\@system, \@out); | ||
| 112 | } | ||
| 113 | |||
| 114 | sub _message_parts { | ||
| 115 | my ($message) = @_; | ||
| 116 | my @parts; | ||
| 117 | push @parts, { text => $message->{content} } | ||
| 118 | if defined $message->{content} && $message->{content} ne ''; | ||
| 119 | for my $block (@{ $message->{contentBlocks} // [] }) { | ||
| 120 | push @parts, { text => $block->{text} } | ||
| 121 | if ($block->{type} // '') eq 'text' | ||
| 122 | && defined $block->{text} && $block->{text} ne ''; | ||
| 123 | } | ||
| 124 | for my $call (@{ $message->{toolCalls} // [] }) { | ||
| 125 | push @parts, { | ||
| 126 | functionCall => { | ||
| 127 | name => $call->{name} // '', | ||
| 128 | args => $call->{arguments} // {}, | ||
| 129 | }, | ||
| 130 | }; | ||
| 131 | } | ||
| 132 | return \@parts; | ||
| 133 | } | ||
| 134 | |||
| 135 | sub _escape { | ||
| 136 | my ($value) = @_; | ||
| 137 | $value =~ s/([^A-Za-z0-9._~-])/sprintf("%%%02X", ord($1))/ge; | ||
| 138 | return $value; | ||
| 139 | } | ||
| 140 | |||
| 141 | sub _tool_mode { | ||
| 142 | my ($choice) = @_; | ||
| 143 | return 'ANY' if defined $choice && $choice eq 'required'; | ||
| 144 | return 'NONE' if defined $choice && $choice eq 'none'; | ||
| 145 | return 'AUTO'; | ||
| 146 | } | ||
| 147 | |||
| 148 | sub _parse_response { | ||
| 149 | my ($data) = @_; | ||
| 150 | praxo_error('MODEL_HTTP', 'gemini: ' . ($data->{error}{message} // 'error')) | ||
| 151 | if ref($data->{error}) eq 'HASH'; | ||
| 152 | my $candidate = ref($data->{candidates}) eq 'ARRAY' | ||
| 153 | ? $data->{candidates}[0] : undef; | ||
| 154 | praxo_error('MODEL_HTTP', 'gemini: empty response') | ||
| 155 | unless ref($candidate) eq 'HASH'; | ||
| 156 | my $text = ''; | ||
| 157 | my @calls; | ||
| 158 | for my $part (@{ $candidate->{content}{parts} // [] }) { | ||
| 159 | $text .= $part->{text} // '' if defined $part->{text}; | ||
| 160 | if (ref($part->{functionCall}) eq 'HASH') { | ||
| 161 | push @calls, Praxo::Models::tool_call( | ||
| 162 | id => 'call-' . (scalar(@calls) + 1), | ||
| 163 | name => $part->{functionCall}{name} // '', | ||
| 164 | arguments => $part->{functionCall}{args} // {}, | ||
| 165 | ); | ||
| 166 | } | ||
| 167 | } | ||
| 168 | my $usage = ref($data->{usageMetadata}) eq 'HASH' ? $data->{usageMetadata} : {}; | ||
| 169 | return Praxo::Models::response( | ||
| 170 | message => Praxo::Models::message_assistant($text, \@calls), | ||
| 171 | usage => Praxo::Models::usage( | ||
| 172 | input_tokens => $usage->{promptTokenCount} // 0, | ||
| 173 | output_tokens => $usage->{candidatesTokenCount} // 0, | ||
| 174 | cached_tokens => $usage->{cachedContentTokenCount} // 0, | ||
| 175 | ), | ||
| 176 | finish_reason => $candidate->{finishReason}, | ||
| 177 | ); | ||
| 178 | } | ||
| 179 | |||
| 180 | sub _post { | ||
| 181 | my ($self, $path, $payload) = @_; | ||
| 182 | my %headers = ( | ||
| 183 | 'content-type' => 'application/json', | ||
| 184 | 'x-goog-api-key' => $self->{api_key}, | ||
| 185 | ); | ||
| 186 | my $http = HTTP::Tiny->new(timeout => $self->{timeout}); | ||
| 187 | my $response = $http->post( | ||
| 188 | $self->{base_url} . $path, | ||
| 189 | { headers => \%headers, content => Praxo::Json::stringify($payload) }, | ||
| 190 | ); | ||
| 191 | praxo_error( | ||
| 192 | 'PROVIDER_HTTP', | ||
| 193 | 'provider returned HTTP ' . ($response->{status} // 0) . ': ' . ($response->{content} // ''), | ||
| 194 | ) unless $response->{success}; | ||
| 195 | return Praxo::Json::parse($response->{content}); | ||
| 196 | } | ||
| 197 | |||
| 198 | 1; |