gemini-ai 4.0.0 → 4.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +1 -0
- data/.rubocop.yml +3 -0
- data/Gemfile +3 -1
- data/Gemfile.lock +36 -16
- data/README.md +303 -9
- data/components/errors.rb +2 -1
- data/controllers/client.rb +37 -3
- data/gemini-ai.gemspec +1 -1
- data/static/gem.rb +1 -1
- data/template.md +297 -9
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7fa034b81983b8925267cdc2bb4cbf6170b7d5ecccfb26a705d70a1643f1e7ad
|
4
|
+
data.tar.gz: 077eec4e066836c80d8e0d6e26940086588876548f8d0e38b01a0a89aaf74646
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e2be4160caa66645c8baa9519f69059caa36e4c699098e2138e713736d1f7a1aaba27d6ca9a62ba64ee350e313cea5c0bee27eaf9685f576a33527c0f837d563
|
7
|
+
data.tar.gz: f4561c6e6cef7cfc3daa6e2c8c534d03624993cfbce3f9a75ae4eff55b081de694aa30d261c891eaf0acf463b3fd883b48317d142856131fc8253957d0ef9ef5
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--require spec_helper
|
data/.rubocop.yml
CHANGED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
gemini-ai (4.
|
4
|
+
gemini-ai (4.2.0)
|
5
5
|
event_stream_parser (~> 1.0)
|
6
|
-
faraday (~> 2.
|
6
|
+
faraday (~> 2.10)
|
7
7
|
faraday-typhoeus (~> 1.1)
|
8
8
|
googleauth (~> 1.8)
|
9
9
|
typhoeus (~> 1.4, >= 1.4.1)
|
@@ -11,24 +11,26 @@ PATH
|
|
11
11
|
GEM
|
12
12
|
remote: https://rubygems.org/
|
13
13
|
specs:
|
14
|
-
addressable (2.8.
|
15
|
-
public_suffix (>= 2.0.2, <
|
14
|
+
addressable (2.8.7)
|
15
|
+
public_suffix (>= 2.0.2, < 7.0)
|
16
16
|
ast (2.4.2)
|
17
17
|
base64 (0.2.0)
|
18
18
|
byebug (11.1.3)
|
19
19
|
coderay (1.1.3)
|
20
|
+
diff-lcs (1.5.1)
|
20
21
|
dotenv (3.1.2)
|
21
22
|
ethon (0.16.0)
|
22
23
|
ffi (>= 1.15.0)
|
23
24
|
event_stream_parser (1.0.0)
|
24
|
-
faraday (2.
|
25
|
+
faraday (2.10.0)
|
25
26
|
faraday-net_http (>= 2.0, < 3.2)
|
27
|
+
logger
|
26
28
|
faraday-net_http (3.1.0)
|
27
29
|
net-http
|
28
30
|
faraday-typhoeus (1.1.0)
|
29
31
|
faraday (~> 2.0)
|
30
32
|
typhoeus (~> 1.4)
|
31
|
-
ffi (1.
|
33
|
+
ffi (1.17.0)
|
32
34
|
google-cloud-env (2.1.1)
|
33
35
|
faraday (>= 1.0, < 3.a)
|
34
36
|
googleauth (1.11.0)
|
@@ -39,16 +41,17 @@ GEM
|
|
39
41
|
os (>= 0.9, < 2.0)
|
40
42
|
signet (>= 0.16, < 2.a)
|
41
43
|
json (2.7.2)
|
42
|
-
jwt (2.8.
|
44
|
+
jwt (2.8.2)
|
43
45
|
base64
|
44
46
|
language_server-protocol (3.17.0.3)
|
47
|
+
logger (1.6.0)
|
45
48
|
method_source (1.1.0)
|
46
49
|
multi_json (1.15.0)
|
47
50
|
net-http (0.4.1)
|
48
51
|
uri
|
49
52
|
os (1.1.4)
|
50
|
-
parallel (1.
|
51
|
-
parser (3.3.
|
53
|
+
parallel (1.25.1)
|
54
|
+
parser (3.3.4.0)
|
52
55
|
ast (~> 2.4.1)
|
53
56
|
racc
|
54
57
|
pry (0.14.2)
|
@@ -57,25 +60,40 @@ GEM
|
|
57
60
|
pry-byebug (3.10.1)
|
58
61
|
byebug (~> 11.0)
|
59
62
|
pry (>= 0.13, < 0.15)
|
60
|
-
public_suffix (
|
61
|
-
racc (1.
|
63
|
+
public_suffix (6.0.0)
|
64
|
+
racc (1.8.0)
|
62
65
|
rainbow (3.1.1)
|
63
66
|
regexp_parser (2.9.2)
|
64
|
-
rexml (3.2
|
65
|
-
strscan
|
66
|
-
|
67
|
+
rexml (3.3.2)
|
68
|
+
strscan
|
69
|
+
rspec (3.13.0)
|
70
|
+
rspec-core (~> 3.13.0)
|
71
|
+
rspec-expectations (~> 3.13.0)
|
72
|
+
rspec-mocks (~> 3.13.0)
|
73
|
+
rspec-core (3.13.0)
|
74
|
+
rspec-support (~> 3.13.0)
|
75
|
+
rspec-expectations (3.13.1)
|
76
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
77
|
+
rspec-support (~> 3.13.0)
|
78
|
+
rspec-mocks (3.13.1)
|
79
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
80
|
+
rspec-support (~> 3.13.0)
|
81
|
+
rspec-support (3.13.1)
|
82
|
+
rubocop (1.65.0)
|
67
83
|
json (~> 2.3)
|
68
84
|
language_server-protocol (>= 3.17.0)
|
69
85
|
parallel (~> 1.10)
|
70
86
|
parser (>= 3.3.0.2)
|
71
87
|
rainbow (>= 2.2.2, < 4.0)
|
72
|
-
regexp_parser (>=
|
88
|
+
regexp_parser (>= 2.4, < 3.0)
|
73
89
|
rexml (>= 3.2.5, < 4.0)
|
74
90
|
rubocop-ast (>= 1.31.1, < 2.0)
|
75
91
|
ruby-progressbar (~> 1.7)
|
76
92
|
unicode-display_width (>= 2.4.0, < 3.0)
|
77
93
|
rubocop-ast (1.31.3)
|
78
94
|
parser (>= 3.3.1.0)
|
95
|
+
rubocop-rspec (3.0.3)
|
96
|
+
rubocop (~> 1.61)
|
79
97
|
ruby-progressbar (1.13.0)
|
80
98
|
signet (0.19.0)
|
81
99
|
addressable (~> 2.8)
|
@@ -95,7 +113,9 @@ DEPENDENCIES
|
|
95
113
|
dotenv (~> 3.1, >= 3.1.2)
|
96
114
|
gemini-ai!
|
97
115
|
pry-byebug (~> 3.10, >= 3.10.1)
|
98
|
-
|
116
|
+
rspec (~> 3.13)
|
117
|
+
rubocop (~> 1.65)
|
118
|
+
rubocop-rspec (~> 3.0, >= 3.0.3)
|
99
119
|
|
100
120
|
BUNDLED WITH
|
101
121
|
2.4.22
|
data/README.md
CHANGED
@@ -9,7 +9,7 @@ A Ruby Gem for interacting with [Gemini](https://deepmind.google/technologies/ge
|
|
9
9
|
## TL;DR and Quick Start
|
10
10
|
|
11
11
|
```ruby
|
12
|
-
gem 'gemini-ai', '~> 4.
|
12
|
+
gem 'gemini-ai', '~> 4.2.0'
|
13
13
|
```
|
14
14
|
|
15
15
|
```ruby
|
@@ -34,6 +34,17 @@ client = Gemini.new(
|
|
34
34
|
options: { model: 'gemini-pro', server_sent_events: true }
|
35
35
|
)
|
36
36
|
|
37
|
+
# With the Service Account Credentials File contents
|
38
|
+
client = Gemini.new(
|
39
|
+
credentials: {
|
40
|
+
service: 'vertex-ai-api',
|
41
|
+
file_contents: File.read('google-credentials.json'),
|
42
|
+
# file_contents: ENV['GOOGLE_CREDENTIALS_FILE_CONTENTS'],
|
43
|
+
region: 'us-east4'
|
44
|
+
},
|
45
|
+
options: { model: 'gemini-pro', server_sent_events: true }
|
46
|
+
)
|
47
|
+
|
37
48
|
# With Application Default Credentials
|
38
49
|
client = Gemini.new(
|
39
50
|
credentials: {
|
@@ -100,6 +111,12 @@ Result:
|
|
100
111
|
- [Server-Sent Events (SSE) Hang](#server-sent-events-sse-hang)
|
101
112
|
- [Non-Streaming](#non-streaming)
|
102
113
|
- [Back-and-Forth Conversations](#back-and-forth-conversations)
|
114
|
+
- [Safety Settings](#safety-settings)
|
115
|
+
- [System Instructions](#system-instructions)
|
116
|
+
- [Counting Tokens](#counting-tokens)
|
117
|
+
- [JSON Format Responses](#json-format-responses)
|
118
|
+
- [JSON Schema](#json-schema)
|
119
|
+
- [Models That Support JSON](#models-that-support-json)
|
103
120
|
- [Tools (Functions) Calling](#tools-functions-calling)
|
104
121
|
- [New Functionalities and APIs](#new-functionalities-and-apis)
|
105
122
|
- [Request Options](#request-options)
|
@@ -121,11 +138,11 @@ Result:
|
|
121
138
|
### Installing
|
122
139
|
|
123
140
|
```sh
|
124
|
-
gem install gemini-ai -v 4.
|
141
|
+
gem install gemini-ai -v 4.2.0
|
125
142
|
```
|
126
143
|
|
127
144
|
```sh
|
128
|
-
gem 'gemini-ai', '~> 4.
|
145
|
+
gem 'gemini-ai', '~> 4.2.0'
|
129
146
|
```
|
130
147
|
|
131
148
|
### Credentials
|
@@ -207,7 +224,7 @@ Similar to [Option 2](#option-2-service-account-credentials-file-vertex-ai-api),
|
|
207
224
|
For local development, you can generate your default credentials using the [gcloud CLI](https://cloud.google.com/sdk/gcloud) as follows:
|
208
225
|
|
209
226
|
```sh
|
210
|
-
gcloud auth application-default login
|
227
|
+
gcloud auth application-default login
|
211
228
|
```
|
212
229
|
|
213
230
|
For more details about alternative methods and different environments, check the official documentation:
|
@@ -245,6 +262,23 @@ Remember that hardcoding your API key in code is unsafe; it's preferable to use
|
|
245
262
|
}
|
246
263
|
```
|
247
264
|
|
265
|
+
Alternatively, you can pass the file contents instead of the path:
|
266
|
+
```ruby
|
267
|
+
{
|
268
|
+
service: 'vertex-ai-api',
|
269
|
+
file_contents: File.read('google-credentials.json'),
|
270
|
+
region: 'us-east4'
|
271
|
+
}
|
272
|
+
```
|
273
|
+
|
274
|
+
```ruby
|
275
|
+
{
|
276
|
+
service: 'vertex-ai-api',
|
277
|
+
file_contents: ENV['GOOGLE_CREDENTIALS_FILE_CONTENTS'],
|
278
|
+
region: 'us-east4'
|
279
|
+
}
|
280
|
+
```
|
281
|
+
|
248
282
|
**Option 3**: For _Application Default Credentials_, omit both the `api_key` and the `file_path`:
|
249
283
|
|
250
284
|
```ruby
|
@@ -303,6 +337,17 @@ client = Gemini.new(
|
|
303
337
|
options: { model: 'gemini-pro', server_sent_events: true }
|
304
338
|
)
|
305
339
|
|
340
|
+
# With the Service Account Credentials File contents
|
341
|
+
client = Gemini.new(
|
342
|
+
credentials: {
|
343
|
+
service: 'vertex-ai-api',
|
344
|
+
file_contents: File.read('google-credentials.json'),
|
345
|
+
# file_contents: ENV['GOOGLE_CREDENTIALS_FILE_CONTENTS'],
|
346
|
+
region: 'us-east4'
|
347
|
+
},
|
348
|
+
options: { model: 'gemini-pro', server_sent_events: true }
|
349
|
+
)
|
350
|
+
|
306
351
|
# With Application Default Credentials
|
307
352
|
client = Gemini.new(
|
308
353
|
credentials: {
|
@@ -316,7 +361,7 @@ client = Gemini.new(
|
|
316
361
|
|
317
362
|
## Available Models
|
318
363
|
|
319
|
-
These models are accessible to the repository **author** as of
|
364
|
+
These models are accessible to the repository **author** as of June 2025 in the `us-east4` region. Access to models may vary by region, user, and account. All models here are expected to work, if you can access them. This is just a reference of what a "typical" user may expect to have access to right away:
|
320
365
|
|
321
366
|
| Model | Vertex AI | Generative Language |
|
322
367
|
|------------------------------------------|:---------:|:-------------------:|
|
@@ -324,9 +369,9 @@ These models are accessible to the repository **author** as of May 2025 in the `
|
|
324
369
|
| gemini-pro | ✅ | ✅ |
|
325
370
|
| gemini-1.5-pro-preview-0514 | ✅ | 🔒 |
|
326
371
|
| gemini-1.5-pro-preview-0409 | ✅ | 🔒 |
|
327
|
-
| gemini-1.5-pro |
|
372
|
+
| gemini-1.5-pro | ✅ | ✅ |
|
328
373
|
| gemini-1.5-flash-preview-0514 | ✅ | 🔒 |
|
329
|
-
| gemini-1.5-flash |
|
374
|
+
| gemini-1.5-flash | ✅ | ✅ |
|
330
375
|
| gemini-1.0-pro-vision-latest | 🔒 | 🔒 |
|
331
376
|
| gemini-1.0-pro-vision-001 | ✅ | 🔒 |
|
332
377
|
| gemini-1.0-pro-vision | ✅ | 🔒 |
|
@@ -334,6 +379,11 @@ These models are accessible to the repository **author** as of May 2025 in the `
|
|
334
379
|
| gemini-1.0-pro-002 | ✅ | 🔒 |
|
335
380
|
| gemini-1.0-pro-001 | ✅ | ✅ |
|
336
381
|
| gemini-1.0-pro | ✅ | ✅ |
|
382
|
+
| gemini-ultra | 🔒 | 🔒 |
|
383
|
+
| gemini-1.0-ultra | 🔒 | 🔒 |
|
384
|
+
| gemini-1.0-ultra-001 | 🔒 | 🔒 |
|
385
|
+
| text-embedding-preview-0514 | 🔒 | 🔒 |
|
386
|
+
| text-embedding-preview-0409 | 🔒 | 🔒 |
|
337
387
|
| text-embedding-004 | ✅ | ✅ |
|
338
388
|
| embedding-001 | 🔒 | ✅ |
|
339
389
|
| text-multilingual-embedding-002 | ✅ | 🔒 |
|
@@ -379,6 +429,17 @@ client = Gemini.new(
|
|
379
429
|
options: { model: 'gemini-pro', server_sent_events: true }
|
380
430
|
)
|
381
431
|
|
432
|
+
# With the Service Account Credentials File contents
|
433
|
+
client = Gemini.new(
|
434
|
+
credentials: {
|
435
|
+
service: 'vertex-ai-api',
|
436
|
+
file_contents: File.read('google-credentials.json'),
|
437
|
+
# file_contents: ENV['GOOGLE_CREDENTIALS_FILE_CONTENTS'],
|
438
|
+
region: 'us-east4'
|
439
|
+
},
|
440
|
+
options: { model: 'gemini-pro', server_sent_events: true }
|
441
|
+
)
|
442
|
+
|
382
443
|
# With Application Default Credentials
|
383
444
|
client = Gemini.new(
|
384
445
|
credentials: {
|
@@ -853,6 +914,229 @@ Result:
|
|
853
914
|
} }]
|
854
915
|
```
|
855
916
|
|
917
|
+
### Safety Settings
|
918
|
+
|
919
|
+
You can [configure safety attributes](https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/configure-safety-attributes) for your requests.
|
920
|
+
|
921
|
+
Harm Categories:
|
922
|
+
> `HARM_CATEGORY_UNSPECIFIED`, `HARM_CATEGORY_HARASSMENT`, `HARM_CATEGORY_HATE_SPEECH`, `HARM_CATEGORY_SEXUALLY_EXPLICIT`, `HARM_CATEGORY_DANGEROUS_CONTENT`.
|
923
|
+
|
924
|
+
Thresholds:
|
925
|
+
> `BLOCK_NONE`, `BLOCK_ONLY_HIGH`, `BLOCK_MEDIUM_AND_ABOVE`, `BLOCK_LOW_AND_ABOVE`, `HARM_BLOCK_THRESHOLD_UNSPECIFIED`.
|
926
|
+
|
927
|
+
Example:
|
928
|
+
```ruby
|
929
|
+
client.stream_generate_content(
|
930
|
+
{
|
931
|
+
contents: { role: 'user', parts: { text: 'hi!' } },
|
932
|
+
safetySettings: [
|
933
|
+
{
|
934
|
+
category: 'HARM_CATEGORY_UNSPECIFIED',
|
935
|
+
threshold: 'BLOCK_ONLY_HIGH'
|
936
|
+
},
|
937
|
+
{
|
938
|
+
category: 'HARM_CATEGORY_HARASSMENT',
|
939
|
+
threshold: 'BLOCK_ONLY_HIGH'
|
940
|
+
},
|
941
|
+
{
|
942
|
+
category: 'HARM_CATEGORY_HATE_SPEECH',
|
943
|
+
threshold: 'BLOCK_ONLY_HIGH'
|
944
|
+
},
|
945
|
+
{
|
946
|
+
category: 'HARM_CATEGORY_SEXUALLY_EXPLICIT',
|
947
|
+
threshold: 'BLOCK_ONLY_HIGH'
|
948
|
+
},
|
949
|
+
{
|
950
|
+
category: 'HARM_CATEGORY_DANGEROUS_CONTENT',
|
951
|
+
threshold: 'BLOCK_ONLY_HIGH'
|
952
|
+
}
|
953
|
+
]
|
954
|
+
}
|
955
|
+
)
|
956
|
+
```
|
957
|
+
|
958
|
+
Google started to block the usage of `BLOCK_NONE` unless:
|
959
|
+
|
960
|
+
> _User has requested a restricted HarmBlockThreshold setting BLOCK_NONE. You can get access either (a) through an allowlist via your Google account team, or (b) by switching your account type to monthly invoiced billing via this instruction: https://cloud.google.com/billing/docs/how-to/invoiced-billing_
|
961
|
+
|
962
|
+
### System Instructions
|
963
|
+
|
964
|
+
Some models support [system instructions](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/prompts/system-instructions):
|
965
|
+
|
966
|
+
```ruby
|
967
|
+
client.stream_generate_content(
|
968
|
+
{ contents: { role: 'user', parts: { text: 'Hi! Who are you?' } },
|
969
|
+
system_instruction: { role: 'user', parts: { text: 'Your name is Neko.' } } }
|
970
|
+
)
|
971
|
+
```
|
972
|
+
|
973
|
+
Output:
|
974
|
+
```text
|
975
|
+
Hi! I'm Neko, a factual language model from Google AI.
|
976
|
+
```
|
977
|
+
|
978
|
+
```ruby
|
979
|
+
client.stream_generate_content(
|
980
|
+
{ contents: { role: 'user', parts: { text: 'Hi! Who are you?' } },
|
981
|
+
system_instruction: {
|
982
|
+
role: 'user', parts: [
|
983
|
+
{ text: 'You are a cat.' },
|
984
|
+
{ text: 'Your name is Neko.' }
|
985
|
+
]
|
986
|
+
} }
|
987
|
+
)
|
988
|
+
```
|
989
|
+
|
990
|
+
Output:
|
991
|
+
```text
|
992
|
+
Meow! I'm Neko, a fluffy and playful cat. :3
|
993
|
+
```
|
994
|
+
|
995
|
+
### Counting Tokens
|
996
|
+
|
997
|
+
You can [count tokens](https://ai.google.dev/gemini-api/docs/tokens#count-tokens) and preview how many tokens a request is expected to consume:
|
998
|
+
|
999
|
+
```ruby
|
1000
|
+
client.count_tokens(
|
1001
|
+
{ contents: { role: 'user', parts: { text: 'hi!' } } }
|
1002
|
+
)
|
1003
|
+
```
|
1004
|
+
|
1005
|
+
Output for Generative Language API:
|
1006
|
+
```ruby
|
1007
|
+
{ 'totalTokens' => 3 }
|
1008
|
+
```
|
1009
|
+
|
1010
|
+
Output for Vertex AI API:
|
1011
|
+
|
1012
|
+
```ruby
|
1013
|
+
{ 'totalTokens' => 2, 'totalBillableCharacters' => 3 }
|
1014
|
+
```
|
1015
|
+
|
1016
|
+
### JSON Format Responses
|
1017
|
+
|
1018
|
+
> _As of the writing of this README, only the `vertex-ai-api` service and `gemini` models version `1.5` support this feature._
|
1019
|
+
|
1020
|
+
The Gemini API provides a configuration parameter to [request a response in JSON](https://ai.google.dev/gemini-api/docs/api-overview#json) format:
|
1021
|
+
|
1022
|
+
```ruby
|
1023
|
+
require 'json'
|
1024
|
+
|
1025
|
+
result = client.stream_generate_content(
|
1026
|
+
{
|
1027
|
+
contents: {
|
1028
|
+
role: 'user',
|
1029
|
+
parts: {
|
1030
|
+
text: 'List 3 random colors.'
|
1031
|
+
}
|
1032
|
+
},
|
1033
|
+
generation_config: {
|
1034
|
+
response_mime_type: 'application/json'
|
1035
|
+
}
|
1036
|
+
|
1037
|
+
}
|
1038
|
+
)
|
1039
|
+
|
1040
|
+
json_string = result
|
1041
|
+
.map { |response| response.dig('candidates', 0, 'content', 'parts') }
|
1042
|
+
.map { |parts| parts.map { |part| part['text'] }.join }
|
1043
|
+
.join
|
1044
|
+
|
1045
|
+
puts JSON.parse(json_string).inspect
|
1046
|
+
```
|
1047
|
+
|
1048
|
+
Output:
|
1049
|
+
```ruby
|
1050
|
+
{ 'colors' => ['Dark Salmon', 'Indigo', 'Lavender'] }
|
1051
|
+
```
|
1052
|
+
|
1053
|
+
#### JSON Schema
|
1054
|
+
|
1055
|
+
> _While Gemini 1.5 Flash models only accept a text description of the JSON schema you want returned, the Gemini 1.5 Pro models let you pass a schema object (or a Python type equivalent), and the model output will strictly follow that schema. This is also known as controlled generation or constrained decoding._
|
1056
|
+
|
1057
|
+
You can also provide a [JSON Schema](https://json-schema.org) for the expected JSON output:
|
1058
|
+
|
1059
|
+
```ruby
|
1060
|
+
require 'json'
|
1061
|
+
|
1062
|
+
result = client.stream_generate_content(
|
1063
|
+
{
|
1064
|
+
contents: {
|
1065
|
+
role: 'user',
|
1066
|
+
parts: {
|
1067
|
+
text: 'List 3 random colors.'
|
1068
|
+
}
|
1069
|
+
},
|
1070
|
+
generation_config: {
|
1071
|
+
response_mime_type: 'application/json',
|
1072
|
+
response_schema: {
|
1073
|
+
type: 'object',
|
1074
|
+
properties: {
|
1075
|
+
colors: {
|
1076
|
+
type: 'array',
|
1077
|
+
items: {
|
1078
|
+
type: 'object',
|
1079
|
+
properties: {
|
1080
|
+
name: {
|
1081
|
+
type: 'string'
|
1082
|
+
}
|
1083
|
+
}
|
1084
|
+
}
|
1085
|
+
}
|
1086
|
+
}
|
1087
|
+
}
|
1088
|
+
}
|
1089
|
+
}
|
1090
|
+
)
|
1091
|
+
|
1092
|
+
json_string = result
|
1093
|
+
.map { |response| response.dig('candidates', 0, 'content', 'parts') }
|
1094
|
+
.map { |parts| parts.map { |part| part['text'] }.join }
|
1095
|
+
.join
|
1096
|
+
|
1097
|
+
puts JSON.parse(json_string).inspect
|
1098
|
+
```
|
1099
|
+
|
1100
|
+
Output:
|
1101
|
+
|
1102
|
+
```ruby
|
1103
|
+
{ 'colors' => [
|
1104
|
+
{ 'name' => 'Lavender Blush' },
|
1105
|
+
{ 'name' => 'Medium Turquoise' },
|
1106
|
+
{ 'name' => 'Dark Slate Gray' }
|
1107
|
+
] }
|
1108
|
+
```
|
1109
|
+
|
1110
|
+
#### Models That Support JSON
|
1111
|
+
|
1112
|
+
These models are accessible to the repository **author** as of June 2025 in the `us-east4` region. Access to models may vary by region, user, and account.
|
1113
|
+
|
1114
|
+
- ❌ Does not support JSON mode.
|
1115
|
+
- 🟡 Supports JSON mode but not Schema.
|
1116
|
+
- ✅ Supports JSON mode and Schema.
|
1117
|
+
- 🔒 I don't have access to the model.
|
1118
|
+
|
1119
|
+
| Model | Vertex AI | Generative Language |
|
1120
|
+
|------------------------------------------|:---------:|:-------------------:|
|
1121
|
+
| gemini-pro-vision | ❌ | 🔒 |
|
1122
|
+
| gemini-pro | 🟡 | ❌ |
|
1123
|
+
| gemini-1.5-pro-preview-0514 | ✅ | 🔒 |
|
1124
|
+
| gemini-1.5-pro-preview-0409 | ✅ | 🔒 |
|
1125
|
+
| gemini-1.5-pro | ✅ | ❌ |
|
1126
|
+
| gemini-1.5-flash-preview-0514 | 🟡 | 🔒 |
|
1127
|
+
| gemini-1.5-flash | 🟡 | ❌ |
|
1128
|
+
| gemini-1.0-pro-vision-latest | 🔒 | 🔒 |
|
1129
|
+
| gemini-1.0-pro-vision-001 | ❌ | 🔒 |
|
1130
|
+
| gemini-1.0-pro-vision | ❌ | 🔒 |
|
1131
|
+
| gemini-1.0-pro-latest | 🔒 | ❌ |
|
1132
|
+
| gemini-1.0-pro-002 | 🟡 | 🔒 |
|
1133
|
+
| gemini-1.0-pro-001 | ❌ | ❌ |
|
1134
|
+
| gemini-1.0-pro | 🟡 | ❌ |
|
1135
|
+
| gemini-ultra | 🔒 | 🔒 |
|
1136
|
+
| gemini-1.0-ultra | 🔒 | 🔒 |
|
1137
|
+
| gemini-1.0-ultra-001 | 🔒 | 🔒 |
|
1138
|
+
|
1139
|
+
|
856
1140
|
### Tools (Functions) Calling
|
857
1141
|
|
858
1142
|
> As of the writing of this README, only the `vertex-ai-api` service and the `gemini-pro` model [supports](https://cloud.google.com/vertex-ai/docs/generative-ai/multimodal/function-calling#supported_models) tools (functions) calls.
|
@@ -1123,6 +1407,7 @@ GeminiError
|
|
1123
1407
|
|
1124
1408
|
MissingProjectIdError
|
1125
1409
|
UnsupportedServiceError
|
1410
|
+
ConflictingCredentialsError
|
1126
1411
|
BlockWithoutServerSentEventsError
|
1127
1412
|
|
1128
1413
|
RequestError
|
@@ -1134,7 +1419,14 @@ RequestError
|
|
1134
1419
|
bundle
|
1135
1420
|
rubocop -A
|
1136
1421
|
|
1137
|
-
|
1422
|
+
rspec
|
1423
|
+
|
1424
|
+
bundle exec ruby spec/tasks/run-available-models.rb
|
1425
|
+
bundle exec ruby spec/tasks/run-embed.rb
|
1426
|
+
bundle exec ruby spec/tasks/run-generate.rb
|
1427
|
+
bundle exec ruby spec/tasks/run-json.rb
|
1428
|
+
bundle exec ruby spec/tasks/run-safety.rb
|
1429
|
+
bundle exec ruby spec/tasks/run-system.rb
|
1138
1430
|
```
|
1139
1431
|
|
1140
1432
|
### Purpose
|
@@ -1148,7 +1440,7 @@ gem build gemini-ai.gemspec
|
|
1148
1440
|
|
1149
1441
|
gem signin
|
1150
1442
|
|
1151
|
-
gem push gemini-ai-4.
|
1443
|
+
gem push gemini-ai-4.2.0.gem
|
1152
1444
|
```
|
1153
1445
|
|
1154
1446
|
### Updating the README
|
@@ -1193,6 +1485,8 @@ These resources and references may be useful throughout your learning process.
|
|
1193
1485
|
- [Vertex AI API Documentation](https://cloud.google.com/vertex-ai/docs/reference)
|
1194
1486
|
- [REST Documentation](https://cloud.google.com/vertex-ai/docs/reference/rest)
|
1195
1487
|
- [Get text embeddings](https://cloud.google.com/vertex-ai/generative-ai/docs/embeddings/get-text-embeddings)
|
1488
|
+
- [Use system instructions](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/prompts/system-instructions)
|
1489
|
+
- [Configure safety attributes](https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/configure-safety-attributes)
|
1196
1490
|
- [Google models](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/models)
|
1197
1491
|
- [Model versions and lifecycle](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/model-versioning)
|
1198
1492
|
- [Google DeepMind Gemini](https://deepmind.google/technologies/gemini/)
|
data/components/errors.rb
CHANGED
@@ -4,12 +4,13 @@ module Gemini
|
|
4
4
|
module Errors
|
5
5
|
class GeminiError < StandardError
|
6
6
|
def initialize(message = nil)
|
7
|
-
super
|
7
|
+
super
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
11
|
class MissingProjectIdError < GeminiError; end
|
12
12
|
class UnsupportedServiceError < GeminiError; end
|
13
|
+
class ConflictingCredentialsError < GeminiError; end
|
13
14
|
class BlockWithoutServerSentEventsError < GeminiError; end
|
14
15
|
|
15
16
|
class RequestError < GeminiError
|
data/controllers/client.rb
CHANGED
@@ -21,16 +21,24 @@ module Gemini
|
|
21
21
|
@service = config[:credentials][:service]
|
22
22
|
|
23
23
|
unless %w[vertex-ai-api generative-language-api].include?(@service)
|
24
|
-
raise Errors::UnsupportedServiceError, "Unsupported service: #{@service}"
|
24
|
+
raise Errors::UnsupportedServiceError, "Unsupported service: '#{@service}'."
|
25
25
|
end
|
26
26
|
|
27
|
+
avoid_conflicting_credentials!(config[:credentials])
|
28
|
+
|
27
29
|
if config[:credentials][:api_key]
|
28
30
|
@authentication = :api_key
|
29
31
|
@api_key = config[:credentials][:api_key]
|
30
|
-
elsif config[:credentials][:file_path]
|
32
|
+
elsif config[:credentials][:file_path] || config[:credentials][:file_contents]
|
31
33
|
@authentication = :service_account
|
34
|
+
json_key_io = if config[:credentials][:file_path]
|
35
|
+
File.open(config[:credentials][:file_path])
|
36
|
+
else
|
37
|
+
StringIO.new(config[:credentials][:file_contents])
|
38
|
+
end
|
39
|
+
|
32
40
|
@authorizer = ::Google::Auth::ServiceAccountCredentials.make_creds(
|
33
|
-
json_key_io
|
41
|
+
json_key_io:,
|
34
42
|
scope: 'https://www.googleapis.com/auth/cloud-platform'
|
35
43
|
)
|
36
44
|
else
|
@@ -75,6 +83,21 @@ module Gemini
|
|
75
83
|
end
|
76
84
|
end
|
77
85
|
|
86
|
+
def avoid_conflicting_credentials!(credentials)
|
87
|
+
conflicting_keys = %i[api_key file_path file_contents]
|
88
|
+
|
89
|
+
found = credentials.keys.filter { |key| conflicting_keys.include?(key) }
|
90
|
+
|
91
|
+
return unless found.size > 1
|
92
|
+
|
93
|
+
message = found.sort.each_with_index.map do |key, i|
|
94
|
+
i == found.size - 1 ? "or '#{key}'" : "'#{key}'"
|
95
|
+
end.join(', ')
|
96
|
+
|
97
|
+
raise Errors::ConflictingCredentialsError,
|
98
|
+
"You must choose either #{message}."
|
99
|
+
end
|
100
|
+
|
78
101
|
def predict(payload, server_sent_events: nil, &callback)
|
79
102
|
result = request(
|
80
103
|
"#{@model_address}:predict", payload,
|
@@ -86,6 +109,17 @@ module Gemini
|
|
86
109
|
result
|
87
110
|
end
|
88
111
|
|
112
|
+
def count_tokens(payload, server_sent_events: nil, &callback)
|
113
|
+
result = request(
|
114
|
+
"#{@model_address}:countTokens", payload,
|
115
|
+
server_sent_events:, &callback
|
116
|
+
)
|
117
|
+
|
118
|
+
return result.first if result.is_a?(Array) && result.size == 1
|
119
|
+
|
120
|
+
result
|
121
|
+
end
|
122
|
+
|
89
123
|
def embed_content(payload, server_sent_events: nil, &callback)
|
90
124
|
result = request(
|
91
125
|
"#{@model_address}:embedContent", payload,
|
data/gemini-ai.gemspec
CHANGED
@@ -30,7 +30,7 @@ Gem::Specification.new do |spec|
|
|
30
30
|
spec.require_paths = ['ports/dsl']
|
31
31
|
|
32
32
|
spec.add_dependency 'event_stream_parser', '~> 1.0'
|
33
|
-
spec.add_dependency 'faraday', '~> 2.
|
33
|
+
spec.add_dependency 'faraday', '~> 2.10'
|
34
34
|
spec.add_dependency 'faraday-typhoeus', '~> 1.1'
|
35
35
|
|
36
36
|
# Before upgrading, check this:
|
data/static/gem.rb
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
module Gemini
|
4
4
|
GEM = {
|
5
5
|
name: 'gemini-ai',
|
6
|
-
version: '4.
|
6
|
+
version: '4.2.0',
|
7
7
|
author: 'gbaptista',
|
8
8
|
summary: "Interact with Google's Gemini AI.",
|
9
9
|
description: "A Ruby Gem for interacting with Gemini through Vertex AI, Generative Language API, or AI Studio, Google's generative AI services.",
|
data/template.md
CHANGED
@@ -9,7 +9,7 @@ A Ruby Gem for interacting with [Gemini](https://deepmind.google/technologies/ge
|
|
9
9
|
## TL;DR and Quick Start
|
10
10
|
|
11
11
|
```ruby
|
12
|
-
gem 'gemini-ai', '~> 4.
|
12
|
+
gem 'gemini-ai', '~> 4.2.0'
|
13
13
|
```
|
14
14
|
|
15
15
|
```ruby
|
@@ -34,6 +34,17 @@ client = Gemini.new(
|
|
34
34
|
options: { model: 'gemini-pro', server_sent_events: true }
|
35
35
|
)
|
36
36
|
|
37
|
+
# With the Service Account Credentials File contents
|
38
|
+
client = Gemini.new(
|
39
|
+
credentials: {
|
40
|
+
service: 'vertex-ai-api',
|
41
|
+
file_contents: File.read('google-credentials.json'),
|
42
|
+
# file_contents: ENV['GOOGLE_CREDENTIALS_FILE_CONTENTS'],
|
43
|
+
region: 'us-east4'
|
44
|
+
},
|
45
|
+
options: { model: 'gemini-pro', server_sent_events: true }
|
46
|
+
)
|
47
|
+
|
37
48
|
# With Application Default Credentials
|
38
49
|
client = Gemini.new(
|
39
50
|
credentials: {
|
@@ -77,11 +88,11 @@ Result:
|
|
77
88
|
### Installing
|
78
89
|
|
79
90
|
```sh
|
80
|
-
gem install gemini-ai -v 4.
|
91
|
+
gem install gemini-ai -v 4.2.0
|
81
92
|
```
|
82
93
|
|
83
94
|
```sh
|
84
|
-
gem 'gemini-ai', '~> 4.
|
95
|
+
gem 'gemini-ai', '~> 4.2.0'
|
85
96
|
```
|
86
97
|
|
87
98
|
### Credentials
|
@@ -163,7 +174,7 @@ Similar to [Option 2](#option-2-service-account-credentials-file-vertex-ai-api),
|
|
163
174
|
For local development, you can generate your default credentials using the [gcloud CLI](https://cloud.google.com/sdk/gcloud) as follows:
|
164
175
|
|
165
176
|
```sh
|
166
|
-
gcloud auth application-default login
|
177
|
+
gcloud auth application-default login
|
167
178
|
```
|
168
179
|
|
169
180
|
For more details about alternative methods and different environments, check the official documentation:
|
@@ -201,6 +212,23 @@ Remember that hardcoding your API key in code is unsafe; it's preferable to use
|
|
201
212
|
}
|
202
213
|
```
|
203
214
|
|
215
|
+
Alternatively, you can pass the file contents instead of the path:
|
216
|
+
```ruby
|
217
|
+
{
|
218
|
+
service: 'vertex-ai-api',
|
219
|
+
file_contents: File.read('google-credentials.json'),
|
220
|
+
region: 'us-east4'
|
221
|
+
}
|
222
|
+
```
|
223
|
+
|
224
|
+
```ruby
|
225
|
+
{
|
226
|
+
service: 'vertex-ai-api',
|
227
|
+
file_contents: ENV['GOOGLE_CREDENTIALS_FILE_CONTENTS'],
|
228
|
+
region: 'us-east4'
|
229
|
+
}
|
230
|
+
```
|
231
|
+
|
204
232
|
**Option 3**: For _Application Default Credentials_, omit both the `api_key` and the `file_path`:
|
205
233
|
|
206
234
|
```ruby
|
@@ -259,6 +287,17 @@ client = Gemini.new(
|
|
259
287
|
options: { model: 'gemini-pro', server_sent_events: true }
|
260
288
|
)
|
261
289
|
|
290
|
+
# With the Service Account Credentials File contents
|
291
|
+
client = Gemini.new(
|
292
|
+
credentials: {
|
293
|
+
service: 'vertex-ai-api',
|
294
|
+
file_contents: File.read('google-credentials.json'),
|
295
|
+
# file_contents: ENV['GOOGLE_CREDENTIALS_FILE_CONTENTS'],
|
296
|
+
region: 'us-east4'
|
297
|
+
},
|
298
|
+
options: { model: 'gemini-pro', server_sent_events: true }
|
299
|
+
)
|
300
|
+
|
262
301
|
# With Application Default Credentials
|
263
302
|
client = Gemini.new(
|
264
303
|
credentials: {
|
@@ -272,7 +311,7 @@ client = Gemini.new(
|
|
272
311
|
|
273
312
|
## Available Models
|
274
313
|
|
275
|
-
These models are accessible to the repository **author** as of
|
314
|
+
These models are accessible to the repository **author** as of June 2025 in the `us-east4` region. Access to models may vary by region, user, and account. All models here are expected to work, if you can access them. This is just a reference of what a "typical" user may expect to have access to right away:
|
276
315
|
|
277
316
|
| Model | Vertex AI | Generative Language |
|
278
317
|
|------------------------------------------|:---------:|:-------------------:|
|
@@ -280,9 +319,9 @@ These models are accessible to the repository **author** as of May 2025 in the `
|
|
280
319
|
| gemini-pro | ✅ | ✅ |
|
281
320
|
| gemini-1.5-pro-preview-0514 | ✅ | 🔒 |
|
282
321
|
| gemini-1.5-pro-preview-0409 | ✅ | 🔒 |
|
283
|
-
| gemini-1.5-pro |
|
322
|
+
| gemini-1.5-pro | ✅ | ✅ |
|
284
323
|
| gemini-1.5-flash-preview-0514 | ✅ | 🔒 |
|
285
|
-
| gemini-1.5-flash |
|
324
|
+
| gemini-1.5-flash | ✅ | ✅ |
|
286
325
|
| gemini-1.0-pro-vision-latest | 🔒 | 🔒 |
|
287
326
|
| gemini-1.0-pro-vision-001 | ✅ | 🔒 |
|
288
327
|
| gemini-1.0-pro-vision | ✅ | 🔒 |
|
@@ -290,6 +329,11 @@ These models are accessible to the repository **author** as of May 2025 in the `
|
|
290
329
|
| gemini-1.0-pro-002 | ✅ | 🔒 |
|
291
330
|
| gemini-1.0-pro-001 | ✅ | ✅ |
|
292
331
|
| gemini-1.0-pro | ✅ | ✅ |
|
332
|
+
| gemini-ultra | 🔒 | 🔒 |
|
333
|
+
| gemini-1.0-ultra | 🔒 | 🔒 |
|
334
|
+
| gemini-1.0-ultra-001 | 🔒 | 🔒 |
|
335
|
+
| text-embedding-preview-0514 | 🔒 | 🔒 |
|
336
|
+
| text-embedding-preview-0409 | 🔒 | 🔒 |
|
293
337
|
| text-embedding-004 | ✅ | ✅ |
|
294
338
|
| embedding-001 | 🔒 | ✅ |
|
295
339
|
| text-multilingual-embedding-002 | ✅ | 🔒 |
|
@@ -335,6 +379,17 @@ client = Gemini.new(
|
|
335
379
|
options: { model: 'gemini-pro', server_sent_events: true }
|
336
380
|
)
|
337
381
|
|
382
|
+
# With the Service Account Credentials File contents
|
383
|
+
client = Gemini.new(
|
384
|
+
credentials: {
|
385
|
+
service: 'vertex-ai-api',
|
386
|
+
file_contents: File.read('google-credentials.json'),
|
387
|
+
# file_contents: ENV['GOOGLE_CREDENTIALS_FILE_CONTENTS'],
|
388
|
+
region: 'us-east4'
|
389
|
+
},
|
390
|
+
options: { model: 'gemini-pro', server_sent_events: true }
|
391
|
+
)
|
392
|
+
|
338
393
|
# With Application Default Credentials
|
339
394
|
client = Gemini.new(
|
340
395
|
credentials: {
|
@@ -809,6 +864,229 @@ Result:
|
|
809
864
|
} }]
|
810
865
|
```
|
811
866
|
|
867
|
+
### Safety Settings
|
868
|
+
|
869
|
+
You can [configure safety attributes](https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/configure-safety-attributes) for your requests.
|
870
|
+
|
871
|
+
Harm Categories:
|
872
|
+
> `HARM_CATEGORY_UNSPECIFIED`, `HARM_CATEGORY_HARASSMENT`, `HARM_CATEGORY_HATE_SPEECH`, `HARM_CATEGORY_SEXUALLY_EXPLICIT`, `HARM_CATEGORY_DANGEROUS_CONTENT`.
|
873
|
+
|
874
|
+
Thresholds:
|
875
|
+
> `BLOCK_NONE`, `BLOCK_ONLY_HIGH`, `BLOCK_MEDIUM_AND_ABOVE`, `BLOCK_LOW_AND_ABOVE`, `HARM_BLOCK_THRESHOLD_UNSPECIFIED`.
|
876
|
+
|
877
|
+
Example:
|
878
|
+
```ruby
|
879
|
+
client.stream_generate_content(
|
880
|
+
{
|
881
|
+
contents: { role: 'user', parts: { text: 'hi!' } },
|
882
|
+
safetySettings: [
|
883
|
+
{
|
884
|
+
category: 'HARM_CATEGORY_UNSPECIFIED',
|
885
|
+
threshold: 'BLOCK_ONLY_HIGH'
|
886
|
+
},
|
887
|
+
{
|
888
|
+
category: 'HARM_CATEGORY_HARASSMENT',
|
889
|
+
threshold: 'BLOCK_ONLY_HIGH'
|
890
|
+
},
|
891
|
+
{
|
892
|
+
category: 'HARM_CATEGORY_HATE_SPEECH',
|
893
|
+
threshold: 'BLOCK_ONLY_HIGH'
|
894
|
+
},
|
895
|
+
{
|
896
|
+
category: 'HARM_CATEGORY_SEXUALLY_EXPLICIT',
|
897
|
+
threshold: 'BLOCK_ONLY_HIGH'
|
898
|
+
},
|
899
|
+
{
|
900
|
+
category: 'HARM_CATEGORY_DANGEROUS_CONTENT',
|
901
|
+
threshold: 'BLOCK_ONLY_HIGH'
|
902
|
+
}
|
903
|
+
]
|
904
|
+
}
|
905
|
+
)
|
906
|
+
```
|
907
|
+
|
908
|
+
Google started to block the usage of `BLOCK_NONE` unless:
|
909
|
+
|
910
|
+
> _User has requested a restricted HarmBlockThreshold setting BLOCK_NONE. You can get access either (a) through an allowlist via your Google account team, or (b) by switching your account type to monthly invoiced billing via this instruction: https://cloud.google.com/billing/docs/how-to/invoiced-billing_
|
911
|
+
|
912
|
+
### System Instructions
|
913
|
+
|
914
|
+
Some models support [system instructions](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/prompts/system-instructions):
|
915
|
+
|
916
|
+
```ruby
|
917
|
+
client.stream_generate_content(
|
918
|
+
{ contents: { role: 'user', parts: { text: 'Hi! Who are you?' } },
|
919
|
+
system_instruction: { role: 'user', parts: { text: 'Your name is Neko.' } } }
|
920
|
+
)
|
921
|
+
```
|
922
|
+
|
923
|
+
Output:
|
924
|
+
```text
|
925
|
+
Hi! I'm Neko, a factual language model from Google AI.
|
926
|
+
```
|
927
|
+
|
928
|
+
```ruby
|
929
|
+
client.stream_generate_content(
|
930
|
+
{ contents: { role: 'user', parts: { text: 'Hi! Who are you?' } },
|
931
|
+
system_instruction: {
|
932
|
+
role: 'user', parts: [
|
933
|
+
{ text: 'You are a cat.' },
|
934
|
+
{ text: 'Your name is Neko.' }
|
935
|
+
]
|
936
|
+
} }
|
937
|
+
)
|
938
|
+
```
|
939
|
+
|
940
|
+
Output:
|
941
|
+
```text
|
942
|
+
Meow! I'm Neko, a fluffy and playful cat. :3
|
943
|
+
```
|
944
|
+
|
945
|
+
### Counting Tokens
|
946
|
+
|
947
|
+
You can [count tokens](https://ai.google.dev/gemini-api/docs/tokens#count-tokens) and preview how many tokens a request is expected to consume:
|
948
|
+
|
949
|
+
```ruby
|
950
|
+
client.count_tokens(
|
951
|
+
{ contents: { role: 'user', parts: { text: 'hi!' } } }
|
952
|
+
)
|
953
|
+
```
|
954
|
+
|
955
|
+
Output for Generative Language API:
|
956
|
+
```ruby
|
957
|
+
{ 'totalTokens' => 3 }
|
958
|
+
```
|
959
|
+
|
960
|
+
Output for Vertex AI API:
|
961
|
+
|
962
|
+
```ruby
|
963
|
+
{ 'totalTokens' => 2, 'totalBillableCharacters' => 3 }
|
964
|
+
```
|
965
|
+
|
966
|
+
### JSON Format Responses
|
967
|
+
|
968
|
+
> _As of the writing of this README, only the `vertex-ai-api` service and `gemini` models version `1.5` support this feature._
|
969
|
+
|
970
|
+
The Gemini API provides a configuration parameter to [request a response in JSON](https://ai.google.dev/gemini-api/docs/api-overview#json) format:
|
971
|
+
|
972
|
+
```ruby
|
973
|
+
require 'json'
|
974
|
+
|
975
|
+
result = client.stream_generate_content(
|
976
|
+
{
|
977
|
+
contents: {
|
978
|
+
role: 'user',
|
979
|
+
parts: {
|
980
|
+
text: 'List 3 random colors.'
|
981
|
+
}
|
982
|
+
},
|
983
|
+
generation_config: {
|
984
|
+
response_mime_type: 'application/json'
|
985
|
+
}
|
986
|
+
|
987
|
+
}
|
988
|
+
)
|
989
|
+
|
990
|
+
json_string = result
|
991
|
+
.map { |response| response.dig('candidates', 0, 'content', 'parts') }
|
992
|
+
.map { |parts| parts.map { |part| part['text'] }.join }
|
993
|
+
.join
|
994
|
+
|
995
|
+
puts JSON.parse(json_string).inspect
|
996
|
+
```
|
997
|
+
|
998
|
+
Output:
|
999
|
+
```ruby
|
1000
|
+
{ 'colors' => ['Dark Salmon', 'Indigo', 'Lavender'] }
|
1001
|
+
```
|
1002
|
+
|
1003
|
+
#### JSON Schema
|
1004
|
+
|
1005
|
+
> _While Gemini 1.5 Flash models only accept a text description of the JSON schema you want returned, the Gemini 1.5 Pro models let you pass a schema object (or a Python type equivalent), and the model output will strictly follow that schema. This is also known as controlled generation or constrained decoding._
|
1006
|
+
|
1007
|
+
You can also provide a [JSON Schema](https://json-schema.org) for the expected JSON output:
|
1008
|
+
|
1009
|
+
```ruby
|
1010
|
+
require 'json'
|
1011
|
+
|
1012
|
+
result = client.stream_generate_content(
|
1013
|
+
{
|
1014
|
+
contents: {
|
1015
|
+
role: 'user',
|
1016
|
+
parts: {
|
1017
|
+
text: 'List 3 random colors.'
|
1018
|
+
}
|
1019
|
+
},
|
1020
|
+
generation_config: {
|
1021
|
+
response_mime_type: 'application/json',
|
1022
|
+
response_schema: {
|
1023
|
+
type: 'object',
|
1024
|
+
properties: {
|
1025
|
+
colors: {
|
1026
|
+
type: 'array',
|
1027
|
+
items: {
|
1028
|
+
type: 'object',
|
1029
|
+
properties: {
|
1030
|
+
name: {
|
1031
|
+
type: 'string'
|
1032
|
+
}
|
1033
|
+
}
|
1034
|
+
}
|
1035
|
+
}
|
1036
|
+
}
|
1037
|
+
}
|
1038
|
+
}
|
1039
|
+
}
|
1040
|
+
)
|
1041
|
+
|
1042
|
+
json_string = result
|
1043
|
+
.map { |response| response.dig('candidates', 0, 'content', 'parts') }
|
1044
|
+
.map { |parts| parts.map { |part| part['text'] }.join }
|
1045
|
+
.join
|
1046
|
+
|
1047
|
+
puts JSON.parse(json_string).inspect
|
1048
|
+
```
|
1049
|
+
|
1050
|
+
Output:
|
1051
|
+
|
1052
|
+
```ruby
|
1053
|
+
{ 'colors' => [
|
1054
|
+
{ 'name' => 'Lavender Blush' },
|
1055
|
+
{ 'name' => 'Medium Turquoise' },
|
1056
|
+
{ 'name' => 'Dark Slate Gray' }
|
1057
|
+
] }
|
1058
|
+
```
|
1059
|
+
|
1060
|
+
#### Models That Support JSON
|
1061
|
+
|
1062
|
+
These models are accessible to the repository **author** as of June 2025 in the `us-east4` region. Access to models may vary by region, user, and account.
|
1063
|
+
|
1064
|
+
- ❌ Does not support JSON mode.
|
1065
|
+
- 🟡 Supports JSON mode but not Schema.
|
1066
|
+
- ✅ Supports JSON mode and Schema.
|
1067
|
+
- 🔒 I don't have access to the model.
|
1068
|
+
|
1069
|
+
| Model | Vertex AI | Generative Language |
|
1070
|
+
|------------------------------------------|:---------:|:-------------------:|
|
1071
|
+
| gemini-pro-vision | ❌ | 🔒 |
|
1072
|
+
| gemini-pro | 🟡 | ❌ |
|
1073
|
+
| gemini-1.5-pro-preview-0514 | ✅ | 🔒 |
|
1074
|
+
| gemini-1.5-pro-preview-0409 | ✅ | 🔒 |
|
1075
|
+
| gemini-1.5-pro | ✅ | ❌ |
|
1076
|
+
| gemini-1.5-flash-preview-0514 | 🟡 | 🔒 |
|
1077
|
+
| gemini-1.5-flash | 🟡 | ❌ |
|
1078
|
+
| gemini-1.0-pro-vision-latest | 🔒 | 🔒 |
|
1079
|
+
| gemini-1.0-pro-vision-001 | ❌ | 🔒 |
|
1080
|
+
| gemini-1.0-pro-vision | ❌ | 🔒 |
|
1081
|
+
| gemini-1.0-pro-latest | 🔒 | ❌ |
|
1082
|
+
| gemini-1.0-pro-002 | 🟡 | 🔒 |
|
1083
|
+
| gemini-1.0-pro-001 | ❌ | ❌ |
|
1084
|
+
| gemini-1.0-pro | 🟡 | ❌ |
|
1085
|
+
| gemini-ultra | 🔒 | 🔒 |
|
1086
|
+
| gemini-1.0-ultra | 🔒 | 🔒 |
|
1087
|
+
| gemini-1.0-ultra-001 | 🔒 | 🔒 |
|
1088
|
+
|
1089
|
+
|
812
1090
|
### Tools (Functions) Calling
|
813
1091
|
|
814
1092
|
> As of the writing of this README, only the `vertex-ai-api` service and the `gemini-pro` model [supports](https://cloud.google.com/vertex-ai/docs/generative-ai/multimodal/function-calling#supported_models) tools (functions) calls.
|
@@ -1079,6 +1357,7 @@ GeminiError
|
|
1079
1357
|
|
1080
1358
|
MissingProjectIdError
|
1081
1359
|
UnsupportedServiceError
|
1360
|
+
ConflictingCredentialsError
|
1082
1361
|
BlockWithoutServerSentEventsError
|
1083
1362
|
|
1084
1363
|
RequestError
|
@@ -1090,7 +1369,14 @@ RequestError
|
|
1090
1369
|
bundle
|
1091
1370
|
rubocop -A
|
1092
1371
|
|
1093
|
-
|
1372
|
+
rspec
|
1373
|
+
|
1374
|
+
bundle exec ruby spec/tasks/run-available-models.rb
|
1375
|
+
bundle exec ruby spec/tasks/run-embed.rb
|
1376
|
+
bundle exec ruby spec/tasks/run-generate.rb
|
1377
|
+
bundle exec ruby spec/tasks/run-json.rb
|
1378
|
+
bundle exec ruby spec/tasks/run-safety.rb
|
1379
|
+
bundle exec ruby spec/tasks/run-system.rb
|
1094
1380
|
```
|
1095
1381
|
|
1096
1382
|
### Purpose
|
@@ -1104,7 +1390,7 @@ gem build gemini-ai.gemspec
|
|
1104
1390
|
|
1105
1391
|
gem signin
|
1106
1392
|
|
1107
|
-
gem push gemini-ai-4.
|
1393
|
+
gem push gemini-ai-4.2.0.gem
|
1108
1394
|
```
|
1109
1395
|
|
1110
1396
|
### Updating the README
|
@@ -1149,6 +1435,8 @@ These resources and references may be useful throughout your learning process.
|
|
1149
1435
|
- [Vertex AI API Documentation](https://cloud.google.com/vertex-ai/docs/reference)
|
1150
1436
|
- [REST Documentation](https://cloud.google.com/vertex-ai/docs/reference/rest)
|
1151
1437
|
- [Get text embeddings](https://cloud.google.com/vertex-ai/generative-ai/docs/embeddings/get-text-embeddings)
|
1438
|
+
- [Use system instructions](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/prompts/system-instructions)
|
1439
|
+
- [Configure safety attributes](https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/configure-safety-attributes)
|
1152
1440
|
- [Google models](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/models)
|
1153
1441
|
- [Model versions and lifecycle](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/model-versioning)
|
1154
1442
|
- [Google DeepMind Gemini](https://deepmind.google/technologies/gemini/)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gemini-ai
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 4.
|
4
|
+
version: 4.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- gbaptista
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-07-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: event_stream_parser
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '2.
|
33
|
+
version: '2.10'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '2.
|
40
|
+
version: '2.10'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: faraday-typhoeus
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -94,6 +94,7 @@ extensions: []
|
|
94
94
|
extra_rdoc_files: []
|
95
95
|
files:
|
96
96
|
- ".gitignore"
|
97
|
+
- ".rspec"
|
97
98
|
- ".rubocop.yml"
|
98
99
|
- ".ruby-version"
|
99
100
|
- Gemfile
|