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 +4 -4
- data/README.md +28 -4
- data/lib/omniai/google/chat.rb +3 -9
- data/lib/omniai/google/client.rb +39 -8
- data/lib/omniai/google/config.rb +30 -6
- data/lib/omniai/google/credentials.rb +32 -0
- data/lib/omniai/google/upload.rb +3 -1
- data/lib/omniai/google/version.rb +1 -1
- data/lib/omniai/google.rb +1 -0
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e870633ad811d978c7d3e70313d96d646a2ec03bd2ca989c24ebf84dfe9a1bbc
|
4
|
+
data.tar.gz: 5bdd175f1a6ec4299a76f02d00c66e6e8615f05408c3c77edbad2aca096c67f9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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.
|
40
|
-
config.host = '
|
41
|
-
config.
|
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:
|
data/lib/omniai/google/chat.rb
CHANGED
@@ -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
|
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
|
-
"
|
114
|
+
"#{@client.path}/models/#{@model}:#{operation}"
|
121
115
|
end
|
122
116
|
|
123
117
|
# @return [String]
|
data/lib/omniai/google/client.rb
CHANGED
@@ -24,22 +24,33 @@ module OmniAI
|
|
24
24
|
# @return [String, nil]
|
25
25
|
attr_accessor :version
|
26
26
|
|
27
|
-
# @param api_key [String]
|
28
|
-
# @param
|
29
|
-
# @param
|
30
|
-
# @param
|
31
|
-
# @param
|
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
|
-
|
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/#{@
|
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
|
data/lib/omniai/google/config.rb
CHANGED
@@ -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]
|
14
|
+
# @!attribute [rw] project_id
|
16
15
|
# @return [String, nil]
|
17
|
-
attr_accessor :
|
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
|
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
|
-
@
|
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
|
data/lib/omniai/google/upload.rb
CHANGED
@@ -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
|
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?
|
data/lib/omniai/google.rb
CHANGED
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.
|
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
|
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
|