gemini-ai 3.1.3 → 3.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4c4cbfd3191a65f27ae580b99a8774309f552f288b5e2bbe339d761ad199615a
4
- data.tar.gz: 0616acf06021892ec8ef85088c8c0ae6f486e8c73d774e72ad9a6a961d02c398
3
+ metadata.gz: c726dcc65ecf4cfca2819e6f18da5fd1a3eb96e45888971881c7255631a2f074
4
+ data.tar.gz: 2d1617c38f987a40386b48b8465871e44f60bc2bd00dc4883704f91aa12aaab9
5
5
  SHA512:
6
- metadata.gz: e68c49731057a162bd9ada268c1343b166593acd74dccc6be4bdd59cdbdef2401400618073df7e24781e7e5d00f23bb277b77d083e168afe20959a6a5a937dcc
7
- data.tar.gz: 98bb928f6e8ee5e0bea7b9001868efd17079513da54fbf4a3084f1a75d0a07504220174d001ae4bde77fbedee083d3943e873ee6ad46760e8c34ee2794eed272
6
+ metadata.gz: c3356afb013f69f51c1dfd6f379da7ae615dbbc4750dff25b977bc70a4ccda428a4466cbf4ea60dd7341bbc634882b56d1d76a24c3993a449c97ae566c44daf1
7
+ data.tar.gz: 9412ff5d682105db69caf9cc98688d499f794f76231e741450ee6eebd2c0f49efaeca6376f3b45a424e070e717e702a347ce5cf5cf0eb27229dc8e10727cdb32
data/Gemfile CHANGED
@@ -7,5 +7,5 @@ gemspec
7
7
  group :test, :development do
8
8
  gem 'dotenv', '~> 2.8', '>= 2.8.1'
9
9
  gem 'pry-byebug', '~> 3.10', '>= 3.10.1'
10
- gem 'rubocop', '~> 1.58'
10
+ gem 'rubocop', '~> 1.60', '>= 1.60.1'
11
11
  end
data/Gemfile.lock CHANGED
@@ -1,10 +1,12 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- gemini-ai (3.1.3)
4
+ gemini-ai (3.2.0)
5
5
  event_stream_parser (~> 1.0)
6
6
  faraday (~> 2.9)
7
+ faraday-typhoeus (~> 1.1)
7
8
  googleauth (~> 1.8)
9
+ typhoeus (~> 1.4, >= 1.4.1)
8
10
 
9
11
  GEM
10
12
  remote: https://rubygems.org/
@@ -15,11 +17,17 @@ GEM
15
17
  byebug (11.1.3)
16
18
  coderay (1.1.3)
17
19
  dotenv (2.8.1)
20
+ ethon (0.16.0)
21
+ ffi (>= 1.15.0)
18
22
  event_stream_parser (1.0.0)
19
23
  faraday (2.9.0)
20
24
  faraday-net_http (>= 2.0, < 3.2)
21
25
  faraday-net_http (3.1.0)
22
26
  net-http
27
+ faraday-typhoeus (1.1.0)
28
+ faraday (~> 2.0)
29
+ typhoeus (~> 1.4)
30
+ ffi (1.16.3)
23
31
  google-cloud-env (2.1.0)
24
32
  faraday (>= 1.0, < 3.a)
25
33
  googleauth (1.9.1)
@@ -38,7 +46,7 @@ GEM
38
46
  uri
39
47
  os (1.1.4)
40
48
  parallel (1.24.0)
41
- parser (3.3.0.3)
49
+ parser (3.3.0.5)
42
50
  ast (~> 2.4.1)
43
51
  racc
44
52
  pry (0.14.2)
@@ -52,11 +60,11 @@ GEM
52
60
  rainbow (3.1.1)
53
61
  regexp_parser (2.9.0)
54
62
  rexml (3.2.6)
55
- rubocop (1.59.0)
63
+ rubocop (1.60.1)
56
64
  json (~> 2.3)
57
65
  language_server-protocol (>= 3.17.0)
58
66
  parallel (~> 1.10)
59
- parser (>= 3.2.2.4)
67
+ parser (>= 3.3.0.2)
60
68
  rainbow (>= 2.2.2, < 4.0)
61
69
  regexp_parser (>= 1.8, < 3.0)
62
70
  rexml (>= 3.2.5, < 4.0)
@@ -71,6 +79,8 @@ GEM
71
79
  faraday (>= 0.17.5, < 3.a)
72
80
  jwt (>= 1.5, < 3.0)
73
81
  multi_json (~> 1.10)
82
+ typhoeus (1.4.1)
83
+ ethon (>= 0.9.0)
74
84
  unicode-display_width (2.5.0)
75
85
  uri (0.13.0)
76
86
 
@@ -81,7 +91,7 @@ DEPENDENCIES
81
91
  dotenv (~> 2.8, >= 2.8.1)
82
92
  gemini-ai!
83
93
  pry-byebug (~> 3.10, >= 3.10.1)
84
- rubocop (~> 1.58)
94
+ rubocop (~> 1.60, >= 1.60.1)
85
95
 
86
96
  BUNDLED WITH
87
97
  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', '~> 3.1.3'
12
+ gem 'gemini-ai', '~> 3.2.0'
13
13
  ```
14
14
 
15
15
  ```ruby
@@ -79,6 +79,7 @@ Result:
79
79
  - [Option 2: Service Account Credentials File (Vertex AI API)](#option-2-service-account-credentials-file-vertex-ai-api)
80
80
  - [Option 3: Application Default Credentials (Vertex AI API)](#option-3-application-default-credentials-vertex-ai-api)
81
81
  - [Required Data](#required-data)
82
+ - [Custom Version](#custom-version)
82
83
  - [Usage](#usage)
83
84
  - [Client](#client)
84
85
  - [Methods](#methods)
@@ -97,6 +98,7 @@ Result:
97
98
  - [Tools (Functions) Calling](#tools-functions-calling)
98
99
  - [New Functionalities and APIs](#new-functionalities-and-apis)
99
100
  - [Request Options](#request-options)
101
+ - [Adapter](#adapter)
100
102
  - [Timeout](#timeout)
101
103
  - [Error Handling](#error-handling)
102
104
  - [Rescuing](#rescuing)
@@ -114,11 +116,11 @@ Result:
114
116
  ### Installing
115
117
 
116
118
  ```sh
117
- gem install gemini-ai -v 3.1.3
119
+ gem install gemini-ai -v 3.2.0
118
120
  ```
119
121
 
120
122
  ```sh
121
- gem 'gemini-ai', '~> 3.1.3'
123
+ gem 'gemini-ai', '~> 3.2.0'
122
124
  ```
123
125
 
124
126
  ### Credentials
@@ -270,6 +272,43 @@ You might want to explicitly set a Google Cloud Project ID, which you can do as
270
272
  }
271
273
  ```
272
274
 
275
+ ### Custom Version
276
+
277
+ By default, the gem uses the `v1` version of the APIs. You may want to use a different version:
278
+
279
+ ```ruby
280
+ # With an API key
281
+ client = Gemini.new(
282
+ credentials: {
283
+ service: 'generative-language-api',
284
+ api_key: ENV['GOOGLE_API_KEY'],
285
+ version: 'v1beta'
286
+ },
287
+ options: { model: 'gemini-pro', server_sent_events: true }
288
+ )
289
+
290
+ # With a Service Account Credentials File
291
+ client = Gemini.new(
292
+ credentials: {
293
+ service: 'vertex-ai-api',
294
+ file_path: 'google-credentials.json',
295
+ region: 'us-east4',
296
+ version: 'v1beta'
297
+ },
298
+ options: { model: 'gemini-pro', server_sent_events: true }
299
+ )
300
+
301
+ # With Application Default Credentials
302
+ client = Gemini.new(
303
+ credentials: {
304
+ service: 'vertex-ai-api',
305
+ region: 'us-east4',
306
+ version: 'v1beta'
307
+ },
308
+ options: { model: 'gemini-pro', server_sent_events: true }
309
+ )
310
+ ```
311
+
273
312
  ## Usage
274
313
 
275
314
  ### Client
@@ -876,6 +915,24 @@ result = client.request(
876
915
 
877
916
  ### Request Options
878
917
 
918
+ #### Adapter
919
+
920
+ To enable streaming, the gem uses [Faraday](https://github.com/lostisland/faraday) with the [Typhoeus](https://github.com/typhoeus/typhoeus) adapter by default.
921
+
922
+ You can use a different adapter if you want:
923
+
924
+ ```ruby
925
+ require 'faraday/net_http'
926
+
927
+ client = Gemini.new(
928
+ credentials: { service: 'vertex-ai-api', region: 'us-east4' },
929
+ options: {
930
+ model: 'gemini-pro',
931
+ connection: { adapter: :net_http }
932
+ }
933
+ )
934
+ ```
935
+
879
936
  #### Timeout
880
937
 
881
938
  You can set the maximum number of seconds to wait for the request to complete with the `timeout` option:
@@ -982,7 +1039,7 @@ gem build gemini-ai.gemspec
982
1039
 
983
1040
  gem signin
984
1041
 
985
- gem push gemini-ai-3.1.3.gem
1042
+ gem push gemini-ai-3.2.0.gem
986
1043
  ```
987
1044
 
988
1045
  ### Updating the README
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'event_stream_parser'
4
4
  require 'faraday'
5
+ require 'faraday/typhoeus'
5
6
  require 'json'
6
7
  require 'googleauth'
7
8
 
@@ -12,7 +13,17 @@ module Gemini
12
13
  class Client
13
14
  ALLOWED_REQUEST_OPTIONS = %i[timeout open_timeout read_timeout write_timeout].freeze
14
15
 
16
+ DEFAULT_FARADAY_ADAPTER = :typhoeus
17
+
18
+ DEFAULT_SERVICE_VERSION = 'v1'
19
+
15
20
  def initialize(config)
21
+ @service = config[:credentials][:service]
22
+
23
+ unless %w[vertex-ai-api generative-language-api].include?(@service)
24
+ raise Errors::UnsupportedServiceError, "Unsupported service: #{@service}"
25
+ end
26
+
16
27
  if config[:credentials][:api_key]
17
28
  @authentication = :api_key
18
29
  @api_key = config[:credentials][:api_key]
@@ -33,21 +44,21 @@ module Gemini
33
44
  raise Errors::MissingProjectIdError, 'Could not determine project_id, which is required.' if @project_id.nil?
34
45
  end
35
46
 
36
- @service = config[:credentials][:service]
47
+ @service_version = config.dig(:credentials, :version) || DEFAULT_SERVICE_VERSION
37
48
 
38
49
  @address = case @service
39
50
  when 'vertex-ai-api'
40
- "https://#{config[:credentials][:region]}-aiplatform.googleapis.com/v1/projects/#{@project_id}/locations/#{config[:credentials][:region]}/publishers/google/models/#{config[:options][:model]}"
51
+ "https://#{config[:credentials][:region]}-aiplatform.googleapis.com/#{@service_version}/projects/#{@project_id}/locations/#{config[:credentials][:region]}/publishers/google/models/#{config[:options][:model]}"
41
52
  when 'generative-language-api'
42
- "https://generativelanguage.googleapis.com/v1/models/#{config[:options][:model]}"
43
- else
44
- raise Errors::UnsupportedServiceError, "Unsupported service: #{@service}"
53
+ "https://generativelanguage.googleapis.com/#{@service_version}/models/#{config[:options][:model]}"
45
54
  end
46
55
 
47
56
  @server_sent_events = config.dig(:options, :server_sent_events)
48
57
 
49
58
  @request_options = config.dig(:options, :connection, :request)
50
59
 
60
+ @faraday_adapter = config.dig(:options, :connection, :adapter) || DEFAULT_FARADAY_ADAPTER
61
+
51
62
  @request_options = if @request_options.is_a?(Hash)
52
63
  @request_options.select do |key, _|
53
64
  ALLOWED_REQUEST_OPTIONS.include?(key)
@@ -87,6 +98,7 @@ module Gemini
87
98
  results = []
88
99
 
89
100
  response = Faraday.new(request: @request_options) do |faraday|
101
+ faraday.adapter @faraday_adapter
90
102
  faraday.response :raise_error
91
103
  end.post do |request|
92
104
  request.url url
@@ -98,6 +110,9 @@ module Gemini
98
110
  request.body = payload.to_json
99
111
 
100
112
  if server_sent_events_enabled
113
+
114
+ partial_json = ''
115
+
101
116
  parser = EventStreamParser::Parser.new
102
117
 
103
118
  request.options.on_data = proc do |chunk, bytes, env|
@@ -107,20 +122,27 @@ module Gemini
107
122
  end
108
123
 
109
124
  parser.feed(chunk) do |type, data, id, reconnection_time|
110
- parsed_data = safe_parse_json(data)
111
- result = {
112
- event: safe_parse_json(data),
113
- parsed: { type:, data:, id:, reconnection_time: },
114
- raw: { chunk:, bytes:, env: }
115
- }
125
+ partial_json += data
126
+
127
+ parsed_json = safe_parse_json(partial_json)
128
+
129
+ if parsed_json
130
+ result = {
131
+ event: parsed_json,
132
+ parsed: { type:, data:, id:, reconnection_time: },
133
+ raw: { chunk:, bytes:, env: }
134
+ }
135
+
136
+ callback.call(result[:event], result[:parsed], result[:raw]) unless callback.nil?
116
137
 
117
- callback.call(result[:event], result[:parsed], result[:raw]) unless callback.nil?
138
+ results << result
118
139
 
119
- results << result
140
+ partial_json = ''
120
141
 
121
- if parsed_data['candidates']
122
- parsed_data['candidates'].find do |candidate|
123
- !candidate['finishReason'].nil? && candidate['finishReason'] != ''
142
+ if parsed_json['candidates']
143
+ parsed_json['candidates'].find do |candidate|
144
+ !candidate['finishReason'].nil? && candidate['finishReason'] != ''
145
+ end
124
146
  end
125
147
  end
126
148
  end
@@ -136,9 +158,9 @@ module Gemini
136
158
  end
137
159
 
138
160
  def safe_parse_json(raw)
139
- raw.start_with?('{', '[') ? JSON.parse(raw) : raw
161
+ raw.to_s.lstrip.start_with?('{', '[') ? JSON.parse(raw) : nil
140
162
  rescue JSON::ParserError
141
- raw
163
+ nil
142
164
  end
143
165
  end
144
166
  end
data/gemini-ai.gemspec CHANGED
@@ -31,7 +31,9 @@ Gem::Specification.new do |spec|
31
31
 
32
32
  spec.add_dependency 'event_stream_parser', '~> 1.0'
33
33
  spec.add_dependency 'faraday', '~> 2.9'
34
+ spec.add_dependency 'faraday-typhoeus', '~> 1.1'
34
35
  spec.add_dependency 'googleauth', '~> 1.8'
36
+ spec.add_dependency 'typhoeus', '~> 1.4', '>= 1.4.1'
35
37
 
36
38
  spec.metadata['rubygems_mfa_required'] = 'true'
37
39
  end
data/static/gem.rb CHANGED
@@ -3,7 +3,7 @@
3
3
  module Gemini
4
4
  GEM = {
5
5
  name: 'gemini-ai',
6
- version: '3.1.3',
6
+ version: '3.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', '~> 3.1.3'
12
+ gem 'gemini-ai', '~> 3.2.0'
13
13
  ```
14
14
 
15
15
  ```ruby
@@ -77,11 +77,11 @@ Result:
77
77
  ### Installing
78
78
 
79
79
  ```sh
80
- gem install gemini-ai -v 3.1.3
80
+ gem install gemini-ai -v 3.2.0
81
81
  ```
82
82
 
83
83
  ```sh
84
- gem 'gemini-ai', '~> 3.1.3'
84
+ gem 'gemini-ai', '~> 3.2.0'
85
85
  ```
86
86
 
87
87
  ### Credentials
@@ -233,6 +233,43 @@ You might want to explicitly set a Google Cloud Project ID, which you can do as
233
233
  }
234
234
  ```
235
235
 
236
+ ### Custom Version
237
+
238
+ By default, the gem uses the `v1` version of the APIs. You may want to use a different version:
239
+
240
+ ```ruby
241
+ # With an API key
242
+ client = Gemini.new(
243
+ credentials: {
244
+ service: 'generative-language-api',
245
+ api_key: ENV['GOOGLE_API_KEY'],
246
+ version: 'v1beta'
247
+ },
248
+ options: { model: 'gemini-pro', server_sent_events: true }
249
+ )
250
+
251
+ # With a Service Account Credentials File
252
+ client = Gemini.new(
253
+ credentials: {
254
+ service: 'vertex-ai-api',
255
+ file_path: 'google-credentials.json',
256
+ region: 'us-east4',
257
+ version: 'v1beta'
258
+ },
259
+ options: { model: 'gemini-pro', server_sent_events: true }
260
+ )
261
+
262
+ # With Application Default Credentials
263
+ client = Gemini.new(
264
+ credentials: {
265
+ service: 'vertex-ai-api',
266
+ region: 'us-east4',
267
+ version: 'v1beta'
268
+ },
269
+ options: { model: 'gemini-pro', server_sent_events: true }
270
+ )
271
+ ```
272
+
236
273
  ## Usage
237
274
 
238
275
  ### Client
@@ -839,6 +876,24 @@ result = client.request(
839
876
 
840
877
  ### Request Options
841
878
 
879
+ #### Adapter
880
+
881
+ To enable streaming, the gem uses [Faraday](https://github.com/lostisland/faraday) with the [Typhoeus](https://github.com/typhoeus/typhoeus) adapter by default.
882
+
883
+ You can use a different adapter if you want:
884
+
885
+ ```ruby
886
+ require 'faraday/net_http'
887
+
888
+ client = Gemini.new(
889
+ credentials: { service: 'vertex-ai-api', region: 'us-east4' },
890
+ options: {
891
+ model: 'gemini-pro',
892
+ connection: { adapter: :net_http }
893
+ }
894
+ )
895
+ ```
896
+
842
897
  #### Timeout
843
898
 
844
899
  You can set the maximum number of seconds to wait for the request to complete with the `timeout` option:
@@ -945,7 +1000,7 @@ gem build gemini-ai.gemspec
945
1000
 
946
1001
  gem signin
947
1002
 
948
- gem push gemini-ai-3.1.3.gem
1003
+ gem push gemini-ai-3.2.0.gem
949
1004
  ```
950
1005
 
951
1006
  ### Updating the README
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: 3.1.3
4
+ version: 3.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - gbaptista
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-01-13 00:00:00.000000000 Z
11
+ date: 2024-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: event_stream_parser
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '2.9'
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday-typhoeus
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.1'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.1'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: googleauth
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -52,9 +66,29 @@ dependencies:
52
66
  - - "~>"
53
67
  - !ruby/object:Gem::Version
54
68
  version: '1.8'
69
+ - !ruby/object:Gem::Dependency
70
+ name: typhoeus
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.4'
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: 1.4.1
79
+ type: :runtime
80
+ prerelease: false
81
+ version_requirements: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - "~>"
84
+ - !ruby/object:Gem::Version
85
+ version: '1.4'
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: 1.4.1
55
89
  description: A Ruby Gem for interacting with Gemini through Vertex AI, Generative
56
90
  Language API, or AI Studio, Google's generative AI services.
57
- email:
91
+ email:
58
92
  executables: []
59
93
  extensions: []
60
94
  extra_rdoc_files: []
@@ -82,7 +116,7 @@ metadata:
82
116
  homepage_uri: https://github.com/gbaptista/gemini-ai
83
117
  source_code_uri: https://github.com/gbaptista/gemini-ai
84
118
  rubygems_mfa_required: 'true'
85
- post_install_message:
119
+ post_install_message:
86
120
  rdoc_options: []
87
121
  require_paths:
88
122
  - ports/dsl
@@ -98,7 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
98
132
  version: '0'
99
133
  requirements: []
100
134
  rubygems_version: 3.3.3
101
- signing_key:
135
+ signing_key:
102
136
  specification_version: 4
103
137
  summary: Interact with Google's Gemini AI.
104
138
  test_files: []