geminai 1.1.0 → 1.2.0
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 +68 -1
- data/lib/geminai/client.rb +13 -2
- data/lib/geminai/configuration.rb +10 -0
- data/lib/geminai/version.rb +1 -1
- data/lib/geminai.rb +14 -1
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 1e37b88649fe531aa20ebbc70d3c452f694c11d151c1faf97ba49960b3b7445a
|
|
4
|
+
data.tar.gz: 3bf4c91537790276633c98ce1a8ca92a94d3cb44560e9708beef85e02f052066
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 789cc18e017f30e17df21c17d7c36507f935948a5537c89a268e96889a1029259a3e96acdddfe8dacb2c24780ac8958563a592fa90145fb32c859e9b562510b1
|
|
7
|
+
data.tar.gz: 1a6510f4c084578c2d77a40432d179d7fe23a508f3698d53634001b7dab2805e5821d9b1283cda55bc9b9d478bdbc10c537a099293a0a582428d5ce333e04c46
|
data/README.md
CHANGED
|
@@ -11,10 +11,35 @@ gem 'geminai'
|
|
|
11
11
|
|
|
12
12
|
## Setup
|
|
13
13
|
|
|
14
|
+
### Option 1: Global Configuration (Recommended for Rails initializers)
|
|
15
|
+
|
|
16
|
+
You can configure the client settings globally (e.g. in `config/initializers/geminai.rb`):
|
|
17
|
+
|
|
18
|
+
```ruby
|
|
19
|
+
require 'geminai'
|
|
20
|
+
|
|
21
|
+
Geminai.configure do |config|
|
|
22
|
+
config.api_key = ENV['GEMINI_API_KEY']
|
|
23
|
+
# config.base_url = "https://generativelanguage.googleapis.com" # Optional, default value
|
|
24
|
+
end
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Once configured globally, you can instantiate the client without passing arguments:
|
|
28
|
+
|
|
29
|
+
```ruby
|
|
30
|
+
ai = Geminai.new
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Option 2: Direct Instantiation
|
|
34
|
+
|
|
35
|
+
Alternatively, you can instantiate the client by passing the API key directly:
|
|
36
|
+
|
|
14
37
|
```ruby
|
|
15
38
|
require 'geminai'
|
|
16
39
|
|
|
17
40
|
ai = Geminai.new(ENV['GEMINI_API_KEY'])
|
|
41
|
+
# or with a custom base url:
|
|
42
|
+
ai = Geminai.new('YOUR_API_KEY', base_url: 'https://custom-url.com')
|
|
18
43
|
```
|
|
19
44
|
|
|
20
45
|
---
|
|
@@ -31,7 +56,7 @@ prompt = "what is the latest news on alphabet's stock price?"
|
|
|
31
56
|
interaction = client.interact(
|
|
32
57
|
model: 'gemini-3.5-flash',
|
|
33
58
|
input: prompt,
|
|
34
|
-
tools: [
|
|
59
|
+
tools: [:google_search],
|
|
35
60
|
store: false # Operate statelessly
|
|
36
61
|
)
|
|
37
62
|
|
|
@@ -122,6 +147,48 @@ edit_interaction = client.interact(
|
|
|
122
147
|
)
|
|
123
148
|
```
|
|
124
149
|
|
|
150
|
+
### 5. Structured JSON Output (JSON Schema)
|
|
151
|
+
|
|
152
|
+
Geminai supports structured JSON output using JSON schemas. You can pass a schema layout to define target properties, types, and constraints for the model output:
|
|
153
|
+
|
|
154
|
+
```ruby
|
|
155
|
+
schema = {
|
|
156
|
+
name: :string,
|
|
157
|
+
price: :number,
|
|
158
|
+
url: :string
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
interaction = client.interact(
|
|
162
|
+
model: 'gemini-3.5-flash',
|
|
163
|
+
input: 'Recommend a good mechanical keyboard with its name, estimated price in USD, and a manufacturer URL.',
|
|
164
|
+
schema: schema,
|
|
165
|
+
store: false
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
# The output text is guaranteed to be a JSON string adhering to the schema
|
|
169
|
+
puts interaction.output_text
|
|
170
|
+
# => "{\"name\":\"Keychron K2\",\"price\":79.99,\"url\":\"https://www.keychron.com/\"}"
|
|
171
|
+
|
|
172
|
+
# You can also request a top-level list/array by wrapping the schema in a Ruby Array:
|
|
173
|
+
array_schema = [
|
|
174
|
+
{
|
|
175
|
+
name: :string,
|
|
176
|
+
price: :number,
|
|
177
|
+
url: :string
|
|
178
|
+
}
|
|
179
|
+
]
|
|
180
|
+
|
|
181
|
+
array_interaction = client.interact(
|
|
182
|
+
model: 'gemini-3.5-flash',
|
|
183
|
+
input: 'Recommend three good mechanical keyboards.',
|
|
184
|
+
schema: array_schema,
|
|
185
|
+
store: false
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
puts array_interaction.output_text
|
|
189
|
+
# => "[{\"name\":\"Keychron K2\",\"price\":79.99,\"url\":\"https://www.keychron.com/\"}, ...]"
|
|
190
|
+
```
|
|
191
|
+
|
|
125
192
|
## Running Tests
|
|
126
193
|
|
|
127
194
|
To run the integration tests (which execute live requests to the API), verify your `GEMINI_API_KEY` is present in your environment and run:
|
data/lib/geminai/client.rb
CHANGED
|
@@ -7,8 +7,8 @@ module Geminai
|
|
|
7
7
|
attr_reader :api_key, :base_url
|
|
8
8
|
|
|
9
9
|
def initialize(api_key: nil, base_url: nil)
|
|
10
|
-
@api_key = api_key ||
|
|
11
|
-
@base_url = base_url ||
|
|
10
|
+
@api_key = api_key || Geminai.configuration.api_key
|
|
11
|
+
@base_url = base_url || Geminai.configuration.base_url
|
|
12
12
|
|
|
13
13
|
if @api_key.nil? || @api_key.empty?
|
|
14
14
|
raise ArgumentError, "API Key is required"
|
|
@@ -56,6 +56,17 @@ module Geminai
|
|
|
56
56
|
body[:generation_config] = {speech_config: [{voice: voice}]}
|
|
57
57
|
end
|
|
58
58
|
|
|
59
|
+
# Handle tools normalization if present
|
|
60
|
+
if options.key?(:tools)
|
|
61
|
+
options[:tools] = Array(options[:tools]).map do |tool|
|
|
62
|
+
if tool.is_a?(Symbol) || tool.is_a?(String)
|
|
63
|
+
{type: tool.to_s}
|
|
64
|
+
else
|
|
65
|
+
tool
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
59
70
|
# Merge other options, allowing them to override shortcuts if explicitly provided
|
|
60
71
|
options.each do |k, v|
|
|
61
72
|
if body[k].is_a?(Hash) && v.is_a?(Hash)
|
data/lib/geminai/version.rb
CHANGED
data/lib/geminai.rb
CHANGED
|
@@ -4,10 +4,23 @@ require_relative "geminai/grounding_metadata"
|
|
|
4
4
|
require_relative "geminai/interaction"
|
|
5
5
|
require_relative "geminai/client"
|
|
6
6
|
require_relative "geminai/schema_builder"
|
|
7
|
+
require_relative "geminai/configuration"
|
|
7
8
|
|
|
8
9
|
module Geminai
|
|
10
|
+
class << self
|
|
11
|
+
attr_writer :configuration
|
|
12
|
+
|
|
13
|
+
def configuration
|
|
14
|
+
@configuration ||= Configuration.new
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def configure
|
|
18
|
+
yield(configuration)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
9
22
|
# Helper to construct a client
|
|
10
|
-
def self.new(api_key, base_url: nil)
|
|
23
|
+
def self.new(api_key = nil, base_url: nil)
|
|
11
24
|
Client.new(api_key: api_key, base_url: base_url)
|
|
12
25
|
end
|
|
13
26
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: geminai
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- swlkr
|
|
@@ -18,6 +18,7 @@ files:
|
|
|
18
18
|
- README.md
|
|
19
19
|
- lib/geminai.rb
|
|
20
20
|
- lib/geminai/client.rb
|
|
21
|
+
- lib/geminai/configuration.rb
|
|
21
22
|
- lib/geminai/grounding_metadata.rb
|
|
22
23
|
- lib/geminai/interaction.rb
|
|
23
24
|
- lib/geminai/schema_builder.rb
|