| 1 | package Langertha::Engine::Gemini; | ||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Google Gemini API | ||||||||||||||||||||||||||||||||||
| 3 | 9 | our $VERSION = '0.502'; | |||||||||||||||||||||||||||||||||
| 4 | 9 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | 9 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 6 | 9 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 7 | 9 | ▶ | use Langertha::ToolChoice; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 8 | 9 | ▶ | use Langertha::Response; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 9 | 9 | ▶ | use Langertha::ToolCall; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||
| 11 | 9 | extends 'Langertha::Engine::Remote'; | |||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||
| 13 | 9 | with map { 'Langertha::Role::'.$_ } qw( | |||||||||||||||||||||||||||||||||
| 14 | Models | ||||||||||||||||||||||||||||||||||
| 15 | Chat | ||||||||||||||||||||||||||||||||||
| 16 | Temperature | ||||||||||||||||||||||||||||||||||
| 17 | ResponseSize | ||||||||||||||||||||||||||||||||||
| 18 | SystemPrompt | ||||||||||||||||||||||||||||||||||
| 19 | ResponseFormat | ||||||||||||||||||||||||||||||||||
| 20 | Streaming | ||||||||||||||||||||||||||||||||||
| 21 | Tools | ||||||||||||||||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||
| 25 | 2 | ▶ | sub default_response_size { 2048 } | ||||||||||||||||||||||||||||||||
|
Subroutine
default_response_size: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||
| 27 | 2 | ▶ | sub content_format { 'gemini' } | ||||||||||||||||||||||||||||||||
|
Subroutine
content_format: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||
| 29 | 9 | has api_key => ( | |||||||||||||||||||||||||||||||||
| 30 | is => 'ro', | ||||||||||||||||||||||||||||||||||
| 31 | lazy_build => 1, | ||||||||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||||||||
| 33 | sub _build_api_key { | ||||||||||||||||||||||||||||||||||
| 34 | 0 | ▶ | my ( $self ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
_build_api_key: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||
| 35 | return $ENV{LANGERTHA_GEMINI_API_KEY} | ||||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | || croak "".(ref $self)." requires LANGERTHA_GEMINI_API_KEY or api_key set"; | ||||||||||||||||||||||||||||||||
|
Condition0%0/3
$ENV{'LANGERTHA_GEMINI_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_GEMINI_API_KEY or api_key set')
MC/DC0%0/2
$ENV{'LANGERTHA_GEMINI_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_GEMINI_API_KEY or api_key set')
$ENV{'LANGERTHA_GEMINI_API_KEY'}
croak('' . ref($self) . ' requires LANGERTHA_GEMINI_API_KEY or api_key set')
Truth table0%0/3
A || B
A= $ENV{'LANGERTHA_GEMINI_API_KEY'}B= croak('' . ref($self) . ' requires LANGERTHA_GEMINI_API_KEY or api_key set')
| |||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||
| 40 | has '+url' => ( | ||||||||||||||||||||||||||||||||||
| 41 | lazy => 1, | ||||||||||||||||||||||||||||||||||
| 42 | 4 | ▶ | default => sub { 'https://generativelanguage.googleapis.com' }, | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 43 | 9 | ); | |||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||
| 45 | 3 | ▶ | sub default_model { 'gemini-2.5-flash' } | ||||||||||||||||||||||||||||||||
|
Subroutine
default_model: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||
| 47 | sub chat_request { | ||||||||||||||||||||||||||||||||||
| 48 | 2 | ▶ | my ( $self, $messages, %extra ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
chat_request: called CC=21 SCAR=43.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||
| 50 | # Translate tool_choice (canonical / OpenAI / Anthropic shapes) into | ||||||||||||||||||||||||||||||||||
| 51 | # Gemini's toolConfig.functionCallingConfig form. | ||||||||||||||||||||||||||||||||||
| 52 | 2 | ▶ | if ( exists $extra{tool_choice} && defined $extra{tool_choice} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $extra{"tool_choice"} and defined $extra{"tool_choice"}
MC/DC0%0/2
exists $extra{"tool_choice"} and defined $extra{"tool_choice"}
exists $extra{"tool_choice"}
defined $extra{"tool_choice"}
Truth table33%1/3
A and B
A= exists $extra{"tool_choice"}B= defined $extra{"tool_choice"}
| |||||||||||||||||||||||||||||||||||
| 53 | 0 | my $tc = Langertha::ToolChoice->from_hash( delete $extra{tool_choice} ); | |||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | if ($tc) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 55 | 0 | my $cfg = $tc->to_gemini; | |||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | $extra{toolConfig} = $cfg if $cfg; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||
| 60 | # Convert messages to Gemini format | ||||||||||||||||||||||||||||||||||
| 61 | 2 | my @gemini_contents; | |||||||||||||||||||||||||||||||||
| 62 | my $system_instruction; | ||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||
| 64 | 2 | for my $message (@{$messages}) { | |||||||||||||||||||||||||||||||||
| 65 | 3 | ▶ | if ($message->{role} eq 'system') { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 66 | # Gemini uses systemInstruction field for system messages | ||||||||||||||||||||||||||||||||||
| 67 | 1 | ▶ | $system_instruction .= "\n\n" if $system_instruction; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 68 | 1 | $system_instruction .= $message->{content}; | |||||||||||||||||||||||||||||||||
| 69 | } elsif ($message->{parts}) { | ||||||||||||||||||||||||||||||||||
| 70 | # Already in Gemini format (e.g. from format_tool_results) | ||||||||||||||||||||||||||||||||||
| 71 | 0 | push @gemini_contents, $message; | |||||||||||||||||||||||||||||||||
| 72 | } else { | ||||||||||||||||||||||||||||||||||
| 73 | # Convert role: 'assistant' -> 'model' for Gemini | ||||||||||||||||||||||||||||||||||
| 74 | 2 | ▶ | my $role = $message->{role} eq 'assistant' ? 'model' : $message->{role}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 75 | push @gemini_contents, { | ||||||||||||||||||||||||||||||||||
| 76 | role => $role, | ||||||||||||||||||||||||||||||||||
| 77 | 2 | parts => [{ text => $message->{content} }], | |||||||||||||||||||||||||||||||||
| 78 | }; | ||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||
| 82 | # Build the URL with model and API key | ||||||||||||||||||||||||||||||||||
| 83 | 2 | my $model_name = $self->chat_model; | |||||||||||||||||||||||||||||||||
| 84 | 2 | my $url = $self->url . "/v1beta/models/${model_name}:generateContent?key=" . $self->api_key; | |||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||
| 86 | 2 | my %request_body = ( | |||||||||||||||||||||||||||||||||
| 87 | contents => \@gemini_contents, | ||||||||||||||||||||||||||||||||||
| 88 | ); | ||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||
| 90 | # Add system instruction if present | ||||||||||||||||||||||||||||||||||
| 91 | 2 | ▶ | if ($system_instruction) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 92 | $request_body{systemInstruction} = { | ||||||||||||||||||||||||||||||||||
| 93 | 1 | parts => [{ text => $system_instruction }], | |||||||||||||||||||||||||||||||||
| 94 | }; | ||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||
| 97 | # Add generation config | ||||||||||||||||||||||||||||||||||
| 98 | 2 | my %generation_config; | |||||||||||||||||||||||||||||||||
| 99 | 2 | ▶ | if ($self->get_response_size) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 100 | 2 | $generation_config{maxOutputTokens} = $self->get_response_size; | |||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||
| 102 | 2 | ▶ | if ($self->has_temperature) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 103 | 1 | $generation_config{temperature} = $self->temperature; | |||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||
| 106 | # Translate response_format -> Gemini's generationConfig.responseSchema / | ||||||||||||||||||||||||||||||||||
| 107 | # responseMimeType. Accepts the OpenAI-shape response_format hash so that | ||||||||||||||||||||||||||||||||||
| 108 | # callers can hand the same payload to any engine. | ||||||||||||||||||||||||||||||||||
| 109 | 2 | ▶ | if ( $self->has_response_format ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 110 | 0 | my $rf = $self->response_format; | |||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | my $type = ref($rf) eq 'HASH' ? ( $rf->{type} // '' ) : ''; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$$rf{"type"} // ""
MC/DC0%0/1
$$rf{"type"} // ""
$$rf{"type"}
Truth table0%0/2
A
A= $$rf{"type"}
| |||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | if ( $type eq 'json_object' ) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 113 | 0 | $generation_config{responseMimeType} = 'application/json'; | |||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||
| 115 | ▶ | elsif ( $type eq 'json_schema' | |||||||||||||||||||||||||||||||||
|
Condition0%0/6
$type eq "json_schema" and ref $$rf{"json_schema"} eq "HASH" and ref $$rf{"json_schema"}{"schema"} eq "HASH"
$type eq "json_schema" and ref $$rf{"json_schema"} eq "HASH"
MC/DC0%0/3
$type eq "json_schema" and ref $$rf{"json_schema"} eq "HASH" and ref $$rf{"json_schema"}{"schema"} eq "HASH"
$type eq "json_schema"
ref $$rf{"json_schema"} eq "HASH"
ref $$rf{"json_schema"}{"schema"} eq "HASH"
Truth table0%0/4
A and B and C
A= $type eq "json_schema"B= ref $$rf{"json_schema"} eq "HASH"C= ref $$rf{"json_schema"}{"schema"} eq "HASH"
| |||||||||||||||||||||||||||||||||||
| 116 | && ref( $rf->{json_schema} ) eq 'HASH' | ||||||||||||||||||||||||||||||||||
| 117 | && ref( $rf->{json_schema}{schema} ) eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||
| 118 | 0 | $generation_config{responseMimeType} = 'application/json'; | |||||||||||||||||||||||||||||||||
| 119 | 0 | $generation_config{responseSchema} = $rf->{json_schema}{schema}; | |||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||
| 123 | 2 | ▶ | $request_body{generationConfig} = \%generation_config if %generation_config; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||
| 125 | return $self->generate_http_request( | ||||||||||||||||||||||||||||||||||
| 126 | POST => $url, | ||||||||||||||||||||||||||||||||||
| 127 | 0 | ▶ | sub { $self->chat_response(shift) }, | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 128 | 2 | %request_body, | |||||||||||||||||||||||||||||||||
| 129 | %extra, | ||||||||||||||||||||||||||||||||||
| 130 | ); | ||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||
| 133 | sub update_request { | ||||||||||||||||||||||||||||||||||
| 134 | 5 | ▶ | my ( $self, $request ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
update_request: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 135 | 5 | $request->header('content-type', 'application/json'); | |||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||
| 138 | sub chat_response { | ||||||||||||||||||||||||||||||||||
| 139 | 3 | ▶ | my ( $self, $response ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
chat_response: called CC=15 SCAR=28.0
| |||||||||||||||||||||||||||||||||||
| 140 | 3 | my $data = $self->parse_response($response); | |||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||
| 142 | # Gemini response format: candidates[0].content.parts[].text | ||||||||||||||||||||||||||||||||||
| 143 | 3 | ▶ | my $candidates = $data->{candidates} || []; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$$data{'candidates'} || []
MC/DC0%0/1
$$data{'candidates'} || []
$$data{'candidates'}
Truth table50%1/2
A
A= $$data{'candidates'}
| |||||||||||||||||||||||||||||||||||
| 144 | 3 | my $text = ''; | |||||||||||||||||||||||||||||||||
| 145 | 3 | my $finish_reason; | |||||||||||||||||||||||||||||||||
| 146 | my $thinking; | ||||||||||||||||||||||||||||||||||
| 147 | 3 | ▶ | if (@$candidates) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 148 | 3 | my $candidate = $candidates->[0]; | |||||||||||||||||||||||||||||||||
| 149 | 3 | ▶ | my $content = $candidate->{content} || {}; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$$candidate{'content'} || {}
MC/DC0%0/1
$$candidate{'content'} || {}
$$candidate{'content'}
Truth table50%1/2
A
A= $$candidate{'content'}
| |||||||||||||||||||||||||||||||||||
| 150 | 3 | ▶ | my $parts = $content->{parts} || []; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$$content{'parts'} || []
MC/DC0%0/1
$$content{'parts'} || []
$$content{'parts'}
Truth table50%1/2
A
A= $$content{'parts'}
| |||||||||||||||||||||||||||||||||||
| 151 | 3 | my @text_parts; | |||||||||||||||||||||||||||||||||
| 152 | my @thought_parts; | ||||||||||||||||||||||||||||||||||
| 153 | 3 | for my $part (@$parts) { | |||||||||||||||||||||||||||||||||
| 154 | 4 | ▶ | next unless exists $part->{text}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 155 | 4 | ▶ | if ($part->{thought}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 156 | 1 | push @thought_parts, $part->{text}; | |||||||||||||||||||||||||||||||||
| 157 | } else { | ||||||||||||||||||||||||||||||||||
| 158 | 3 | push @text_parts, $part->{text}; | |||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||
| 161 | 3 | $text = join('', @text_parts); | |||||||||||||||||||||||||||||||||
| 162 | 3 | ▶ | $thinking = join("\n", @thought_parts) if @thought_parts; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 163 | 3 | $finish_reason = $candidate->{finishReason}; | |||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||
| 166 | # Normalize Gemini usage metadata | ||||||||||||||||||||||||||||||||||
| 167 | 3 | my $usage; | |||||||||||||||||||||||||||||||||
| 168 | 3 | ▶ | if (my $um = $data->{usageMetadata}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 169 | $usage = { | ||||||||||||||||||||||||||||||||||
| 170 | prompt_tokens => $um->{promptTokenCount}, | ||||||||||||||||||||||||||||||||||
| 171 | completion_tokens => $um->{candidatesTokenCount}, | ||||||||||||||||||||||||||||||||||
| 172 | total_tokens => $um->{totalTokenCount}, | ||||||||||||||||||||||||||||||||||
| 173 | 3 | }; | |||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||
| 176 | 3 | my @tcs = Langertha::ToolCall->extract($data); | |||||||||||||||||||||||||||||||||
| 177 | return Langertha::Response->new( | ||||||||||||||||||||||||||||||||||
| 178 | content => $text, | ||||||||||||||||||||||||||||||||||
| 179 | raw => $data, | ||||||||||||||||||||||||||||||||||
| 180 | 3 | ▶ | $data->{modelVersion} ? ( model => $data->{modelVersion} ) : (), | ||||||||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||||||||
| 181 | defined $finish_reason ? ( finish_reason => $finish_reason ) : (), | ||||||||||||||||||||||||||||||||||
| 182 | $usage ? ( usage => $usage ) : (), | ||||||||||||||||||||||||||||||||||
| 183 | defined $thinking ? ( thinking => $thinking ) : (), | ||||||||||||||||||||||||||||||||||
| 184 | @tcs ? ( tool_calls => [ @tcs ] ) : (), | ||||||||||||||||||||||||||||||||||
| 185 | ); | ||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||
| 188 | 0 | ▶ | sub stream_format { 'sse' } | ||||||||||||||||||||||||||||||||
|
Subroutine
stream_format: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||
| 190 | sub chat_stream_request { | ||||||||||||||||||||||||||||||||||
| 191 | 1 | ▶ | my ( $self, $messages, %extra ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
chat_stream_request: called CC=13 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||
| 193 | # Same tool_choice translation as chat_request. | ||||||||||||||||||||||||||||||||||
| 194 | 1 | ▶ | if ( exists $extra{tool_choice} && defined $extra{tool_choice} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $extra{"tool_choice"} and defined $extra{"tool_choice"}
MC/DC0%0/2
exists $extra{"tool_choice"} and defined $extra{"tool_choice"}
exists $extra{"tool_choice"}
defined $extra{"tool_choice"}
Truth table33%1/3
A and B
A= exists $extra{"tool_choice"}B= defined $extra{"tool_choice"}
| |||||||||||||||||||||||||||||||||||
| 195 | 0 | my $tc = Langertha::ToolChoice->from_hash( delete $extra{tool_choice} ); | |||||||||||||||||||||||||||||||||
| 196 | 0 | ▶ | if ($tc) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 197 | 0 | my $cfg = $tc->to_gemini; | |||||||||||||||||||||||||||||||||
| 198 | 0 | ▶ | $extra{toolConfig} = $cfg if $cfg; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||
| 202 | # Convert messages to Gemini format (same as non-streaming) | ||||||||||||||||||||||||||||||||||
| 203 | 1 | my @gemini_contents; | |||||||||||||||||||||||||||||||||
| 204 | my $system_instruction; | ||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||
| 206 | 1 | for my $message (@{$messages}) { | |||||||||||||||||||||||||||||||||
| 207 | 2 | ▶ | if ($message->{role} eq 'system') { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 208 | 1 | ▶ | $system_instruction .= "\n\n" if $system_instruction; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 209 | 1 | $system_instruction .= $message->{content}; | |||||||||||||||||||||||||||||||||
| 210 | } else { | ||||||||||||||||||||||||||||||||||
| 211 | 1 | ▶ | my $role = $message->{role} eq 'assistant' ? 'model' : $message->{role}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 212 | push @gemini_contents, { | ||||||||||||||||||||||||||||||||||
| 213 | role => $role, | ||||||||||||||||||||||||||||||||||
| 214 | 1 | parts => [{ text => $message->{content} }], | |||||||||||||||||||||||||||||||||
| 215 | }; | ||||||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||
| 219 | # Build the URL for streaming endpoint | ||||||||||||||||||||||||||||||||||
| 220 | 1 | my $model_name = $self->chat_model; | |||||||||||||||||||||||||||||||||
| 221 | 1 | my $url = $self->url . "/v1beta/models/${model_name}:streamGenerateContent?key=" . $self->api_key . "&alt=sse"; | |||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||
| 223 | 1 | my %request_body = ( | |||||||||||||||||||||||||||||||||
| 224 | contents => \@gemini_contents, | ||||||||||||||||||||||||||||||||||
| 225 | ); | ||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||
| 227 | 1 | ▶ | if ($system_instruction) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 228 | $request_body{systemInstruction} = { | ||||||||||||||||||||||||||||||||||
| 229 | 1 | parts => [{ text => $system_instruction }], | |||||||||||||||||||||||||||||||||
| 230 | }; | ||||||||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||
| 233 | 1 | my %generation_config; | |||||||||||||||||||||||||||||||||
| 234 | 1 | ▶ | if ($self->get_response_size) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 235 | 1 | $generation_config{maxOutputTokens} = $self->get_response_size; | |||||||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||||||||
| 237 | 1 | ▶ | if ($self->has_temperature) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 238 | 1 | $generation_config{temperature} = $self->temperature; | |||||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||
| 241 | 1 | ▶ | $request_body{generationConfig} = \%generation_config if %generation_config; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||
| 243 | return $self->generate_http_request( | ||||||||||||||||||||||||||||||||||
| 244 | POST => $url, | ||||||||||||||||||||||||||||||||||
| 245 | ▶ | sub {}, | |||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 246 | 1 | %request_body, | |||||||||||||||||||||||||||||||||
| 247 | %extra, | ||||||||||||||||||||||||||||||||||
| 248 | ); | ||||||||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||
| 251 | sub parse_stream_chunk { | ||||||||||||||||||||||||||||||||||
| 252 | 0 | ▶ | my ( $self, $data, $event ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
parse_stream_chunk: not called CC=10 SCAR=47.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||
| 254 | 0 | require Langertha::Stream::Chunk; | |||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||
| 256 | # Gemini streaming format is similar to non-streaming | ||||||||||||||||||||||||||||||||||
| 257 | 0 | ▶ | my $candidates = $data->{candidates} || []; | ||||||||||||||||||||||||||||||||
|
Condition0%0/2
$$data{'candidates'} || []
MC/DC0%0/1
$$data{'candidates'} || []
$$data{'candidates'}
Truth table0%0/2
A
A= $$data{'candidates'}
| |||||||||||||||||||||||||||||||||||
| 258 | 0 | ▶ | return undef unless @$candidates; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||
| 260 | 0 | my $candidate = $candidates->[0]; | |||||||||||||||||||||||||||||||||
| 261 | 0 | ▶ | my $content = $candidate->{content} || {}; | ||||||||||||||||||||||||||||||||
|
Condition0%0/2
$$candidate{'content'} || {}
MC/DC0%0/1
$$candidate{'content'} || {}
$$candidate{'content'}
Truth table0%0/2
A
A= $$candidate{'content'}
| |||||||||||||||||||||||||||||||||||
| 262 | 0 | ▶ | my $parts = $content->{parts} || []; | ||||||||||||||||||||||||||||||||
|
Condition0%0/2
$$content{'parts'} || []
MC/DC0%0/1
$$content{'parts'} || []
$$content{'parts'}
Truth table0%0/2
A
A= $$content{'parts'}
| |||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||
| 264 | 0 | my $text = ''; | |||||||||||||||||||||||||||||||||
| 265 | 0 | ▶ | $text = $parts->[0]->{text} if @$parts && $parts->[0]->{text}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@$parts and $$parts[0]{"text"}
MC/DC0%0/2
@$parts and $$parts[0]{"text"}
@$parts
$$parts[0]{"text"}
Truth table0%0/3
A and B
A= @$partsB= $$parts[0]{"text"}
| |||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||
| 267 | 0 | my $finish_reason = $candidate->{finishReason}; | |||||||||||||||||||||||||||||||||
| 268 | 0 | ▶ | my $is_final = defined $finish_reason && $finish_reason ne ''; | ||||||||||||||||||||||||||||||||
|
Condition0%0/3
defined $finish_reason && $finish_reason ne ''
MC/DC0%0/2
defined $finish_reason && $finish_reason ne ''
defined $finish_reason
$finish_reason ne ''
Truth table0%0/3
A && B
A= defined $finish_reasonB= $finish_reason ne ''
| |||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||
| 270 | return Langertha::Stream::Chunk->new( | ||||||||||||||||||||||||||||||||||
| 271 | content => $text, | ||||||||||||||||||||||||||||||||||
| 272 | raw => $data, | ||||||||||||||||||||||||||||||||||
| 273 | is_final => $is_final, | ||||||||||||||||||||||||||||||||||
| 274 | $finish_reason ? (finish_reason => $finish_reason) : (), | ||||||||||||||||||||||||||||||||||
| 275 | 0 | ▶ | $data->{usageMetadata} ? (usage => $data->{usageMetadata}) : (), | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 276 | ); | ||||||||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||
| 279 | # Dynamic model listing with token pagination | ||||||||||||||||||||||||||||||||||
| 280 | sub list_models_request { | ||||||||||||||||||||||||||||||||||
| 281 | 2 | ▶ | my ($self, %params) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
list_models_request: called CC=2 SCAR=9.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 282 | 2 | my $url = $self->url . '/v1beta/models?key=' . $self->api_key; | |||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||
| 284 | # Add pagination params if provided | ||||||||||||||||||||||||||||||||||
| 285 | 2 | ▶ | if (%params) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 286 | 0 | require URI; | |||||||||||||||||||||||||||||||||
| 287 | 0 | my $uri = URI->new($url); | |||||||||||||||||||||||||||||||||
| 288 | 0 | my %query = $uri->query_form; | |||||||||||||||||||||||||||||||||
| 289 | 0 | $uri->query_form(%query, %params); | |||||||||||||||||||||||||||||||||
| 290 | 0 | $url = $uri->as_string; | |||||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||
| 293 | return $self->generate_http_request( | ||||||||||||||||||||||||||||||||||
| 294 | GET => $url, | ||||||||||||||||||||||||||||||||||
| 295 | 1 | ▶ | sub { $self->list_models_response(shift) }, | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 296 | 2 | ); | |||||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||
| 299 | sub list_models_response { | ||||||||||||||||||||||||||||||||||
| 300 | 2 | ▶ | my ($self, $response) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
list_models_response: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 301 | 2 | my $data = $self->parse_response($response); | |||||||||||||||||||||||||||||||||
| 302 | 2 | return $data; | |||||||||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||
| 305 | sub _fetch_all_models { | ||||||||||||||||||||||||||||||||||
| 306 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
_fetch_all_models: called CC=4 SCAR=14.1
| |||||||||||||||||||||||||||||||||||
| 307 | 1 | my @all_models; | |||||||||||||||||||||||||||||||||
| 308 | my $page_token; | ||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||
| 310 | 1 | ▶ | do { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 311 | 1 | ▶ | my $request = $self->list_models_request( | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 312 | $page_token ? (pageToken => $page_token) : () | ||||||||||||||||||||||||||||||||||
| 313 | ); | ||||||||||||||||||||||||||||||||||
| 314 | 1 | my $response = $self->user_agent->request($request); | |||||||||||||||||||||||||||||||||
| 315 | 1 | my $data = $request->response_call->($response); | |||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||
| 317 | 1 | ▶ | push @all_models, @{$data->{models} || []}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 318 | 1 | $page_token = $data->{nextPageToken}; | |||||||||||||||||||||||||||||||||
| 319 | } while ($page_token); | ||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||
| 321 | 1 | return \@all_models; | |||||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||
| 324 | sub list_models { | ||||||||||||||||||||||||||||||||||
| 325 | 1 | ▶ | my ($self, %opts) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
list_models: called CC=6 SCAR=20.2
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||
| 327 | # Check cache unless force_refresh requested | ||||||||||||||||||||||||||||||||||
| 328 | 1 | ▶ | unless ($opts{force_refresh}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 329 | 1 | my $cache = $self->_models_cache; | |||||||||||||||||||||||||||||||||
| 330 | 1 | ▶ | if ($cache->{timestamp} && time - $cache->{timestamp} < $self->models_cache_ttl) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$$cache{"timestamp"} and time - $$cache{"timestamp"} < $self->models_cache_ttl
MC/DC0%0/2
$$cache{"timestamp"} and time - $$cache{"timestamp"} < $self->models_cache_ttl
$$cache{"timestamp"}
time - $$cache{"timestamp"} < $self->models_cache_ttl
Truth table33%1/3
A and B
A= $$cache{"timestamp"}B= time - $$cache{"timestamp"} < $self->models_cache_ttl
| |||||||||||||||||||||||||||||||||||
| 331 | 0 | ▶ | return $opts{full} ? $cache->{models} : $cache->{model_ids}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||
| 335 | # Fetch all pages from API | ||||||||||||||||||||||||||||||||||
| 336 | 1 | my $models = $self->_fetch_all_models; | |||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||
| 338 | # Extract IDs and update cache | ||||||||||||||||||||||||||||||||||
| 339 | # Gemini uses 'name' field like "models/gemini-2.0-flash" | ||||||||||||||||||||||||||||||||||
| 340 | my @model_ids = map { | ||||||||||||||||||||||||||||||||||
| 341 | 1 | my $name = $_->{name}; | |||||||||||||||||||||||||||||||||
| 342 | 3 | $name =~ s{^models/}{}; # Strip "models/" prefix | |||||||||||||||||||||||||||||||||
| 343 | 3 | $name; | |||||||||||||||||||||||||||||||||
| 344 | } @$models; | ||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||
| 346 | 1 | $self->_models_cache({ | |||||||||||||||||||||||||||||||||
| 347 | timestamp => time, | ||||||||||||||||||||||||||||||||||
| 348 | models => $models, | ||||||||||||||||||||||||||||||||||
| 349 | model_ids => \@model_ids, | ||||||||||||||||||||||||||||||||||
| 350 | }); | ||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||
| 352 | 1 | ▶ | return $opts{full} ? $models : \@model_ids; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||
| 356 | # Tool calling support (MCP) | ||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||
| 358 | sub format_tools { | ||||||||||||||||||||||||||||||||||
| 359 | 1 | ▶ | my ( $self, $mcp_tools ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
format_tools: called CC=3 SCAR=13.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 360 | return [{ | ||||||||||||||||||||||||||||||||||
| 361 | functionDeclarations => [map { | ||||||||||||||||||||||||||||||||||
| 362 | 1 | { | |||||||||||||||||||||||||||||||||
| 363 | name => $_->{name}, | ||||||||||||||||||||||||||||||||||
| 364 | description => $_->{description}, | ||||||||||||||||||||||||||||||||||
| 365 | parameters => $_->{input_schema} // $_->{inputSchema} // $_->{parameters}, | ||||||||||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||||||||
| 367 | 2 | ▶ | } @$mcp_tools], | ||||||||||||||||||||||||||||||||
|
Condition33%2/6
($$_{"input_schema"} // $$_{"inputSchema"}) // $$_{"parameters"}
$$_{"input_schema"} // $$_{"inputSchema"}
MC/DC0%0/3
($$_{"input_schema"} // $$_{"inputSchema"}) // $$_{"parameters"}
$$_{"input_schema"}
$$_{"inputSchema"}
$$_{"parameters"}
Truth table25%1/4
A // B // C
A= $$_{"input_schema"}B= $$_{"inputSchema"}C= $$_{"parameters"}
| |||||||||||||||||||||||||||||||||||
| 368 | }]; | ||||||||||||||||||||||||||||||||||
| 369 | } | ||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||
| 371 | sub response_tool_calls { | ||||||||||||||||||||||||||||||||||
| 372 | 3 | ▶ | my ( $self, $data ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
response_tool_calls: called CC=4 SCAR=14.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 373 | 3 | ▶ | my $candidates = $data->{candidates} || []; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$$data{'candidates'} || []
MC/DC0%0/1
$$data{'candidates'} || []
$$data{'candidates'}
Truth table50%1/2
A
A= $$data{'candidates'}
| |||||||||||||||||||||||||||||||||||
| 374 | 3 | ▶ | return [] unless @$candidates; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 375 | 3 | ▶ | my $parts = $candidates->[0]{content}{parts} || []; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$$candidates[0]{'content'}{'parts'} || []
MC/DC0%0/1
$$candidates[0]{'content'}{'parts'} || []
$$candidates[0]{'content'}{'parts'}
Truth table50%1/2
A
A= $$candidates[0]{'content'}{'parts'}
| |||||||||||||||||||||||||||||||||||
| 376 | 3 | return [grep { exists $_->{functionCall} } @$parts]; | |||||||||||||||||||||||||||||||||
| 377 | } | ||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||
| 379 | sub extract_tool_call { | ||||||||||||||||||||||||||||||||||
| 380 | 0 | ▶ | my ( $self, $tc ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
extract_tool_call: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 381 | 0 | return ( $tc->{functionCall}{name}, $tc->{functionCall}{args} ); | |||||||||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||
| 384 | sub response_text_content { | ||||||||||||||||||||||||||||||||||
| 385 | 2 | ▶ | my ( $self, $data ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
response_text_content: called CC=4 SCAR=14.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 386 | 2 | ▶ | my $candidates = $data->{candidates} || []; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$$data{'candidates'} || []
MC/DC0%0/1
$$data{'candidates'} || []
$$data{'candidates'}
Truth table50%1/2
A
A= $$data{'candidates'}
| |||||||||||||||||||||||||||||||||||
| 387 | 2 | ▶ | return '' unless @$candidates; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 388 | 2 | ▶ | my $parts = $candidates->[0]{content}{parts} || []; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$$candidates[0]{'content'}{'parts'} || []
MC/DC0%0/1
$$candidates[0]{'content'}{'parts'} || []
$$candidates[0]{'content'}{'parts'}
Truth table50%1/2
A
A= $$candidates[0]{'content'}{'parts'}
| |||||||||||||||||||||||||||||||||||
| 389 | 2 | return join('', map { $_->{text} } grep { exists $_->{text} } @$parts); | |||||||||||||||||||||||||||||||||
| 390 | } | ||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||
| 392 | sub format_tool_results { | ||||||||||||||||||||||||||||||||||
| 393 | 1 | ▶ | my ( $self, $data, $results ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
format_tool_results: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 394 | 1 | my $candidate = $data->{candidates}[0]; | |||||||||||||||||||||||||||||||||
| 395 | return ( | ||||||||||||||||||||||||||||||||||
| 396 | { role => 'model', parts => $candidate->{content}{parts} }, | ||||||||||||||||||||||||||||||||||
| 397 | { role => 'user', parts => [ | ||||||||||||||||||||||||||||||||||
| 398 | map { | ||||||||||||||||||||||||||||||||||
| 399 | 1 | my $content = $_->{result}{content}; | |||||||||||||||||||||||||||||||||
| 400 | # Gemini expects response as a plain object, not an array | ||||||||||||||||||||||||||||||||||
| 401 | 1 | ▶ | my $text = join('', map { $_->{text} // '' } @$content); | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$$_{"text"} // ""
MC/DC0%0/1
$$_{"text"} // ""
$$_{"text"}
Truth table50%1/2
A
A= $$_{"text"}
| |||||||||||||||||||||||||||||||||||
| 402 | { | ||||||||||||||||||||||||||||||||||
| 403 | functionResponse => { | ||||||||||||||||||||||||||||||||||
| 404 | name => $_->{tool_call}{functionCall}{name}, | ||||||||||||||||||||||||||||||||||
| 405 | 1 | response => { result => $text }, | |||||||||||||||||||||||||||||||||
| 406 | }, | ||||||||||||||||||||||||||||||||||
| 407 | } | ||||||||||||||||||||||||||||||||||
| 408 | } @$results | ||||||||||||||||||||||||||||||||||
| 409 | ]}, | ||||||||||||||||||||||||||||||||||
| 410 | ); | ||||||||||||||||||||||||||||||||||
| 411 | } | ||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||
| 413 | 9 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||||
| 416 | 9 | 1; | |||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||
| 418 | __END__ | ||||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||||
| 420 | =pod | ||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||
| 422 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||
| 424 | =head1 NAME | ||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||
| 426 | Langertha::Engine::Gemini - Google Gemini API | ||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||
| 428 | =head1 VERSION | ||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||
| 430 | version 0.502 | ||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||
| 432 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||
| 434 | use Langertha::Engine::Gemini; | ||||||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||||||
| 436 | my $gemini = Langertha::Engine::Gemini->new( | ||||||||||||||||||||||||||||||||||
| 437 | api_key => $ENV{GEMINI_API_KEY}, | ||||||||||||||||||||||||||||||||||
| 438 | model => 'gemini-2.5-flash', | ||||||||||||||||||||||||||||||||||
| 439 | response_size => 4096, | ||||||||||||||||||||||||||||||||||
| 440 | temperature => 0.7, | ||||||||||||||||||||||||||||||||||
| 441 | ); | ||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||
| 443 | # Simple chat | ||||||||||||||||||||||||||||||||||
| 444 | my $response = $gemini->simple_chat('Explain quantum computing in simple terms'); | ||||||||||||||||||||||||||||||||||
| 445 | print $response; | ||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||
| 447 | # Streaming | ||||||||||||||||||||||||||||||||||
| 448 | $gemini->simple_chat_stream(sub { | ||||||||||||||||||||||||||||||||||
| 449 | my ($chunk) = @_; | ||||||||||||||||||||||||||||||||||
| 450 | print $chunk->content; | ||||||||||||||||||||||||||||||||||
| 451 | }, 'Write a poem about Perl'); | ||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||
| 453 | # Async with Future::AsyncAwait | ||||||||||||||||||||||||||||||||||
| 454 | use Future::AsyncAwait; | ||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||
| 456 | async sub ask_gemini { | ||||||||||||||||||||||||||||||||||
| 457 | my $response = await $gemini->simple_chat_f( | ||||||||||||||||||||||||||||||||||
| 458 | 'What are the benefits of functional programming?' | ||||||||||||||||||||||||||||||||||
| 459 | ); | ||||||||||||||||||||||||||||||||||
| 460 | say $response; | ||||||||||||||||||||||||||||||||||
| 461 | } | ||||||||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||||||||
| 463 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||||||||
| 465 | Provides access to Google's Gemini models via the Generative Language API. | ||||||||||||||||||||||||||||||||||
| 466 | Gemini models support multimodal input (text, code, images) and long context | ||||||||||||||||||||||||||||||||||
| 467 | windows. | ||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||
| 469 | Available models include C<gemini-2.5-flash> (fast with thinking, default), | ||||||||||||||||||||||||||||||||||
| 470 | C<gemini-2.5-pro> (most capable), and C<gemini-2.0-flash> (previous | ||||||||||||||||||||||||||||||||||
| 471 | generation). The default API endpoint is | ||||||||||||||||||||||||||||||||||
| 472 | C<https://generativelanguage.googleapis.com>. | ||||||||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||||||||
| 474 | B<THIS API IS WORK IN PROGRESS> | ||||||||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||||||||
| 476 | =head2 api_key | ||||||||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||||||||
| 478 | The Google Generative Language API key. If not provided, reads from | ||||||||||||||||||||||||||||||||||
| 479 | C<LANGERTHA_GEMINI_API_KEY> environment variable. Get your key at | ||||||||||||||||||||||||||||||||||
| 480 | L<https://aistudio.google.com/app/apikey>. Required. | ||||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||||
| 482 | =head2 list_models | ||||||||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||||||||
| 484 | my $model_ids = $engine->list_models; | ||||||||||||||||||||||||||||||||||
| 485 | my $models = $engine->list_models(full => 1); | ||||||||||||||||||||||||||||||||||
| 486 | my $models = $engine->list_models(force_refresh => 1); | ||||||||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||||||||
| 488 | Fetches available models from the Gemini API using token pagination. Returns | ||||||||||||||||||||||||||||||||||
| 489 | an ArrayRef of model ID strings (with the C<models/> prefix stripped) by | ||||||||||||||||||||||||||||||||||
| 490 | default, or full model objects when C<full => 1> is passed. Results are cached | ||||||||||||||||||||||||||||||||||
| 491 | for C<models_cache_ttl> seconds (default: 3600). | ||||||||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||||||||
| 493 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||||||||
| 495 | =over | ||||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||||
| 497 | =item * L<https://aistudio.google.com/status> - Google AI Studio service status | ||||||||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||||||||
| 499 | =item * L<https://ai.google.dev/gemini-api/docs> - Official Gemini API documentation | ||||||||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||||||||
| 501 | =item * L<https://aistudio.google.com/> - Google AI Studio for testing | ||||||||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||||||||
| 503 | =item * L<Langertha::Role::Chat> - Chat interface methods | ||||||||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||||||||
| 505 | =item * L<Langertha::Role::Tools> - MCP tool calling interface | ||||||||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||||||||
| 507 | =item * L<Langertha::Role::Streaming> - Streaming support (SSE format) | ||||||||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||||||||
| 509 | =item * L<Langertha::Engine::Anthropic> - Another non-OpenAI-compatible engine | ||||||||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||||||||
| 511 | =back | ||||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||||
| 513 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||||||||
| 515 | =head2 Issues | ||||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||||
| 517 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||||||||
| 518 | L<https://github.com/Getty/langertha/issues>. | ||||||||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||||||||
| 520 | =head2 IRC | ||||||||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||||||||
| 522 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||||
| 524 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||||||||
| 526 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||||||||
| 528 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||||||||
| 530 | Torsten Raudssus <getty@cpan.org> | ||||||||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||||||||
| 532 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||||||||
| 534 | This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>. | ||||||||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||||||||
| 536 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||
| 537 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||||||||
| 539 | =cut | ||||||||||||||||||||||||||||||||||