omniai-google 0.1.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +14 -0
- data/README.md +85 -16
- data/lib/omniai/google/chat/chunk.rb +24 -0
- data/lib/omniai/google/chat/completion.rb +24 -0
- data/lib/omniai/google/chat/stream.rb +19 -0
- data/lib/omniai/google/chat.rb +90 -0
- data/lib/omniai/google/client.rb +62 -0
- data/lib/omniai/google/config.rb +18 -0
- data/lib/omniai/google/version.rb +1 -1
- data/lib/omniai/google.rb +17 -3
- metadata +19 -13
- data/.rspec +0 -3
- data/.rubocop.yml +0 -8
- data/Rakefile +0 -12
- data/sig/omniai/google.rbs +0 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 76062652be0ccdf946a2c8550e79e378c97ad74559ca51fba7820752f26ccd9a
|
4
|
+
data.tar.gz: bd5f0d7445969ed68b68ba32698207a7890d527eec119bd880387bd33c3db3e0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 774242379d7f13feb348f54c1138ca34081afd913ca67dd61ca9704fea907254c7394c228ced6934d96ee0c6b01a06e282395368e4a122d84058f0257e970e6c
|
7
|
+
data.tar.gz: baa4cd754b22a6239ddd90bf8fd7dfdcbab2032f54ec2915b8291a00deb5992f713035dad5f6d4dc025e3e2a14b57cf41b89327ceb38c047325ffd9e91e4122c
|
data/Gemfile
ADDED
data/README.md
CHANGED
@@ -1,31 +1,100 @@
|
|
1
|
-
#
|
1
|
+
# OmniAI::Google
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/omniai/google`. To experiment with that code, run `bin/console` for an interactive prompt.
|
3
|
+
A Google implementation of the [OmniAI](https://github.com/ksylvest/omniai) APIs.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
9
|
-
|
7
|
+
```sh
|
8
|
+
gem install omniai-google
|
9
|
+
```
|
10
10
|
|
11
|
-
|
11
|
+
## Usage
|
12
12
|
|
13
|
-
|
13
|
+
### Client
|
14
14
|
|
15
|
-
|
15
|
+
A client is setup as follows if `ENV['GOOGLE_API_KEY']` exists:
|
16
16
|
|
17
|
-
|
17
|
+
```ruby
|
18
|
+
client = OmniAI::Google::Client.new
|
19
|
+
```
|
18
20
|
|
19
|
-
|
21
|
+
A client may also be passed the following options:
|
22
|
+
|
23
|
+
- `api_key` (required - default is `ENV['GOOGLE_API_KEY']`)
|
24
|
+
- `host` (optional)
|
25
|
+
- `version` (optional - options are `v1` or `v1beta`)
|
26
|
+
|
27
|
+
### Configuration
|
28
|
+
|
29
|
+
Global configuration is supported for the following options:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
OmniAI::Google.configure do |config|
|
33
|
+
config.api_key = 'sk-...' # default: ENV['GOOGLE_API_KEY']
|
34
|
+
config.host = '...' # default: 'https://generativelanguage.googleapis.com'
|
35
|
+
config.version = 'v1beta' # default: 'v1'
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
### Chat
|
40
|
+
|
41
|
+
A chat completion is generated by passing in prompts using any a variety of formats:
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
completion = client.chat('Tell me a joke!')
|
45
|
+
completion.choice.message.content # 'Why did the chicken cross the road? To get to the other side.'
|
46
|
+
```
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
completion = client.chat({
|
50
|
+
role: OmniAI::Chat::Role::USER,
|
51
|
+
content: 'Is it wise to jump off a bridge?'
|
52
|
+
})
|
53
|
+
completion.choice.message.content # 'No.'
|
54
|
+
```
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
completion = client.chat([
|
58
|
+
{
|
59
|
+
role: OmniAI::Chat::Role::USER,
|
60
|
+
content: 'You are a helpful assistant.'
|
61
|
+
},
|
62
|
+
'What is the capital of Canada?',
|
63
|
+
])
|
64
|
+
completion.choice.message.content # 'The capital of Canada is Ottawa.'
|
65
|
+
```
|
66
|
+
|
67
|
+
#### Model
|
68
|
+
|
69
|
+
`model` takes an optional string (default is `gemini-1.5-pro`):
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
completion = client.chat('How fast is a cheetah?', model: OmniAI::Google::Chat::Model::GEMINI_FLASH)
|
73
|
+
completion.choice.message.content # 'A cheetah can reach speeds over 100 km/h.'
|
74
|
+
```
|
75
|
+
|
76
|
+
[Google API Reference `model`](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/model-versioning#gemini-model-versions)
|
77
|
+
|
78
|
+
#### Temperature
|
79
|
+
|
80
|
+
`temperature` takes an optional float between `0.0` and ` 2.0`:
|
20
81
|
|
21
|
-
|
82
|
+
```ruby
|
83
|
+
completion = client.chat('Pick a number between 1 and 5', temperature: 2.0)
|
84
|
+
completion.choice.message.content # '3'
|
85
|
+
```
|
22
86
|
|
23
|
-
|
87
|
+
[Google API Reference `temperature`](https://ai.google.dev/api/rest/v1/GenerationConfig)
|
24
88
|
|
25
|
-
|
89
|
+
#### Stream
|
26
90
|
|
27
|
-
|
91
|
+
`stream` takes an optional a proc to stream responses in real-time chunks instead of waiting for a complete response:
|
28
92
|
|
29
|
-
|
93
|
+
```ruby
|
94
|
+
stream = proc do |chunk|
|
95
|
+
print(chunk.choice.delta.content) # 'Better', 'three', 'hours', ...
|
96
|
+
end
|
97
|
+
client.chat('Be poetic.', stream:)
|
98
|
+
```
|
30
99
|
|
31
|
-
|
100
|
+
[Google API Reference `stream`](https://ai.google.dev/gemini-api/docs/api-overview#stream)
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module OmniAI
|
4
|
+
module Google
|
5
|
+
class Chat
|
6
|
+
# A chunk given when streaming.
|
7
|
+
class Chunk < OmniAI::Chat::Chunk
|
8
|
+
# @return [Array<OmniAI::Chat::Choice>]
|
9
|
+
def choices
|
10
|
+
@choices ||= [].tap do |choices|
|
11
|
+
@data['candidates'].each do |candidate|
|
12
|
+
candidate['content']['parts'].each do |part|
|
13
|
+
choices << OmniAI::Chat::Choice.new(data: {
|
14
|
+
'index' => candidate['index'],
|
15
|
+
'delta' => { 'role' => candidate['content']['role'], 'content' => part['text'] },
|
16
|
+
})
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module OmniAI
|
4
|
+
module Google
|
5
|
+
class Chat
|
6
|
+
# A completion returned by the API.
|
7
|
+
class Completion < OmniAI::Chat::Completion
|
8
|
+
# @return [Array<OmniAI::Chat::Choice>]
|
9
|
+
def choices
|
10
|
+
@choices ||= [].tap do |entries|
|
11
|
+
@data['candidates'].each do |candidate|
|
12
|
+
candidate['content']['parts'].each do |part|
|
13
|
+
entries << OmniAI::Chat::Choice.new(data: {
|
14
|
+
'index' => candidate['index'],
|
15
|
+
'message' => { 'role' => candidate['content']['role'], 'content' => part['text'] },
|
16
|
+
})
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module OmniAI
|
4
|
+
module Google
|
5
|
+
class Chat
|
6
|
+
# A stream given when streaming.
|
7
|
+
class Stream < OmniAI::Chat::Stream
|
8
|
+
# @yield [OmniAI::Chat::Chunk]
|
9
|
+
def stream!(&)
|
10
|
+
@response.body.each do |chunk|
|
11
|
+
@parser.feed(chunk) do |_, data|
|
12
|
+
yield(Chunk.new(data: JSON.parse(data)))
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module OmniAI
|
4
|
+
module Google
|
5
|
+
# A Google chat implementation.
|
6
|
+
#
|
7
|
+
# Usage:
|
8
|
+
#
|
9
|
+
# chat = OmniAI::Google::Chat.new(client: client)
|
10
|
+
# chat.completion('Tell me a joke.')
|
11
|
+
# chat.completion(['Tell me a joke.'])
|
12
|
+
# chat.completion({ role: 'user', content: 'Tell me a joke.' })
|
13
|
+
# chat.completion([{ role: 'system', content: 'Tell me a joke.' }])
|
14
|
+
class Chat < OmniAI::Chat
|
15
|
+
module Model
|
16
|
+
GEMINI_1_0_PRO = 'gemini-1.0-pro'
|
17
|
+
GEMINI_1_5_PRO = 'gemini-1.5-pro'
|
18
|
+
GEMINI_1_5_FLASH = 'gemini-1.5-flash'
|
19
|
+
GEMINI_1_0_PRO_LATEST = 'gemini-1.0-pro-latest'
|
20
|
+
GEMINI_1_5_PRO_LATEST = 'gemini-1.5-pro-latest'
|
21
|
+
GEMINI_1_5_FLASH_LATEST = 'gemini-1.5-flash-latest'
|
22
|
+
GEMINI_PRO = GEMINI_1_5_PRO
|
23
|
+
GEMINI_FLASH = GEMINI_1_5_FLASH
|
24
|
+
end
|
25
|
+
|
26
|
+
protected
|
27
|
+
|
28
|
+
# @return [HTTP::Response]
|
29
|
+
def request!
|
30
|
+
@client
|
31
|
+
.connection
|
32
|
+
.accept(:json)
|
33
|
+
.post(path, params: {
|
34
|
+
key: @client.api_key,
|
35
|
+
alt: ('sse' if @stream),
|
36
|
+
}.compact, json: payload)
|
37
|
+
end
|
38
|
+
|
39
|
+
# @param response [HTTP::Response]
|
40
|
+
# @return [OmniAI::Google::Chat::Stream]
|
41
|
+
def stream!(response:)
|
42
|
+
raise Error, "#{self.class.name}#stream! unstreamable" unless @stream
|
43
|
+
|
44
|
+
Stream.new(response:).stream! { |chunk| @stream.call(chunk) }
|
45
|
+
end
|
46
|
+
|
47
|
+
# @param response [HTTP::Response]
|
48
|
+
# @param response [OmniAI::Google::Chat::Completion]
|
49
|
+
def complete!(response:)
|
50
|
+
Completion.new(data: response.parse)
|
51
|
+
end
|
52
|
+
|
53
|
+
# @return [Hash]
|
54
|
+
def payload
|
55
|
+
OmniAI::Google.config.chat_options.merge({
|
56
|
+
contents:,
|
57
|
+
generationConfig: generation_config,
|
58
|
+
}).compact
|
59
|
+
end
|
60
|
+
|
61
|
+
# @return [Hash]
|
62
|
+
def generation_config
|
63
|
+
return unless @temperature
|
64
|
+
|
65
|
+
{ temperature: @temperature }.compact
|
66
|
+
end
|
67
|
+
|
68
|
+
# Example:
|
69
|
+
#
|
70
|
+
# [{ role: 'user', parts: [{ text: '...' }] }]
|
71
|
+
#
|
72
|
+
# @return [Array<Hash>]
|
73
|
+
def contents
|
74
|
+
messages.map do |message|
|
75
|
+
{ role: message[:role], parts: [{ text: message[:content] }] }
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
# @return [String]
|
80
|
+
def path
|
81
|
+
"/#{@client.version}/models/#{@model}:#{operation}"
|
82
|
+
end
|
83
|
+
|
84
|
+
# @return [String]
|
85
|
+
def operation
|
86
|
+
@stream ? 'streamGenerateContent' : 'generateContent'
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module OmniAI
|
4
|
+
module Google
|
5
|
+
# A Google client implementation. Usage:
|
6
|
+
#
|
7
|
+
# w/ `api_key``:
|
8
|
+
# client = OmniAI::Google::Client.new(api_key: '...')
|
9
|
+
#
|
10
|
+
# w/ ENV['GOOGLE_API_KEY']:
|
11
|
+
#
|
12
|
+
# ENV['GOOGLE_API_KEY'] = '...'
|
13
|
+
# client = OmniAI::Google::Client.new
|
14
|
+
#
|
15
|
+
# w/ config:
|
16
|
+
#
|
17
|
+
# OmniAI::Google.configure do |config|
|
18
|
+
# config.api_key = '...'
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# client = OmniAI::Google::Client.new
|
22
|
+
class Client < OmniAI::Client
|
23
|
+
attr_accessor :version
|
24
|
+
|
25
|
+
# @param api_key [String] optional - defaults to `OmniAI::Google.config.api_key`
|
26
|
+
# @param host [String] optional - defaults to `OmniAI::Google.config.host`
|
27
|
+
# @param version [String] optional - defaults to `OmniAI::Google.config.version`
|
28
|
+
# @param logger [Logger] optional - defaults to `OmniAI::Google.config.logger`
|
29
|
+
def initialize(
|
30
|
+
api_key: OmniAI::Google.config.api_key,
|
31
|
+
logger: OmniAI::Google.config.logger,
|
32
|
+
host: OmniAI::Google.config.host,
|
33
|
+
version: OmniAI::Google.config.version
|
34
|
+
)
|
35
|
+
raise(ArgumentError, %(ENV['GOOGLE_API_KEY'] must be defined or `api_key` must be passed)) if api_key.nil?
|
36
|
+
|
37
|
+
super(api_key:, logger:)
|
38
|
+
|
39
|
+
@host = host
|
40
|
+
@version = version
|
41
|
+
end
|
42
|
+
|
43
|
+
# @return [HTTP::Client]
|
44
|
+
def connection
|
45
|
+
HTTP.persistent(@host)
|
46
|
+
end
|
47
|
+
|
48
|
+
# @raise [OmniAI::Error]
|
49
|
+
#
|
50
|
+
# @param messages [String, Array, Hash]
|
51
|
+
# @param model [String] optional
|
52
|
+
# @param format [Symbol] optional :text or :json
|
53
|
+
# @param temperature [Float, nil] optional
|
54
|
+
# @param stream [Proc, nil] optional
|
55
|
+
#
|
56
|
+
# @return [OmniAI::Chat::Completion]
|
57
|
+
def chat(messages, model: Chat::Model::GEMINI_PRO, temperature: nil, format: nil, stream: nil)
|
58
|
+
Chat.process!(messages, model:, temperature:, format:, stream:, client: self)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module OmniAI
|
4
|
+
module Google
|
5
|
+
# Config for the Google `api_key` / `host` / `logger` / `version`, `chat_options`.
|
6
|
+
class Config < OmniAI::Config
|
7
|
+
attr_accessor :chat_options, :version
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
super
|
11
|
+
@api_key = ENV.fetch('GOOGLE_API_KEY', nil)
|
12
|
+
@host = ENV.fetch('GOOGLE_HOST', 'https://generativelanguage.googleapis.com')
|
13
|
+
@version = ENV.fetch('GOOGLE_VERSION', 'v1')
|
14
|
+
@chat_options = {}
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/omniai/google.rb
CHANGED
@@ -1,10 +1,24 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
require 'event_stream_parser'
|
4
|
+
require 'omniai'
|
5
|
+
require 'zeitwerk'
|
6
|
+
|
7
|
+
loader = Zeitwerk::Loader.for_gem
|
8
|
+
loader.push_dir(__dir__, namespace: OmniAI)
|
9
|
+
loader.setup
|
4
10
|
|
5
11
|
module OmniAI
|
12
|
+
# A namespace for everything Google.
|
6
13
|
module Google
|
7
|
-
|
8
|
-
|
14
|
+
# @return [OmniAI::Google::Config]
|
15
|
+
def self.config
|
16
|
+
@config ||= Config.new
|
17
|
+
end
|
18
|
+
|
19
|
+
# @yield [OmniAI::Google::Config]
|
20
|
+
def self.configure
|
21
|
+
yield config
|
22
|
+
end
|
9
23
|
end
|
10
24
|
end
|
metadata
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: omniai-google
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kevin Sylvestre
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-06-
|
11
|
+
date: 2024-06-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: event_stream_parser
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
@@ -25,13 +25,13 @@ dependencies:
|
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: omniai
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
|
-
type: :
|
34
|
+
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
@@ -39,13 +39,13 @@ dependencies:
|
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: zeitwerk
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
|
-
type: :
|
48
|
+
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
@@ -59,16 +59,22 @@ executables: []
|
|
59
59
|
extensions: []
|
60
60
|
extra_rdoc_files: []
|
61
61
|
files:
|
62
|
-
-
|
63
|
-
- ".rubocop.yml"
|
62
|
+
- Gemfile
|
64
63
|
- README.md
|
65
|
-
- Rakefile
|
66
64
|
- lib/omniai/google.rb
|
65
|
+
- lib/omniai/google/chat.rb
|
66
|
+
- lib/omniai/google/chat/chunk.rb
|
67
|
+
- lib/omniai/google/chat/completion.rb
|
68
|
+
- lib/omniai/google/chat/stream.rb
|
69
|
+
- lib/omniai/google/client.rb
|
70
|
+
- lib/omniai/google/config.rb
|
67
71
|
- lib/omniai/google/version.rb
|
68
|
-
- sig/omniai/google.rbs
|
69
72
|
homepage: https://github.com/ksylvest/omniai-google
|
70
73
|
licenses: []
|
71
|
-
metadata:
|
74
|
+
metadata:
|
75
|
+
homepage_uri: https://github.com/ksylvest/omniai-google
|
76
|
+
changelog_uri: https://github.com/ksylvest/omniai-google/releases
|
77
|
+
rubygems_mfa_required: 'true'
|
72
78
|
post_install_message:
|
73
79
|
rdoc_options: []
|
74
80
|
require_paths:
|
@@ -77,7 +83,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
77
83
|
requirements:
|
78
84
|
- - ">="
|
79
85
|
- !ruby/object:Gem::Version
|
80
|
-
version: 3.
|
86
|
+
version: 3.3.0
|
81
87
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
88
|
requirements:
|
83
89
|
- - ">="
|
data/.rspec
DELETED
data/.rubocop.yml
DELETED
data/Rakefile
DELETED