omniai-google 2.2.0 → 2.2.2

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: e870633ad811d978c7d3e70313d96d646a2ec03bd2ca989c24ebf84dfe9a1bbc
4
+ data.tar.gz: 5bdd175f1a6ec4299a76f02d00c66e6e8615f05408c3c77edbad2aca096c67f9
5
5
  SHA512:
6
- metadata.gz: 769fd0c52d6e83fb4bd90e5075e0cc332aca7deb6c56b16808175a0abf0cef97cef55c8991702550747e1b0672e0aec057ad9c8b98c94065f8943ea4a40904d9
7
- data.tar.gz: 8b4f33b903fb5acc6744a85869f8be69113c3b7f0c4b5e0150aed8283aa2747a5c174e564a956963f0d152e27177ff159aa36776bedf7ade83f4fb5bb89f6382
6
+ metadata.gz: bf8f59a91eff99a9b266f9d487b580999322e27107bc0591c12fe070ffe61b1a177452c3504f3f20a2cdcf38aec642a121a4212715c5f6ba594701b5c8f4ec94
7
+ data.tar.gz: 8ea4b0be462ce5df1e502323d2230df7652e83bc57afe8706517fe1c9188680c318ffcfc90a9e6728bd0f238339ab4aaba642dd05c0cd8d31ee7d3b365cadb09
data/README.md CHANGED
@@ -27,21 +27,45 @@ 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
+ #### Configuration w/ Gemini
39
+
40
+ **w/ `api_key`**
41
+
42
+ If using Gemini simply provide an `api_key`:
43
+
44
+ ```ruby
45
+ OmniAI::Google.configure do |config|
46
+ config.api_key = 'sk-...' # defaults is `ENV['GOOGLE_API_KEY']`
47
+ end
48
+ ```
49
+
50
+ #### Configuration w/ Vertex
51
+
52
+ If using Vertex supply the `credentials`, `host`, `location_id` and `project_id`:
36
53
 
37
54
  ```ruby
38
55
  OmniAI::Google.configure do |config|
39
- 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'
56
+ config.credentials = File.open("./credentials.json") # default is `ENV['GOOGLE_CREDENTIALS_PATH']` / `ENV['GOOGLE_CREDENTIALS_JSON']`
57
+ config.host = 'https://us-east4-aiplatform.googleapis.com' # default is `ENV['GOOGLE_HOST']`
58
+ config.location_id = 'us-east4' # defaults is `ENV['GOOGLE_LOCATION_ID']`
59
+ config.project_id = '...' # defaults is `ENV['GOOGLE_PROJECT_ID']`
42
60
  end
43
61
  ```
44
62
 
63
+ Credentials may be configured using:
64
+
65
+ 1. A `File` / `String` / `Pathname`.
66
+ 2. Assigning `ENV['GOOGLE_CREDENTIALS_PATH']` as the path to the `credentials.json`.
67
+ 3. Assigning `ENV['GOOGLE_CREDENTIALS_JSON']` to the contents of `credentials.json`.
68
+
45
69
  ### Chat
46
70
 
47
71
  A chat completion is generated by passing in a simple text prompt:
@@ -73,7 +73,7 @@ module OmniAI
73
73
  .connection
74
74
  .accept(:json)
75
75
  .post(path, params: {
76
- key: @client.api_key,
76
+ key: (@client.api_key unless @client.credentials?),
77
77
  alt: ("sse" if stream?),
78
78
  }.compact, json: payload)
79
79
  end
@@ -99,7 +99,7 @@ module OmniAI
99
99
 
100
100
  # @return [Hash]
101
101
  def generation_config
102
- response_mime_type = (JSON_MIME_TYPE if json_mime_type?)
102
+ response_mime_type = (JSON_MIME_TYPE if @format.eql?(:json))
103
103
 
104
104
  return unless @temperature || response_mime_type
105
105
 
@@ -109,15 +109,9 @@ module OmniAI
109
109
  }.compact
110
110
  end
111
111
 
112
- # Checks if setting a jsonMimeType is supported
113
- # @return [Boolean]
114
- def json_mime_type?
115
- @client.version == OmniAI::Google::Config::Version::BETA && @format.eql?(:json)
116
- end
117
-
118
112
  # @return [String]
119
113
  def path
120
- "/#{@client.version}/models/#{@model}:#{operation}"
114
+ "#{@client.path}/models/#{@model}:#{operation}"
121
115
  end
122
116
 
123
117
  # @return [String]
@@ -24,22 +24,33 @@ 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 project_id [String] default is `OmniAI::Google.config.project_id`
29
+ # @param location_id [String] default is `OmniAI::Google.config.location_id`
30
+ # @param credentials [Google::Auth::ServiceAccountCredentials] default is `OmniAI::Google.config.credentials`
31
+ # @param host [String] default is `OmniAI::Google.config.host`
32
+ # @param version [String] default is `OmniAI::Google.config.version`
33
+ # @param logger [Logger] default is `OmniAI::Google.config.logger`
34
+ # @param timeout [Integer] default is `OmniAI::Google.config.timeout`
32
35
  def initialize(
33
36
  api_key: OmniAI::Google.config.api_key,
37
+ project_id: OmniAI::Google.config.project_id,
38
+ location_id: OmniAI::Google.config.location_id,
39
+ credentials: OmniAI::Google.config.credentials,
34
40
  logger: OmniAI::Google.config.logger,
35
41
  host: OmniAI::Google.config.host,
36
42
  version: OmniAI::Google.config.version,
37
43
  timeout: OmniAI::Google.config.timeout
38
44
  )
39
- raise(ArgumentError, %(ENV['GOOGLE_API_KEY'] must be defined or `api_key` must be passed)) if api_key.nil?
45
+ if api_key.nil? && credentials.nil?
46
+ raise(ArgumentError, "either an `api_key` or `credentials` must be provided")
47
+ end
40
48
 
41
49
  super(api_key:, host:, logger:, timeout:)
42
50
 
51
+ @project_id = project_id
52
+ @location_id = location_id
53
+ @credentials = credentials
43
54
  @version = version
44
55
  end
45
56
 
@@ -79,12 +90,32 @@ module OmniAI
79
90
 
80
91
  # @return [String]
81
92
  def path
82
- if @project_id
83
- "/#{@version}/projects/#{@project_id}/locations/#{@location}/publishers/google"
93
+ if @project_id && @location_id
94
+ "/#{@version}/projects/#{@project_id}/locations/#{@location_id}/publishers/google"
84
95
  else
85
96
  "/#{@version}"
86
97
  end
87
98
  end
99
+
100
+ # @return [HTTP::Client]
101
+ def connection
102
+ http = super
103
+ http = http.auth(auth) if credentials?
104
+ http
105
+ end
106
+
107
+ # @return [Boolean]
108
+ def credentials?
109
+ !@credentials.nil?
110
+ end
111
+
112
+ private
113
+
114
+ # @return [String] e.g. "Bearer ..."
115
+ def auth
116
+ @credentials.fetch_access_token!
117
+ "Bearer #{@credentials.access_token}"
118
+ end
88
119
  end
89
120
  end
90
121
  end
@@ -10,26 +10,50 @@ module OmniAI
10
10
  end
11
11
 
12
12
  DEFAULT_HOST = "https://generativelanguage.googleapis.com"
13
- DEFAULT_VERSION = Version::BETA
14
13
 
15
- # @!attribute [rw] version
14
+ # @!attribute [rw] project_id
16
15
  # @return [String, nil]
17
- attr_accessor :version
16
+ attr_accessor :project_id
17
+
18
+ # @!attribute [rw] location_id
19
+ # @return [String, nil]
20
+ attr_accessor :location_id
18
21
 
19
22
  # @param api_key [String, nil] optional - defaults to `ENV['GOOGLE_API_KEY']`
23
+ # @param project_id [String, nil] optional - defaults to `ENV['GOOGLE_PROJECT_ID']`
24
+ # @param location_id [String, nil] optional - defaults to `ENV['GOOGLE_LOCATION_ID']`
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
+ project_id: ENV.fetch("GOOGLE_PROJECT_ID", nil),
32
+ location_id: ENV.fetch("GOOGLE_LOCATION_ID", nil),
26
33
  host: ENV.fetch("GOOGLE_HOST", DEFAULT_HOST),
27
- 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:)
32
- @version = version
38
+ @project_id = project_id
39
+ @location_id = location_id
40
+ end
41
+
42
+ # @return [String]
43
+ def version
44
+ @host.eql?(DEFAULT_HOST) ? Version::BETA : Version::STABLE
45
+ end
46
+
47
+ # @return [Google::Auth::ServiceAccountCredentials, nil]
48
+ def credentials
49
+ return @credentials if defined?(@credentials)
50
+
51
+ Credentials.detect
52
+ end
53
+
54
+ # @param value [String, File, Google::Auth::ServiceAccountCredentials, nil]
55
+ def credentials=(value)
56
+ @credentials = Credentials.parse(value)
33
57
  end
34
58
  end
35
59
  end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OmniAI
4
+ module Google
5
+ # @example
6
+ # OmniAI::Google::Credentials.parse(Google::Auth::ServiceAccountCredentials.make_creds(...))
7
+ # OmniAI::Google::Credentials.parse(File.open("./credentials.json"))
8
+ # OmniAI::Google::Credentials.parse("./credentials.json")
9
+ module Credentials
10
+ SCOPE = %w[https://www.googleapis.com/auth/cloud-platform].join(",")
11
+
12
+ # @return [Google::Auth::ServiceAccountCredentials, nil]
13
+ def self.detect
14
+ case
15
+ when ENV.key?("GOOGLE_CREDENTIALS_PATH") then parse(Pathname.new(ENV.fetch("GOOGLE_CREDENTIALS_PATH")))
16
+ when ENV.key?("GOOGLE_CREDENTIALS_JSON") then parse(StringIO.new(ENV.fetch("GOOGLE_CREDENTIALS_JSON")))
17
+ end
18
+ end
19
+
20
+ # @param value [Google::Auth::ServiceAccountCredentials, IO, Pathname, String nil]
21
+ # @return [Google::Auth::ServiceAccountCredentials]
22
+ def self.parse(value)
23
+ case value
24
+ when ::Google::Auth::ServiceAccountCredentials then value
25
+ when IO, StringIO then ::Google::Auth::ServiceAccountCredentials.make_creds(json_key_io: value, scope: SCOPE)
26
+ when Pathname then parse(File.open(value))
27
+ when String then parse(StringIO.new(value))
28
+ end
29
+ end
30
+ end
31
+ end
32
+ 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.2"
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.2
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-04-03 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
@@ -76,6 +90,7 @@ files:
76
90
  - lib/omniai/google/chat/usage_serializer.rb
77
91
  - lib/omniai/google/client.rb
78
92
  - lib/omniai/google/config.rb
93
+ - lib/omniai/google/credentials.rb
79
94
  - lib/omniai/google/embed.rb
80
95
  - lib/omniai/google/upload.rb
81
96
  - lib/omniai/google/upload/file.rb