Class: RubyLLM::Protocols::Gemini

Inherits:
RubyLLM::Protocol show all
Includes:
Gemini::Caches, Gemini::Chat, Gemini::Embeddings, Gemini::Images, Gemini::Media, Gemini::Models, Gemini::Speech, Gemini::Streaming, Gemini::Tools, Gemini::Transcription, Gemini::Videos
Defined in:
lib/ruby_llm/protocols/gemini.rb,
lib/ruby_llm/protocols/gemini/chat.rb,
lib/ruby_llm/protocols/gemini/files.rb,
lib/ruby_llm/protocols/gemini/media.rb,
lib/ruby_llm/protocols/gemini/tools.rb,
lib/ruby_llm/protocols/gemini/caches.rb,
lib/ruby_llm/protocols/gemini/images.rb,
lib/ruby_llm/protocols/gemini/models.rb,
lib/ruby_llm/protocols/gemini/speech.rb,
lib/ruby_llm/protocols/gemini/videos.rb,
lib/ruby_llm/protocols/gemini/batches.rb,
lib/ruby_llm/protocols/gemini/streaming.rb,
lib/ruby_llm/protocols/gemini/embeddings.rb,
lib/ruby_llm/protocols/gemini/transcription.rb,
lib/ruby_llm/protocols/gemini/embedding_batches.rb,
lib/ruby_llm/protocols/gemini/file_transcription.rb,
lib/ruby_llm/protocols/gemini/live_transcription.rb

Overview

rubocop:disable Style/Documentation

Defined Under Namespace

Modules: Batches, Caches, Chat, EmbeddingBatches, Embeddings, FileTranscription, Images, Media, Models, Speech, Streaming, Tools, Transcription, Videos Classes: Files, LiveTranscription

Constant Summary collapse

SERVER_TOOL_ALIASES =

Gemini nests each tool's options inside its key, so aliases are lambdas placing the options there.

%i[
  google_search url_context code_execution file_search google_maps
].to_h do |tool_key|
  [tool_key, ->(options) { { tool: { tool_key => Support::Utils.deep_symbolize_keys(options) } } }]
end.merge(
  web_search: ->(options) { { tool: { google_search: Support::Utils.deep_symbolize_keys(options) } } },
  web_fetch: ->(options) { { tool: { url_context: Support::Utils.deep_symbolize_keys(options) } } }
).freeze

Instance Attribute Summary

Attributes inherited from RubyLLM::Protocol

#config, #connection, #model, #provider

Instance Method Summary collapse

Methods inherited from RubyLLM::Protocol

abstract, #animate_later, #apply_compaction, #apply_compaction_headers, #apply_end_user, #cache_content, #compact, #complete, #count_tokens, #delete_cache, #embed, #extend_cache, #find_cache, #initialize, #list_models, #moderate, #ocr, #paint, #parse_error, #parse_image_responses, #post_image, #post_video, #preprocess_message, #raise_transcription_streaming_unsupported, #refresh_video_job, #render, #render_embedding, #render_transcription_options, #render_video_extension_payload, #rerank, #speak, #stream_speech, #stream_speech_response, #stream_transcription, #supports_embedding_media?, #tokenize, #tool_approval_response, #transcribe, #video_extension_attachment, #video_extension_url, #video_request_url

Methods included from RubyLLM::Protocol::BinaryStreaming

#stream_binary

Methods included from RubyLLM::Protocol::Streaming

build_on_data_handler, build_stream_error_response, error_chunk?, failed_http_status, faraday_1?, handle_data, handle_error_chunk, handle_error_event, handle_failed_response, handle_json_error_chunk, handle_sse, handle_stream, json_error_payload?, parse_error_from_json, parse_streaming_error, process_stream_chunk, raise_stream_error, stream_events, stream_response, stream_state

Constructor Details

This class inherits a constructor from RubyLLM::Protocol

Instance Method Details

#attachment_filename(mime_type, index) ⇒ Object



114
115
116
117
118
119
120
121
122
# File 'lib/ruby_llm/protocols/gemini/media.rb', line 114

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



106
107
108
109
110
111
112
# File 'lib/ruby_llm/protocols/gemini/media.rb', line 106

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



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/ruby_llm/protocols/gemini/media.rb', line 90

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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ruby_llm/protocols/gemini/media.rb', line 69

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?
  [text, attachments]
end

#server_tool_aliasesObject



30
31
32
# File 'lib/ruby_llm/protocols/gemini.rb', line 30

def server_tool_aliases
  SERVER_TOOL_ALIASES
end