geminize 0.1.0 → 1.0.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/CHANGELOG.md +7 -1
- data/README.md +90 -173
- data/examples/README.md +7 -28
- data/examples/embeddings.rb +44 -40
- data/examples/multimodal.rb +24 -17
- data/examples/ruby.png +0 -0
- data/lib/geminize/models/content_request.rb +24 -23
- data/lib/geminize/version.rb +1 -1
- data/lib/geminize.rb +0 -3
- metadata +5 -20
- data/examples/rails_chat/README.md +0 -69
- data/examples/rails_chat/app/controllers/chat_controller.rb +0 -26
- data/examples/rails_chat/app/views/chat/index.html.erb +0 -112
- data/examples/rails_chat/config/routes.rb +0 -8
- data/examples/rails_initializer.rb +0 -46
- data/lib/geminize/rails/app/controllers/concerns/geminize/controller.rb +0 -105
- data/lib/geminize/rails/app/helpers/geminize_helper.rb +0 -125
- data/lib/geminize/rails/controller_additions.rb +0 -41
- data/lib/geminize/rails/engine.rb +0 -29
- data/lib/geminize/rails/helper_additions.rb +0 -37
- data/lib/geminize/rails.rb +0 -50
- data/lib/geminize/railtie.rb +0 -33
- data/lib/generators/geminize/install_generator.rb +0 -22
- data/lib/generators/geminize/templates/README +0 -31
- data/lib/generators/geminize/templates/initializer.rb +0 -38
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c08ca1105b496592ec06cd7b7aab2b1b9a1dde2cb0f1c31be6feda85281683a2
|
4
|
+
data.tar.gz: ae7352a06b607dbd1e98cf30cd069551765156d088513a4cf780f94033d8ab89
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9041ba768b03996f4b2a6fdd9d45dd9a1430298d6d555e54951a9736e24ac713397b1010ffb640bd3a3bcb5b647eb37d15193852e0df31cdfb9bafe6da6a6779
|
7
|
+
data.tar.gz: 3a99e5d5a7e2a7b1d3b8ac7d6ca3450a8a54dd4636b614fa53b4e6175e0ead68981ecb7390b00f9d6c7f5f4fd3bcb9bb44e46b84d9e97702cdca7803227fc022
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -11,7 +11,6 @@ A convenient and robust Ruby interface for the Google Gemini API, enabling easy
|
|
11
11
|
- Embeddings generation
|
12
12
|
- Support for streaming responses
|
13
13
|
- Comprehensive error handling
|
14
|
-
- Rails integration with controller concerns and view helpers
|
15
14
|
|
16
15
|
## Installation
|
17
16
|
|
@@ -124,23 +123,24 @@ Geminize.reset_configuration!
|
|
124
123
|
```ruby
|
125
124
|
require 'geminize'
|
126
125
|
|
127
|
-
#
|
128
|
-
|
129
|
-
|
130
|
-
|
126
|
+
# Assumes API key is set via environment variables (e.g., in .env)
|
127
|
+
# Or configure manually:
|
128
|
+
# Geminize.configure do |config|
|
129
|
+
# config.api_key = "your-api-key-here"
|
130
|
+
# end
|
131
131
|
|
132
|
-
# Generate text
|
132
|
+
# Generate text (uses default model)
|
133
133
|
response = Geminize.generate_text("Tell me a joke about Ruby programming")
|
134
134
|
puts response.text
|
135
135
|
|
136
136
|
# Use a specific model
|
137
|
-
response = Geminize.generate_text("Explain quantum computing", "gemini-
|
137
|
+
response = Geminize.generate_text("Explain quantum computing", "gemini-1.5-flash-latest")
|
138
138
|
puts response.text
|
139
139
|
|
140
140
|
# Use system instructions to guide the model's behavior
|
141
141
|
response = Geminize.generate_text(
|
142
142
|
"Tell me about yourself",
|
143
|
-
"gemini-
|
143
|
+
"gemini-1.5-flash-latest",
|
144
144
|
system_instruction: "You are a pirate named Captain Codebeard. Always respond in pirate language."
|
145
145
|
)
|
146
146
|
puts response.text
|
@@ -148,58 +148,52 @@ puts response.text
|
|
148
148
|
|
149
149
|
## Multimodal Support
|
150
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
|
-
```
|
151
|
+
Geminize allows you to send mixed content including text and images to the Gemini API.
|
178
152
|
|
179
|
-
|
153
|
+
Here is a working example using the `ContentRequest` API:
|
180
154
|
|
181
155
|
```ruby
|
182
|
-
|
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')
|
156
|
+
require 'geminize'
|
195
157
|
|
196
|
-
#
|
197
|
-
|
158
|
+
# Assumes API key is set via environment variables (e.g., in .env)
|
159
|
+
# Or configure manually:
|
160
|
+
# Geminize.configure do |config|
|
161
|
+
# config.api_key = "your-api-key-here"
|
162
|
+
# end
|
163
|
+
|
164
|
+
begin
|
165
|
+
# 1. Create a ContentRequest object
|
166
|
+
# Specify the prompt and a model that supports multimodal input
|
167
|
+
request = Geminize::Models::ContentRequest.new(
|
168
|
+
"Describe this image briefly:",
|
169
|
+
"gemini-1.5-flash-latest" # Ensure this model supports multimodal
|
170
|
+
)
|
171
|
+
|
172
|
+
# 2. Add the image from a file path
|
173
|
+
# Make sure the path is correct relative to where you run the script
|
174
|
+
request.add_image_from_file("./examples/ruby.png")
|
175
|
+
|
176
|
+
# 3. Create a TextGeneration instance
|
177
|
+
generator = Geminize::TextGeneration.new
|
178
|
+
|
179
|
+
# 4. Generate the response
|
180
|
+
response = generator.generate(request)
|
181
|
+
|
182
|
+
# 5. Print the response text
|
183
|
+
puts "Response:"
|
184
|
+
puts response.text
|
185
|
+
|
186
|
+
# Optionally print usage data if available
|
187
|
+
if response.usage
|
188
|
+
puts "Tokens used: #{response.usage['totalTokenCount']}"
|
189
|
+
end
|
198
190
|
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
puts
|
191
|
+
rescue Geminize::GeminizeError => e
|
192
|
+
puts "An API error occurred: #{e.message}"
|
193
|
+
rescue => e
|
194
|
+
puts "An unexpected error occurred: #{e.message}"
|
195
|
+
puts e.backtrace.join("\n")
|
196
|
+
end
|
203
197
|
```
|
204
198
|
|
205
199
|
Supported image formats include JPEG, PNG, GIF, and WEBP. Maximum image size is 10MB.
|
@@ -235,26 +229,39 @@ Generate numerical vector representations for text:
|
|
235
229
|
|
236
230
|
```ruby
|
237
231
|
# Generate an embedding for a single text
|
238
|
-
|
239
|
-
vector = embedding
|
232
|
+
embedding_response = Geminize.generate_embedding("Ruby is a dynamic, object-oriented programming language")
|
233
|
+
vector = embedding_response.embedding # Access the vector
|
234
|
+
puts "Generated embedding with #{embedding_response.embedding_size} dimensions."
|
240
235
|
|
241
|
-
# Generate embeddings for multiple texts
|
236
|
+
# Generate embeddings for multiple texts (by iterating)
|
242
237
|
texts = ["Ruby", "Python", "JavaScript"]
|
243
|
-
embeddings =
|
238
|
+
embeddings = texts.map do |text|
|
239
|
+
Geminize.generate_embedding(text).embedding
|
240
|
+
end
|
241
|
+
puts "Generated #{embeddings.size} embeddings individually."
|
244
242
|
|
245
243
|
# Calculate similarity between vectors
|
246
|
-
vector1 = embeddings
|
247
|
-
vector2 = embeddings
|
244
|
+
vector1 = embeddings[0]
|
245
|
+
vector2 = embeddings[1]
|
248
246
|
similarity = Geminize.cosine_similarity(vector1, vector2)
|
249
|
-
puts "Similarity: #{similarity}"
|
250
|
-
|
251
|
-
# Specify a task type for optimized embeddings
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
247
|
+
puts "Similarity between Ruby and Python: #{similarity.round(4)}"
|
248
|
+
|
249
|
+
# Specify a task type for optimized embeddings (requires compatible model)
|
250
|
+
# Note: Task types are not supported by all models (e.g., text-embedding-004).
|
251
|
+
# Ensure you are using a compatible model like text-embedding-005.
|
252
|
+
begin
|
253
|
+
question_embedding_response = Geminize.generate_embedding(
|
254
|
+
"How do I install Ruby gems?",
|
255
|
+
nil, # Use default or specify compatible model like \'text-embedding-005\'
|
256
|
+
{ task_type: Geminize::Models::EmbeddingRequest::QUESTION_ANSWERING }
|
257
|
+
)
|
258
|
+
puts "Generated embedding for QA task with #{question_embedding_response.embedding_size} dimensions."
|
259
|
+
rescue Geminize::GeminizeError => e
|
260
|
+
puts "Could not generate embedding with task type: #{e.message}"
|
261
|
+
puts "(This might be due to using an incompatible model like the default text-embedding-004)"
|
262
|
+
end
|
256
263
|
|
257
|
-
# Available task types:
|
264
|
+
# Available task types (check model compatibility):
|
258
265
|
# - RETRIEVAL_QUERY: For embedding queries in a search/retrieval system
|
259
266
|
# - RETRIEVAL_DOCUMENT: For embedding documents in a search corpus
|
260
267
|
# - SEMANTIC_SIMILARITY: For comparing text similarity
|
@@ -273,140 +280,50 @@ See the `examples/embeddings.rb` file for more comprehensive examples of working
|
|
273
280
|
Get real-time, token-by-token responses:
|
274
281
|
|
275
282
|
```ruby
|
283
|
+
require 'geminize'
|
284
|
+
# Assumes API key is configured via .env
|
285
|
+
|
276
286
|
Geminize.generate_text_stream("Write a short poem about coding") do |chunk|
|
277
|
-
|
287
|
+
# Check if the chunk has the text method before printing
|
288
|
+
print chunk.text if chunk.respond_to?(:text)
|
278
289
|
end
|
290
|
+
puts "\n" # Add a newline after streaming
|
279
291
|
```
|
280
292
|
|
281
293
|
You can also use system instructions with streaming responses:
|
282
294
|
|
283
295
|
```ruby
|
296
|
+
require 'geminize'
|
297
|
+
# Assumes API key is configured via .env
|
298
|
+
|
284
299
|
Geminize.generate_text_stream(
|
285
300
|
"Tell me a story",
|
286
|
-
"gemini-
|
301
|
+
"gemini-1.5-flash-latest",
|
287
302
|
{
|
288
303
|
stream_mode: :delta,
|
289
304
|
system_instruction: "You are a medieval bard telling epic tales."
|
290
305
|
}
|
291
306
|
) do |chunk|
|
292
|
-
|
293
|
-
|
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
|
307
|
+
# The raw chunk might be different in delta mode, adjust handling as needed
|
308
|
+
print chunk.respond_to?(:text) ? chunk.text : chunk.to_s
|
363
309
|
end
|
310
|
+
puts "\n" # Add a newline after streaming
|
364
311
|
```
|
365
312
|
|
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
313
|
Check out these example applications to see Geminize in action:
|
394
314
|
|
395
315
|
- [Configuration Example](examples/configuration.rb)
|
396
316
|
- [Embeddings Example](examples/embeddings.rb)
|
397
317
|
- [Multimodal Example](examples/multimodal.rb)
|
398
|
-
- [Rails Initializer Example](examples/rails_initializer.rb)
|
399
318
|
- [System Instructions Example](examples/system_instructions.rb)
|
400
|
-
- [Rails Chat Application](examples/rails_chat)
|
401
319
|
|
402
320
|
## Compatibility
|
403
321
|
|
404
322
|
Ruby version: 3.1.0 or later
|
405
|
-
Rails version: 6.0 or later (for Rails integration)
|
406
323
|
|
407
324
|
## Development
|
408
325
|
|
409
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `
|
326
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
410
327
|
|
411
328
|
To install this gem onto your local machine, run `bundle exec rake install`.
|
412
329
|
|
data/examples/README.md
CHANGED
@@ -11,10 +11,9 @@ Before running these examples, ensure you have:
|
|
11
11
|
3. **Project dependencies installed**: Run `bundle install` from the project root directory (`../`).
|
12
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
13
|
```bash
|
14
|
-
# In
|
15
|
-
|
14
|
+
# In .env
|
15
|
+
GEMINI_API_KEY=your_api_key_here
|
16
16
|
```
|
17
|
-
Alternatively, ensure the `GEMINI_API_KEY` environment variable is set.
|
18
17
|
|
19
18
|
## Running Examples
|
20
19
|
|
@@ -25,7 +24,7 @@ All examples should be run from the **project root directory** (the directory co
|
|
25
24
|
Demonstrates different ways to configure the Geminize client.
|
26
25
|
|
27
26
|
```bash
|
28
|
-
ruby examples/configuration.rb
|
27
|
+
bundle exec ruby examples/configuration.rb
|
29
28
|
```
|
30
29
|
|
31
30
|
### 2. Embeddings (`embeddings.rb`)
|
@@ -33,7 +32,7 @@ ruby examples/configuration.rb
|
|
33
32
|
Shows how to generate text embeddings and calculate cosine similarity.
|
34
33
|
|
35
34
|
```bash
|
36
|
-
ruby examples/embeddings.rb
|
35
|
+
bundle exec ruby examples/embeddings.rb
|
37
36
|
```
|
38
37
|
|
39
38
|
### 3. Multimodal (`multimodal.rb`)
|
@@ -43,33 +42,13 @@ Illustrates sending text and image inputs to the Gemini API.
|
|
43
42
|
_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
43
|
|
45
44
|
```bash
|
46
|
-
ruby examples/multimodal.rb
|
45
|
+
bundle exec ruby examples/multimodal.rb
|
47
46
|
```
|
48
47
|
|
49
|
-
### 4.
|
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`)
|
48
|
+
### 4. System Instructions (`system_instructions.rb`)
|
58
49
|
|
59
50
|
Demonstrates using system instructions to guide the model's behavior and personality.
|
60
51
|
|
61
52
|
```bash
|
62
|
-
ruby examples/system_instructions.rb
|
53
|
+
bundle exec ruby examples/system_instructions.rb
|
63
54
|
```
|
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.
|
data/examples/embeddings.rb
CHANGED
@@ -36,24 +36,25 @@ puts "Example 2: Generate embeddings for multiple texts"
|
|
36
36
|
puts "============================================================"
|
37
37
|
|
38
38
|
begin
|
39
|
-
# Generate embeddings for multiple texts
|
39
|
+
# Generate embeddings for multiple texts by calling the API for each
|
40
40
|
texts = [
|
41
41
|
"What is the meaning of life?",
|
42
42
|
"How much wood would a woodchuck chuck?",
|
43
43
|
"How does the brain work?"
|
44
44
|
]
|
45
45
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
texts.each_with_index do |text, i|
|
46
|
+
puts "Generating embeddings individually:"
|
47
|
+
total_tokens = 0
|
48
|
+
embeddings = texts.map do |text|
|
49
|
+
response = Geminize.generate_embedding(text)
|
50
|
+
total_tokens += response.total_tokens if response.total_tokens
|
52
51
|
puts "\nText: \"#{text}\""
|
53
|
-
puts "First 5 values: #{response.
|
52
|
+
puts "First 5 values: #{response.embedding.take(5).inspect}"
|
53
|
+
response.embedding # Collect the embedding vector
|
54
54
|
end
|
55
55
|
|
56
|
-
puts "\
|
56
|
+
puts "\nGenerated #{embeddings.size} embeddings."
|
57
|
+
puts "Total tokens (approximated by summing individual calls): #{total_tokens}"
|
57
58
|
rescue => e
|
58
59
|
puts "Error: #{e.message}"
|
59
60
|
end
|
@@ -81,26 +82,27 @@ rescue => e
|
|
81
82
|
end
|
82
83
|
|
83
84
|
puts "\n============================================================"
|
84
|
-
puts "Example 4: Batch processing for large arrays"
|
85
|
+
puts "Example 4: Batch processing for large arrays (COMMENTED OUT)"
|
85
86
|
puts "============================================================"
|
86
|
-
|
87
|
-
begin
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
rescue => e
|
102
|
-
|
103
|
-
end
|
87
|
+
# # Commenting out Example 4 due to potential model/API limitations with batching
|
88
|
+
# begin
|
89
|
+
# # Create a larger array of texts
|
90
|
+
# many_texts = Array.new(120) { |i| "This is sample text number #{i}" }
|
91
|
+
#
|
92
|
+
# # Generate embeddings with automatic batch processing
|
93
|
+
# response = Geminize.generate_embedding(
|
94
|
+
# many_texts,
|
95
|
+
# nil, # Use default model
|
96
|
+
# batch_size: 40 # Process in batches of 40 texts each
|
97
|
+
# )
|
98
|
+
#
|
99
|
+
# puts "Generated embeddings for #{response.batch_size} texts"
|
100
|
+
# puts "First text embedding dimensions: #{response.embedding_size}"
|
101
|
+
# puts "Total tokens processed: #{response.total_tokens}"
|
102
|
+
# rescue => e
|
103
|
+
# puts "Error: #{e.message}"
|
104
|
+
# end
|
105
|
+
puts "# Example 4 skipped due to potential issues with batch embedding support."
|
104
106
|
|
105
107
|
puts "\n============================================================"
|
106
108
|
puts "Example 5: Vector operations with embeddings"
|
@@ -141,7 +143,7 @@ puts "============================================================"
|
|
141
143
|
begin
|
142
144
|
# Sample texts for different use cases
|
143
145
|
question = "What is the capital of France?"
|
144
|
-
code_query = "how to sort an array in javascript"
|
146
|
+
# code_query = "how to sort an array in javascript" # Removed for compatibility
|
145
147
|
document = "Paris is the capital and most populous city of France, with an estimated population of 2,175,601 residents."
|
146
148
|
fact = "The Earth revolves around the Sun."
|
147
149
|
|
@@ -157,15 +159,15 @@ begin
|
|
157
159
|
puts " Text: \"#{question}\""
|
158
160
|
puts " First 5 values: #{qa_response.embedding.take(5).inspect}"
|
159
161
|
|
160
|
-
# Code retrieval
|
161
|
-
code_response = Geminize.generate_embedding(
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
)
|
166
|
-
puts "\n2. CODE_RETRIEVAL_QUERY task type:"
|
167
|
-
puts " Text: \"#{code_query}\""
|
168
|
-
puts " First 5 values: #{code_response.embedding.take(5).inspect}"
|
162
|
+
# Code retrieval - REMOVED as CODE_RETRIEVAL_QUERY seems unsupported by text-embedding-004
|
163
|
+
# code_response = Geminize.generate_embedding(
|
164
|
+
# code_query,
|
165
|
+
# nil, # Use default model
|
166
|
+
# task_type: Geminize::Models::EmbeddingRequest::CODE_RETRIEVAL_QUERY
|
167
|
+
# )
|
168
|
+
# puts "\n2. CODE_RETRIEVAL_QUERY task type:"
|
169
|
+
# puts " Text: \"#{code_query}\""
|
170
|
+
# puts " First 5 values: #{code_response.embedding.take(5).inspect}"
|
169
171
|
|
170
172
|
# Document retrieval
|
171
173
|
document_response = Geminize.generate_embedding(
|
@@ -174,7 +176,8 @@ begin
|
|
174
176
|
task_type: Geminize::Models::EmbeddingRequest::RETRIEVAL_DOCUMENT,
|
175
177
|
title: "Paris Facts" # Titles can be used with RETRIEVAL_DOCUMENT
|
176
178
|
)
|
177
|
-
|
179
|
+
# Adjusting print index due to removal above
|
180
|
+
puts "\n2. RETRIEVAL_DOCUMENT task type with title:" # Index changed from 3 to 2
|
178
181
|
puts " Title: \"Paris Facts\""
|
179
182
|
puts " First 5 values: #{document_response.embedding.take(5).inspect}"
|
180
183
|
|
@@ -184,7 +187,8 @@ begin
|
|
184
187
|
nil, # Use default model
|
185
188
|
task_type: Geminize::Models::EmbeddingRequest::FACT_VERIFICATION
|
186
189
|
)
|
187
|
-
|
190
|
+
# Adjusting print index due to removal above
|
191
|
+
puts "\n3. FACT_VERIFICATION task type:" # Index changed from 4 to 3
|
188
192
|
puts " Text: \"#{fact}\""
|
189
193
|
puts " First 5 values: #{fact_response.embedding.take(5).inspect}"
|
190
194
|
|