Class: RubyLLM::Providers::Gemini

Inherits:
RubyLLM::Provider show all
Includes:
Chat, Embeddings, Images, Media, Models, Streaming, Tools, Transcription
Defined in:
lib/ruby_llm/providers/gemini.rb,
lib/ruby_llm/providers/gemini/chat.rb,
lib/ruby_llm/providers/gemini/media.rb,
lib/ruby_llm/providers/gemini/tools.rb,
lib/ruby_llm/providers/gemini/images.rb,
lib/ruby_llm/providers/gemini/models.rb,
lib/ruby_llm/providers/gemini/streaming.rb,
lib/ruby_llm/providers/gemini/embeddings.rb,
lib/ruby_llm/providers/gemini/capabilities.rb,
lib/ruby_llm/providers/gemini/transcription.rb

Overview

rubocop:disable Style/Documentation

Direct Known Subclasses

VertexAI

Defined Under Namespace

Modules: Capabilities, Chat, Embeddings, Images, Media, Models, Streaming, Tools, Transcription

Constant Summary

Constants included from Transcription

Transcription::DEFAULT_PROMPT

Instance Attribute Summary

Attributes inherited from RubyLLM::Provider

#config, #connection

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Media

format_attachment, format_content, format_text, format_text_file

Methods included from Tools

#extract_tool_calls, #format_tool_call, #format_tool_result, #format_tools

Methods included from Streaming

#build_chunk, #stream_url

Methods included from Transcription

#transcribe

Methods included from Models

models_url, parse_list_models_response

Methods included from Images

#images_url, #parse_image_response, #render_image_payload

Methods included from Embeddings

embedding_url, parse_embedding_response, render_embedding_payload

Methods included from Chat

completion_url, render_payload

Methods inherited from RubyLLM::Provider

#capabilities, #complete, #configuration_requirements, configured?, #configured?, configured_providers, configured_remote_providers, #embed, for, #format_messages, #format_tool_calls, #initialize, #list_models, #local?, local?, local_providers, #moderate, #name, name, #paint, #parse_error, #parse_tool_calls, providers, register, remote?, #remote?, remote_providers, resolve, slug, #slug, #transcribe

Methods included from Streaming

handle_stream, stream_response

Constructor Details

This class inherits a constructor from RubyLLM::Provider

Class Method Details

.capabilitiesObject



27
28
29
# File 'lib/ruby_llm/providers/gemini.rb', line 27

def capabilities
  Gemini::Capabilities
end

.configuration_requirementsObject



31
32
33
# File 'lib/ruby_llm/providers/gemini.rb', line 31

def configuration_requirements
  i[gemini_api_key]
end

Instance Method Details

#api_baseObject



16
17
18
# File 'lib/ruby_llm/providers/gemini.rb', line 16

def api_base
  @config.gemini_api_base || 'https://generativelanguage.googleapis.com/v1beta'
end

#attachment_filename(mime_type, index) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/ruby_llm/providers/gemini/media.rb', line 101

def attachment_filename(mime_type, index)
  return "gemini_attachment_#{index + 1}" unless mime_type

  extension = mime_type.split('/').last.to_s
  extension = 'jpg' if extension == 'jpeg'
  extension = 'txt' if extension == 'plain'
  extension = extension.tr('+', '.')
  "gemini_attachment_#{index + 1}.#{extension}"
end

#build_file_attachment(file_data, index) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/ruby_llm/providers/gemini/media.rb', line 93

def build_file_attachment(file_data, index)
  uri = file_data['fileUri']
  return unless uri

  filename = file_data['filename'] || attachment_filename(file_data['mimeType'], index)
  RubyLLM::Attachment.new(uri, filename:)
end

#build_inline_attachment(inline_data, index) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ruby_llm/providers/gemini/media.rb', line 77

def build_inline_attachment(inline_data, index)
  encoded = inline_data['data']
  return unless encoded

  mime_type = inline_data['mimeType']
  decoded = Base64.decode64(encoded)
  io = StringIO.new(decoded)
  io.set_encoding(Encoding::BINARY) if io.respond_to?(:set_encoding)

  filename = attachment_filename(mime_type, index)
  RubyLLM::Attachment.new(io, filename:)
rescue ArgumentError => e
  RubyLLM.logger.warn "Failed to decode Gemini inline data attachment: #{e.message}"
  nil
end

#build_response_content(parts) ⇒ Object

rubocop:disable Metrics/PerceivedComplexity



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ruby_llm/providers/gemini/media.rb', line 54

def build_response_content(parts) # rubocop:disable Metrics/PerceivedComplexity
  text = []
  attachments = []

  parts.each_with_index do |part, index|
    if part['text']
      text << part['text']
    elsif part['inlineData']
      attachment = build_inline_attachment(part['inlineData'], index)
      attachments << attachment if attachment
    elsif part['fileData']
      attachment = build_file_attachment(part['fileData'], index)
      attachments << attachment if attachment
    end
  end

  text = text.join
  text = nil if text.empty?
  return text if attachments.empty?

  Content.new(text:, attachments:)
end

#headersObject



20
21
22
23
24
# File 'lib/ruby_llm/providers/gemini.rb', line 20

def headers
  {
    'x-goog-api-key' => @config.gemini_api_key
  }
end