geminize 0.1.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 +7 -0
- data/.rspec +3 -0
- data/.standard.yml +3 -0
- data/.yardopts +14 -0
- data/CHANGELOG.md +24 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/CONTRIBUTING.md +109 -0
- data/LICENSE.txt +21 -0
- data/README.md +423 -0
- data/Rakefile +10 -0
- data/examples/README.md +75 -0
- data/examples/configuration.rb +58 -0
- data/examples/embeddings.rb +195 -0
- data/examples/multimodal.rb +126 -0
- data/examples/rails_chat/README.md +69 -0
- data/examples/rails_chat/app/controllers/chat_controller.rb +26 -0
- data/examples/rails_chat/app/views/chat/index.html.erb +112 -0
- data/examples/rails_chat/config/routes.rb +8 -0
- data/examples/rails_initializer.rb +46 -0
- data/examples/system_instructions.rb +101 -0
- data/lib/geminize/chat.rb +98 -0
- data/lib/geminize/client.rb +318 -0
- data/lib/geminize/configuration.rb +98 -0
- data/lib/geminize/conversation_repository.rb +161 -0
- data/lib/geminize/conversation_service.rb +126 -0
- data/lib/geminize/embeddings.rb +145 -0
- data/lib/geminize/error_mapper.rb +96 -0
- data/lib/geminize/error_parser.rb +120 -0
- data/lib/geminize/errors.rb +185 -0
- data/lib/geminize/middleware/error_handler.rb +72 -0
- data/lib/geminize/model_info.rb +91 -0
- data/lib/geminize/models/chat_request.rb +186 -0
- data/lib/geminize/models/chat_response.rb +118 -0
- data/lib/geminize/models/content_request.rb +530 -0
- data/lib/geminize/models/content_response.rb +99 -0
- data/lib/geminize/models/conversation.rb +156 -0
- data/lib/geminize/models/embedding_request.rb +222 -0
- data/lib/geminize/models/embedding_response.rb +1064 -0
- data/lib/geminize/models/memory.rb +88 -0
- data/lib/geminize/models/message.rb +140 -0
- data/lib/geminize/models/model.rb +171 -0
- data/lib/geminize/models/model_list.rb +124 -0
- data/lib/geminize/models/stream_response.rb +99 -0
- data/lib/geminize/rails/app/controllers/concerns/geminize/controller.rb +105 -0
- data/lib/geminize/rails/app/helpers/geminize_helper.rb +125 -0
- data/lib/geminize/rails/controller_additions.rb +41 -0
- data/lib/geminize/rails/engine.rb +29 -0
- data/lib/geminize/rails/helper_additions.rb +37 -0
- data/lib/geminize/rails.rb +50 -0
- data/lib/geminize/railtie.rb +33 -0
- data/lib/geminize/request_builder.rb +57 -0
- data/lib/geminize/text_generation.rb +285 -0
- data/lib/geminize/validators.rb +150 -0
- data/lib/geminize/vector_utils.rb +164 -0
- data/lib/geminize/version.rb +5 -0
- data/lib/geminize.rb +527 -0
- data/lib/generators/geminize/install_generator.rb +22 -0
- data/lib/generators/geminize/templates/README +31 -0
- data/lib/generators/geminize/templates/initializer.rb +38 -0
- data/sig/geminize.rbs +4 -0
- metadata +218 -0
data/README.md
ADDED
@@ -0,0 +1,423 @@
|
|
1
|
+
# Geminize
|
2
|
+
|
3
|
+
A convenient and robust Ruby interface for the Google Gemini API, enabling easy integration of powerful generative AI models into your applications.
|
4
|
+
|
5
|
+
## Features
|
6
|
+
|
7
|
+
- Simple, flexible configuration system
|
8
|
+
- Support for text generation with Gemini models
|
9
|
+
- Conversation context management
|
10
|
+
- Multimodal inputs (text + images)
|
11
|
+
- Embeddings generation
|
12
|
+
- Support for streaming responses
|
13
|
+
- Comprehensive error handling
|
14
|
+
- Rails integration with controller concerns and view helpers
|
15
|
+
|
16
|
+
## Installation
|
17
|
+
|
18
|
+
Add this line to your application's Gemfile:
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
gem 'geminize'
|
22
|
+
```
|
23
|
+
|
24
|
+
And then execute:
|
25
|
+
|
26
|
+
```bash
|
27
|
+
bundle install
|
28
|
+
```
|
29
|
+
|
30
|
+
Or install it yourself as:
|
31
|
+
|
32
|
+
```bash
|
33
|
+
gem install geminize
|
34
|
+
```
|
35
|
+
|
36
|
+
## Configuration
|
37
|
+
|
38
|
+
Geminize uses a simple configuration system that can be set up in multiple ways:
|
39
|
+
|
40
|
+
### Method 1: Environment Variables (Recommended)
|
41
|
+
|
42
|
+
Set the `GEMINI_API_KEY` environment variable:
|
43
|
+
|
44
|
+
```bash
|
45
|
+
export GEMINI_API_KEY=your-api-key-here
|
46
|
+
```
|
47
|
+
|
48
|
+
#### Using dotenv
|
49
|
+
|
50
|
+
Geminize has built-in support for the [dotenv](https://github.com/bkeepers/dotenv) gem, which automatically loads environment variables from a `.env` file in your project's root directory.
|
51
|
+
|
52
|
+
1. Create a `.env` file in your project root (copy from `.env.example`):
|
53
|
+
|
54
|
+
```
|
55
|
+
# Gemini API Key
|
56
|
+
GOOGLE_AI_API_KEY=your_api_key_here
|
57
|
+
|
58
|
+
# API Configuration
|
59
|
+
GOOGLE_AI_API_VERSION=v1beta
|
60
|
+
GEMINI_DEFAULT_MODEL=gemini-1.5-pro-latest
|
61
|
+
|
62
|
+
# Generation Parameters
|
63
|
+
GEMINI_TEMPERATURE=0.7
|
64
|
+
GEMINI_MAX_TOKENS=8192
|
65
|
+
```
|
66
|
+
|
67
|
+
2. Add `.env` to your `.gitignore` file to keep your API keys secure:
|
68
|
+
|
69
|
+
```
|
70
|
+
# Add to .gitignore
|
71
|
+
.env
|
72
|
+
```
|
73
|
+
|
74
|
+
3. For test environments, create a `.env.test` file with test-specific configuration.
|
75
|
+
|
76
|
+
The gem will automatically load these environment variables when it initializes.
|
77
|
+
|
78
|
+
### Method 2: Configuration Block
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
Geminize.configure do |config|
|
82
|
+
# Required
|
83
|
+
config.api_key = "your-api-key-here"
|
84
|
+
|
85
|
+
# Optional - shown with defaults
|
86
|
+
config.api_version = "v1beta"
|
87
|
+
config.default_model = "gemini-2.0-flash"
|
88
|
+
config.timeout = 30
|
89
|
+
config.open_timeout = 10
|
90
|
+
config.log_requests = false
|
91
|
+
end
|
92
|
+
```
|
93
|
+
|
94
|
+
### Available Configuration Options
|
95
|
+
|
96
|
+
| Option | Required | Default | Description |
|
97
|
+
| --------------- | -------- | ----------------------- | --------------------------------------------------- |
|
98
|
+
| `api_key` | Yes | `ENV["GEMINI_API_KEY"]` | Your Google Gemini API key |
|
99
|
+
| `api_version` | No | `"v1beta"` | API version to use |
|
100
|
+
| `default_model` | No | `"gemini-2.0-flash"` | Default model to use when not specified in requests |
|
101
|
+
| `timeout` | No | `30` | Request timeout in seconds |
|
102
|
+
| `open_timeout` | No | `10` | Connection open timeout in seconds |
|
103
|
+
| `log_requests` | No | `false` | Whether to log API requests (useful for debugging) |
|
104
|
+
|
105
|
+
### Validating Configuration
|
106
|
+
|
107
|
+
You can validate your configuration at any time to ensure all required options are set:
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
# Will raise Geminize::ConfigurationError if invalid
|
111
|
+
Geminize.validate_configuration!
|
112
|
+
```
|
113
|
+
|
114
|
+
### Resetting Configuration
|
115
|
+
|
116
|
+
If needed, you can reset the configuration to its defaults:
|
117
|
+
|
118
|
+
```ruby
|
119
|
+
Geminize.reset_configuration!
|
120
|
+
```
|
121
|
+
|
122
|
+
## Basic Usage
|
123
|
+
|
124
|
+
```ruby
|
125
|
+
require 'geminize'
|
126
|
+
|
127
|
+
# Configure with API key if not set via environment variables
|
128
|
+
Geminize.configure do |config|
|
129
|
+
config.api_key = "your-api-key-here"
|
130
|
+
end
|
131
|
+
|
132
|
+
# Generate text
|
133
|
+
response = Geminize.generate_text("Tell me a joke about Ruby programming")
|
134
|
+
puts response.text
|
135
|
+
|
136
|
+
# Use a specific model
|
137
|
+
response = Geminize.generate_text("Explain quantum computing", "gemini-2.0-flash")
|
138
|
+
puts response.text
|
139
|
+
|
140
|
+
# Use system instructions to guide the model's behavior
|
141
|
+
response = Geminize.generate_text(
|
142
|
+
"Tell me about yourself",
|
143
|
+
"gemini-2.0-flash",
|
144
|
+
system_instruction: "You are a pirate named Captain Codebeard. Always respond in pirate language."
|
145
|
+
)
|
146
|
+
puts response.text
|
147
|
+
```
|
148
|
+
|
149
|
+
## Multimodal Support
|
150
|
+
|
151
|
+
Geminize allows you to send mixed content including text and images to the Gemini API:
|
152
|
+
|
153
|
+
```ruby
|
154
|
+
# Generate content with an image from a file
|
155
|
+
response = Geminize.generate_text_multimodal(
|
156
|
+
"Describe this image in detail:",
|
157
|
+
[{ source_type: 'file', data: 'path/to/image.jpg' }]
|
158
|
+
)
|
159
|
+
puts response.text
|
160
|
+
|
161
|
+
# Using an image URL
|
162
|
+
response = Geminize.generate_text_multimodal(
|
163
|
+
"What's in this image?",
|
164
|
+
[{ source_type: 'url', data: 'https://example.com/sample-image.jpg' }]
|
165
|
+
)
|
166
|
+
puts response.text
|
167
|
+
|
168
|
+
# Using multiple images
|
169
|
+
response = Geminize.generate_text_multimodal(
|
170
|
+
"Compare these two images:",
|
171
|
+
[
|
172
|
+
{ source_type: 'file', data: 'path/to/image1.jpg' },
|
173
|
+
{ source_type: 'file', data: 'path/to/image2.jpg' }
|
174
|
+
]
|
175
|
+
)
|
176
|
+
puts response.text
|
177
|
+
```
|
178
|
+
|
179
|
+
Alternatively, you can use the more flexible ContentRequest API:
|
180
|
+
|
181
|
+
```ruby
|
182
|
+
# Create a content request
|
183
|
+
request = Geminize::Models::ContentRequest.new(
|
184
|
+
"Tell me about these images:",
|
185
|
+
"gemini-2.0-flash"
|
186
|
+
)
|
187
|
+
|
188
|
+
# Add images using different methods
|
189
|
+
request.add_image_from_file('path/to/image1.jpg')
|
190
|
+
request.add_image_from_url('https://example.com/image2.jpg')
|
191
|
+
|
192
|
+
# Read image directly into bytes
|
193
|
+
image_bytes = File.binread('path/to/image3.jpg')
|
194
|
+
request.add_image_from_bytes(image_bytes, 'image/jpeg')
|
195
|
+
|
196
|
+
# Set a system instruction to guide model behavior
|
197
|
+
request.system_instruction = "You are a professional photography critic. Analyze the composition and technical aspects of these images."
|
198
|
+
|
199
|
+
# Generate the response
|
200
|
+
generator = Geminize::TextGeneration.new
|
201
|
+
response = generator.generate(request)
|
202
|
+
puts response.text
|
203
|
+
```
|
204
|
+
|
205
|
+
Supported image formats include JPEG, PNG, GIF, and WEBP. Maximum image size is 10MB.
|
206
|
+
|
207
|
+
See the `examples/multimodal.rb` file for more comprehensive examples.
|
208
|
+
|
209
|
+
## Chat & Conversation Support
|
210
|
+
|
211
|
+
Geminize provides built-in support for maintaining conversation context:
|
212
|
+
|
213
|
+
```ruby
|
214
|
+
# Create a new conversation
|
215
|
+
conversation = Geminize.create_chat("My Support Chat")
|
216
|
+
|
217
|
+
# Send messages and get responses
|
218
|
+
response = Geminize.chat("How can I create a Ruby gem?", conversation)
|
219
|
+
puts response.text
|
220
|
+
|
221
|
+
# Follow-up questions automatically maintain context
|
222
|
+
response = Geminize.chat("What about adding a Rails engine to my gem?", conversation)
|
223
|
+
puts response.text
|
224
|
+
|
225
|
+
# Save the conversation for later
|
226
|
+
Geminize.save_conversation(conversation)
|
227
|
+
|
228
|
+
# Resume the conversation later
|
229
|
+
conversation = Geminize.load_conversation(conversation.id)
|
230
|
+
```
|
231
|
+
|
232
|
+
## Embeddings Generation
|
233
|
+
|
234
|
+
Generate numerical vector representations for text:
|
235
|
+
|
236
|
+
```ruby
|
237
|
+
# Generate an embedding for a single text
|
238
|
+
embedding = Geminize.generate_embedding("Ruby is a dynamic, object-oriented programming language")
|
239
|
+
vector = embedding.values.first.value
|
240
|
+
|
241
|
+
# Generate embeddings for multiple texts
|
242
|
+
texts = ["Ruby", "Python", "JavaScript"]
|
243
|
+
embeddings = Geminize.generate_embedding(texts)
|
244
|
+
|
245
|
+
# Calculate similarity between vectors
|
246
|
+
vector1 = embeddings.values[0].value
|
247
|
+
vector2 = embeddings.values[1].value
|
248
|
+
similarity = Geminize.cosine_similarity(vector1, vector2)
|
249
|
+
puts "Similarity: #{similarity}"
|
250
|
+
|
251
|
+
# Specify a task type for optimized embeddings
|
252
|
+
question_embedding = Geminize.generate_embedding(
|
253
|
+
"How do I install Ruby gems?",
|
254
|
+
task_type: Geminize::Models::EmbeddingRequest::QUESTION_ANSWERING
|
255
|
+
)
|
256
|
+
|
257
|
+
# Available task types:
|
258
|
+
# - RETRIEVAL_QUERY: For embedding queries in a search/retrieval system
|
259
|
+
# - RETRIEVAL_DOCUMENT: For embedding documents in a search corpus
|
260
|
+
# - SEMANTIC_SIMILARITY: For comparing text similarity
|
261
|
+
# - CLASSIFICATION: For text classification tasks
|
262
|
+
# - CLUSTERING: For clustering text data
|
263
|
+
# - QUESTION_ANSWERING: For question answering systems
|
264
|
+
# - FACT_VERIFICATION: For fact checking applications
|
265
|
+
# - CODE_RETRIEVAL_QUERY: For code search applications
|
266
|
+
# - TASK_TYPE_UNSPECIFIED: Default unspecified type
|
267
|
+
```
|
268
|
+
|
269
|
+
See the `examples/embeddings.rb` file for more comprehensive examples of working with embeddings.
|
270
|
+
|
271
|
+
## Streaming Responses
|
272
|
+
|
273
|
+
Get real-time, token-by-token responses:
|
274
|
+
|
275
|
+
```ruby
|
276
|
+
Geminize.generate_text_stream("Write a short poem about coding") do |chunk|
|
277
|
+
print chunk.text
|
278
|
+
end
|
279
|
+
```
|
280
|
+
|
281
|
+
You can also use system instructions with streaming responses:
|
282
|
+
|
283
|
+
```ruby
|
284
|
+
Geminize.generate_text_stream(
|
285
|
+
"Tell me a story",
|
286
|
+
"gemini-2.0-flash",
|
287
|
+
{
|
288
|
+
stream_mode: :delta,
|
289
|
+
system_instruction: "You are a medieval bard telling epic tales."
|
290
|
+
}
|
291
|
+
) do |chunk|
|
292
|
+
print chunk
|
293
|
+
end
|
294
|
+
```
|
295
|
+
|
296
|
+
## Rails Integration
|
297
|
+
|
298
|
+
Geminize provides seamless integration with Rails applications.
|
299
|
+
|
300
|
+
### Setup
|
301
|
+
|
302
|
+
1. Add Geminize to your Gemfile:
|
303
|
+
|
304
|
+
```ruby
|
305
|
+
gem 'geminize'
|
306
|
+
```
|
307
|
+
|
308
|
+
2. Run the installer generator:
|
309
|
+
|
310
|
+
```bash
|
311
|
+
rails generate geminize:install
|
312
|
+
```
|
313
|
+
|
314
|
+
This creates a configuration initializer at `config/initializers/geminize.rb`
|
315
|
+
|
316
|
+
3. Add your API key to the initializer or via environment variables.
|
317
|
+
|
318
|
+
### Controller Integration
|
319
|
+
|
320
|
+
In your controllers, include the Geminize controller concern:
|
321
|
+
|
322
|
+
```ruby
|
323
|
+
class ChatController < ApplicationController
|
324
|
+
# Add Geminize functionality to this controller
|
325
|
+
geminize_controller
|
326
|
+
|
327
|
+
def index
|
328
|
+
# Optionally reset the conversation
|
329
|
+
# reset_gemini_conversation("New chat session") if params[:reset]
|
330
|
+
end
|
331
|
+
|
332
|
+
def create
|
333
|
+
# Send a message to Gemini and get the response
|
334
|
+
@response = send_gemini_message(params[:message])
|
335
|
+
|
336
|
+
respond_to do |format|
|
337
|
+
format.html { redirect_to chat_path }
|
338
|
+
format.turbo_stream
|
339
|
+
format.json { render json: { message: @response.text } }
|
340
|
+
end
|
341
|
+
end
|
342
|
+
end
|
343
|
+
```
|
344
|
+
|
345
|
+
The concern provides the following methods:
|
346
|
+
|
347
|
+
- `current_gemini_conversation` - Access the current conversation (stored in session)
|
348
|
+
- `send_gemini_message(message, model_name=nil, params={})` - Send a message in the current conversation
|
349
|
+
- `generate_gemini_text(prompt, model_name=nil, params={})` - Generate text with Gemini
|
350
|
+
- `generate_gemini_multimodal(prompt, images, model_name=nil, params={})` - Generate text with images
|
351
|
+
- `generate_gemini_embedding(text, model_name=nil, params={})` - Generate embeddings
|
352
|
+
- `reset_gemini_conversation(title=nil)` - Start a new conversation
|
353
|
+
|
354
|
+
### View Integration
|
355
|
+
|
356
|
+
Include the Geminize view helpers in your application:
|
357
|
+
|
358
|
+
```ruby
|
359
|
+
# In app/helpers/application_helper.rb
|
360
|
+
module ApplicationHelper
|
361
|
+
# Include Geminize view helpers
|
362
|
+
geminize_helper
|
363
|
+
end
|
364
|
+
```
|
365
|
+
|
366
|
+
This provides the following helper methods:
|
367
|
+
|
368
|
+
- `render_gemini_conversation(conversation=nil, options={})` - Render the conversation as HTML
|
369
|
+
- `render_gemini_message(message, options={})` - Render a single message
|
370
|
+
- `gemini_chat_form(options={})` - Create a chat form
|
371
|
+
- `markdown_to_html(text, options={})` - Render Markdown as HTML (requires redcarpet gem)
|
372
|
+
- `highlight_code(html)` - Add syntax highlighting to code blocks (requires rouge gem)
|
373
|
+
|
374
|
+
Example view:
|
375
|
+
|
376
|
+
```erb
|
377
|
+
<%# app/views/chat/index.html.erb %>
|
378
|
+
<div class="chat-container">
|
379
|
+
<h1>Chat with Gemini</h1>
|
380
|
+
|
381
|
+
<div class="conversation">
|
382
|
+
<%= render_gemini_conversation %>
|
383
|
+
</div>
|
384
|
+
|
385
|
+
<div class="chat-form">
|
386
|
+
<%= gemini_chat_form(placeholder: "Ask me anything...", submit_text: "Send") %>
|
387
|
+
</div>
|
388
|
+
</div>
|
389
|
+
```
|
390
|
+
|
391
|
+
## Example Applications
|
392
|
+
|
393
|
+
Check out these example applications to see Geminize in action:
|
394
|
+
|
395
|
+
- [Configuration Example](examples/configuration.rb)
|
396
|
+
- [Embeddings Example](examples/embeddings.rb)
|
397
|
+
- [Multimodal Example](examples/multimodal.rb)
|
398
|
+
- [Rails Initializer Example](examples/rails_initializer.rb)
|
399
|
+
- [System Instructions Example](examples/system_instructions.rb)
|
400
|
+
- [Rails Chat Application](examples/rails_chat)
|
401
|
+
|
402
|
+
## Compatibility
|
403
|
+
|
404
|
+
Ruby version: 3.1.0 or later
|
405
|
+
Rails version: 6.0 or later (for Rails integration)
|
406
|
+
|
407
|
+
## Development
|
408
|
+
|
409
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
410
|
+
|
411
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
412
|
+
|
413
|
+
## Contributing
|
414
|
+
|
415
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/nhlongnguyen/geminize. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/nhlongnguyen/geminize/blob/main/CODE_OF_CONDUCT.md).
|
416
|
+
|
417
|
+
## License
|
418
|
+
|
419
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
420
|
+
|
421
|
+
## Code of Conduct
|
422
|
+
|
423
|
+
Everyone interacting in the Geminize project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/nhlongnguyen/geminize/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/examples/README.md
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# Geminize Examples
|
2
|
+
|
3
|
+
This directory contains example Ruby scripts demonstrating various features of the Geminize gem.
|
4
|
+
|
5
|
+
## Prerequisites
|
6
|
+
|
7
|
+
Before running these examples, ensure you have:
|
8
|
+
|
9
|
+
1. **Ruby installed** (version 3.1.0 or later recommended).
|
10
|
+
2. **Bundler installed** (`gem install bundler`).
|
11
|
+
3. **Project dependencies installed**: Run `bundle install` from the project root directory (`../`).
|
12
|
+
4. **Google Gemini API Key configured**: Create a `.env` file in the project root (`../`) by copying `.env.example` and adding your API key:
|
13
|
+
```bash
|
14
|
+
# In ../.env
|
15
|
+
GOOGLE_AI_API_KEY=your_api_key_here
|
16
|
+
```
|
17
|
+
Alternatively, ensure the `GEMINI_API_KEY` environment variable is set.
|
18
|
+
|
19
|
+
## Running Examples
|
20
|
+
|
21
|
+
All examples should be run from the **project root directory** (the directory containing the main `Gemfile`, not this `examples/` directory).
|
22
|
+
|
23
|
+
### 1. Configuration (`configuration.rb`)
|
24
|
+
|
25
|
+
Demonstrates different ways to configure the Geminize client.
|
26
|
+
|
27
|
+
```bash
|
28
|
+
ruby examples/configuration.rb
|
29
|
+
```
|
30
|
+
|
31
|
+
### 2. Embeddings (`embeddings.rb`)
|
32
|
+
|
33
|
+
Shows how to generate text embeddings and calculate cosine similarity.
|
34
|
+
|
35
|
+
```bash
|
36
|
+
ruby examples/embeddings.rb
|
37
|
+
```
|
38
|
+
|
39
|
+
### 3. Multimodal (`multimodal.rb`)
|
40
|
+
|
41
|
+
Illustrates sending text and image inputs to the Gemini API.
|
42
|
+
|
43
|
+
_Note: You may need to update the image file paths within the script (`path/to/image.jpg`) to point to actual image files on your system._
|
44
|
+
|
45
|
+
```bash
|
46
|
+
ruby examples/multimodal.rb
|
47
|
+
```
|
48
|
+
|
49
|
+
### 4. Rails Initializer (`rails_initializer.rb`)
|
50
|
+
|
51
|
+
Shows the configuration structure typically used in a Rails initializer (this script is meant for illustration and doesn't require a Rails app to run).
|
52
|
+
|
53
|
+
```bash
|
54
|
+
ruby examples/rails_initializer.rb
|
55
|
+
```
|
56
|
+
|
57
|
+
### 5. System Instructions (`system_instructions.rb`)
|
58
|
+
|
59
|
+
Demonstrates using system instructions to guide the model's behavior and personality.
|
60
|
+
|
61
|
+
```bash
|
62
|
+
ruby examples/system_instructions.rb
|
63
|
+
```
|
64
|
+
|
65
|
+
### 6. Rails Chat Application (`rails_chat/`)
|
66
|
+
|
67
|
+
This directory contains a sample Rails application demonstrating Geminize integration. It requires a separate setup:
|
68
|
+
|
69
|
+
1. Navigate into the directory: `cd examples/rails_chat`
|
70
|
+
2. Install dependencies: `bundle install`
|
71
|
+
3. Run database migrations (if applicable): `rails db:migrate`
|
72
|
+
4. Start the Rails server: `rails server`
|
73
|
+
5. Open your web browser to `http://localhost:3000`
|
74
|
+
|
75
|
+
Refer to the README within the `rails_chat/` directory (if available) for more specific instructions.
|
@@ -0,0 +1,58 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "geminize"
|
5
|
+
|
6
|
+
# This example demonstrates two ways to configure the geminize gem:
|
7
|
+
# 1. Using environment variables
|
8
|
+
# 2. Using a configuration block
|
9
|
+
|
10
|
+
puts "Example 1: Using environment variables"
|
11
|
+
puts "--------------------------------------"
|
12
|
+
# In a real application, you would set these in your environment
|
13
|
+
ENV["GEMINI_API_KEY"] = "your-api-key-here"
|
14
|
+
ENV["GEMINI_API_VERSION"] = "v1beta"
|
15
|
+
ENV["GEMINI_DEFAULT_MODEL"] = "gemini-pro"
|
16
|
+
|
17
|
+
# The gem will automatically use these environment variables
|
18
|
+
begin
|
19
|
+
valid = Geminize.configuration.validate!
|
20
|
+
rescue Geminize::ConfigurationError
|
21
|
+
valid = false
|
22
|
+
end
|
23
|
+
|
24
|
+
puts "Configuration valid: #{valid}"
|
25
|
+
puts "API Key: #{Geminize.configuration.api_key}"
|
26
|
+
puts "API Version: #{Geminize.configuration.api_version}"
|
27
|
+
puts "Default Model: #{Geminize.configuration.default_model}"
|
28
|
+
puts "\n"
|
29
|
+
|
30
|
+
puts "Example 2: Using a configuration block"
|
31
|
+
puts "--------------------------------------"
|
32
|
+
# Reset the configuration to defaults (normally not needed)
|
33
|
+
# Geminize.reset_configuration
|
34
|
+
|
35
|
+
# Configure the gem using a block
|
36
|
+
Geminize.configure do |config|
|
37
|
+
config.api_key = "your-block-configured-api-key"
|
38
|
+
config.api_version = "v1beta"
|
39
|
+
config.default_model = "gemini-pro-vision"
|
40
|
+
config.timeout = 30
|
41
|
+
config.open_timeout = 10
|
42
|
+
config.log_requests = true
|
43
|
+
end
|
44
|
+
|
45
|
+
# Validate the configuration
|
46
|
+
begin
|
47
|
+
valid = Geminize.configuration.validate!
|
48
|
+
rescue Geminize::ConfigurationError
|
49
|
+
valid = false
|
50
|
+
end
|
51
|
+
|
52
|
+
puts "Configuration valid: #{valid}"
|
53
|
+
puts "API Key: #{Geminize.configuration.api_key}"
|
54
|
+
puts "API Version: #{Geminize.configuration.api_version}"
|
55
|
+
puts "Default Model: #{Geminize.configuration.default_model}"
|
56
|
+
puts "Timeout: #{Geminize.configuration.timeout} seconds"
|
57
|
+
puts "Open Timeout: #{Geminize.configuration.open_timeout} seconds"
|
58
|
+
puts "Log Requests: #{Geminize.configuration.log_requests}"
|