WWW::Gemini

Raku package for connecting with Google's Gemini.
It is based on the Web API described in Gemini API documentation.
The design and implementation of the package closely follows those of
"WWW::PaLM", [AAp1], and
"WWW::OpenAI", [AAp2].
Installation
From Zef ecosystem:
zef install WWW::Gemini
From GitHub:
zef install https://github.com/antononcube/Raku-WWW-Gemini
Usage examples
Set GEMINI_API_KEY before calling the API. The text, image, and embedding defaults are
gemini-3.8-flash, gemini-3.1-flash-image, and gemini-embedding-2.
Override them with the GEMINI_DEFAULT_MODEL, GEMINI_DEFAULT_IMAGE_GENERATION_MODEL,
and GEMINI_DEFAULT_EMBEDDING_MODEL environment variables or pass :model per request.
use WWW::Gemini;
say gemini-generation('Who wrote Dune?', :format<values>);
say gemini-generation('Summarize this text', :model<gemini-3.5-flash-lite>, :format<values>);
say gemini-generation('Explain this proof', :thinking-level<low>, :format<values>);
say gemini-count-tokens('How many tokens?', :format<values>);
gemini-models() queries Google's live model catalog. gemini-known-models() in
WWW::Gemini::Models is a convenience snapshot; a valid bare model ID can be passed
even if it is newer than the package.
Embeddings
my @vectors = gemini-embed-content(['first document', 'second document'],
:output-dimensionality(768), :format<values>);
The default gemini-embedding-2 accepts text, images, video, audio, and PDFs as
content parts. Supply a Gemini content map for one combined multimodal embedding:
my $embedding = gemini-embed-content(
%(parts => [
{text => 'Describe this picture'},
{inlineData => {mimeType => 'image/png', data => $base64-png}}
]), :format<values>);
A list of inputs makes separate requests. Embedding 2 does not send :task-type
to the API. For text input, the package converts the five supported task types to
Google's text prefixes for compatibility. You can also prefix the text yourself,
as shown in examples/Basic-invocations.raku. :task-type remains an API option for
gemini-embedding-001. When moving an existing vector index to Embedding 2,
re-embed every item: its vectors cannot be compared with Embedding 001 vectors.
Vision and image generation
say gemini-generation('Describe these pictures',
:images(['resources/ThreeHunters.jpg', 'data:image/png;base64,...']),
:format<values>);
my $result = gemini-generate-image('Draw a red fox in a forest',
:response-format(%(image => %(aspectRatio => '16:9', imageSize => '2K'))),
:format<values>);
# $result contains text and/or inlineData parts (base64 image data).
Image input accepts local files or data:image/...;base64,... URIs. A local file
is encoded as JPEG. Use a data URI to preserve another MIME type. Image generation
uses generateContent and supports :response-modalities, :response-format, and
:thinking-level. :format<values> returns plain text for text-only replies and
keeps structured parts for image and tool replies. Use :format<hash> for the full
response, including thought signatures; pass signed model parts back unchanged in
multi-turn function-calling and image-editing conversations.
Gemini 3 models support one candidate. Gemini 3.8 requests omit sampling options;
passing :temperature, :top-p, or :top-k with that model raises an error.
Command Line Interface
Maker suite access
The package provides a Command Line Interface (CLI) script:
gemini-prompt --help
The CLI defaults to Gemini 3.8 Flash. For example:
gemini-prompt -m=gemini-3.5-flash-lite Explain recursion
Remark: When the authorization key argument "auth-key" is specified set to "Whatever"
then gemini-prompt attempts to use one of the env variables GEMINI_API_KEY or PALM_API_KEY.
Mermaid diagram
The following flowchart corresponds to the steps in the package function gemini-prompt:
graph TD
UI[/Some natural language text/]
TO[/"Gemini<br/>Processed output"/]
WR[[Web request]]
Gemini{{Gemini}}
PJ[Parse JSON]
Q{Return<br>hash?}
MSTC[Compose query]
MURL[[Make URL]]
TTC[Process]
QAK{Auth key<br>supplied?}
EAK[["Try to find<br>GEMINI_API_KEY<br>or<br>PALM_API_KEY<br>in %*ENV"]]
QEAF{Auth key<br>found?}
NAK[/Cannot find auth key/]
UI --> QAK
QAK --> |yes|MSTC
QAK --> |no|EAK
EAK --> QEAF
MSTC --> TTC
QEAF --> |no|NAK
QEAF --> |yes|TTC
TTC -.-> MURL -.-> WR -.-> TTC
WR -.-> |URL|Gemini
Gemini -.-> |JSON|WR
TTC --> Q
Q --> |yes|PJ
Q --> |no|TO
PJ --> TO
TODO
- TODO Implementation
- DONE Function calling support
- TODO Image generation
- TODO Image editing
- TODO Audio generation
- TODO Live AI support
- TODO Documentation
- DONE Core functionalities
- DONE Function calling / tool workflow notebook
- TODO Image generation
- TODO Comparison of OpenAI vs Google image generation
- TODO Thinking model(s) demo
References
Articles
[AA1] Anton Antonov,
"Workflows with LLM functions",
(2023),
RakuForPredictions at WordPress.
[AA2] Anton Antonov,
"Number guessing games: Gemini vs ChatGPT"
(2023),
RakuForPredictions at WordPress.
[ZG1] Zoubin Ghahramani,
"Introducing Gemini 2",
(2023),
Google Official Blog on AI.
[AAp1] Anton Antonov,
WWW::PaLM Raku package,
(2023-2024),
GitHub/antononcube.
[AAp2] Anton Antonov,
WWW::OpenAI Raku package,
(2023-2024),
GitHub/antononcube.
[AAp3] Anton Antonov,
LLM::Functions Raku package,
(2023-2024),
GitHub/antononcube.
[AAp4] Anton Antonov,
Image::Markup::Utilities Raku package,
(2023-2024),
GitHub/antononcube.
[AAp5] Anton Antonov,
ML::FindTextualAnswer Raku package,
(2023-2024),
GitHub/antononcube.