omniai-google 2.2.0 → 2.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2c0e81a12702acac4c59da29e174f77c053d068856063a0752e7f1e86dfa9870
4
- data.tar.gz: 05e0cc7cc84038408a94769eb8d1b30c5fdbccd3f4ee21847d50721d64e8e41f
3
+ metadata.gz: b09fe2dc85e70bef186e70d5e6da204bf003fab07907147c9de01fcc86cfe5e2
4
+ data.tar.gz: 2869a79e69a256b7420eccc72f33f9cabae2683d12c8f6fc8911fdbfd2e9ccbd
5
5
  SHA512:
6
- metadata.gz: 769fd0c52d6e83fb4bd90e5075e0cc332aca7deb6c56b16808175a0abf0cef97cef55c8991702550747e1b0672e0aec057ad9c8b98c94065f8943ea4a40904d9
7
- data.tar.gz: 8b4f33b903fb5acc6744a85869f8be69113c3b7f0c4b5e0150aed8283aa2747a5c174e564a956963f0d152e27177ff159aa36776bedf7ade83f4fb5bb89f6382
6
+ metadata.gz: a9b7cce4e8fb35a74e7b03e63a819a4bec124b07d232931e5ceb52ddab3b500b10d881edec565e73c1d92c1657cc6ea8903683484092933d219b93458e054591
7
+ data.tar.gz: 7b3f222ddf9d002b9467ed9a54a177257c6981a1d6f70fbaaddecce939920e5d5f4b4c9fa419bd1cc8b3b0bb8c03d39c37e59ed2ba70e5ccf523d72a5a3a3516
data/README.md CHANGED
@@ -27,21 +27,66 @@ client = OmniAI::Google::Client.new
27
27
  A client may also be passed the following options:
28
28
 
29
29
  - `api_key` (required - default is `ENV['GOOGLE_API_KEY']`)
30
+ - `credentials` (optional)
30
31
  - `host` (optional)
31
32
  - `version` (optional - options are `v1` or `v1beta`)
32
33
 
33
34
  ### Configuration
34
35
 
35
- Global configuration is supported for the following options:
36
+ Vertex AI and Google AI offer different options for interacting w/ Google's AI APIs. Checkout the [Vertex AI and Google AI differences](https://cloud.google.com/vertex-ai/generative-ai/docs/overview#how-gemini-vertex-different-gemini-aistudio) to determine which option best fits your requirements.
37
+
38
+ #### Authentication
39
+
40
+ **w/ `api_key`**
41
+
42
+ The quickest way to authenticate (available if using Google AI) is by using an API key:
36
43
 
37
44
  ```ruby
38
45
  OmniAI::Google.configure do |config|
39
46
  config.api_key = 'sk-...' # default: ENV['GOOGLE_API_KEY']
40
- config.host = '...' # default: 'https://generativelanguage.googleapis.com'
41
- config.version = OmniAI::Google::Config::Version::BETA # either 'v1' or 'v1beta'
42
47
  end
43
48
  ```
44
49
 
50
+ **w/ `credentials`**
51
+
52
+ An alternative approach for authentication (required if using Vertex AI) is to use credentials directly:
53
+
54
+ ```ruby
55
+ require 'googleauth'
56
+
57
+ credentials = Google::Auth::ServiceAccountCredentials.make_creds(
58
+ json_key_io: File.open('credentials.json'),
59
+ scope: 'https://www.googleapis.com/auth/cloud-platform'
60
+ )
61
+
62
+ OmniAI::Google.configure do |config|
63
+ config.credentials = credentials
64
+ end
65
+ ```
66
+
67
+ #### Host
68
+
69
+ The host (defaults to `https://generativelanguage.googleapis.com`) may be changed (required if using Vertex AI) using:
70
+
71
+ ```ruby
72
+ OmniAI::Google.configure do |config|
73
+ config.host = 'https://us-east4-aiplatform.googleapis.com' # see https://cloud.google.com/vertex-ai/docs/general/locations
74
+ end
75
+ ```
76
+
77
+ #### Version
78
+
79
+ The version (defaults to `v1beta`) may be changed using:
80
+
81
+ ```ruby
82
+ OmniAI::Google.configure do |config|
83
+ # ...
84
+ config.version = OmniAI::Google::Config::Version::STABLE # see https://ai.google.dev/gemini-api/docs/api-versions
85
+ end
86
+ ```
87
+
88
+ _The default API version is configured to **v1beta** instead of **v1** due to various missing features in **v1**._
89
+
45
90
  ### Chat
46
91
 
47
92
  A chat completion is generated by passing in a simple text prompt:
@@ -117,7 +117,7 @@ module OmniAI
117
117
 
118
118
  # @return [String]
119
119
  def path
120
- "/#{@client.version}/models/#{@model}:#{operation}"
120
+ "#{@client.path}/models/#{@model}:#{operation}"
121
121
  end
122
122
 
123
123
  # @return [String]
@@ -24,22 +24,27 @@ module OmniAI
24
24
  # @return [String, nil]
25
25
  attr_accessor :version
26
26
 
27
- # @param api_key [String] optional - defaults to `OmniAI::Google.config.api_key`
28
- # @param host [String] optional - defaults to `OmniAI::Google.config.host`
29
- # @param version [String] optional - defaults to `OmniAI::Google.config.version`
30
- # @param logger [Logger] optional - defaults to `OmniAI::Google.config.logger`
31
- # @param timeout [Integer] optional - defaults to `OmniAI::Google.config.timeout`
27
+ # @param api_key [String] default is `OmniAI::Google.config.api_key`
28
+ # @param credentials [Google::Auth::ServiceAccountCredentials] default is `OmniAI::Google.config.credentials`
29
+ # @param host [String] default is `OmniAI::Google.config.host`
30
+ # @param version [String] default is `OmniAI::Google.config.version`
31
+ # @param logger [Logger] default is `OmniAI::Google.config.logger`
32
+ # @param timeout [Integer] default is `OmniAI::Google.config.timeout`
32
33
  def initialize(
33
34
  api_key: OmniAI::Google.config.api_key,
35
+ credentials: OmniAI::Google.config.credentials,
34
36
  logger: OmniAI::Google.config.logger,
35
37
  host: OmniAI::Google.config.host,
36
38
  version: OmniAI::Google.config.version,
37
39
  timeout: OmniAI::Google.config.timeout
38
40
  )
39
- raise(ArgumentError, %(ENV['GOOGLE_API_KEY'] must be defined or `api_key` must be passed)) if api_key.nil?
41
+ if api_key.nil? && credentials.nil?
42
+ raise(ArgumentError, "either an `api_key` or `credentials` must be provided")
43
+ end
40
44
 
41
45
  super(api_key:, host:, logger:, timeout:)
42
46
 
47
+ @credentials = credentials
43
48
  @version = version
44
49
  end
45
50
 
@@ -79,12 +84,45 @@ module OmniAI
79
84
 
80
85
  # @return [String]
81
86
  def path
82
- if @project_id
83
- "/#{@version}/projects/#{@project_id}/locations/#{@location}/publishers/google"
87
+ if project && location
88
+ "/#{@version}/projects/#{project}/locations/#{location}/publishers/google"
84
89
  else
85
90
  "/#{@version}"
86
91
  end
87
92
  end
93
+
94
+ # @return [HTTP::Client]
95
+ def connection
96
+ http = super
97
+ http = http.auth(auth) if auth?
98
+ http
99
+ end
100
+
101
+ private
102
+
103
+ # @return [String, nil]
104
+ def location
105
+ @location ||= begin
106
+ match = @host.match(%r{//(?<location>[\w\-]+)-aiplatform\.googleapis\.com})
107
+ match[:location] if match
108
+ end
109
+ end
110
+
111
+ # @return [String, nil]
112
+ def project
113
+ @credentials&.project_id
114
+ end
115
+
116
+ # @return [Boolean]
117
+ def auth?
118
+ !@credentials.nil?
119
+ end
120
+
121
+ # @return [String] e.g. "Bearer ..."
122
+ def auth
123
+ @credentials.fetch_access_token!
124
+ "Bearer #{@credentials.access_token}"
125
+ end
88
126
  end
89
127
  end
90
128
  end
@@ -16,19 +16,26 @@ module OmniAI
16
16
  # @return [String, nil]
17
17
  attr_accessor :version
18
18
 
19
+ # @!attribute [rw] credentials
20
+ # @return [String, nil]
21
+ attr_accessor :credentials
22
+
19
23
  # @param api_key [String, nil] optional - defaults to `ENV['GOOGLE_API_KEY']`
24
+ # @param credentials [Google::Auth::ServiceAccountCredentials, nil] optional
20
25
  # @param host [String, nil] optional - defaults to `ENV['GOOGLE_HOST'] w/ fallback to `DEFAULT_HOST`
21
26
  # @param version [String, nil] optional - defaults to `ENV['GOOGLE_VERSION'] w/ fallback to `DEFAULT_VERSION`
22
- # @param logger [Logger, nil] optional - defaults to
27
+ # @param logger [Logger, nil] optional
23
28
  # @param timeout [Integer, Hash, nil] optional
24
29
  def initialize(
25
30
  api_key: ENV.fetch("GOOGLE_API_KEY", nil),
31
+ credentials: nil,
26
32
  host: ENV.fetch("GOOGLE_HOST", DEFAULT_HOST),
27
33
  version: ENV.fetch("GOOGLE_VERSION", DEFAULT_VERSION),
28
34
  logger: nil,
29
35
  timeout: nil
30
36
  )
31
37
  super(api_key:, host:, logger:, timeout:)
38
+ @credentials = credentials
32
39
  @version = version
33
40
  end
34
41
  end
@@ -27,7 +27,9 @@ module OmniAI
27
27
  response = @client
28
28
  .connection
29
29
  .headers({ "X-Goog-Upload-Protocol" => "raw" })
30
- .post("/upload/#{@client.version}/files?key=#{@client.api_key}", body: HTTP::FormData::File.new(io))
30
+ .post("/upload/#{@client.version}/files",
31
+ params: { key: @client.api_key }.compact,
32
+ body: HTTP::FormData::File.new(io))
31
33
  end
32
34
 
33
35
  raise OmniAI::HTTPError, response.flush unless response.status.ok?
@@ -2,6 +2,6 @@
2
2
 
3
3
  module OmniAI
4
4
  module Google
5
- VERSION = "2.2.0"
5
+ VERSION = "2.2.1"
6
6
  end
7
7
  end
data/lib/omniai/google.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "googleauth"
3
4
  require "event_stream_parser"
4
5
  require "omniai"
5
6
  require "zeitwerk"
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omniai-google
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0
4
+ version: 2.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Sylvestre
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-03-27 00:00:00.000000000 Z
10
+ date: 2025-03-31 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: event_stream_parser
@@ -23,6 +23,20 @@ dependencies:
23
23
  - - ">="
24
24
  - !ruby/object:Gem::Version
25
25
  version: '0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: googleauth
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
26
40
  - !ruby/object:Gem::Dependency
27
41
  name: omniai
28
42
  requirement: !ruby/object:Gem::Requirement