| 1 | package Langertha::Engine::Gemini; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Google Gemini API | ||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 60 | our $VERSION = '0.503'; | |||||||||||||||||||||||||||||||||||||||||||||
| 4 | 60 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 60 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 60 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 60 | ▶ | use Langertha::ToolChoice; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 60 | ▶ | use Langertha::Response; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 60 | ▶ | use Langertha::ToolCall; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 60 | ▶ | use Langertha::CachedContent; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 60 | extends 'Langertha::Engine::Remote'; | |||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 60 | with map { 'Langertha::Role::'.$_ } qw( | |||||||||||||||||||||||||||||||||||||||||||||
| 15 | Models | ||||||||||||||||||||||||||||||||||||||||||||||
| 16 | Chat | ||||||||||||||||||||||||||||||||||||||||||||||
| 17 | Temperature | ||||||||||||||||||||||||||||||||||||||||||||||
| 18 | ReasoningEffort | ||||||||||||||||||||||||||||||||||||||||||||||
| 19 | ResponseSize | ||||||||||||||||||||||||||||||||||||||||||||||
| 20 | SystemPrompt | ||||||||||||||||||||||||||||||||||||||||||||||
| 21 | ResponseFormat | ||||||||||||||||||||||||||||||||||||||||||||||
| 22 | Streaming | ||||||||||||||||||||||||||||||||||||||||||||||
| 23 | Tools | ||||||||||||||||||||||||||||||||||||||||||||||
| 24 | CachedContent | ||||||||||||||||||||||||||||||||||||||||||||||
| 25 | ImageInput | ||||||||||||||||||||||||||||||||||||||||||||||
| 26 | Embedding | ||||||||||||||||||||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 71 | ▶ | sub _build_reasoning_wire_format { 'gemini' } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_reasoning_wire_format: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||
| 31 | # Gemini splits its reasoning knob by model generation: Gemini 2.5-* takes | ||||||||||||||||||||||||||||||||||||||||||||||
| 32 | # an integer thinking_budget (no level vocabulary), Gemini 3 takes a | ||||||||||||||||||||||||||||||||||||||||||||||
| 33 | # thinkingLevel (minimal|low|medium|high, clamped to the model family's | ||||||||||||||||||||||||||||||||||||||||||||||
| 34 | # subset by Langertha::Reasoning). Exactly one native control is honored per | ||||||||||||||||||||||||||||||||||||||||||||||
| 35 | # model. Reflect that in the capability flags so callers can ask | ||||||||||||||||||||||||||||||||||||||||||||||
| 36 | # supports('thinking_budget') vs supports('reasoning_effort') and get the | ||||||||||||||||||||||||||||||||||||||||||||||
| 37 | # truth for the configured model. | ||||||||||||||||||||||||||||||||||||||||||||||
| 38 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 39 | # The same around also gates cached_content: explicit cachedContent | ||||||||||||||||||||||||||||||||||||||||||||||
| 40 | # resources are an "on" feature for Gemini 2.5+ and Gemini 3. Older | ||||||||||||||||||||||||||||||||||||||||||||||
| 41 | # generations (1.x, 2.0) don't accept the cachedContents REST endpoints, | ||||||||||||||||||||||||||||||||||||||||||||||
| 42 | # so we drop the flag there. | ||||||||||||||||||||||||||||||||||||||||||||||
| 43 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 44 | # parallel_tool_use is cleared on every model (karr k241): the v1beta | ||||||||||||||||||||||||||||||||||||||||||||||
| 45 | # ToolConfig is retrievalConfig | functionCallingConfig (mode + | ||||||||||||||||||||||||||||||||||||||||||||||
| 46 | # allowedFunctionNames) | includeServerSideToolInvocations, with no parallel | ||||||||||||||||||||||||||||||||||||||||||||||
| 47 | # knob (discovery doc revision 20260924). The model emits several | ||||||||||||||||||||||||||||||||||||||||||||||
| 48 | # functionCall parts on its own; there is nothing to switch off. | ||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 462 | ▶ | around engine_capabilities => sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 462 | my ( $orig, $self, @rest ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 51 | 462 | my $caps = $self->$orig(@rest); | |||||||||||||||||||||||||||||||||||||||||||||
| 52 | 462 | delete $caps->{parallel_tool_use}; | |||||||||||||||||||||||||||||||||||||||||||||
| 53 | 462 | ▶ | my $model = $self->can('chat_model') ? ( $self->chat_model // '' ) : ''; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$self->chat_model // ""
MC/DC0%0/1
$self->chat_model // ""
$self->chat_model
Truth table50%1/2
A
A= $self->chat_model
| |||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 462 | ▶ | if ( $model =~ /\Agemini-2\.5/ ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 153 | $caps->{thinking_budget} = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 56 | 153 | delete $caps->{reasoning_effort}; | |||||||||||||||||||||||||||||||||||||||||||||
| 57 | 153 | $caps->{cached_content} = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 59 | elsif ( $model =~ /\Agemini-3/ ) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 60 | # Gemini 3 (and any future unknown Gemini generation) keeps the | ||||||||||||||||||||||||||||||||||||||||||||||
| 61 | # default reasoning_effort cap; thinking_budget is not advertised. | ||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 215 | delete $caps->{thinking_budget}; | |||||||||||||||||||||||||||||||||||||||||||||
| 63 | 215 | $caps->{cached_content} = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 65 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 94 | delete $caps->{thinking_budget}; | |||||||||||||||||||||||||||||||||||||||||||||
| 67 | 94 | delete $caps->{cached_content}; | |||||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 462 | return $caps; | |||||||||||||||||||||||||||||||||||||||||||||
| 70 | 60 | }; | |||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||
| 72 | # image_input (k266, ADR 0019 k266 Update): every Gemini chat model takes | ||||||||||||||||||||||||||||||||||||||||||||||
| 73 | # inline_data image parts (llm-advisor, docs only, 2026-09-25), so the family | ||||||||||||||||||||||||||||||||||||||||||||||
| 74 | # keeps the role-derived flag. Cleared: TTS / Live / native-audio / | ||||||||||||||||||||||||||||||||||||||||||||||
| 75 | # transcription and embedding ids, the lyria/imagen/veo generators (advisor | ||||||||||||||||||||||||||||||||||||||||||||||
| 76 | # table), plus the text-only Gemini 1.0 Pro and aqa. Gemma: Gemma 3 and 4 are | ||||||||||||||||||||||||||||||||||||||||||||||
| 77 | # multimodal and keep the flag; Gemma 1/2 and the text-only gemma-3-1b clear | ||||||||||||||||||||||||||||||||||||||||||||||
| 78 | # it (decision k266: restrict the exclusion to the text-only sizes rather than | ||||||||||||||||||||||||||||||||||||||||||||||
| 79 | # the whole family). | ||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 462 | ▶ | sub model_capability_corrections { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
model_capability_corrections: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 81 | return ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 462 | qr/(?:-tts|-live|native-audio|transcribe|embedding)/ => { image_input => 0 }, | |||||||||||||||||||||||||||||||||||||||||||||
| 83 | qr/\A(?:lyria|imagen|veo|aqa)/ => { image_input => 0 }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 84 | qr/\Agemini-(?:1\.0-)?pro(?:-\d+)?\z/ => { image_input => 0 }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 85 | qr/\Agemma-(?:[12](?!\d)|3-1b)/ => { image_input => 0 }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 86 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||
| 89 | # Tool-result images (karr k344): functionResponse.parts[].inlineData is | ||||||||||||||||||||||||||||||||||||||||||||||
| 90 | # documented for the Gemini 3 series only (function-calling guide, v1beta; | ||||||||||||||||||||||||||||||||||||||||||||||
| 91 | # docs-derived, not live-verified, 2026-09-29). Other models keep the k336 | ||||||||||||||||||||||||||||||||||||||||||||||
| 92 | # placeholder string; a later generation joins when its docs say so. The | ||||||||||||||||||||||||||||||||||||||||||||||
| 93 | # (?!\d) guard keeps a hypothetical gemini-30 out (ADR 0023 k196). | ||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 52 | ▶ | sub _tool_result_images_on_wire { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_tool_result_images_on_wire: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 52 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 96 | 52 | ▶ | return ( $self->chat_model // '' ) =~ /\Agemini-3(?!\d)/ ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$self->chat_model // ""
MC/DC0%0/1
$self->chat_model // ""
$self->chat_model
Truth table50%1/2
A
A= $self->chat_model
| |||||||||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||||||
| 99 | # Tool-result PDFs (karr k361): the same guide section lists "Documents: | ||||||||||||||||||||||||||||||||||||||||||||||
| 100 | # application/pdf, text/plain" beside the image types for Gemini 3 | ||||||||||||||||||||||||||||||||||||||||||||||
| 101 | # multimodal function responses (ai.google.dev/gemini-api/docs/ | ||||||||||||||||||||||||||||||||||||||||||||||
| 102 | # generate-content/function-calling, fetched 2026-09-30; docs-derived, not | ||||||||||||||||||||||||||||||||||||||||||||||
| 103 | # live-verified), so a PDF follows the image predicate. (A text/plain blob is | ||||||||||||||||||||||||||||||||||||||||||||||
| 104 | # already decoded into the result string.) | ||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 26 | ▶ | sub _tool_result_pdf_on_wire { shift->_tool_result_images_on_wire } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_tool_result_pdf_on_wire: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 576 | ▶ | sub default_response_size { 2048 } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_response_size: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 85 | ▶ | sub content_format { 'gemini' } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
content_format: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 60 | has api_key => ( | |||||||||||||||||||||||||||||||||||||||||||||
| 113 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||
| 114 | lazy_build => 1, | ||||||||||||||||||||||||||||||||||||||||||||||
| 115 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 3 | ▶ | sub _build_api_key { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_api_key: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 3 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 118 | return $ENV{LANGERTHA_GEMINI_API_KEY} | ||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 3 | ▶ | || croak "".(ref $self)." requires LANGERTHA_GEMINI_API_KEY or api_key set"; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/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 table33%1/3
A || B
A= $ENV{'LANGERTHA_GEMINI_API_KEY'}B= croak('' . ref($self) . ' requires LANGERTHA_GEMINI_API_KEY or api_key set')
| |||||||||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||||||
| 123 | has '+url' => ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 124 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 172 | ▶ | default => sub { 'https://generativelanguage.googleapis.com' }, | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 60 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 60 | has cached_content => ( | |||||||||||||||||||||||||||||||||||||||||||||
| 129 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||
| 130 | isa => 'Maybe[Langertha::CachedContent]', | ||||||||||||||||||||||||||||||||||||||||||||||
| 131 | predicate => 'has_cached_content', | ||||||||||||||||||||||||||||||||||||||||||||||
| 132 | clearer => 'clear_cached_content', | ||||||||||||||||||||||||||||||||||||||||||||||
| 133 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 31 | ▶ | sub default_model { 'gemini-3-flash-preview' } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_model: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||||||
| 138 | # --- endpoint + auth seam ------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||
| 139 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 140 | # Every request URL in this engine is assembled here, and the credential is | ||||||||||||||||||||||||||||||||||||||||||||||
| 141 | # placed by gemini_auth_query alone. A second consumer of the Gemini dialect | ||||||||||||||||||||||||||||||||||||||||||||||
| 142 | # (Vertex AI express or standard, a Gemini-native gateway) differs from the | ||||||||||||||||||||||||||||||||||||||||||||||
| 143 | # Developer API exclusively in base URL, API version, model-path prefix and | ||||||||||||||||||||||||||||||||||||||||||||||
| 144 | # auth scheme — never in the request envelope — so such a shim is a subclass | ||||||||||||||||||||||||||||||||||||||||||||||
| 145 | # that overrides here rather than a copy of chat_request (ADR 0016, karr #88). | ||||||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 392 | ▶ | sub gemini_api_version { 'v1beta' } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
gemini_api_version: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 391 | ▶ | sub gemini_auth_query { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
gemini_auth_query: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 391 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 151 | 391 | my $key = $self->api_key; | |||||||||||||||||||||||||||||||||||||||||||||
| 152 | 389 | ▶ | return defined $key && length $key ? ( key => $key ) : (); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $key and length $key
MC/DC50%1/2
defined $key and length $key
defined $key
length $key
Truth table66%2/3
A and B
A= defined $keyB= length $key
| |||||||||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 402 | ▶ | sub gemini_endpoint { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
gemini_endpoint: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 402 | my ( $self, $path ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 157 | 402 | return $self->url . '/' . $self->gemini_api_version . '/' . $path; | |||||||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 398 | ▶ | sub gemini_url { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
gemini_url: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 398 | my ( $self, $path, @query ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 162 | 398 | my $url = $self->gemini_endpoint($path); | |||||||||||||||||||||||||||||||||||||||||||||
| 163 | 398 | my @params = ( $self->gemini_auth_query, @query ); | |||||||||||||||||||||||||||||||||||||||||||||
| 164 | 396 | ▶ | return $url unless @params; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 165 | # Values are interpolated verbatim, exactly as the call sites did before: | ||||||||||||||||||||||||||||||||||||||||||||||
| 166 | # the API key and the fixed switches (alt=sse) are URL-safe, and encoding | ||||||||||||||||||||||||||||||||||||||||||||||
| 167 | # them here would change bytes on the wire for no gain. | ||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 382 | my @pairs; | |||||||||||||||||||||||||||||||||||||||||||||
| 169 | 382 | ▶ | while ( my ( $name, $value ) = splice @params, 0, 2 ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 471 | push @pairs, $name . '=' . $value; | |||||||||||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 382 | return $url . '?' . join( '&', @pairs ); | |||||||||||||||||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 309 | ▶ | sub gemini_model_url { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
gemini_model_url: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 309 | my ( $self, $model, $method, @query ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 177 | 309 | return $self->gemini_url( 'models/' . $model . ':' . $method, @query ); | |||||||||||||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||||||
| 181 | # The REST contract names the field `cachedContent` and takes the resource | ||||||||||||||||||||||||||||||||||||||||||||||
| 182 | # name as a plain string (https://ai.google.dev/api/generate-content). A | ||||||||||||||||||||||||||||||||||||||||||||||
| 183 | # request naming a cache may not also set systemInstruction, tools or | ||||||||||||||||||||||||||||||||||||||||||||||
| 184 | # toolConfig: the server answers 400 "CachedContent can not be used with | ||||||||||||||||||||||||||||||||||||||||||||||
| 185 | # GenerateContent request setting system_instruction, tools or tool_config. | ||||||||||||||||||||||||||||||||||||||||||||||
| 186 | # Proposed fix: move those values to CachedContent from GenerateContent | ||||||||||||||||||||||||||||||||||||||||||||||
| 187 | # request." They come from the cache, so they are left out here, with one | ||||||||||||||||||||||||||||||||||||||||||||||
| 188 | # carp per engine (k327). Both JSON spellings of each field are dropped. | ||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 290 | ▶ | sub _cached_content_reference { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_cached_content_reference: called CC=12 SCAR=24.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 290 | my ( $self, $body, $extra ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 191 | 290 | ▶ | return unless $self->can('cached_content') && defined $self->cached_content; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->can("cached_content") and defined $self->cached_content
MC/DC50%1/2
$self->can("cached_content") and defined $self->cached_content
$self->can("cached_content")
defined $self->cached_content
Truth table66%2/3
A and B
A= $self->can("cached_content")B= defined $self->cached_content
| |||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 14 | my $cc = $self->cached_content; | |||||||||||||||||||||||||||||||||||||||||||||
| 193 | croak "Langertha::Engine::Gemini: cached_content must be a Langertha::CachedContent with a 'name'" | ||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 14 | ▶ | unless ref($cc) && eval { $cc->isa('Langertha::CachedContent') && $cc->has_name }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
ref $cc and eval {
$cc->isa('Langertha::CachedContent') && $cc->has_name
MC/DC50%1/2
ref $cc and eval {
do {
$cc->isa("Langertha::CachedContent") and $cc->has_name
}
}
ref $cc
eval {
do {
$cc->isa("Langertha::CachedContent") and $cc->has_name
}
}
MC/DC50%1/2
$cc->isa('Langertha::CachedContent') && $cc->has_name
$cc->isa('Langertha::CachedContent')
$cc->has_name
Truth table66%2/3
A and B
A= ref $ccB= eval {
do {
$cc->isa("Langertha::CachedContent") and $cc->has_name
}
}
Truth table66%2/3
A && B
A= $cc->isa('Langertha::CachedContent')B= $cc->has_name
| |||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 13 | $body->{cachedContent} = $cc->name; | |||||||||||||||||||||||||||||||||||||||||||||
| 196 | 13 | my @dropped; | |||||||||||||||||||||||||||||||||||||||||||||
| 197 | 13 | for my $field ( | |||||||||||||||||||||||||||||||||||||||||||||
| 198 | [ systemInstruction => 'system_instruction' ], | ||||||||||||||||||||||||||||||||||||||||||||||
| 199 | [ tools => 'tools' ], | ||||||||||||||||||||||||||||||||||||||||||||||
| 200 | [ toolConfig => 'tool_config' ], | ||||||||||||||||||||||||||||||||||||||||||||||
| 201 | ) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 39 | my $sent = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 203 | 39 | for my $key ( @$field ) { | |||||||||||||||||||||||||||||||||||||||||||||
| 204 | 78 | ▶ | $sent = 1 if defined delete $body->{$key}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 78 | ▶ | $sent = 1 if defined delete $extra->{$key}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 39 | ▶ | push @dropped, $field->[0] if $sent; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 13 | ▶ | $self->_langertha_carp( "".( ref $self ).": not sending " . join( ', ', @dropped ) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 210 | . " -- cached_content " . $cc->name . " is bound, and a request naming a cache takes" | ||||||||||||||||||||||||||||||||||||||||||||||
| 211 | . " systemInstruction, tools and toolConfig from the cache", 'cached_content_overrides' ) | ||||||||||||||||||||||||||||||||||||||||||||||
| 212 | if @dropped; | ||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 13 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 210 | ▶ | sub chat_request { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
chat_request: called CC=30 SCAR=34.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 210 | my ( $self, $messages, %extra ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||||||
| 219 | # Canonical per-request controls (chat_f, karr #46) beat the engine | ||||||||||||||||||||||||||||||||||||||||||||||
| 220 | # attributes on a per-key basis; the rest of %extra passes straight through. | ||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 210 | ▶ | my $controls = delete $extra{controls} // {}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $extra{"controls"} // {}
MC/DC100%1/1
delete $extra{"controls"} // {}
delete $extra{"controls"}
Truth table100%2/2
A
A= delete $extra{"controls"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 222 | # No parallel knob on this wire (parallel_tool_use is cleared): a value the | ||||||||||||||||||||||||||||||||||||||||||||||
| 223 | # caller set is only dropped, with the shared carp (karr k241). | ||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 210 | $self->_parallel_tool_calls_kwarg( \%extra, $controls ); | |||||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||||||
| 226 | # Translate tool_choice (canonical / OpenAI / Anthropic shapes) into | ||||||||||||||||||||||||||||||||||||||||||||||
| 227 | # Gemini's toolConfig.functionCallingConfig form. | ||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 210 | ▶ | if ( exists $extra{tool_choice} && defined $extra{tool_choice} ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $extra{"tool_choice"} and defined $extra{"tool_choice"}
MC/DC50%1/2
exists $extra{"tool_choice"} and defined $extra{"tool_choice"}
exists $extra{"tool_choice"}
defined $extra{"tool_choice"}
Truth table66%2/3
A and B
A= exists $extra{"tool_choice"}B= defined $extra{"tool_choice"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 6 | my $tc = Langertha::ToolChoice->from_hash( delete $extra{tool_choice} ); | |||||||||||||||||||||||||||||||||||||||||||||
| 230 | 6 | ▶ | if ($tc) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 6 | my $cfg = $tc->to( $self->tool_wire_format ); | |||||||||||||||||||||||||||||||||||||||||||||
| 232 | 6 | ▶ | $extra{toolConfig} = $cfg if $cfg; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||||||
| 236 | # Convert messages to Gemini format | ||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 210 | my @gemini_contents; | |||||||||||||||||||||||||||||||||||||||||||||
| 238 | my $system_instruction; | ||||||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 210 | for my $message (@{$messages}) { | |||||||||||||||||||||||||||||||||||||||||||||
| 241 | 255 | ▶ | if ($message->{role} eq 'system') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 242 | # Gemini uses systemInstruction field for system messages | ||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 24 | ▶ | $system_instruction .= "\n\n" if $system_instruction; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 244 | # Array content arrives as parts (Role::Chat, karr k269); keep its text. | ||||||||||||||||||||||||||||||||||||||||||||||
| 245 | $system_instruction .= $message->{parts} | ||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 2 | ▶ | ? join( "\n", map { $_->{text} // () } @{ $message->{parts} } ) | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$$_{"text"} // ()
MC/DC0%0/2
$$_{"text"} // ()
$$_{"text"}
()
Truth table33%1/3
A // B
A= $$_{"text"}B= ()
| |||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 24 | ▶ | : $message->{content}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 248 | } elsif ($message->{parts}) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 249 | # Already in Gemini format (e.g. from format_tool_results) | ||||||||||||||||||||||||||||||||||||||||||||||
| 250 | 36 | push @gemini_contents, $message; | |||||||||||||||||||||||||||||||||||||||||||||
| 251 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 252 | # Convert role: 'assistant' -> 'model' for Gemini | ||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 195 | ▶ | my $role = $message->{role} eq 'assistant' ? 'model' : $message->{role}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 254 | push @gemini_contents, { | ||||||||||||||||||||||||||||||||||||||||||||||
| 255 | role => $role, | ||||||||||||||||||||||||||||||||||||||||||||||
| 256 | 195 | parts => [{ text => $message->{content} }], | |||||||||||||||||||||||||||||||||||||||||||||
| 257 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||||||
| 261 | # Build the URL with model and API key. A per-request model names the model | ||||||||||||||||||||||||||||||||||||||||||||||
| 262 | # in the path, never in the body (karr k357). | ||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 210 | my $model_name = $self->_url_model( \%extra ); | |||||||||||||||||||||||||||||||||||||||||||||
| 264 | 210 | my $url = $self->gemini_model_url( $model_name, 'generateContent' ); | |||||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 210 | my %request_body = ( | |||||||||||||||||||||||||||||||||||||||||||||
| 267 | contents => \@gemini_contents, | ||||||||||||||||||||||||||||||||||||||||||||||
| 268 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||||||
| 270 | # Add system instruction if present | ||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 210 | ▶ | if ($system_instruction) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 272 | $request_body{systemInstruction} = { | ||||||||||||||||||||||||||||||||||||||||||||||
| 273 | 24 | parts => [{ text => $system_instruction }], | |||||||||||||||||||||||||||||||||||||||||||||
| 274 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||||||
| 277 | # Reference an explicit cachedContent resource by name when one was bound | ||||||||||||||||||||||||||||||||||||||||||||||
| 278 | # via $engine->cached_content (karr #22, k327). | ||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 210 | $self->_cached_content_reference( \%request_body, \%extra ); | |||||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||||||
| 281 | # Add generation config | ||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 209 | my %generation_config; | |||||||||||||||||||||||||||||||||||||||||||||
| 283 | 209 | ▶ | if ( exists $controls->{max_tokens} ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 284 | 1 | $generation_config{maxOutputTokens} = $controls->{max_tokens}; | |||||||||||||||||||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 286 | elsif ($self->get_response_size) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 208 | $generation_config{maxOutputTokens} = $self->get_response_size; | |||||||||||||||||||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 209 | ▶ | if ( exists $controls->{temperature} ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 2 | $generation_config{temperature} = $controls->{temperature}; | |||||||||||||||||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 292 | elsif ($self->has_temperature) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 2 | $generation_config{temperature} = $self->temperature; | |||||||||||||||||||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||||||
| 296 | # Translate response_format -> Gemini's generationConfig.responseJsonSchema / | ||||||||||||||||||||||||||||||||||||||||||||||
| 297 | # responseMimeType. Accepts the OpenAI-shape response_format hash so that | ||||||||||||||||||||||||||||||||||||||||||||||
| 298 | # callers can hand the same payload to any engine. A per-request | ||||||||||||||||||||||||||||||||||||||||||||||
| 299 | # response_format (chat_f) beats the engine attribute, and is removed from | ||||||||||||||||||||||||||||||||||||||||||||||
| 300 | # the extras either way: generateContent has no top-level response_format | ||||||||||||||||||||||||||||||||||||||||||||||
| 301 | # field and would carry it as dead weight while the schema went missing. | ||||||||||||||||||||||||||||||||||||||||||||||
| 302 | # chat_f hands us an OpenAI-shaped json_schema.schema (already JSON Schema), | ||||||||||||||||||||||||||||||||||||||||||||||
| 303 | # so it goes into responseJsonSchema — the field that accepts JSON Schema | ||||||||||||||||||||||||||||||||||||||||||||||
| 304 | # directly. The older responseSchema is deprecated and wanted Google's own | ||||||||||||||||||||||||||||||||||||||||||||||
| 305 | # Schema proto dialect, not raw JSON Schema (ai.google.dev/api/generate-content, | ||||||||||||||||||||||||||||||||||||||||||||||
| 306 | # verified 2026-09-01 — karr k140). responseMimeType stays required. | ||||||||||||||||||||||||||||||||||||||||||||||
| 307 | my $rf = exists $controls->{response_format} | ||||||||||||||||||||||||||||||||||||||||||||||
| 308 | ? delete $controls->{response_format} | ||||||||||||||||||||||||||||||||||||||||||||||
| 309 | : exists $extra{response_format} | ||||||||||||||||||||||||||||||||||||||||||||||
| 310 | ? delete $extra{response_format} | ||||||||||||||||||||||||||||||||||||||||||||||
| 311 | 209 | ▶ | : $self->has_response_format ? $self->response_format : undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 312 | 209 | ▶ | if ( defined $rf ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 313 | 6 | ▶ | my $type = ref($rf) eq 'HASH' ? ( $rf->{type} // '' ) : ''; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$$rf{"type"} // ""
MC/DC0%0/1
$$rf{"type"} // ""
$$rf{"type"}
Truth table50%1/2
A
A= $$rf{"type"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 6 | ▶ | if ( $type eq 'json_object' ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 1 | $generation_config{responseMimeType} = 'application/json'; | |||||||||||||||||||||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 317 | ▶ | elsif ( $type eq 'json_schema' | |||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%2/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 table25%1/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"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 318 | && ref( $rf->{json_schema} ) eq 'HASH' | ||||||||||||||||||||||||||||||||||||||||||||||
| 319 | && ref( $rf->{json_schema}{schema} ) eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 320 | 5 | $generation_config{responseMimeType} = 'application/json'; | |||||||||||||||||||||||||||||||||||||||||||||
| 321 | 5 | $generation_config{responseJsonSchema} = $rf->{json_schema}{schema}; | |||||||||||||||||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||||||
| 325 | # Merge reasoning effort / thinking_budget -> | ||||||||||||||||||||||||||||||||||||||||||||||
| 326 | # generationConfig.thinkingConfig.thinkingLevel (Gemini 3) or | ||||||||||||||||||||||||||||||||||||||||||||||
| 327 | # thinkingConfig.thinkingBudget (Gemini 2.5). Langertha::Reasoning owns | ||||||||||||||||||||||||||||||||||||||||||||||
| 328 | # the per-model placement; a per-request control (chat_f, karr #46) beats | ||||||||||||||||||||||||||||||||||||||||||||||
| 329 | # the engine attribute on a per-key basis. | ||||||||||||||||||||||||||||||||||||||||||||||
| 330 | 209 | my @reasoning = $self->reasoning_kwargs_for(%$controls); | |||||||||||||||||||||||||||||||||||||||||||||
| 331 | 193 | ▶ | %generation_config = ( %generation_config, @reasoning ) if @reasoning; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||||||
| 333 | 193 | ▶ | $request_body{generationConfig} = \%generation_config if %generation_config; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||||||
| 335 | return $self->generate_http_request( | ||||||||||||||||||||||||||||||||||||||||||||||
| 336 | POST => $url, | ||||||||||||||||||||||||||||||||||||||||||||||
| 337 | 37 | ▶ | sub { $self->chat_response(shift) }, | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 338 | 193 | %request_body, | |||||||||||||||||||||||||||||||||||||||||||||
| 339 | %extra, | ||||||||||||||||||||||||||||||||||||||||||||||
| 340 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||||||
| 343 | # --- embeddings (k309) ----------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||
| 344 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 345 | # A string goes to models/{m}:embedContent, an ArrayRef to | ||||||||||||||||||||||||||||||||||||||||||||||
| 346 | # models/{m}:batchEmbedContents with one EmbedContentRequest per input | ||||||||||||||||||||||||||||||||||||||||||||||
| 347 | # (ai.google.dev/api/embeddings, fetched 2026-09-25). taskType / title / | ||||||||||||||||||||||||||||||||||||||||||||||
| 348 | # outputDimensionality belong in embedContentConfig; the top-level spellings | ||||||||||||||||||||||||||||||||||||||||||||||
| 349 | # are deprecated, so the snake_case extras are placed there. Everything else | ||||||||||||||||||||||||||||||||||||||||||||||
| 350 | # in %extra goes into each EmbedContentRequest verbatim (ADR 0004). | ||||||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||||||
| 352 | 7 | ▶ | sub default_embedding_model { 'gemini-embedding-001' } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_embedding_model: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||||||
| 354 | 60 | my %EMBED_CONFIG_KEY = ( | |||||||||||||||||||||||||||||||||||||||||||||
| 355 | task_type => 'taskType', | ||||||||||||||||||||||||||||||||||||||||||||||
| 356 | output_dimensionality => 'outputDimensionality', | ||||||||||||||||||||||||||||||||||||||||||||||
| 357 | title => 'title', | ||||||||||||||||||||||||||||||||||||||||||||||
| 358 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||||||
| 360 | 20 | ▶ | sub embedding_request { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
embedding_request: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 361 | 20 | my ( $self, $input, %extra ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 362 | 20 | my $model = $self->embedding_model; | |||||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||||||
| 364 | my %config = ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 365 | defined $self->embedding_dimensions ? ( outputDimensionality => $self->embedding_dimensions ) : (), | ||||||||||||||||||||||||||||||||||||||||||||||
| 366 | 20 | ▶ | %{ delete $extra{embedContentConfig} // {} }, | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
delete $extra{"embedContentConfig"} // {}
MC/DC100%1/1
delete $extra{"embedContentConfig"} // {}
delete $extra{"embedContentConfig"}
Truth table100%2/2
A
A= delete $extra{"embedContentConfig"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 367 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 368 | 20 | for my $key ( sort keys %EMBED_CONFIG_KEY ) { | |||||||||||||||||||||||||||||||||||||||||||||
| 369 | 60 | ▶ | $config{ $EMBED_CONFIG_KEY{$key} } = delete $extra{$key} if exists $extra{$key}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 371 | 27 | ▶ | my $embed_request = sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 372 | 27 | my ( $text ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 373 | return { | ||||||||||||||||||||||||||||||||||||||||||||||
| 374 | 27 | ▶ | model => 'models/' . $model, | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 375 | content => { parts => [ { text => $text } ] }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 376 | %config ? ( embedContentConfig => {%config} ) : (), | ||||||||||||||||||||||||||||||||||||||||||||||
| 377 | %extra, | ||||||||||||||||||||||||||||||||||||||||||||||
| 378 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 379 | 20 | }; | |||||||||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||||||||||
| 381 | 20 | ▶ | if ( ref $input eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 382 | return $self->generate_http_request( | ||||||||||||||||||||||||||||||||||||||||||||||
| 383 | POST => $self->gemini_model_url( $model, 'batchEmbedContents' ), | ||||||||||||||||||||||||||||||||||||||||||||||
| 384 | 4 | ▶ | sub { $self->embedding_response( shift, $input ) }, | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 385 | 6 | requests => [ map { $embed_request->($_) } @{$input} ], | |||||||||||||||||||||||||||||||||||||||||||||
| 386 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 388 | return $self->generate_http_request( | ||||||||||||||||||||||||||||||||||||||||||||||
| 389 | POST => $self->gemini_model_url( $model, 'embedContent' ), | ||||||||||||||||||||||||||||||||||||||||||||||
| 390 | 5 | ▶ | sub { $self->embedding_response( shift, $input ) }, | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 391 | 14 | %{ $embed_request->($input) }, | |||||||||||||||||||||||||||||||||||||||||||||
| 392 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||||||||||||||||||
| 396 | 9 | ▶ | sub embedding_response { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
embedding_response: called CC=15 SCAR=27.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 397 | 9 | my ( $self, $response, $input ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 398 | 9 | my $data = $self->parse_response($response); | |||||||||||||||||||||||||||||||||||||||||||||
| 399 | 9 | my $batch = ref $input eq 'ARRAY'; | |||||||||||||||||||||||||||||||||||||||||||||
| 400 | my @embeddings = ref $data ne 'HASH' ? () | ||||||||||||||||||||||||||||||||||||||||||||||
| 401 | 4 | ▶ | : $batch ? ( ref $data->{embeddings} eq 'ARRAY' ? @{ $data->{embeddings} } : () ) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 402 | 9 | ▶ | : ( defined $data->{embedding} ? $data->{embedding} : () ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 403 | 9 | ▶ | my @vectors = map { ref $_ eq 'HASH' ? $_->{values} : undef } @embeddings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 404 | # No vector is no result: never hand back undef as if it were one (k290). | ||||||||||||||||||||||||||||||||||||||||||||||
| 405 | 9 | ▶ | if ( !@vectors || grep { ref $_ ne 'ARRAY' || !@{$_} } @vectors ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
not @vectors or grep {ref $_ ne "ARRAY" or not @{$_;};} @vectors
ref $_ ne 'ARRAY' || !@{$_;}
MC/DC100%2/2
not @vectors or grep {ref $_ ne "ARRAY" or not @{$_;};} @vectors
not @vectors
grep {ref $_ ne "ARRAY" or not @{$_;};} @vectors
MC/DC50%1/2
ref $_ ne 'ARRAY' || !@{$_;}
ref $_ ne 'ARRAY'
!@{$_;}
Truth table100%3/3
not(A) or B
A= not @vectorsB= grep {ref $_ ne "ARRAY" or not @{$_;};} @vectors
Truth table66%2/3
A || not(B)
A= ref $_ ne 'ARRAY'B= !@{$_;}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 406 | my $err = ref $data eq 'HASH' && $data->{error} | ||||||||||||||||||||||||||||||||||||||||||||||
| 407 | ? ( ref $data->{error} eq 'HASH' ? $data->{error}{message} : $data->{error} ) | ||||||||||||||||||||||||||||||||||||||||||||||
| 408 | 3 | ▶ | : undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
ref $data eq "HASH" and $$data{"error"}
MC/DC50%1/2
ref $data eq "HASH" and $$data{"error"}
ref $data eq "HASH"
$$data{"error"}
Truth table66%2/3
A and B
A= ref $data eq "HASH"B= $$data{"error"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 409 | 3 | ▶ | croak "".(ref $self)." embedding response contained no vector" | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 410 | . ( defined $err ? " (error: $err)" : '' ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 411 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 412 | 6 | ▶ | return $vectors[0] unless $batch; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 413 | croak "".(ref $self)." embedding response returned ".scalar(@vectors) | ||||||||||||||||||||||||||||||||||||||||||||||
| 414 | 1 | ." vectors for ".scalar(@{$input})." inputs" | |||||||||||||||||||||||||||||||||||||||||||||
| 415 | 3 | ▶ | unless @vectors == @{$input}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 416 | 2 | return \@vectors; | |||||||||||||||||||||||||||||||||||||||||||||
| 417 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||||||||||||||||
| 420 | 354 | ▶ | sub update_request { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
update_request: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 421 | 354 | my ( $self, $request ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 422 | 354 | $request->header('content-type', 'application/json'); | |||||||||||||||||||||||||||||||||||||||||||||
| 423 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||||||
| 425 | 91 | ▶ | sub chat_response { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
chat_response: called CC=24 SCAR=31.8
| |||||||||||||||||||||||||||||||||||||||||||||||
| 426 | 91 | my ( $self, $response ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 427 | 91 | my $data = $self->parse_response($response); | |||||||||||||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||||||||||||||
| 429 | # Gemini response format: candidates[0].content.parts[].text | ||||||||||||||||||||||||||||||||||||||||||||||
| 430 | 86 | ▶ | my $candidates = $data->{candidates} || []; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$$data{'candidates'} || []
MC/DC100%1/1
$$data{'candidates'} || []
$$data{'candidates'}
Truth table100%2/2
A
A= $$data{'candidates'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 431 | 86 | my $text = ''; | |||||||||||||||||||||||||||||||||||||||||||||
| 432 | 86 | my $finish_reason; | |||||||||||||||||||||||||||||||||||||||||||||
| 433 | my $thinking; | ||||||||||||||||||||||||||||||||||||||||||||||
| 434 | 86 | ▶ | if (@$candidates) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 435 | 71 | my $candidate = $candidates->[0]; | |||||||||||||||||||||||||||||||||||||||||||||
| 436 | 71 | ▶ | 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'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 437 | 71 | ▶ | 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'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 438 | 71 | my @text_parts; | |||||||||||||||||||||||||||||||||||||||||||||
| 439 | my @thought_parts; | ||||||||||||||||||||||||||||||||||||||||||||||
| 440 | 71 | for my $part (@$parts) { | |||||||||||||||||||||||||||||||||||||||||||||
| 441 | 83 | ▶ | next unless exists $part->{text}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 442 | 69 | ▶ | if ($part->{thought}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 443 | 7 | push @thought_parts, $part->{text}; | |||||||||||||||||||||||||||||||||||||||||||||
| 444 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 445 | 62 | push @text_parts, $part->{text}; | |||||||||||||||||||||||||||||||||||||||||||||
| 446 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 447 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 448 | 71 | $text = join('', @text_parts); | |||||||||||||||||||||||||||||||||||||||||||||
| 449 | 71 | ▶ | $thinking = join("\n", @thought_parts) if @thought_parts; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 450 | 71 | $finish_reason = $candidate->{finishReason}; | |||||||||||||||||||||||||||||||||||||||||||||
| 451 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 452 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 453 | # No candidate: a blocked prompt says why in promptFeedback.blockReason. | ||||||||||||||||||||||||||||||||||||||||||||||
| 454 | # A block is an answer, so it is reported as finish_reason, verbatim like | ||||||||||||||||||||||||||||||||||||||||||||||
| 455 | # a candidate's finishReason (SAFETY, PROHIBITED_CONTENT, ...). Anything | ||||||||||||||||||||||||||||||||||||||||||||||
| 456 | # else without a candidate is no answer and croaks. -- karr k301 | ||||||||||||||||||||||||||||||||||||||||||||||
| 457 | 15 | $finish_reason = $self->_gemini_block_reason($data); | |||||||||||||||||||||||||||||||||||||||||||||
| 458 | 15 | ▶ | unless ( defined $finish_reason ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 459 | 6 | my $error = $self->_body_error_text( $data->{error} ); | |||||||||||||||||||||||||||||||||||||||||||||
| 460 | 6 | ▶ | croak "".(ref $self)." response carried an error: $error" if defined $error; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 461 | 1 | croak "".(ref $self)." response contained no candidates"; | |||||||||||||||||||||||||||||||||||||||||||||
| 462 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 463 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||||||||||||||||||||
| 465 | # Normalize Gemini usage metadata. cachedContentTokenCount is surfaced | ||||||||||||||||||||||||||||||||||||||||||||||
| 466 | # when present so callers can monitor cache-hit rate (karr #22, 22e). | ||||||||||||||||||||||||||||||||||||||||||||||
| 467 | # See https://ai.google.dev/api/generate-content (usageMetadata). | ||||||||||||||||||||||||||||||||||||||||||||||
| 468 | # Langertha::Usage->from_hash reads both spellings (ADR 0018 tier 1), but the | ||||||||||||||||||||||||||||||||||||||||||||||
| 469 | # rename stays: Usage's %{} overload serves this hash verbatim, so | ||||||||||||||||||||||||||||||||||||||||||||||
| 470 | # $response->usage->{prompt_tokens} / {cached_content_token_count} are | ||||||||||||||||||||||||||||||||||||||||||||||
| 471 | # public back-compat keys (Langertha::CachedContent POD, karr k197). | ||||||||||||||||||||||||||||||||||||||||||||||
| 472 | # The snake_case keys take OpenAI's meaning: thoughtsTokenCount (billed as | ||||||||||||||||||||||||||||||||||||||||||||||
| 473 | # output, not part of candidatesTokenCount) is counted in completion_tokens | ||||||||||||||||||||||||||||||||||||||||||||||
| 474 | # and shown under completion_tokens_details.reasoning_tokens; the tool-use | ||||||||||||||||||||||||||||||||||||||||||||||
| 475 | # prompt (toolUsePromptTokenCount) in prompt_tokens (k299). | ||||||||||||||||||||||||||||||||||||||||||||||
| 476 | 80 | my $usage; | |||||||||||||||||||||||||||||||||||||||||||||
| 477 | 80 | ▶ | if (my $um = $data->{usageMetadata}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 478 | 18 | my $thoughts = $um->{thoughtsTokenCount}; | |||||||||||||||||||||||||||||||||||||||||||||
| 479 | my ( $prompt, $tool_prompt, $candidates ) = | ||||||||||||||||||||||||||||||||||||||||||||||
| 480 | 18 | @{$um}{qw( promptTokenCount toolUsePromptTokenCount candidatesTokenCount )}; | |||||||||||||||||||||||||||||||||||||||||||||
| 481 | $usage = { | ||||||||||||||||||||||||||||||||||||||||||||||
| 482 | prompt_tokens => defined $tool_prompt ? ( $prompt // 0 ) + $tool_prompt : $prompt, | ||||||||||||||||||||||||||||||||||||||||||||||
| 483 | completion_tokens => defined $thoughts ? ( $candidates // 0 ) + $thoughts : $candidates, | ||||||||||||||||||||||||||||||||||||||||||||||
| 484 | total_tokens => $um->{totalTokenCount}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 485 | 18 | ▶ | }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition50%2/4
$prompt // 0
$candidates // 0
MC/DC0%0/1
$prompt // 0
$prompt
MC/DC0%0/1
$candidates // 0
$candidates
Truth table50%1/2
A
A= $prompt
Truth table50%1/2
A
A= $candidates
| |||||||||||||||||||||||||||||||||||||||||||||||
| 486 | 18 | ▶ | $usage->{completion_tokens_details} = { reasoning_tokens => $thoughts } if defined $thoughts; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 487 | 18 | ▶ | if ( defined $um->{cachedContentTokenCount} ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 488 | 2 | $usage->{cached_content_token_count} = $um->{cachedContentTokenCount}; | |||||||||||||||||||||||||||||||||||||||||||||
| 489 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 490 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||||||||||||||||||||
| 492 | 80 | my @tcs = Langertha::ToolCall->extract( $self->tool_wire_format, $data ); | |||||||||||||||||||||||||||||||||||||||||||||
| 493 | return Langertha::Response->new( | ||||||||||||||||||||||||||||||||||||||||||||||
| 494 | content => $text, | ||||||||||||||||||||||||||||||||||||||||||||||
| 495 | raw => $data, | ||||||||||||||||||||||||||||||||||||||||||||||
| 496 | $data->{responseId} ? ( id => $data->{responseId} ) : (), | ||||||||||||||||||||||||||||||||||||||||||||||
| 497 | 80 | ▶ | $data->{modelVersion} ? ( model => $data->{modelVersion} ) : (), | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 498 | defined $finish_reason ? ( finish_reason => $finish_reason ) : (), | ||||||||||||||||||||||||||||||||||||||||||||||
| 499 | $usage ? ( usage => $usage ) : (), | ||||||||||||||||||||||||||||||||||||||||||||||
| 500 | defined $thinking ? ( thinking => $thinking ) : (), | ||||||||||||||||||||||||||||||||||||||||||||||
| 501 | @tcs ? ( tool_calls => [ @tcs ] ) : (), | ||||||||||||||||||||||||||||||||||||||||||||||
| 502 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 503 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||||||||||||||||||||
| 505 | # promptFeedback.blockReason of a candidate-less answer (a blocked prompt), | ||||||||||||||||||||||||||||||||||||||||||||||
| 506 | # undef when there is none. -- karr k301, k311 | ||||||||||||||||||||||||||||||||||||||||||||||
| 507 | 25 | ▶ | sub _gemini_block_reason { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_gemini_block_reason: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||||||||||||||||||||
| 508 | 25 | my ( $self, $data ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 509 | 25 | ▶ | my $feedback = ref $data->{promptFeedback} eq 'HASH' ? $data->{promptFeedback} : {}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 510 | 25 | my $reason = $feedback->{blockReason}; | |||||||||||||||||||||||||||||||||||||||||||||
| 511 | 25 | ▶ | return defined $reason && !ref $reason && length $reason ? $reason : undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
defined $reason and not ref $reason and length $reason
defined $reason and not ref $reason
MC/DC33%1/3
defined $reason and not ref $reason and length $reason
defined $reason
not ref $reason
length $reason
Truth table50%2/4
A and not(B) and C
A= defined $reasonB= not ref $reasonC= length $reason
| |||||||||||||||||||||||||||||||||||||||||||||||
| 512 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||||||||||||||||||||
| 514 | # The tool loops croak on a blocked prompt (Role::Tools, karr k339): the | ||||||||||||||||||||||||||||||||||||||||||||||
| 515 | # candidate-less answer chat_response reports as finish_reason. | ||||||||||||||||||||||||||||||||||||||||||||||
| 516 | 30 | ▶ | sub _tool_loop_block_reason { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_tool_loop_block_reason: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 517 | 30 | my ( $self, $response ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 518 | 30 | my $data = $response->raw; | |||||||||||||||||||||||||||||||||||||||||||||
| 519 | 30 | ▶ | return undef unless ref $data eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 520 | 30 | ▶ | return undef if ref $data->{candidates} eq 'ARRAY' && @{ $data->{candidates} }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $$data{"candidates"} eq "ARRAY" and @{$$data{"candidates"};}
MC/DC50%1/2
ref $$data{"candidates"} eq "ARRAY" and @{$$data{"candidates"};}
ref $$data{"candidates"} eq "ARRAY"
@{$$data{"candidates"};}
Truth table66%2/3
A and B
A= ref $$data{"candidates"} eq "ARRAY"B= @{$$data{"candidates"};}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 521 | 7 | return $self->_gemini_block_reason($data); | |||||||||||||||||||||||||||||||||||||||||||||
| 522 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||||||||||||||||||||
| 525 | 4 | ▶ | sub stream_format { 'sse' } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
stream_format: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||||||||||||||||||||
| 527 | 80 | ▶ | sub chat_stream_request { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
chat_stream_request: called CC=30 SCAR=37.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 528 | 80 | my ( $self, $messages, %extra ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||||||||||||||||||||
| 530 | # Canonical per-request controls (chat_f, karr #46) beat the engine | ||||||||||||||||||||||||||||||||||||||||||||||
| 531 | # attributes on a per-key basis; the rest of %extra passes straight through. | ||||||||||||||||||||||||||||||||||||||||||||||
| 532 | 80 | ▶ | my $controls = delete $extra{controls} // {}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $extra{"controls"} // {}
MC/DC100%1/1
delete $extra{"controls"} // {}
delete $extra{"controls"}
Truth table100%2/2
A
A= delete $extra{"controls"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 533 | # No parallel knob on this wire (parallel_tool_use is cleared): a value the | ||||||||||||||||||||||||||||||||||||||||||||||
| 534 | # caller set is only dropped, with the shared carp (karr k241). | ||||||||||||||||||||||||||||||||||||||||||||||
| 535 | 80 | $self->_parallel_tool_calls_kwarg( \%extra, $controls ); | |||||||||||||||||||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||||||||||||||||||||
| 537 | # Same tool_choice translation as chat_request. | ||||||||||||||||||||||||||||||||||||||||||||||
| 538 | 80 | ▶ | if ( exists $extra{tool_choice} && defined $extra{tool_choice} ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $extra{"tool_choice"} and defined $extra{"tool_choice"}
MC/DC50%1/2
exists $extra{"tool_choice"} and defined $extra{"tool_choice"}
exists $extra{"tool_choice"}
defined $extra{"tool_choice"}
Truth table66%2/3
A and B
A= exists $extra{"tool_choice"}B= defined $extra{"tool_choice"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 539 | 5 | my $tc = Langertha::ToolChoice->from_hash( delete $extra{tool_choice} ); | |||||||||||||||||||||||||||||||||||||||||||||
| 540 | 5 | ▶ | if ($tc) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 541 | 5 | my $cfg = $tc->to( $self->tool_wire_format ); | |||||||||||||||||||||||||||||||||||||||||||||
| 542 | 5 | ▶ | $extra{toolConfig} = $cfg if $cfg; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 543 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 544 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||||||||||||||||||||
| 546 | # Convert messages to Gemini format (same as non-streaming) | ||||||||||||||||||||||||||||||||||||||||||||||
| 547 | 80 | my @gemini_contents; | |||||||||||||||||||||||||||||||||||||||||||||
| 548 | my $system_instruction; | ||||||||||||||||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||||||||||||||||
| 550 | 80 | for my $message (@{$messages}) { | |||||||||||||||||||||||||||||||||||||||||||||
| 551 | 90 | ▶ | if ($message->{role} eq 'system') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 552 | 9 | ▶ | $system_instruction .= "\n\n" if $system_instruction; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 553 | # Array content arrives as parts (Role::Chat, karr k269); keep its text. | ||||||||||||||||||||||||||||||||||||||||||||||
| 554 | $system_instruction .= $message->{parts} | ||||||||||||||||||||||||||||||||||||||||||||||
| 555 | 0 | ▶ | ? join( "\n", map { $_->{text} // () } @{ $message->{parts} } ) | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$$_{"text"} // ()
MC/DC0%0/2
$$_{"text"} // ()
$$_{"text"}
()
Truth table0%0/3
A // B
A= $$_{"text"}B= ()
| |||||||||||||||||||||||||||||||||||||||||||||||
| 556 | 9 | ▶ | : $message->{content}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 557 | } elsif ($message->{parts}) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 558 | # Already in Gemini format (e.g. from format_tool_results) | ||||||||||||||||||||||||||||||||||||||||||||||
| 559 | 7 | push @gemini_contents, $message; | |||||||||||||||||||||||||||||||||||||||||||||
| 560 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 561 | 74 | ▶ | my $role = $message->{role} eq 'assistant' ? 'model' : $message->{role}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 562 | push @gemini_contents, { | ||||||||||||||||||||||||||||||||||||||||||||||
| 563 | role => $role, | ||||||||||||||||||||||||||||||||||||||||||||||
| 564 | 74 | parts => [{ text => $message->{content} }], | |||||||||||||||||||||||||||||||||||||||||||||
| 565 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 566 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 567 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 568 | |||||||||||||||||||||||||||||||||||||||||||||||
| 569 | # Build the URL for streaming endpoint (a per-request model as in chat_request) | ||||||||||||||||||||||||||||||||||||||||||||||
| 570 | 80 | my $model_name = $self->_url_model( \%extra ); | |||||||||||||||||||||||||||||||||||||||||||||
| 571 | 80 | my $url = $self->gemini_model_url( $model_name, 'streamGenerateContent', alt => 'sse' ); | |||||||||||||||||||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||||||||||||||||||||
| 573 | 80 | my %request_body = ( | |||||||||||||||||||||||||||||||||||||||||||||
| 574 | contents => \@gemini_contents, | ||||||||||||||||||||||||||||||||||||||||||||||
| 575 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||||||||||||||||||||
| 577 | 80 | ▶ | if ($system_instruction) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 578 | $request_body{systemInstruction} = { | ||||||||||||||||||||||||||||||||||||||||||||||
| 579 | 9 | parts => [{ text => $system_instruction }], | |||||||||||||||||||||||||||||||||||||||||||||
| 580 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 581 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||||||||||||||||||||
| 583 | # Reference an explicit cachedContent resource by name when one was bound | ||||||||||||||||||||||||||||||||||||||||||||||
| 584 | # via $engine->cached_content (karr #22, k327). Same wire as chat_request. | ||||||||||||||||||||||||||||||||||||||||||||||
| 585 | 80 | $self->_cached_content_reference( \%request_body, \%extra ); | |||||||||||||||||||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||||||||||||||||||||
| 587 | 80 | my %generation_config; | |||||||||||||||||||||||||||||||||||||||||||||
| 588 | 80 | ▶ | if ( exists $controls->{max_tokens} ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 589 | 0 | $generation_config{maxOutputTokens} = $controls->{max_tokens}; | |||||||||||||||||||||||||||||||||||||||||||||
| 590 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 591 | elsif ($self->get_response_size) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 592 | 80 | $generation_config{maxOutputTokens} = $self->get_response_size; | |||||||||||||||||||||||||||||||||||||||||||||
| 593 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 594 | 80 | ▶ | if ( exists $controls->{temperature} ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 595 | 0 | $generation_config{temperature} = $controls->{temperature}; | |||||||||||||||||||||||||||||||||||||||||||||
| 596 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 597 | elsif ($self->has_temperature) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 598 | 1 | $generation_config{temperature} = $self->temperature; | |||||||||||||||||||||||||||||||||||||||||||||
| 599 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||||||||||||||||||||
| 601 | # Translate response_format -> Gemini's generationConfig.responseJsonSchema / | ||||||||||||||||||||||||||||||||||||||||||||||
| 602 | # responseMimeType. Same wire as chat_request: a per-request | ||||||||||||||||||||||||||||||||||||||||||||||
| 603 | # response_format (chat_stream_realtime_f) beats the engine attribute, | ||||||||||||||||||||||||||||||||||||||||||||||
| 604 | # and is removed from the extras either way — generateContent has no | ||||||||||||||||||||||||||||||||||||||||||||||
| 605 | # top-level response_format field and would carry it as dead weight | ||||||||||||||||||||||||||||||||||||||||||||||
| 606 | # while the schema went missing. responseJsonSchema is the current field that | ||||||||||||||||||||||||||||||||||||||||||||||
| 607 | # accepts raw JSON Schema; the deprecated responseSchema wanted Google's Schema | ||||||||||||||||||||||||||||||||||||||||||||||
| 608 | # proto dialect instead (see chat_request — karr k140). | ||||||||||||||||||||||||||||||||||||||||||||||
| 609 | my $rf = exists $controls->{response_format} | ||||||||||||||||||||||||||||||||||||||||||||||
| 610 | ? delete $controls->{response_format} | ||||||||||||||||||||||||||||||||||||||||||||||
| 611 | : exists $extra{response_format} | ||||||||||||||||||||||||||||||||||||||||||||||
| 612 | ? delete $extra{response_format} | ||||||||||||||||||||||||||||||||||||||||||||||
| 613 | 80 | ▶ | : $self->has_response_format ? $self->response_format : undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 614 | 80 | ▶ | if ( defined $rf ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 615 | 4 | ▶ | my $type = ref($rf) eq 'HASH' ? ( $rf->{type} // '' ) : ''; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$$rf{"type"} // ""
MC/DC0%0/1
$$rf{"type"} // ""
$$rf{"type"}
Truth table50%1/2
A
A= $$rf{"type"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 616 | 4 | ▶ | if ( $type eq 'json_object' ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 617 | 1 | $generation_config{responseMimeType} = 'application/json'; | |||||||||||||||||||||||||||||||||||||||||||||
| 618 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 619 | ▶ | elsif ( $type eq 'json_schema' | |||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%2/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 table25%1/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"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 620 | && ref( $rf->{json_schema} ) eq 'HASH' | ||||||||||||||||||||||||||||||||||||||||||||||
| 621 | && ref( $rf->{json_schema}{schema} ) eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 622 | 3 | $generation_config{responseMimeType} = 'application/json'; | |||||||||||||||||||||||||||||||||||||||||||||
| 623 | 3 | $generation_config{responseJsonSchema} = $rf->{json_schema}{schema}; | |||||||||||||||||||||||||||||||||||||||||||||
| 624 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 625 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 626 | |||||||||||||||||||||||||||||||||||||||||||||||
| 627 | 80 | my @reasoning = $self->reasoning_kwargs_for(%$controls); | |||||||||||||||||||||||||||||||||||||||||||||
| 628 | 65 | ▶ | %generation_config = ( %generation_config, @reasoning ) if @reasoning; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 629 | |||||||||||||||||||||||||||||||||||||||||||||||
| 630 | 65 | ▶ | $request_body{generationConfig} = \%generation_config if %generation_config; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 631 | |||||||||||||||||||||||||||||||||||||||||||||||
| 632 | return $self->generate_http_request( | ||||||||||||||||||||||||||||||||||||||||||||||
| 633 | POST => $url, | ||||||||||||||||||||||||||||||||||||||||||||||
| 634 | 0 | ▶ | sub {}, | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 635 | 65 | %request_body, | |||||||||||||||||||||||||||||||||||||||||||||
| 636 | %extra, | ||||||||||||||||||||||||||||||||||||||||||||||
| 637 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 638 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 639 | |||||||||||||||||||||||||||||||||||||||||||||||
| 640 | 29 | ▶ | sub parse_stream_chunk { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_stream_chunk: called CC=18 SCAR=29.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 641 | 29 | my ( $self, $data, $event ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 642 | |||||||||||||||||||||||||||||||||||||||||||||||
| 643 | 29 | require Langertha::Stream::Chunk; | |||||||||||||||||||||||||||||||||||||||||||||
| 644 | |||||||||||||||||||||||||||||||||||||||||||||||
| 645 | # An error inside an open stream arrives as a chunk with a top-level `error` | ||||||||||||||||||||||||||||||||||||||||||||||
| 646 | # object. Skipping it ended the stream as a short, silent success; the croak | ||||||||||||||||||||||||||||||||||||||||||||||
| 647 | # fails the stream, as the OpenAI-compatible parser does. -- karr k317 | ||||||||||||||||||||||||||||||||||||||||||||||
| 648 | 29 | ▶ | if ( ref $data eq 'HASH' && defined $data->{error} ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $data eq "HASH" and defined $$data{"error"}
MC/DC50%1/2
ref $data eq "HASH" and defined $$data{"error"}
ref $data eq "HASH"
defined $$data{"error"}
Truth table66%2/3
A and B
A= ref $data eq "HASH"B= defined $$data{"error"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 649 | 2 | croak "".(ref $self)." stream carried an error: ".$self->_body_error_text( $data->{error} ); | |||||||||||||||||||||||||||||||||||||||||||||
| 650 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 651 | |||||||||||||||||||||||||||||||||||||||||||||||
| 652 | # Gemini streaming format is similar to non-streaming | ||||||||||||||||||||||||||||||||||||||||||||||
| 653 | 27 | ▶ | my $candidates = $data->{candidates} || []; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$$data{'candidates'} || []
MC/DC100%1/1
$$data{'candidates'} || []
$$data{'candidates'}
Truth table100%2/2
A
A= $$data{'candidates'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 654 | 27 | ▶ | unless (@$candidates) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 655 | # A blocked prompt streams a chunk with promptFeedback.blockReason and no | ||||||||||||||||||||||||||||||||||||||||||||||
| 656 | # candidate. Skipping it ended the stream without a finish_reason; it is | ||||||||||||||||||||||||||||||||||||||||||||||
| 657 | # the final chunk, reported like chat_response does (k301). -- karr k311 | ||||||||||||||||||||||||||||||||||||||||||||||
| 658 | 3 | my $block_reason = $self->_gemini_block_reason($data); | |||||||||||||||||||||||||||||||||||||||||||||
| 659 | 3 | ▶ | return undef unless defined $block_reason; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 660 | return Langertha::Stream::Chunk->new( | ||||||||||||||||||||||||||||||||||||||||||||||
| 661 | content => '', | ||||||||||||||||||||||||||||||||||||||||||||||
| 662 | raw => $data, | ||||||||||||||||||||||||||||||||||||||||||||||
| 663 | is_final => 1, | ||||||||||||||||||||||||||||||||||||||||||||||
| 664 | finish_reason => $block_reason, | ||||||||||||||||||||||||||||||||||||||||||||||
| 665 | 1 | ▶ | $data->{usageMetadata} ? ( usage => $data->{usageMetadata} ) : (), | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 666 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 667 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 668 | |||||||||||||||||||||||||||||||||||||||||||||||
| 669 | 24 | my $candidate = $candidates->[0]; | |||||||||||||||||||||||||||||||||||||||||||||
| 670 | 24 | ▶ | 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'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 671 | 24 | ▶ | 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'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 672 | |||||||||||||||||||||||||||||||||||||||||||||||
| 673 | # Same shape as chat_response: walk every part, not just parts[0]. A chunk can | ||||||||||||||||||||||||||||||||||||||||||||||
| 674 | # carry a thought part ahead of (or interleaved with) the answer part; reading | ||||||||||||||||||||||||||||||||||||||||||||||
| 675 | # only parts[0] leaked a thought summary into content and dropped the real | ||||||||||||||||||||||||||||||||||||||||||||||
| 676 | # answer that followed it. A part is a thought summary when `thought` is true; | ||||||||||||||||||||||||||||||||||||||||||||||
| 677 | # such parts feed the chunk's thinking, the rest feed content. -- karr k129 | ||||||||||||||||||||||||||||||||||||||||||||||
| 678 | 24 | my @text_parts; | |||||||||||||||||||||||||||||||||||||||||||||
| 679 | my @thought_parts; | ||||||||||||||||||||||||||||||||||||||||||||||
| 680 | 24 | for my $part (@$parts) { | |||||||||||||||||||||||||||||||||||||||||||||
| 681 | 31 | ▶ | next unless exists $part->{text}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 682 | 29 | ▶ | if ($part->{thought}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 683 | 10 | push @thought_parts, $part->{text}; | |||||||||||||||||||||||||||||||||||||||||||||
| 684 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 685 | 19 | push @text_parts, $part->{text}; | |||||||||||||||||||||||||||||||||||||||||||||
| 686 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 687 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 688 | 24 | my $text = join('', @text_parts); | |||||||||||||||||||||||||||||||||||||||||||||
| 689 | 24 | ▶ | my $thinking = @thought_parts ? join('', @thought_parts) : undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 690 | |||||||||||||||||||||||||||||||||||||||||||||||
| 691 | 24 | my $finish_reason = $candidate->{finishReason}; | |||||||||||||||||||||||||||||||||||||||||||||
| 692 | 24 | ▶ | my $is_final = defined $finish_reason && $finish_reason ne ''; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
defined $finish_reason && $finish_reason ne ''
MC/DC50%1/2
defined $finish_reason && $finish_reason ne ''
defined $finish_reason
$finish_reason ne ''
Truth table66%2/3
A && B
A= defined $finish_reasonB= $finish_reason ne ''
| |||||||||||||||||||||||||||||||||||||||||||||||
| 693 | |||||||||||||||||||||||||||||||||||||||||||||||
| 694 | # A streamed functionCall part arrives whole in one chunk (it is not | ||||||||||||||||||||||||||||||||||||||||||||||
| 695 | # fragmented), so the chunk that carries it is where the call completes. Read | ||||||||||||||||||||||||||||||||||||||||||||||
| 696 | # it with the same ToolCall->extract chat_response uses. -- karr k221 | ||||||||||||||||||||||||||||||||||||||||||||||
| 697 | 24 | my @tool_calls = Langertha::ToolCall->extract( $self->tool_wire_format, $data ); | |||||||||||||||||||||||||||||||||||||||||||||
| 698 | |||||||||||||||||||||||||||||||||||||||||||||||
| 699 | return Langertha::Stream::Chunk->new( | ||||||||||||||||||||||||||||||||||||||||||||||
| 700 | content => $text, | ||||||||||||||||||||||||||||||||||||||||||||||
| 701 | raw => $data, | ||||||||||||||||||||||||||||||||||||||||||||||
| 702 | is_final => $is_final, | ||||||||||||||||||||||||||||||||||||||||||||||
| 703 | $finish_reason ? (finish_reason => $finish_reason) : (), | ||||||||||||||||||||||||||||||||||||||||||||||
| 704 | 24 | ▶ | $data->{usageMetadata} ? (usage => $data->{usageMetadata}) : (), | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 705 | defined $thinking ? ( thinking => $thinking ) : (), | ||||||||||||||||||||||||||||||||||||||||||||||
| 706 | @tool_calls ? ( tool_calls => \@tool_calls ) : (), | ||||||||||||||||||||||||||||||||||||||||||||||
| 707 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 708 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 709 | |||||||||||||||||||||||||||||||||||||||||||||||
| 710 | # Dynamic model listing with token pagination | ||||||||||||||||||||||||||||||||||||||||||||||
| 711 | 14 | ▶ | sub list_models_request { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
list_models_request: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 712 | 14 | my ($self, %params) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 713 | 14 | my $url = $self->gemini_url('models'); | |||||||||||||||||||||||||||||||||||||||||||||
| 714 | |||||||||||||||||||||||||||||||||||||||||||||||
| 715 | # Add pagination params if provided | ||||||||||||||||||||||||||||||||||||||||||||||
| 716 | 14 | ▶ | if (%params) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 717 | 2 | require URI; | |||||||||||||||||||||||||||||||||||||||||||||
| 718 | 2 | my $uri = URI->new($url); | |||||||||||||||||||||||||||||||||||||||||||||
| 719 | 2 | my %query = $uri->query_form; | |||||||||||||||||||||||||||||||||||||||||||||
| 720 | 2 | $uri->query_form(%query, %params); | |||||||||||||||||||||||||||||||||||||||||||||
| 721 | 2 | $url = $uri->as_string; | |||||||||||||||||||||||||||||||||||||||||||||
| 722 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 723 | |||||||||||||||||||||||||||||||||||||||||||||||
| 724 | return $self->generate_http_request( | ||||||||||||||||||||||||||||||||||||||||||||||
| 725 | GET => $url, | ||||||||||||||||||||||||||||||||||||||||||||||
| 726 | 3 | ▶ | sub { $self->list_models_response(shift) }, | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 727 | 14 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 728 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 729 | |||||||||||||||||||||||||||||||||||||||||||||||
| 730 | 4 | ▶ | sub list_models_response { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
list_models_response: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 731 | 4 | my ($self, $response) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 732 | 4 | my $data = $self->parse_response($response); | |||||||||||||||||||||||||||||||||||||||||||||
| 733 | 4 | return $data; | |||||||||||||||||||||||||||||||||||||||||||||
| 734 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 735 | |||||||||||||||||||||||||||||||||||||||||||||||
| 736 | 3 | ▶ | sub _fetch_all_models { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_fetch_all_models: called CC=4 SCAR=14.1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 737 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 738 | 3 | my @all_models; | |||||||||||||||||||||||||||||||||||||||||||||
| 739 | my $page_token; | ||||||||||||||||||||||||||||||||||||||||||||||
| 740 | |||||||||||||||||||||||||||||||||||||||||||||||
| 741 | 3 | ▶ | do { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 742 | 3 | ▶ | my $request = $self->list_models_request( | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 743 | $page_token ? (pageToken => $page_token) : () | ||||||||||||||||||||||||||||||||||||||||||||||
| 744 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 745 | 3 | my $response = $self->user_agent->request($request); | |||||||||||||||||||||||||||||||||||||||||||||
| 746 | 3 | my $data = $request->response_call->($response); | |||||||||||||||||||||||||||||||||||||||||||||
| 747 | |||||||||||||||||||||||||||||||||||||||||||||||
| 748 | 3 | ▶ | push @all_models, @{$data->{models} || []}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$$data{'models'} || []
MC/DC0%0/1
$$data{'models'} || []
$$data{'models'}
Truth table50%1/2
A
A= $$data{'models'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 749 | 3 | $page_token = $data->{nextPageToken}; | |||||||||||||||||||||||||||||||||||||||||||||
| 750 | } while ($page_token); | ||||||||||||||||||||||||||||||||||||||||||||||
| 751 | |||||||||||||||||||||||||||||||||||||||||||||||
| 752 | 3 | return \@all_models; | |||||||||||||||||||||||||||||||||||||||||||||
| 753 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 754 | |||||||||||||||||||||||||||||||||||||||||||||||
| 755 | 3 | ▶ | sub list_models { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
list_models: called CC=6 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 756 | 3 | my ($self, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 757 | |||||||||||||||||||||||||||||||||||||||||||||||
| 758 | # Check cache unless force_refresh requested | ||||||||||||||||||||||||||||||||||||||||||||||
| 759 | 3 | ▶ | unless ($opts{force_refresh}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 760 | 1 | my $cache = $self->_models_cache; | |||||||||||||||||||||||||||||||||||||||||||||
| 761 | 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 762 | 0 | ▶ | return $opts{full} ? $cache->{models} : $cache->{model_ids}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 763 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 764 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 765 | |||||||||||||||||||||||||||||||||||||||||||||||
| 766 | # Fetch all pages from API | ||||||||||||||||||||||||||||||||||||||||||||||
| 767 | 3 | my $models = $self->_fetch_all_models; | |||||||||||||||||||||||||||||||||||||||||||||
| 768 | |||||||||||||||||||||||||||||||||||||||||||||||
| 769 | # Extract IDs and update cache | ||||||||||||||||||||||||||||||||||||||||||||||
| 770 | # Gemini uses 'name' field like "models/gemini-2.0-flash" | ||||||||||||||||||||||||||||||||||||||||||||||
| 771 | my @model_ids = map { | ||||||||||||||||||||||||||||||||||||||||||||||
| 772 | 3 | my $name = $_->{name}; | |||||||||||||||||||||||||||||||||||||||||||||
| 773 | 3 | $name =~ s{^models/}{}; # Strip "models/" prefix | |||||||||||||||||||||||||||||||||||||||||||||
| 774 | 3 | $name; | |||||||||||||||||||||||||||||||||||||||||||||
| 775 | } @$models; | ||||||||||||||||||||||||||||||||||||||||||||||
| 776 | |||||||||||||||||||||||||||||||||||||||||||||||
| 777 | 3 | $self->_models_cache({ | |||||||||||||||||||||||||||||||||||||||||||||
| 778 | timestamp => time, | ||||||||||||||||||||||||||||||||||||||||||||||
| 779 | models => $models, | ||||||||||||||||||||||||||||||||||||||||||||||
| 780 | model_ids => \@model_ids, | ||||||||||||||||||||||||||||||||||||||||||||||
| 781 | }); | ||||||||||||||||||||||||||||||||||||||||||||||
| 782 | |||||||||||||||||||||||||||||||||||||||||||||||
| 783 | 3 | ▶ | return $opts{full} ? $models : \@model_ids; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 784 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 785 | |||||||||||||||||||||||||||||||||||||||||||||||
| 786 | |||||||||||||||||||||||||||||||||||||||||||||||
| 787 | # Tool calling support (MCP) is the tag-driven default in Langertha::Role::Tools. | ||||||||||||||||||||||||||||||||||||||||||||||
| 788 | 107 | ▶ | sub _build_tool_wire_format { 'gemini' } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_tool_wire_format: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 789 | |||||||||||||||||||||||||||||||||||||||||||||||
| 790 | 60 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||||||||||
| 791 | |||||||||||||||||||||||||||||||||||||||||||||||
| 792 | |||||||||||||||||||||||||||||||||||||||||||||||
| 793 | 60 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 794 | |||||||||||||||||||||||||||||||||||||||||||||||
| 795 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||
| 796 | |||||||||||||||||||||||||||||||||||||||||||||||
| 797 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||
| 798 | |||||||||||||||||||||||||||||||||||||||||||||||
| 799 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||||||||||||||
| 800 | |||||||||||||||||||||||||||||||||||||||||||||||
| 801 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||
| 802 | |||||||||||||||||||||||||||||||||||||||||||||||
| 803 | Langertha::Engine::Gemini - Google Gemini API | ||||||||||||||||||||||||||||||||||||||||||||||
| 804 | |||||||||||||||||||||||||||||||||||||||||||||||
| 805 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||||||
| 806 | |||||||||||||||||||||||||||||||||||||||||||||||
| 807 | version 0.503 | ||||||||||||||||||||||||||||||||||||||||||||||
| 808 | |||||||||||||||||||||||||||||||||||||||||||||||
| 809 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||
| 810 | |||||||||||||||||||||||||||||||||||||||||||||||
| 811 | use Langertha::Engine::Gemini; | ||||||||||||||||||||||||||||||||||||||||||||||
| 812 | |||||||||||||||||||||||||||||||||||||||||||||||
| 813 | my $gemini = Langertha::Engine::Gemini->new( | ||||||||||||||||||||||||||||||||||||||||||||||
| 814 | api_key => $ENV{GEMINI_API_KEY}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 815 | model => 'gemini-3-flash-preview', | ||||||||||||||||||||||||||||||||||||||||||||||
| 816 | response_size => 4096, | ||||||||||||||||||||||||||||||||||||||||||||||
| 817 | temperature => 0.7, | ||||||||||||||||||||||||||||||||||||||||||||||
| 818 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 819 | |||||||||||||||||||||||||||||||||||||||||||||||
| 820 | # Simple chat | ||||||||||||||||||||||||||||||||||||||||||||||
| 821 | my $response = $gemini->simple_chat('Explain quantum computing in simple terms'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 822 | print $response; | ||||||||||||||||||||||||||||||||||||||||||||||
| 823 | |||||||||||||||||||||||||||||||||||||||||||||||
| 824 | # Streaming | ||||||||||||||||||||||||||||||||||||||||||||||
| 825 | $gemini->simple_chat_stream(sub { | ||||||||||||||||||||||||||||||||||||||||||||||
| 826 | my ($chunk) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||
| 827 | print $chunk->content; | ||||||||||||||||||||||||||||||||||||||||||||||
| 828 | }, 'Write a poem about Perl'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 829 | |||||||||||||||||||||||||||||||||||||||||||||||
| 830 | # Async with Future::AsyncAwait | ||||||||||||||||||||||||||||||||||||||||||||||
| 831 | use Future::AsyncAwait; | ||||||||||||||||||||||||||||||||||||||||||||||
| 832 | |||||||||||||||||||||||||||||||||||||||||||||||
| 833 | async sub ask_gemini { | ||||||||||||||||||||||||||||||||||||||||||||||
| 834 | my $response = await $gemini->simple_chat_f( | ||||||||||||||||||||||||||||||||||||||||||||||
| 835 | 'What are the benefits of functional programming?' | ||||||||||||||||||||||||||||||||||||||||||||||
| 836 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 837 | say $response; | ||||||||||||||||||||||||||||||||||||||||||||||
| 838 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 839 | |||||||||||||||||||||||||||||||||||||||||||||||
| 840 | # Embeddings (gemini-embedding-001 unless embedding_model is set) | ||||||||||||||||||||||||||||||||||||||||||||||
| 841 | my $vector = $gemini->simple_embedding('Some text to embed'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 842 | my $vectors = $gemini->simple_embedding([ 'first', 'second' ]); | ||||||||||||||||||||||||||||||||||||||||||||||
| 843 | |||||||||||||||||||||||||||||||||||||||||||||||
| 844 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||
| 845 | |||||||||||||||||||||||||||||||||||||||||||||||
| 846 | Provides access to Google's Gemini models via the Generative Language API. | ||||||||||||||||||||||||||||||||||||||||||||||
| 847 | Gemini models support multimodal input (text, code, images) and long context | ||||||||||||||||||||||||||||||||||||||||||||||
| 848 | windows. | ||||||||||||||||||||||||||||||||||||||||||||||
| 849 | |||||||||||||||||||||||||||||||||||||||||||||||
| 850 | Available models include the current stable Flash line C<gemini-3.8-flash> | ||||||||||||||||||||||||||||||||||||||||||||||
| 851 | and C<gemini-3.7-flash> (C<thinkingLevel> low|medium|high, no C<minimal>), | ||||||||||||||||||||||||||||||||||||||||||||||
| 852 | the C<gemini-3-flash-preview> default (fast with thinking, and still accepts | ||||||||||||||||||||||||||||||||||||||||||||||
| 853 | C<thinkingLevel=minimal>), C<gemini-3.1-pro-preview> (most capable), | ||||||||||||||||||||||||||||||||||||||||||||||
| 854 | C<gemini-3.1-flash-lite> (cost-efficient workhorse), and the image-generation | ||||||||||||||||||||||||||||||||||||||||||||||
| 855 | models C<gemini-3.1-flash-image-preview> and C<gemini-3-pro-image-preview>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 856 | The C<gemini-2.5-*> generation is still served but now classed as | ||||||||||||||||||||||||||||||||||||||||||||||
| 857 | previous-generation. The default API endpoint is | ||||||||||||||||||||||||||||||||||||||||||||||
| 858 | C<https://generativelanguage.googleapis.com>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 859 | |||||||||||||||||||||||||||||||||||||||||||||||
| 860 | Embeddings (L<Langertha::Role::Embedding>) use C<gemini-embedding-001> by | ||||||||||||||||||||||||||||||||||||||||||||||
| 861 | default; C<gemini-embedding-2> embeds into a different, incompatible vector | ||||||||||||||||||||||||||||||||||||||||||||||
| 862 | space, so do not mix vectors of the two. See L</embedding_request> for | ||||||||||||||||||||||||||||||||||||||||||||||
| 863 | C<task_type> and C<output_dimensionality>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 864 | |||||||||||||||||||||||||||||||||||||||||||||||
| 865 | B<THIS API IS WORK IN PROGRESS> | ||||||||||||||||||||||||||||||||||||||||||||||
| 866 | |||||||||||||||||||||||||||||||||||||||||||||||
| 867 | =head2 api_key | ||||||||||||||||||||||||||||||||||||||||||||||
| 868 | |||||||||||||||||||||||||||||||||||||||||||||||
| 869 | The Google Generative Language API key. If not provided, reads from | ||||||||||||||||||||||||||||||||||||||||||||||
| 870 | C<LANGERTHA_GEMINI_API_KEY> environment variable. Get your key at | ||||||||||||||||||||||||||||||||||||||||||||||
| 871 | L<https://aistudio.google.com/app/apikey>. Required for the Developer API. | ||||||||||||||||||||||||||||||||||||||||||||||
| 872 | |||||||||||||||||||||||||||||||||||||||||||||||
| 873 | Pass C<< api_key =E<gt> undef >> explicitly to send no key at all (a keyless | ||||||||||||||||||||||||||||||||||||||||||||||
| 874 | proxy or gateway in front of Gemini): the environment variable is then not | ||||||||||||||||||||||||||||||||||||||||||||||
| 875 | read, no C<key> query parameter is added to any URL, and nothing warns. Leaving | ||||||||||||||||||||||||||||||||||||||||||||||
| 876 | C<api_key> out keeps the default: environment variable, croak when unset. | ||||||||||||||||||||||||||||||||||||||||||||||
| 877 | |||||||||||||||||||||||||||||||||||||||||||||||
| 878 | =head2 cached_content | ||||||||||||||||||||||||||||||||||||||||||||||
| 879 | |||||||||||||||||||||||||||||||||||||||||||||||
| 880 | Optional L<Langertha::CachedContent> resource bound to this engine. When | ||||||||||||||||||||||||||||||||||||||||||||||
| 881 | set, every chat request (C<chat>, C<chat_stream>, C<simple_chat_f>, …) | ||||||||||||||||||||||||||||||||||||||||||||||
| 882 | injects C<cachedContent =E<gt> '{name}'> into the generateContent body | ||||||||||||||||||||||||||||||||||||||||||||||
| 883 | so the model serves the request against the cached context. | ||||||||||||||||||||||||||||||||||||||||||||||
| 884 | |||||||||||||||||||||||||||||||||||||||||||||||
| 885 | A request that names a cache takes its system instruction, tools and tool | ||||||||||||||||||||||||||||||||||||||||||||||
| 886 | configuration from the cache: Gemini rejects a C<generateContent> request that | ||||||||||||||||||||||||||||||||||||||||||||||
| 887 | sets C<systemInstruction>, C<tools> or C<toolConfig> next to C<cachedContent> | ||||||||||||||||||||||||||||||||||||||||||||||
| 888 | (HTTP 400). While a cache is bound those three are therefore not sent, even | ||||||||||||||||||||||||||||||||||||||||||||||
| 889 | when the engine's C<system_prompt>, a system message, C<tools> or | ||||||||||||||||||||||||||||||||||||||||||||||
| 890 | C<tool_choice> would set them, and the engine carps once. Put them into the | ||||||||||||||||||||||||||||||||||||||||||||||
| 891 | cache when creating it (L<Langertha::CachedContent/system_instruction>, | ||||||||||||||||||||||||||||||||||||||||||||||
| 892 | L<Langertha::CachedContent/tools>). | ||||||||||||||||||||||||||||||||||||||||||||||
| 893 | |||||||||||||||||||||||||||||||||||||||||||||||
| 894 | Lifecycle (create / get / list / update / delete) is on the role — | ||||||||||||||||||||||||||||||||||||||||||||||
| 895 | L<Langertha::Role::CachedContent/create_cached_content_f> and friends. | ||||||||||||||||||||||||||||||||||||||||||||||
| 896 | Bind a freshly created resource with C<< $engine->cached_content($cc) >>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 897 | |||||||||||||||||||||||||||||||||||||||||||||||
| 898 | Source URL: L<https://ai.google.dev/api/generate-content> (the | ||||||||||||||||||||||||||||||||||||||||||||||
| 899 | C<cachedContent> field on a generateContent body). | ||||||||||||||||||||||||||||||||||||||||||||||
| 900 | |||||||||||||||||||||||||||||||||||||||||||||||
| 901 | =head2 gemini_api_version | ||||||||||||||||||||||||||||||||||||||||||||||
| 902 | |||||||||||||||||||||||||||||||||||||||||||||||
| 903 | The API version segment of every endpoint, C<v1beta> for the Generative | ||||||||||||||||||||||||||||||||||||||||||||||
| 904 | Language API. Override in a subclass serving a different version. Tool | ||||||||||||||||||||||||||||||||||||||||||||||
| 905 | declarations are sent as C<parametersJsonSchema>, which C<v1> does not have. | ||||||||||||||||||||||||||||||||||||||||||||||
| 906 | |||||||||||||||||||||||||||||||||||||||||||||||
| 907 | =head2 gemini_auth_query | ||||||||||||||||||||||||||||||||||||||||||||||
| 908 | |||||||||||||||||||||||||||||||||||||||||||||||
| 909 | The auth seam: returns the credential as a C<< ( name =E<gt> value ) >> query | ||||||||||||||||||||||||||||||||||||||||||||||
| 910 | pair list, C<< ( key =E<gt> $self->api_key ) >> for the Developer API, or the | ||||||||||||||||||||||||||||||||||||||||||||||
| 911 | empty list when C<api_key> is C<undef> or empty. A | ||||||||||||||||||||||||||||||||||||||||||||||
| 912 | consumer that authenticates by header instead returns the empty list here and | ||||||||||||||||||||||||||||||||||||||||||||||
| 913 | sets the header in C<update_request>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 914 | |||||||||||||||||||||||||||||||||||||||||||||||
| 915 | =head2 gemini_endpoint | ||||||||||||||||||||||||||||||||||||||||||||||
| 916 | |||||||||||||||||||||||||||||||||||||||||||||||
| 917 | Composes the credential-free endpoint C<< {url}/{gemini_api_version}/{path} >>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 918 | This is the path half of the seam: a consumer whose resources live under an | ||||||||||||||||||||||||||||||||||||||||||||||
| 919 | extra prefix (Vertex AI's C<< projects/{p}/locations/{l}/ >>) overrides this | ||||||||||||||||||||||||||||||||||||||||||||||
| 920 | one method and every request URL follows. Callers that are about to issue a | ||||||||||||||||||||||||||||||||||||||||||||||
| 921 | request want L</gemini_url> instead — this one carries no credential. | ||||||||||||||||||||||||||||||||||||||||||||||
| 922 | |||||||||||||||||||||||||||||||||||||||||||||||
| 923 | =head2 gemini_url | ||||||||||||||||||||||||||||||||||||||||||||||
| 924 | |||||||||||||||||||||||||||||||||||||||||||||||
| 925 | Builds L</gemini_endpoint> and appends the query string: | ||||||||||||||||||||||||||||||||||||||||||||||
| 926 | first the pairs from L</gemini_auth_query>, then any C<< ( name =E<gt> value ) >> | ||||||||||||||||||||||||||||||||||||||||||||||
| 927 | pairs passed by the caller (e.g. C<< alt =E<gt> 'sse' >>). | ||||||||||||||||||||||||||||||||||||||||||||||
| 928 | |||||||||||||||||||||||||||||||||||||||||||||||
| 929 | =head2 gemini_model_url | ||||||||||||||||||||||||||||||||||||||||||||||
| 930 | |||||||||||||||||||||||||||||||||||||||||||||||
| 931 | Builds the endpoint of one model method, C<< models/{model}:{method} >>, via | ||||||||||||||||||||||||||||||||||||||||||||||
| 932 | L</gemini_url>. The C<models/> prefix lives here so a consumer with a | ||||||||||||||||||||||||||||||||||||||||||||||
| 933 | different resource path (Vertex AI's C<publishers/google/models/>) overrides | ||||||||||||||||||||||||||||||||||||||||||||||
| 934 | one method. | ||||||||||||||||||||||||||||||||||||||||||||||
| 935 | |||||||||||||||||||||||||||||||||||||||||||||||
| 936 | The chat routes pass C<chat_model> as C<{model}>, or a per-request C<model> | ||||||||||||||||||||||||||||||||||||||||||||||
| 937 | (from L<Langertha::Role::Chat/chat_f> or L<Langertha::Chat/model>) for that | ||||||||||||||||||||||||||||||||||||||||||||||
| 938 | request; the override is not sent in the body. | ||||||||||||||||||||||||||||||||||||||||||||||
| 939 | |||||||||||||||||||||||||||||||||||||||||||||||
| 940 | =head2 embedding_request | ||||||||||||||||||||||||||||||||||||||||||||||
| 941 | |||||||||||||||||||||||||||||||||||||||||||||||
| 942 | my $request = $engine->embedding_request($text, %extra); | ||||||||||||||||||||||||||||||||||||||||||||||
| 943 | my $request = $engine->embedding_request(\@texts, | ||||||||||||||||||||||||||||||||||||||||||||||
| 944 | task_type => 'RETRIEVAL_DOCUMENT', output_dimensionality => 768); | ||||||||||||||||||||||||||||||||||||||||||||||
| 945 | |||||||||||||||||||||||||||||||||||||||||||||||
| 946 | Builds an embedding request with C<embedding_model> (default | ||||||||||||||||||||||||||||||||||||||||||||||
| 947 | C<gemini-embedding-001>). A string goes to C<models/{model}:embedContent>, an | ||||||||||||||||||||||||||||||||||||||||||||||
| 948 | ArrayRef of strings to C<models/{model}:batchEmbedContents> as one request per | ||||||||||||||||||||||||||||||||||||||||||||||
| 949 | input. The optional C<task_type>, C<title> and C<output_dimensionality> are | ||||||||||||||||||||||||||||||||||||||||||||||
| 950 | placed in C<embedContentConfig> (as C<taskType>, C<title>, | ||||||||||||||||||||||||||||||||||||||||||||||
| 951 | C<outputDimensionality>), merged with an C<embedContentConfig> you pass | ||||||||||||||||||||||||||||||||||||||||||||||
| 952 | yourself; any other key goes into each request unchanged. In a batch every | ||||||||||||||||||||||||||||||||||||||||||||||
| 953 | input gets the same settings. L<Langertha::Role::Embedding/embedding_dimensions>, | ||||||||||||||||||||||||||||||||||||||||||||||
| 954 | when set, is sent as C<outputDimensionality> unless the call passes one. | ||||||||||||||||||||||||||||||||||||||||||||||
| 955 | |||||||||||||||||||||||||||||||||||||||||||||||
| 956 | =head2 embedding_response | ||||||||||||||||||||||||||||||||||||||||||||||
| 957 | |||||||||||||||||||||||||||||||||||||||||||||||
| 958 | my $vector = $engine->embedding_response($http_response); | ||||||||||||||||||||||||||||||||||||||||||||||
| 959 | my $vectors = $engine->embedding_response($http_response, \@texts); | ||||||||||||||||||||||||||||||||||||||||||||||
| 960 | |||||||||||||||||||||||||||||||||||||||||||||||
| 961 | Parses an embedding answer; the parser built by L</embedding_request> passes | ||||||||||||||||||||||||||||||||||||||||||||||
| 962 | the input itself. For a string it returns C<embedding.values>, for an ArrayRef | ||||||||||||||||||||||||||||||||||||||||||||||
| 963 | input C<embeddings[].values>, one vector per input in input order. A count | ||||||||||||||||||||||||||||||||||||||||||||||
| 964 | that does not match the number of inputs croaks, and so does a body without a | ||||||||||||||||||||||||||||||||||||||||||||||
| 965 | vector, naming the engine and any C<error> it carries; it never returns | ||||||||||||||||||||||||||||||||||||||||||||||
| 966 | C<undef>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 967 | |||||||||||||||||||||||||||||||||||||||||||||||
| 968 | =head2 chat_response | ||||||||||||||||||||||||||||||||||||||||||||||
| 969 | |||||||||||||||||||||||||||||||||||||||||||||||
| 970 | my $response = $engine->chat_response($http_response); | ||||||||||||||||||||||||||||||||||||||||||||||
| 971 | |||||||||||||||||||||||||||||||||||||||||||||||
| 972 | Parses a C<generateContent> answer into a L<Langertha::Response> from the | ||||||||||||||||||||||||||||||||||||||||||||||
| 973 | first candidate; C<finish_reason> is its C<finishReason> as Gemini spells it. | ||||||||||||||||||||||||||||||||||||||||||||||
| 974 | A blocked prompt (no candidate, C<promptFeedback.blockReason>) is an answer | ||||||||||||||||||||||||||||||||||||||||||||||
| 975 | with C<content> C<''> and the C<blockReason> as C<finish_reason> (e.g. | ||||||||||||||||||||||||||||||||||||||||||||||
| 976 | C<SAFETY>). A body with neither croaks, naming the engine and any C<error> it | ||||||||||||||||||||||||||||||||||||||||||||||
| 977 | carries. On a stream, the blocked prompt's chunk is the final chunk, with | ||||||||||||||||||||||||||||||||||||||||||||||
| 978 | C<content> C<''> and the C<blockReason> as C<finish_reason>. A stream chunk | ||||||||||||||||||||||||||||||||||||||||||||||
| 979 | with a top-level C<error> object croaks | ||||||||||||||||||||||||||||||||||||||||||||||
| 980 | C<"E<lt>engineE<gt> stream carried an error: E<lt>messageE<gt> (E<lt>codeE<gt>)">, | ||||||||||||||||||||||||||||||||||||||||||||||
| 981 | which fails the stream. | ||||||||||||||||||||||||||||||||||||||||||||||
| 982 | |||||||||||||||||||||||||||||||||||||||||||||||
| 983 | =head2 list_models | ||||||||||||||||||||||||||||||||||||||||||||||
| 984 | |||||||||||||||||||||||||||||||||||||||||||||||
| 985 | my $model_ids = $engine->list_models; | ||||||||||||||||||||||||||||||||||||||||||||||
| 986 | my $models = $engine->list_models(full => 1); | ||||||||||||||||||||||||||||||||||||||||||||||
| 987 | my $models = $engine->list_models(force_refresh => 1); | ||||||||||||||||||||||||||||||||||||||||||||||
| 988 | |||||||||||||||||||||||||||||||||||||||||||||||
| 989 | Fetches available models from the Gemini API using token pagination. Returns | ||||||||||||||||||||||||||||||||||||||||||||||
| 990 | an ArrayRef of model ID strings (with the C<models/> prefix stripped) by | ||||||||||||||||||||||||||||||||||||||||||||||
| 991 | default, or full model objects when C<full => 1> is passed. Results are cached | ||||||||||||||||||||||||||||||||||||||||||||||
| 992 | for C<models_cache_ttl> seconds (default: 3600). | ||||||||||||||||||||||||||||||||||||||||||||||
| 993 | |||||||||||||||||||||||||||||||||||||||||||||||
| 994 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||||||
| 995 | |||||||||||||||||||||||||||||||||||||||||||||||
| 996 | =over | ||||||||||||||||||||||||||||||||||||||||||||||
| 997 | |||||||||||||||||||||||||||||||||||||||||||||||
| 998 | =item * L<https://aistudio.google.com/status> - Google AI Studio service status | ||||||||||||||||||||||||||||||||||||||||||||||
| 999 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1000 | =item * L<https://ai.google.dev/gemini-api/docs> - Official Gemini API documentation | ||||||||||||||||||||||||||||||||||||||||||||||
| 1001 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1002 | =item * L<https://aistudio.google.com/> - Google AI Studio for testing | ||||||||||||||||||||||||||||||||||||||||||||||
| 1003 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1004 | =item * L<Langertha::Role::Chat> - Chat interface methods | ||||||||||||||||||||||||||||||||||||||||||||||
| 1005 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1006 | =item * L<Langertha::Role::Tools> - MCP tool calling interface | ||||||||||||||||||||||||||||||||||||||||||||||
| 1007 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1008 | =item * L<Langertha::Role::Streaming> - Streaming support (SSE format) | ||||||||||||||||||||||||||||||||||||||||||||||
| 1009 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1010 | =item * L<Langertha::Role::Embedding> - Embedding interface (C<simple_embedding>, C<simple_embedding_f>) | ||||||||||||||||||||||||||||||||||||||||||||||
| 1011 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1012 | =item * L<https://ai.google.dev/api/embeddings> - embedContent / batchEmbedContents reference | ||||||||||||||||||||||||||||||||||||||||||||||
| 1013 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1014 | =item * L<Langertha::Engine::Anthropic> - Another non-OpenAI-compatible engine | ||||||||||||||||||||||||||||||||||||||||||||||
| 1015 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1016 | =back | ||||||||||||||||||||||||||||||||||||||||||||||
| 1017 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1018 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||||||
| 1019 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1020 | =head2 Issues | ||||||||||||||||||||||||||||||||||||||||||||||
| 1021 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1022 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||||||||||||||||||||
| 1023 | L<https://github.com/Getty/langertha/issues>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 1024 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1025 | =head2 IRC | ||||||||||||||||||||||||||||||||||||||||||||||
| 1026 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1027 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||||||||||||||||||||
| 1028 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1029 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||||||||||||||||||||
| 1030 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1031 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||||||||||||||||||||
| 1032 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1033 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||
| 1034 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1035 | Torsten Raudssus <getty@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||||||
| 1036 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1037 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||||||
| 1038 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1039 | This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 1040 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1041 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||||||
| 1042 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||||||||||||||
| 1043 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1044 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||