Class: RailsAi::Providers::GeminiAdapter
- Defined in:
- lib/rails_ai/providers/gemini_adapter.rb
Constant Summary collapse
- GEMINI_API_BASE =
"https://generativelanguage.googleapis.com/v1beta"
Instance Method Summary collapse
-
#analyze_image!(image:, prompt:, model: "gemini-2.0-flash-exp", **opts) ⇒ Object
Multimodal analysis - Gemini supports image and video analysis.
- #analyze_video!(video:, prompt:, model: "gemini-2.0-flash-exp", **opts) ⇒ Object
-
#chat!(messages:, model:, **opts) ⇒ Object
Text-based operations.
- #create_variation!(image:, **opts) ⇒ Object
- #edit_image!(image:, prompt:, **opts) ⇒ Object
- #edit_video!(video:, prompt:, **opts) ⇒ Object
- #embed!(texts:, model:, **opts) ⇒ Object
-
#generate_image!(prompt:, model: "gemini-2.0-flash-exp", **opts) ⇒ Object
Image generation - Gemini 2.0 Flash supports image generation.
-
#generate_speech!(text:, model: "gemini-2.0-flash-exp", **opts) ⇒ Object
Audio generation - Gemini 2.0 Flash supports audio generation.
-
#generate_video!(prompt:, model: "gemini-2.0-flash-exp", **opts) ⇒ Object
Video generation - Gemini 2.0 Flash supports video generation.
-
#initialize ⇒ GeminiAdapter
constructor
A new instance of GeminiAdapter.
- #stream_chat!(messages:, model:, **opts, &on_token) ⇒ Object
- #transcribe_audio!(audio:, model: "gemini-2.0-flash-exp", **opts) ⇒ Object
Constructor Details
#initialize ⇒ GeminiAdapter
Returns a new instance of GeminiAdapter.
12 13 14 15 |
# File 'lib/rails_ai/providers/gemini_adapter.rb', line 12 def initialize @api_key = ENV.fetch("GEMINI_API_KEY") super end |
Instance Method Details
#analyze_image!(image:, prompt:, model: "gemini-2.0-flash-exp", **opts) ⇒ Object
Multimodal analysis - Gemini supports image and video analysis
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 |
# File 'lib/rails_ai/providers/gemini_adapter.rb', line 351 def analyze_image!(image:, prompt:, model: "gemini-2.0-flash-exp", **opts) return "[stubbed] Image analysis: #{prompt}" if RailsAi.config.stub_responses contents = [ { parts: [ { text: prompt }, { inlineData: { mimeType: detect_image_type(image), data: extract_base64_data(image) } } ] } ] response = make_request( "models/#{model}:generateContent", { contents: contents, generationConfig: { maxOutputTokens: opts[:max_tokens] || RailsAi.config.token_limit, temperature: opts[:temperature] || 0.7, topP: opts[:top_p] || 0.8, topK: opts[:top_k] || 40, **opts.except(:max_tokens, :temperature, :top_p, :top_k) } } ) response.dig("candidates", 0, "content", "parts", 0, "text") end |
#analyze_video!(video:, prompt:, model: "gemini-2.0-flash-exp", **opts) ⇒ Object
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 |
# File 'lib/rails_ai/providers/gemini_adapter.rb', line 385 def analyze_video!(video:, prompt:, model: "gemini-2.0-flash-exp", **opts) return "[stubbed] Video analysis: #{prompt}" if RailsAi.config.stub_responses contents = [ { parts: [ { text: prompt }, { inlineData: { mimeType: "video/mp4", data: extract_base64_data(video) } } ] } ] response = make_request( "models/#{model}:generateContent", { contents: contents, generationConfig: { maxOutputTokens: opts[:max_tokens] || RailsAi.config.token_limit, temperature: opts[:temperature] || 0.7, topP: opts[:top_p] || 0.8, topK: opts[:top_k] || 40, **opts.except(:max_tokens, :temperature, :top_p, :top_k) } } ) response.dig("candidates", 0, "content", "parts", 0, "text") end |
#chat!(messages:, model:, **opts) ⇒ Object
Text-based operations
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/rails_ai/providers/gemini_adapter.rb', line 18 def chat!(messages:, model:, **opts) return "(stubbed) #{messages.last[:content]}" if RailsAi.config.stub_responses # Convert OpenAI format to Gemini format = () response = make_request( "models/#{model}:generateContent", { contents: , generationConfig: { maxOutputTokens: opts[:max_tokens] || RailsAi.config.token_limit, temperature: opts[:temperature] || 0.7, topP: opts[:top_p] || 0.8, topK: opts[:top_k] || 40, **opts.except(:max_tokens, :temperature, :top_p, :top_k) } } ) response.dig("candidates", 0, "content", "parts", 0, "text") end |
#create_variation!(image:, **opts) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/rails_ai/providers/gemini_adapter.rb', line 173 def create_variation!(image:, **opts) return "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==" if RailsAi.config.stub_responses # Create variations using Gemini 2.0 Flash variation_prompt = "Create a variation of this image with similar style but different composition." contents = [ { parts: [ { text: variation_prompt }, { inlineData: { mimeType: detect_image_type(image), data: extract_base64_data(image) } } ] } ] response = make_request( "models/gemini-2.0-flash-exp:generateContent", { contents: contents, generationConfig: { maxOutputTokens: 1000, temperature: opts[:temperature] || 0.8, **opts } } ) image_data = response.dig("candidates", 0, "content", "parts", 0, "inlineData", "data") if image_data "data:image/png;base64,#{image_data}" else raise "Image variation failed: No image data in response" end end |
#edit_image!(image:, prompt:, **opts) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/rails_ai/providers/gemini_adapter.rb', line 132 def edit_image!(image:, prompt:, **opts) return "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==" if RailsAi.config.stub_responses # Gemini doesn't have direct image editing, but we can use it to generate variations image_prompt = "Edit this image: #{prompt}. Show the edited version." contents = [ { parts: [ { text: image_prompt }, { inlineData: { mimeType: detect_image_type(image), data: extract_base64_data(image) } } ] } ] response = make_request( "models/gemini-2.0-flash-exp:generateContent", { contents: contents, generationConfig: { maxOutputTokens: 1000, temperature: opts[:temperature] || 0.7, **opts } } ) # Extract generated image data image_data = response.dig("candidates", 0, "content", "parts", 0, "inlineData", "data") if image_data "data:image/png;base64,#{image_data}" else raise "Image editing failed: No image data in response" end end |
#edit_video!(video:, prompt:, **opts) ⇒ Object
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/rails_ai/providers/gemini_adapter.rb', line 245 def edit_video!(video:, prompt:, **opts) return "data:video/mp4;base64,AAAAIGZ0eXBpc29tAAACAGlzb21pc28yYXZjMW1wNDEAAAAIZnJlZQAAAB1tZGF0AQAAARxtYXNrAAAAAG1wNDEAAAAAIG1kYXQ=" if RailsAi.config.stub_responses video_prompt = "Edit this video: #{prompt}. Show the edited version." contents = [ { parts: [ { text: video_prompt }, { inlineData: { mimeType: "video/mp4", data: extract_base64_data(video) } } ] } ] response = make_request( "models/gemini-2.0-flash-exp:generateContent", { contents: contents, generationConfig: { maxOutputTokens: 1000, temperature: opts[:temperature] || 0.7, **opts } } ) video_data = response.dig("candidates", 0, "content", "parts", 0, "inlineData", "data") if video_data "data:video/mp4;base64,#{video_data}" else raise "Video editing failed: No video data in response" end end |
#embed!(texts:, model:, **opts) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/rails_ai/providers/gemini_adapter.rb', line 65 def (texts:, model:, **opts) return Array.new(texts.length) { [0.0] * 768 } if RailsAi.config.stub_responses # Gemini has embedding models response = make_request( "models/#{model}:embedContent", { content: { parts: texts.map { |text| { text: text } } } } ) # Handle both single and batch embedding responses if texts.length == 1 [response.dig("embedding", "values")] else # For multiple texts, we need to make separate requests or use batch embedding texts.map do |text| single_response = make_request( "models/#{model}:embedContent", { content: { parts: [{ text: text }] } } ) single_response.dig("embedding", "values") end end end |
#generate_image!(prompt:, model: "gemini-2.0-flash-exp", **opts) ⇒ Object
Image generation - Gemini 2.0 Flash supports image generation
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/rails_ai/providers/gemini_adapter.rb', line 98 def generate_image!(prompt:, model: "gemini-2.0-flash-exp", **opts) return "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==" if RailsAi.config.stub_responses # Use Gemini 2.0 Flash for image generation response = make_request( "models/#{model}:generateContent", { contents: [ { parts: [ { text: "Generate an image: #{prompt}" } ] } ], generationConfig: { maxOutputTokens: 1000, temperature: opts[:temperature] || 0.7, **opts } } ) # Extract image data from response image_data = response.dig("candidates", 0, "content", "parts", 0, "inlineData", "data") if image_data "data:image/png;base64,#{image_data}" else # Fallback: return a placeholder or raise error raise "Image generation failed: No image data in response" end end |
#generate_speech!(text:, model: "gemini-2.0-flash-exp", **opts) ⇒ Object
Audio generation - Gemini 2.0 Flash supports audio generation
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/rails_ai/providers/gemini_adapter.rb', line 285 def generate_speech!(text:, model: "gemini-2.0-flash-exp", **opts) return "data:audio/mp3;base64,SUQzBAAAAAAAI1RTU0UAAAAPAAADTGF2ZjU4Ljc2LjEwMAAAAAAAAAAAAAAA//tQxAADB8AhSmAhIIEVWWWU" if RailsAi.config.stub_responses response = make_request( "models/#{model}:generateContent", { contents: [ { parts: [ { text: "Generate speech for: #{text}" } ] } ], generationConfig: { maxOutputTokens: 1000, temperature: opts[:temperature] || 0.7, **opts } } ) audio_data = response.dig("candidates", 0, "content", "parts", 0, "inlineData", "data") if audio_data "data:audio/mp3;base64,#{audio_data}" else raise "Speech generation failed: No audio data in response" end end |
#generate_video!(prompt:, model: "gemini-2.0-flash-exp", **opts) ⇒ Object
Video generation - Gemini 2.0 Flash supports video generation
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/rails_ai/providers/gemini_adapter.rb', line 214 def generate_video!(prompt:, model: "gemini-2.0-flash-exp", **opts) return "data:video/mp4;base64,AAAAIGZ0eXBpc29tAAACAGlzb21pc28yYXZjMW1wNDEAAAAIZnJlZQAAAB1tZGF0AQAAARxtYXNrAAAAAG1wNDEAAAAAIG1kYXQ=" if RailsAi.config.stub_responses response = make_request( "models/#{model}:generateContent", { contents: [ { parts: [ { text: "Generate a video: #{prompt}" } ] } ], generationConfig: { maxOutputTokens: 1000, temperature: opts[:temperature] || 0.7, **opts } } ) video_data = response.dig("candidates", 0, "content", "parts", 0, "inlineData", "data") if video_data "data:video/mp4;base64,#{video_data}" else raise "Video generation failed: No video data in response" end end |
#stream_chat!(messages:, model:, **opts, &on_token) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/rails_ai/providers/gemini_adapter.rb', line 41 def stream_chat!(messages:, model:, **opts, &on_token) return on_token.call("(stubbed stream)") if RailsAi.config.stub_responses # Convert OpenAI format to Gemini format = () make_streaming_request( "models/#{model}:streamGenerateContent", { contents: , generationConfig: { maxOutputTokens: opts[:max_tokens] || RailsAi.config.token_limit, temperature: opts[:temperature] || 0.7, topP: opts[:top_p] || 0.8, topK: opts[:top_k] || 40, **opts.except(:max_tokens, :temperature, :top_p, :top_k) } } ) do |chunk| text = chunk.dig("candidates", 0, "content", "parts", 0, "text") on_token.call(text) if text end end |
#transcribe_audio!(audio:, model: "gemini-2.0-flash-exp", **opts) ⇒ Object
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 |
# File 'lib/rails_ai/providers/gemini_adapter.rb', line 316 def transcribe_audio!(audio:, model: "gemini-2.0-flash-exp", **opts) return "[stubbed transcription]" if RailsAi.config.stub_responses contents = [ { parts: [ { text: "Transcribe this audio:" }, { inlineData: { mimeType: "audio/mp3", data: extract_base64_data(audio) } } ] } ] response = make_request( "models/#{model}:generateContent", { contents: contents, generationConfig: { maxOutputTokens: 1000, temperature: opts[:temperature] || 0.1, **opts } } ) response.dig("candidates", 0, "content", "parts", 0, "text") end |