gemini-ai 1.0.0 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/Gemfile.lock +2 -2
- data/README.md +138 -16
- data/controllers/client.rb +42 -8
- data/static/gem.rb +2 -2
- data/template.md +134 -16
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3c6743cedf074b42aed890203de2d5db7697b6302c349c66bae9ac538bf2c680
|
4
|
+
data.tar.gz: ad0c8dd1ba69f58c0e37ed170bbd758231adea02245a63eb10fffa9a57fa2400
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 558e2a13c12931f6bfbde1d625cb38b7779e08d8eb5bc0a845861902f50fbcbc43b64c67348220f35316b4a37504daa220c334b0479fb5493973bc49ab34e77c
|
7
|
+
data.tar.gz: 2df9f463de540a3d76f86252bd178ca5638caca7e172d59ea244f5f12d04c86d22dcbe2e219049a33ad318ffecc9aaff07c28ffe30aa69ebc109befa08acbcca
|
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
gemini-ai (1.0
|
4
|
+
gemini-ai (2.1.0)
|
5
5
|
event_stream_parser (~> 1.0)
|
6
6
|
faraday (~> 2.7, >= 2.7.12)
|
7
7
|
googleauth (~> 1.9, >= 1.9.1)
|
@@ -36,7 +36,7 @@ GEM
|
|
36
36
|
method_source (1.0.0)
|
37
37
|
multi_json (1.15.0)
|
38
38
|
os (1.1.4)
|
39
|
-
parallel (1.
|
39
|
+
parallel (1.24.0)
|
40
40
|
parser (3.2.2.4)
|
41
41
|
ast (~> 2.4.1)
|
42
42
|
racc
|
data/README.md
CHANGED
@@ -1,21 +1,46 @@
|
|
1
1
|
# Gemini AI
|
2
2
|
|
3
|
-
A Ruby Gem for interacting with [Gemini](https://deepmind.google/technologies/gemini/) through [Vertex AI](https://cloud.google.com/vertex-ai), Google's generative AI
|
3
|
+
A Ruby Gem for interacting with [Gemini](https://deepmind.google/technologies/gemini/) through [Vertex AI](https://cloud.google.com/vertex-ai), [Generative Language API](https://ai.google.dev/api/rest), or [AI Studio](https://makersuite.google.com), Google's generative AI services.
|
4
|
+
|
5
|
+
![The logo shows a gemstone split into red and blue halves, symbolizing Ruby programming and Gemini AI. It's surrounded by a circuit-like design on a dark blue backdrop.](https://raw.githubusercontent.com/gbaptista/assets/main/gemini-ai/ruby-gemini-ai.png)
|
4
6
|
|
5
7
|
> _This Gem is designed to provide low-level access to Gemini, enabling people to build abstractions on top of it. If you are interested in more high-level abstractions or more user-friendly tools, you may want to consider [Nano Bots](https://github.com/icebaker/ruby-nano-bots) 💎 🤖._
|
6
8
|
|
7
9
|
## TL;DR and Quick Start
|
8
10
|
|
9
11
|
```ruby
|
10
|
-
gem 'gemini-ai', '~> 1.0'
|
12
|
+
gem 'gemini-ai', '~> 2.1.0'
|
11
13
|
```
|
12
14
|
|
13
15
|
```ruby
|
14
16
|
require 'gemini-ai'
|
15
17
|
|
18
|
+
# With an API key
|
19
|
+
client = Gemini.new(
|
20
|
+
credentials: {
|
21
|
+
service: 'generative-language-api',
|
22
|
+
api_key: ENV['GOOGLE_API_KEY']
|
23
|
+
},
|
24
|
+
options: { model: 'gemini-pro', stream: false }
|
25
|
+
)
|
26
|
+
|
27
|
+
# With a Service Account Credentials File
|
28
|
+
client = Gemini.new(
|
29
|
+
credentials: {
|
30
|
+
service: 'vertex-ai-api',
|
31
|
+
file_path: 'google-credentials.json',
|
32
|
+
region: 'us-east4'
|
33
|
+
},
|
34
|
+
options: { model: 'gemini-pro', stream: false }
|
35
|
+
)
|
36
|
+
|
37
|
+
# With Application Default Credentials
|
16
38
|
client = Gemini.new(
|
17
|
-
credentials: {
|
18
|
-
|
39
|
+
credentials: {
|
40
|
+
service: 'vertex-ai-api',
|
41
|
+
region: 'us-east4'
|
42
|
+
},
|
43
|
+
options: { model: 'gemini-pro', stream: false }
|
19
44
|
)
|
20
45
|
|
21
46
|
result = client.stream_generate_content({
|
@@ -48,7 +73,11 @@ Result:
|
|
48
73
|
- [TL;DR and Quick Start](#tldr-and-quick-start)
|
49
74
|
- [Index](#index)
|
50
75
|
- [Setup](#setup)
|
76
|
+
- [Installing](#installing)
|
51
77
|
- [Credentials](#credentials)
|
78
|
+
- [Option 1: API Key (Generative Language API)](#option-1-api-key-generative-language-api)
|
79
|
+
- [Option 2: Service Account Credentials File (Vertex AI API)](#option-2-service-account-credentials-file-vertex-ai-api)
|
80
|
+
- [Option 3: Application Default Credentials (Vertex AI API)](#option-3-application-default-credentials-vertex-ai-api)
|
52
81
|
- [Required Data](#required-data)
|
53
82
|
- [Usage](#usage)
|
54
83
|
- [Client](#client)
|
@@ -68,18 +97,31 @@ Result:
|
|
68
97
|
|
69
98
|
## Setup
|
70
99
|
|
100
|
+
### Installing
|
101
|
+
|
71
102
|
```sh
|
72
|
-
gem install gemini-ai -v 1.0
|
103
|
+
gem install gemini-ai -v 2.1.0
|
73
104
|
```
|
74
105
|
|
75
106
|
```sh
|
76
|
-
gem 'gemini-ai', '~> 1.0'
|
107
|
+
gem 'gemini-ai', '~> 2.1.0'
|
77
108
|
```
|
78
109
|
|
79
110
|
### Credentials
|
80
111
|
|
81
112
|
> ⚠️ DISCLAIMER: Be careful with what you are doing, and never trust others' code related to this. These commands and instructions alter the level of access to your Google Cloud Account, and running them naively can lead to security risks as well as financial risks. People with access to your account can use it to steal data or incur charges. Run these commands at your own responsibility and due diligence; expect no warranties from the contributors of this project.
|
82
113
|
|
114
|
+
#### Option 1: API Key (Generative Language API)
|
115
|
+
|
116
|
+
You need a [Google Cloud](https://console.cloud.google.com) [_Project_](https://cloud.google.com/resource-manager/docs/creating-managing-projects), and then you can generate an API Key through the Google Cloud Console [here](https://console.cloud.google.com/apis/credentials).
|
117
|
+
|
118
|
+
You also need to enable the _Generative Language API_ service in your Google Cloud Console, which can be done [here](https://console.cloud.google.com/apis/library/generativelanguage.googleapis.com).
|
119
|
+
|
120
|
+
|
121
|
+
Alternatively, you can generate an API Key through _Google AI Studio_ [here](https://makersuite.google.com/app/apikey). However, this approach will automatically create a project for you in your Google Cloud Account.
|
122
|
+
|
123
|
+
#### Option 2: Service Account Credentials File (Vertex AI API)
|
124
|
+
|
83
125
|
You need a [Google Cloud](https://console.cloud.google.com) [_Project_](https://cloud.google.com/resource-manager/docs/creating-managing-projects) and a [_Service Account_](https://cloud.google.com/iam/docs/service-account-overview) to use [Vertex AI](https://cloud.google.com/vertex-ai) API.
|
84
126
|
|
85
127
|
After creating them, you need to enable the Vertex AI API for your project by clicking `Enable` here: [Vertex AI API](https://console.cloud.google.com/apis/library/aiplatform.googleapis.com).
|
@@ -132,16 +174,56 @@ gcloud projects add-iam-policy-binding PROJECT_ID \
|
|
132
174
|
--role='roles/ml.admin'
|
133
175
|
```
|
134
176
|
|
135
|
-
|
177
|
+
#### Option 3: Application Default Credentials (Vertex AI API)
|
178
|
+
|
179
|
+
Similar to [Option 2](#option-2-service-account-credentials-file-vertex-ai-api), but you don't need to download a `google-credentials.json`. [_Application Default Credentials_](https://cloud.google.com/docs/authentication/application-default-credentials) automatically find credentials based on the application environment.
|
180
|
+
|
181
|
+
For local development, you can generate your default credentials using the [gcloud CLI](https://cloud.google.com/sdk/gcloud) as follows:
|
182
|
+
|
183
|
+
```sh
|
184
|
+
gcloud auth application-default login
|
185
|
+
```
|
186
|
+
|
187
|
+
For more details about alternative methods and different environments, check the official documentation:
|
188
|
+
[Set up Application Default Credentials](https://cloud.google.com/docs/authentication/provide-credentials-adc)
|
136
189
|
|
137
190
|
#### Required Data
|
138
191
|
|
139
|
-
After
|
192
|
+
After choosing an option, you should have all the necessary data and access to use Gemini.
|
193
|
+
|
194
|
+
**Option 1**, for API Key:
|
195
|
+
|
196
|
+
```ruby
|
197
|
+
{
|
198
|
+
service: 'generative-language-api',
|
199
|
+
api_key: 'GOOGLE_API_KEY'
|
200
|
+
}
|
201
|
+
```
|
202
|
+
|
203
|
+
Remember that hardcoding your API key in code is unsafe; it's preferable to use environment variables:
|
140
204
|
|
141
205
|
```ruby
|
142
206
|
{
|
207
|
+
service: 'generative-language-api',
|
208
|
+
api_key: ENV['GOOGLE_API_KEY']
|
209
|
+
}
|
210
|
+
```
|
211
|
+
|
212
|
+
**Option 2**: For the Service Account, provide a `google-credentials.json` file and a `REGION`:
|
213
|
+
|
214
|
+
```ruby
|
215
|
+
{
|
216
|
+
service: 'vertex-ai-api',
|
143
217
|
file_path: 'google-credentials.json',
|
144
|
-
|
218
|
+
region: 'us-east4'
|
219
|
+
}
|
220
|
+
```
|
221
|
+
|
222
|
+
**Option 3**: For _Application Default Credentials_, omit both the `api_key` and the `file_path`:
|
223
|
+
|
224
|
+
```ruby
|
225
|
+
{
|
226
|
+
service: 'vertex-ai-api',
|
145
227
|
region: 'us-east4'
|
146
228
|
}
|
147
229
|
```
|
@@ -160,6 +242,15 @@ Tokyo, Japan (asia-northeast1)
|
|
160
242
|
|
161
243
|
You can follow here if new regions are available: [Gemini API](https://cloud.google.com/vertex-ai/docs/generative-ai/model-reference/gemini)
|
162
244
|
|
245
|
+
You might want to explicitly set a Google Cloud Project ID, which you can do as follows:
|
246
|
+
|
247
|
+
```ruby
|
248
|
+
{
|
249
|
+
service: 'vertex-ai-api',
|
250
|
+
project_id: 'PROJECT_ID'
|
251
|
+
}
|
252
|
+
```
|
253
|
+
|
163
254
|
## Usage
|
164
255
|
|
165
256
|
### Client
|
@@ -169,9 +260,32 @@ Create a new client:
|
|
169
260
|
```ruby
|
170
261
|
require 'gemini-ai'
|
171
262
|
|
263
|
+
# With an API key
|
264
|
+
client = Gemini.new(
|
265
|
+
credentials: {
|
266
|
+
service: 'generative-language-api',
|
267
|
+
api_key: ENV['GOOGLE_API_KEY']
|
268
|
+
},
|
269
|
+
options: { model: 'gemini-pro', stream: false }
|
270
|
+
)
|
271
|
+
|
272
|
+
# With a Service Account Credentials File
|
172
273
|
client = Gemini.new(
|
173
|
-
credentials: {
|
174
|
-
|
274
|
+
credentials: {
|
275
|
+
service: 'vertex-ai-api',
|
276
|
+
file_path: 'google-credentials.json',
|
277
|
+
region: 'us-east4'
|
278
|
+
},
|
279
|
+
options: { model: 'gemini-pro', stream: false }
|
280
|
+
)
|
281
|
+
|
282
|
+
# With Application Default Credentials
|
283
|
+
client = Gemini.new(
|
284
|
+
credentials: {
|
285
|
+
service: 'vertex-ai-api',
|
286
|
+
region: 'us-east4'
|
287
|
+
},
|
288
|
+
options: { model: 'gemini-pro', stream: false }
|
175
289
|
)
|
176
290
|
```
|
177
291
|
|
@@ -210,8 +324,8 @@ Result:
|
|
210
324
|
You can set up the client to use streaming for all supported endpoints:
|
211
325
|
```ruby
|
212
326
|
client = Gemini.new(
|
213
|
-
credentials: {
|
214
|
-
|
327
|
+
credentials: { ... },
|
328
|
+
options: { model: 'gemini-pro', stream: true }
|
215
329
|
)
|
216
330
|
```
|
217
331
|
|
@@ -328,7 +442,7 @@ Result:
|
|
328
442
|
|
329
443
|
### Tools (Functions) Calling
|
330
444
|
|
331
|
-
> As of the writing of this README, only the `gemini-pro` model [supports](https://cloud.google.com/vertex-ai/docs/generative-ai/multimodal/function-calling#supported_models) tools (functions) calls.
|
445
|
+
> 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.
|
332
446
|
|
333
447
|
You can provide specifications for [tools (functions)](https://cloud.google.com/vertex-ai/docs/generative-ai/multimodal/function-calling) using [JSON Schema](https://json-schema.org) to generate potential calls to them:
|
334
448
|
|
@@ -500,11 +614,17 @@ gem build gemini-ai.gemspec
|
|
500
614
|
|
501
615
|
gem signin
|
502
616
|
|
503
|
-
gem push gemini-ai-1.0.
|
617
|
+
gem push gemini-ai-2.1.0.gem
|
504
618
|
```
|
505
619
|
|
506
620
|
### Updating the README
|
507
621
|
|
622
|
+
Install [Babashka](https://babashka.org):
|
623
|
+
|
624
|
+
```sh
|
625
|
+
curl -s https://raw.githubusercontent.com/babashka/babashka/master/install | sudo bash
|
626
|
+
```
|
627
|
+
|
508
628
|
Update the `template.md` file and then:
|
509
629
|
|
510
630
|
```sh
|
@@ -532,6 +652,8 @@ mlp README.md -p 8076
|
|
532
652
|
|
533
653
|
These resources and references may be useful throughout your learning process.
|
534
654
|
|
655
|
+
- [Google AI for Developers](https://ai.google.dev)
|
656
|
+
- [Get started with the Gemini API ](https://ai.google.dev/docs)
|
535
657
|
- [Getting Started with the Vertex AI Gemini API with cURL](https://github.com/GoogleCloudPlatform/generative-ai/blob/main/gemini/getting-started/intro_gemini_curl.ipynb)
|
536
658
|
- [Gemini API Documentation](https://cloud.google.com/vertex-ai/docs/generative-ai/model-reference/gemini)
|
537
659
|
- [Vertex AI API Documentation](https://cloud.google.com/vertex-ai/docs/reference)
|
@@ -544,4 +666,4 @@ These resources and references may be useful throughout your learning process.
|
|
544
666
|
|
545
667
|
This is not an official Google project, nor is it affiliated with Google in any way.
|
546
668
|
|
547
|
-
|
669
|
+
This software is distributed under the [MIT License](https://github.com/gbaptista/gemini-ai/blob/main/LICENSE). This license includes a disclaimer of warranty. Moreover, the authors assume no responsibility for any damage or costs that may result from using this project. Use the Gemini AI Ruby Gem at your own risk.
|
data/controllers/client.rb
CHANGED
@@ -9,14 +9,40 @@ module Gemini
|
|
9
9
|
module Controllers
|
10
10
|
class Client
|
11
11
|
def initialize(config)
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
12
|
+
if config[:credentials][:api_key]
|
13
|
+
@authentication = :api_key
|
14
|
+
@api_key = config[:credentials][:api_key]
|
15
|
+
elsif config[:credentials][:file_path]
|
16
|
+
@authentication = :service_account
|
17
|
+
@authorizer = ::Google::Auth::ServiceAccountCredentials.make_creds(
|
18
|
+
json_key_io: File.open(config[:credentials][:file_path]),
|
19
|
+
scope: 'https://www.googleapis.com/auth/cloud-platform'
|
20
|
+
)
|
21
|
+
else
|
22
|
+
@authentication = :default_credentials
|
23
|
+
@authorizer = ::Google::Auth.get_application_default
|
24
|
+
end
|
25
|
+
|
26
|
+
if @authentication == :service_account || @authentication == :default_credentials
|
27
|
+
@project_id = if config[:credentials][:project_id].nil?
|
28
|
+
@authorizer.project_id || @authorizer.quota_project_id
|
29
|
+
else
|
30
|
+
config[:credentials][:project_id]
|
31
|
+
end
|
32
|
+
|
33
|
+
raise StandardError, 'Could not determine project_id, which is required.' if @project_id.nil?
|
34
|
+
end
|
16
35
|
|
17
|
-
@address =
|
36
|
+
@address = case config[:credentials][:service]
|
37
|
+
when 'vertex-ai-api'
|
38
|
+
"https://#{config[:credentials][:region]}-aiplatform.googleapis.com/v1/projects/#{@project_id}/locations/#{config[:credentials][:region]}/publishers/google/models/#{config[:options][:model]}"
|
39
|
+
when 'generative-language-api'
|
40
|
+
"https://generativelanguage.googleapis.com/v1/models/#{config[:options][:model]}"
|
41
|
+
else
|
42
|
+
raise StandardError, "Unsupported service: #{config[:credentials][:service]}"
|
43
|
+
end
|
18
44
|
|
19
|
-
@stream = config[:
|
45
|
+
@stream = config[:options][:stream]
|
20
46
|
end
|
21
47
|
|
22
48
|
def stream_generate_content(payload, stream: nil, &callback)
|
@@ -26,7 +52,12 @@ module Gemini
|
|
26
52
|
def request(path, payload, stream: nil, &callback)
|
27
53
|
stream_enabled = stream.nil? ? @stream : stream
|
28
54
|
url = "#{@address}:#{path}"
|
29
|
-
|
55
|
+
params = []
|
56
|
+
|
57
|
+
params << 'alt=sse' if stream_enabled
|
58
|
+
params << "key=#{@api_key}" if @authentication == :api_key
|
59
|
+
|
60
|
+
url += "?#{params.join('&')}" if params.size.positive?
|
30
61
|
|
31
62
|
if !callback.nil? && !stream_enabled
|
32
63
|
raise StandardError, 'You are trying to use a block without stream enabled."'
|
@@ -37,7 +68,10 @@ module Gemini
|
|
37
68
|
response = Faraday.new.post do |request|
|
38
69
|
request.url url
|
39
70
|
request.headers['Content-Type'] = 'application/json'
|
40
|
-
|
71
|
+
if @authentication == :service_account || @authentication == :default_credentials
|
72
|
+
request.headers['Authorization'] = "Bearer #{@authorizer.fetch_access_token!['access_token']}"
|
73
|
+
end
|
74
|
+
|
41
75
|
request.body = payload.to_json
|
42
76
|
|
43
77
|
if stream_enabled
|
data/static/gem.rb
CHANGED
@@ -3,10 +3,10 @@
|
|
3
3
|
module Gemini
|
4
4
|
GEM = {
|
5
5
|
name: 'gemini-ai',
|
6
|
-
version: '1.0
|
6
|
+
version: '2.1.0',
|
7
7
|
author: 'gbaptista',
|
8
8
|
summary: "Interact with Google's Gemini AI.",
|
9
|
-
description: "A Ruby Gem for interacting with Gemini through Vertex AI, Google's generative AI
|
9
|
+
description: "A Ruby Gem for interacting with Gemini through Vertex AI, Generative Language API, or AI Studio, Google's generative AI services.",
|
10
10
|
github: 'https://github.com/gbaptista/gemini-ai',
|
11
11
|
gem_server: 'https://rubygems.org',
|
12
12
|
license: 'MIT',
|
data/template.md
CHANGED
@@ -1,21 +1,46 @@
|
|
1
1
|
# Gemini AI
|
2
2
|
|
3
|
-
A Ruby Gem for interacting with [Gemini](https://deepmind.google/technologies/gemini/) through [Vertex AI](https://cloud.google.com/vertex-ai), Google's generative AI
|
3
|
+
A Ruby Gem for interacting with [Gemini](https://deepmind.google/technologies/gemini/) through [Vertex AI](https://cloud.google.com/vertex-ai), [Generative Language API](https://ai.google.dev/api/rest), or [AI Studio](https://makersuite.google.com), Google's generative AI services.
|
4
|
+
|
5
|
+
![The logo shows a gemstone split into red and blue halves, symbolizing Ruby programming and Gemini AI. It's surrounded by a circuit-like design on a dark blue backdrop.](https://raw.githubusercontent.com/gbaptista/assets/main/gemini-ai/ruby-gemini-ai.png)
|
4
6
|
|
5
7
|
> _This Gem is designed to provide low-level access to Gemini, enabling people to build abstractions on top of it. If you are interested in more high-level abstractions or more user-friendly tools, you may want to consider [Nano Bots](https://github.com/icebaker/ruby-nano-bots) 💎 🤖._
|
6
8
|
|
7
9
|
## TL;DR and Quick Start
|
8
10
|
|
9
11
|
```ruby
|
10
|
-
gem 'gemini-ai', '~> 1.0'
|
12
|
+
gem 'gemini-ai', '~> 2.1.0'
|
11
13
|
```
|
12
14
|
|
13
15
|
```ruby
|
14
16
|
require 'gemini-ai'
|
15
17
|
|
18
|
+
# With an API key
|
19
|
+
client = Gemini.new(
|
20
|
+
credentials: {
|
21
|
+
service: 'generative-language-api',
|
22
|
+
api_key: ENV['GOOGLE_API_KEY']
|
23
|
+
},
|
24
|
+
options: { model: 'gemini-pro', stream: false }
|
25
|
+
)
|
26
|
+
|
27
|
+
# With a Service Account Credentials File
|
28
|
+
client = Gemini.new(
|
29
|
+
credentials: {
|
30
|
+
service: 'vertex-ai-api',
|
31
|
+
file_path: 'google-credentials.json',
|
32
|
+
region: 'us-east4'
|
33
|
+
},
|
34
|
+
options: { model: 'gemini-pro', stream: false }
|
35
|
+
)
|
36
|
+
|
37
|
+
# With Application Default Credentials
|
16
38
|
client = Gemini.new(
|
17
|
-
credentials: {
|
18
|
-
|
39
|
+
credentials: {
|
40
|
+
service: 'vertex-ai-api',
|
41
|
+
region: 'us-east4'
|
42
|
+
},
|
43
|
+
options: { model: 'gemini-pro', stream: false }
|
19
44
|
)
|
20
45
|
|
21
46
|
result = client.stream_generate_content({
|
@@ -49,18 +74,31 @@ Result:
|
|
49
74
|
|
50
75
|
## Setup
|
51
76
|
|
77
|
+
### Installing
|
78
|
+
|
52
79
|
```sh
|
53
|
-
gem install gemini-ai -v 1.0
|
80
|
+
gem install gemini-ai -v 2.1.0
|
54
81
|
```
|
55
82
|
|
56
83
|
```sh
|
57
|
-
gem 'gemini-ai', '~> 1.0'
|
84
|
+
gem 'gemini-ai', '~> 2.1.0'
|
58
85
|
```
|
59
86
|
|
60
87
|
### Credentials
|
61
88
|
|
62
89
|
> ⚠️ DISCLAIMER: Be careful with what you are doing, and never trust others' code related to this. These commands and instructions alter the level of access to your Google Cloud Account, and running them naively can lead to security risks as well as financial risks. People with access to your account can use it to steal data or incur charges. Run these commands at your own responsibility and due diligence; expect no warranties from the contributors of this project.
|
63
90
|
|
91
|
+
#### Option 1: API Key (Generative Language API)
|
92
|
+
|
93
|
+
You need a [Google Cloud](https://console.cloud.google.com) [_Project_](https://cloud.google.com/resource-manager/docs/creating-managing-projects), and then you can generate an API Key through the Google Cloud Console [here](https://console.cloud.google.com/apis/credentials).
|
94
|
+
|
95
|
+
You also need to enable the _Generative Language API_ service in your Google Cloud Console, which can be done [here](https://console.cloud.google.com/apis/library/generativelanguage.googleapis.com).
|
96
|
+
|
97
|
+
|
98
|
+
Alternatively, you can generate an API Key through _Google AI Studio_ [here](https://makersuite.google.com/app/apikey). However, this approach will automatically create a project for you in your Google Cloud Account.
|
99
|
+
|
100
|
+
#### Option 2: Service Account Credentials File (Vertex AI API)
|
101
|
+
|
64
102
|
You need a [Google Cloud](https://console.cloud.google.com) [_Project_](https://cloud.google.com/resource-manager/docs/creating-managing-projects) and a [_Service Account_](https://cloud.google.com/iam/docs/service-account-overview) to use [Vertex AI](https://cloud.google.com/vertex-ai) API.
|
65
103
|
|
66
104
|
After creating them, you need to enable the Vertex AI API for your project by clicking `Enable` here: [Vertex AI API](https://console.cloud.google.com/apis/library/aiplatform.googleapis.com).
|
@@ -113,16 +151,56 @@ gcloud projects add-iam-policy-binding PROJECT_ID \
|
|
113
151
|
--role='roles/ml.admin'
|
114
152
|
```
|
115
153
|
|
116
|
-
|
154
|
+
#### Option 3: Application Default Credentials (Vertex AI API)
|
155
|
+
|
156
|
+
Similar to [Option 2](#option-2-service-account-credentials-file-vertex-ai-api), but you don't need to download a `google-credentials.json`. [_Application Default Credentials_](https://cloud.google.com/docs/authentication/application-default-credentials) automatically find credentials based on the application environment.
|
157
|
+
|
158
|
+
For local development, you can generate your default credentials using the [gcloud CLI](https://cloud.google.com/sdk/gcloud) as follows:
|
159
|
+
|
160
|
+
```sh
|
161
|
+
gcloud auth application-default login
|
162
|
+
```
|
163
|
+
|
164
|
+
For more details about alternative methods and different environments, check the official documentation:
|
165
|
+
[Set up Application Default Credentials](https://cloud.google.com/docs/authentication/provide-credentials-adc)
|
117
166
|
|
118
167
|
#### Required Data
|
119
168
|
|
120
|
-
After
|
169
|
+
After choosing an option, you should have all the necessary data and access to use Gemini.
|
170
|
+
|
171
|
+
**Option 1**, for API Key:
|
172
|
+
|
173
|
+
```ruby
|
174
|
+
{
|
175
|
+
service: 'generative-language-api',
|
176
|
+
api_key: 'GOOGLE_API_KEY'
|
177
|
+
}
|
178
|
+
```
|
179
|
+
|
180
|
+
Remember that hardcoding your API key in code is unsafe; it's preferable to use environment variables:
|
121
181
|
|
122
182
|
```ruby
|
123
183
|
{
|
184
|
+
service: 'generative-language-api',
|
185
|
+
api_key: ENV['GOOGLE_API_KEY']
|
186
|
+
}
|
187
|
+
```
|
188
|
+
|
189
|
+
**Option 2**: For the Service Account, provide a `google-credentials.json` file and a `REGION`:
|
190
|
+
|
191
|
+
```ruby
|
192
|
+
{
|
193
|
+
service: 'vertex-ai-api',
|
124
194
|
file_path: 'google-credentials.json',
|
125
|
-
|
195
|
+
region: 'us-east4'
|
196
|
+
}
|
197
|
+
```
|
198
|
+
|
199
|
+
**Option 3**: For _Application Default Credentials_, omit both the `api_key` and the `file_path`:
|
200
|
+
|
201
|
+
```ruby
|
202
|
+
{
|
203
|
+
service: 'vertex-ai-api',
|
126
204
|
region: 'us-east4'
|
127
205
|
}
|
128
206
|
```
|
@@ -141,6 +219,15 @@ Tokyo, Japan (asia-northeast1)
|
|
141
219
|
|
142
220
|
You can follow here if new regions are available: [Gemini API](https://cloud.google.com/vertex-ai/docs/generative-ai/model-reference/gemini)
|
143
221
|
|
222
|
+
You might want to explicitly set a Google Cloud Project ID, which you can do as follows:
|
223
|
+
|
224
|
+
```ruby
|
225
|
+
{
|
226
|
+
service: 'vertex-ai-api',
|
227
|
+
project_id: 'PROJECT_ID'
|
228
|
+
}
|
229
|
+
```
|
230
|
+
|
144
231
|
## Usage
|
145
232
|
|
146
233
|
### Client
|
@@ -150,9 +237,32 @@ Create a new client:
|
|
150
237
|
```ruby
|
151
238
|
require 'gemini-ai'
|
152
239
|
|
240
|
+
# With an API key
|
241
|
+
client = Gemini.new(
|
242
|
+
credentials: {
|
243
|
+
service: 'generative-language-api',
|
244
|
+
api_key: ENV['GOOGLE_API_KEY']
|
245
|
+
},
|
246
|
+
options: { model: 'gemini-pro', stream: false }
|
247
|
+
)
|
248
|
+
|
249
|
+
# With a Service Account Credentials File
|
153
250
|
client = Gemini.new(
|
154
|
-
credentials: {
|
155
|
-
|
251
|
+
credentials: {
|
252
|
+
service: 'vertex-ai-api',
|
253
|
+
file_path: 'google-credentials.json',
|
254
|
+
region: 'us-east4'
|
255
|
+
},
|
256
|
+
options: { model: 'gemini-pro', stream: false }
|
257
|
+
)
|
258
|
+
|
259
|
+
# With Application Default Credentials
|
260
|
+
client = Gemini.new(
|
261
|
+
credentials: {
|
262
|
+
service: 'vertex-ai-api',
|
263
|
+
region: 'us-east4'
|
264
|
+
},
|
265
|
+
options: { model: 'gemini-pro', stream: false }
|
156
266
|
)
|
157
267
|
```
|
158
268
|
|
@@ -191,8 +301,8 @@ Result:
|
|
191
301
|
You can set up the client to use streaming for all supported endpoints:
|
192
302
|
```ruby
|
193
303
|
client = Gemini.new(
|
194
|
-
credentials: {
|
195
|
-
|
304
|
+
credentials: { ... },
|
305
|
+
options: { model: 'gemini-pro', stream: true }
|
196
306
|
)
|
197
307
|
```
|
198
308
|
|
@@ -309,7 +419,7 @@ Result:
|
|
309
419
|
|
310
420
|
### Tools (Functions) Calling
|
311
421
|
|
312
|
-
> As of the writing of this README, only the `gemini-pro` model [supports](https://cloud.google.com/vertex-ai/docs/generative-ai/multimodal/function-calling#supported_models) tools (functions) calls.
|
422
|
+
> 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.
|
313
423
|
|
314
424
|
You can provide specifications for [tools (functions)](https://cloud.google.com/vertex-ai/docs/generative-ai/multimodal/function-calling) using [JSON Schema](https://json-schema.org) to generate potential calls to them:
|
315
425
|
|
@@ -481,11 +591,17 @@ gem build gemini-ai.gemspec
|
|
481
591
|
|
482
592
|
gem signin
|
483
593
|
|
484
|
-
gem push gemini-ai-1.0.
|
594
|
+
gem push gemini-ai-2.1.0.gem
|
485
595
|
```
|
486
596
|
|
487
597
|
### Updating the README
|
488
598
|
|
599
|
+
Install [Babashka](https://babashka.org):
|
600
|
+
|
601
|
+
```sh
|
602
|
+
curl -s https://raw.githubusercontent.com/babashka/babashka/master/install | sudo bash
|
603
|
+
```
|
604
|
+
|
489
605
|
Update the `template.md` file and then:
|
490
606
|
|
491
607
|
```sh
|
@@ -513,6 +629,8 @@ mlp README.md -p 8076
|
|
513
629
|
|
514
630
|
These resources and references may be useful throughout your learning process.
|
515
631
|
|
632
|
+
- [Google AI for Developers](https://ai.google.dev)
|
633
|
+
- [Get started with the Gemini API ](https://ai.google.dev/docs)
|
516
634
|
- [Getting Started with the Vertex AI Gemini API with cURL](https://github.com/GoogleCloudPlatform/generative-ai/blob/main/gemini/getting-started/intro_gemini_curl.ipynb)
|
517
635
|
- [Gemini API Documentation](https://cloud.google.com/vertex-ai/docs/generative-ai/model-reference/gemini)
|
518
636
|
- [Vertex AI API Documentation](https://cloud.google.com/vertex-ai/docs/reference)
|
@@ -525,4 +643,4 @@ These resources and references may be useful throughout your learning process.
|
|
525
643
|
|
526
644
|
This is not an official Google project, nor is it affiliated with Google in any way.
|
527
645
|
|
528
|
-
|
646
|
+
This software is distributed under the [MIT License](https://github.com/gbaptista/gemini-ai/blob/main/LICENSE). This license includes a disclaimer of warranty. Moreover, the authors assume no responsibility for any damage or costs that may result from using this project. Use the Gemini AI Ruby Gem at your own risk.
|
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: 1.0
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- gbaptista
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-12-
|
11
|
+
date: 2023-12-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: event_stream_parser
|
@@ -64,8 +64,8 @@ dependencies:
|
|
64
64
|
- - ">="
|
65
65
|
- !ruby/object:Gem::Version
|
66
66
|
version: 1.9.1
|
67
|
-
description: A Ruby Gem for interacting with Gemini through Vertex AI,
|
68
|
-
AI
|
67
|
+
description: A Ruby Gem for interacting with Gemini through Vertex AI, Generative
|
68
|
+
Language API, or AI Studio, Google's generative AI services.
|
69
69
|
email:
|
70
70
|
executables: []
|
71
71
|
extensions: []
|
@@ -107,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
107
107
|
- !ruby/object:Gem::Version
|
108
108
|
version: '0'
|
109
109
|
requirements: []
|
110
|
-
rubygems_version: 3.
|
110
|
+
rubygems_version: 3.3.3
|
111
111
|
signing_key:
|
112
112
|
specification_version: 4
|
113
113
|
summary: Interact with Google's Gemini AI.
|