class RubyLLM::Providers::Gemini
Native Gemini API implementation
Public Class Methods
Source
# File lib/ruby_llm/providers/gemini.rb, line 27 def capabilities Gemini::Capabilities end
Source
# File lib/ruby_llm/providers/gemini.rb, line 31 def configuration_options %i[gemini_api_key gemini_api_base] end
# File lib/ruby_llm/providers/gemini.rb, line 35 def configuration_requirements %i[gemini_api_key] end
Public Instance Methods
Source
# File lib/ruby_llm/providers/gemini.rb, line 16 def api_base @config.gemini_api_base || 'https://generativelanguage.googleapis.com/v1beta' end
# File lib/ruby_llm/providers/gemini/media.rb, line 108 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
# File lib/ruby_llm/providers/gemini/media.rb, line 100 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
# File lib/ruby_llm/providers/gemini/media.rb, line 84 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
# File lib/ruby_llm/providers/gemini/media.rb, line 61 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
Source
# File lib/ruby_llm/providers/gemini.rb, line 20 def headers { 'x-goog-api-key' => @config.gemini_api_key } end