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 +4 -4
- data/README.md +48 -3
- data/lib/omniai/google/chat.rb +1 -1
- data/lib/omniai/google/client.rb +46 -8
- data/lib/omniai/google/config.rb +8 -1
- data/lib/omniai/google/upload.rb +3 -1
- data/lib/omniai/google/version.rb +1 -1
- data/lib/omniai/google.rb +1 -0
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b09fe2dc85e70bef186e70d5e6da204bf003fab07907147c9de01fcc86cfe5e2
|
4
|
+
data.tar.gz: 2869a79e69a256b7420eccc72f33f9cabae2683d12c8f6fc8911fdbfd2e9ccbd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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:
|
data/lib/omniai/google/chat.rb
CHANGED
data/lib/omniai/google/client.rb
CHANGED
@@ -24,22 +24,27 @@ 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 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
|
-
|
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
|
83
|
-
"/#{@version}/projects/#{
|
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
|
data/lib/omniai/google/config.rb
CHANGED
@@ -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
|
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
|
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.1
|
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-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
|