class RubyLLM::Attachment
A class representing a file attachment.
Constants
- ACTIVE_STORAGE_CLASS_NAMES
- DOCUMENT_EXTENSIONS
Attributes
Public Class Methods
Source
# File lib/ruby_llm/attachment.rb, line 22 def initialize(source, filename: nil) @source = source @source = source_type_cast @filename = filename || source_filename determine_mime_type end
Public Instance Methods
Source
# File lib/ruby_llm/attachment.rb, line 42 def active_storage? ACTIVE_STORAGE_CLASS_NAMES.any? { |class_name| source_is_a?(class_name) } end
Source
# File lib/ruby_llm/attachment.rb, line 105 def audio? RubyLLM::MimeType.audio? mime_type end
Source
# File lib/ruby_llm/attachment.rb, line 46 def content return @content if defined?(@content) && !@content.nil? if url? fetch_content elsif path? load_content_from_path elsif active_storage? load_content_from_active_storage elsif io_like? load_content_from_io else RubyLLM.logger.warn "Source is neither a URL, path, ActiveStorage, nor IO-like: #{@source.class}" nil end @content end
Source
# File lib/ruby_llm/attachment.rb, line 124 def document? return false if pdf? || text? RubyLLM::MimeType.document?(mime_type) || DOCUMENT_EXTENSIONS.include?(extension) end
Source
# File lib/ruby_llm/attachment.rb, line 65 def encoded Base64.strict_encode64(content) end
Source
# File lib/ruby_llm/attachment.rb, line 130 def extension extension = File.extname(filename.to_s).delete_prefix('.').downcase extension.empty? ? nil : extension end
Source
# File lib/ruby_llm/attachment.rb, line 77 def for_llm case type when :text "<file name='#{filename}' mime_type='#{mime_type}'>#{content}</file>" else "data:#{mime_type};base64,#{encoded}" end end
Source
# File lib/ruby_llm/attachment.rb, line 109 def format case mime_type when 'audio/mpeg' 'mp3' when 'audio/wav', 'audio/wave', 'audio/x-wav' 'wav' else mime_type.split('/').last end end
Source
# File lib/ruby_llm/attachment.rb, line 97 def image? RubyLLM::MimeType.image? mime_type end
Source
# File lib/ruby_llm/attachment.rb, line 38 def io_like? @source.respond_to?(:read) && !path? && !active_storage? end
Source
# File lib/ruby_llm/attachment.rb, line 34 def path? @source.is_a?(Pathname) || (@source.is_a?(String) && !url?) end
Source
# File lib/ruby_llm/attachment.rb, line 120 def pdf? RubyLLM::MimeType.pdf? mime_type end
Source
# File lib/ruby_llm/attachment.rb, line 69 def save(path) return unless io_like? File.open(path, 'w') do |f| f.puts(@source.read) end end
Source
# File lib/ruby_llm/attachment.rb, line 135 def text? RubyLLM::MimeType.text? mime_type end
Source
# File lib/ruby_llm/attachment.rb, line 139 def to_h { type: type, source: @source } end
Source
# File lib/ruby_llm/attachment.rb, line 86 def type return :image if image? return :video if video? return :audio if audio? return :pdf if pdf? return :text if text? return :document if document? :unknown end
Source
# File lib/ruby_llm/attachment.rb, line 30 def url? @source.is_a?(URI) || (@source.is_a?(String) && @source.match?(%r{^https?://})) end
Source
# File lib/ruby_llm/attachment.rb, line 101 def video? RubyLLM::MimeType.video? mime_type end