https://github.com/antononcube/raku-www-gemini
Raku package for using Google's Gemini Web API (https://gemini.google.com).
https://github.com/antononcube/raku-www-gemini
google-gemini google-gemini-ai google-gemini-api large-language-models llm raku rakulang
Last synced: 7 months ago
JSON representation
Raku package for using Google's Gemini Web API (https://gemini.google.com).
- Host: GitHub
- URL: https://github.com/antononcube/raku-www-gemini
- Owner: antononcube
- License: artistic-2.0
- Created: 2024-03-07T14:05:43.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2025-06-14T18:47:51.000Z (12 months ago)
- Last Synced: 2025-06-14T20:01:39.131Z (12 months ago)
- Topics: google-gemini, google-gemini-ai, google-gemini-api, large-language-models, llm, raku, rakulang
- Language: Raku
- Homepage: https://raku.land/zef:antononcube/WWW::Gemini
- Size: 227 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README-work.md
- Changelog: Changes
- License: LICENSE
Awesome Lists containing this project
README
# WWW::Gemini
Raku package for connecting with [Google's Gemini](https://gemini.google.com/app).
It is based on the Web API described in [Gemini's API documentation](https://ai.google.dev/docs/gemini_api_overview).
The design and implementation of the package closely follows those of
["WWW::PaLM"](https://raku.land/zef:antononcube/WWW::PaLM), [AAp1], and
["WWW::OpenAI"](https://raku.land/zef:antononcube/WWW::OpenAI), [AAp2].
## Installation
From [Zef ecosystem](https://raku.land):
```
zef install WWW::Gemini
```
From GitHub:
```
zef install https://github.com/antononcube/Raku-WWW-Gemini
```
-----
## Usage examples
Show models:
```perl6
use WWW::Gemini;
gemini-models()
```
Show text generation:
```perl6
.say for gemini-generate-content('what is the population in Brazil?', format => 'values');
```
Using a synonym function:
```perl6
.say for gemini-generation('Who wrote the book "Dune"?');
```
### Embeddings
Show text embeddings:
```perl6
use Data::TypeSystem;
my @vecs = gemini-embed-content(["say something nice!",
"shout something bad!",
"where is the best coffee made?"],
format => 'values');
say "Shape: ", deduce-type(@vecs);
.say for @vecs;
```
### Counting tokens
Here we show how to find the number of tokens in a text:
```perl6
my $text = q:to/END/;
AI has made surprising successes but cannot solve all scientific problems due to computational irreducibility.
END
gemini-count-tokens($text, format => 'values');
```
### Vision
If the function `gemini-completion` is given a list of images, textual results corresponding to those images is returned.
The argument "images" is a list of image URLs, image file names, or image Base64 representations. (Any combination of those element types.)
Here is an example with [this image](https://raw.githubusercontent.com/antononcube/Raku-WWW-Gemini/main/resources/ThreeHunters.jpg):
```perl6
my $fname = $*CWD ~ '/resources/ThreeHunters.jpg';
my @images = [$fname,];
say gemini-generation("Give concise descriptions of the images.", :@images, format => 'values');
```
When a file name is given to the argument "images" of `gemini-completion` then
the function `encode-image` of
["Image::Markup::Utilities"](https://raku.land/zef:antononcube/Image::Markup::Utilities), [AAp4],
is applied to it.
-------
## Command Line Interface
### Maker suite access
The package provides a Command Line Interface (CLI) script:
```shell
gemini-prompt --help
```
**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`:
```mermaid
graph TD
UI[/Some natural language text/]
TO[/"Gemini
Processed output"/]
WR[[Web request]]
Gemini{{Gemini}}
PJ[Parse JSON]
Q{Return
hash?}
MSTC[Compose query]
MURL[[Make URL]]
TTC[Process]
QAK{Auth key
supplied?}
EAK[["Try to find
GEMINI_API_KEY
or
PALM_API_KEY
in %*ENV"]]
QEAF{Auth key
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
- [X] DONE Function calling support
- [ ] TODO Image generation
- [ ] TODO Image editing
- [ ] TODO Audio generation
- [ ] TODO Live AI support
- [ ] TODO Documentation
- [ ] DONE Core functionalities
- [X] 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"](https://rakuforprediction.wordpress.com/2023/08/01/workflows-with-llm-functions/),
(2023),
[RakuForPredictions at WordPress](https://rakuforprediction.wordpress.com).
[AA2] Anton Antonov,
["Number guessing games: Gemini vs ChatGPT"](https://rakuforprediction.wordpress.com/2023/08/06/number-guessing-games-gemini-vs-chatgpt/)
(2023),
[RakuForPredictions at WordPress](https://rakuforprediction.wordpress.com).
[ZG1] Zoubin Ghahramani,
["Introducing Gemini 2"](https://blog.google/technology/ai/google-gemini-2-ai-large-language-model/),
(2023),
[Google Official Blog on AI](https://blog.google/technology/ai/).
### Packages, platforms
[AAp1] Anton Antonov,
[WWW::PaLM Raku package](https://github.com/antononcube/Raku-WWW-PaLM),
(2023-2024),
[GitHub/antononcube](https://github.com/antononcube).
[AAp2] Anton Antonov,
[WWW::OpenAI Raku package](https://github.com/antononcube/Raku-WWW-OpenAI),
(2023-2024),
[GitHub/antononcube](https://github.com/antononcube).
[AAp3] Anton Antonov,
[LLM::Functions Raku package](https://github.com/antononcube/Raku-LLM-Functions),
(2023-2024),
[GitHub/antononcube](https://github.com/antononcube).
[AAp4] Anton Antonov,
[Image::Markup::Utilities Raku package](https://github.com/antononcube/Raku-Image-Markup-Utilities),
(2023-2024),
[GitHub/antononcube](https://github.com/antononcube).
[AAp5] Anton Antonov,
[ML::FindTextualAnswer Raku package](https://github.com/antononcube/Raku-ML-FindTextualAnswer),
(2023-2024),
[GitHub/antononcube](https://github.com/antononcube).