Class: RubyLLM::Attachment
- Inherits:
-
Object
- Object
- RubyLLM::Attachment
- Defined in:
- lib/ruby_llm/attachment.rb
Overview
A class representing a file attachment.
Instance Attribute Summary collapse
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#mime_type ⇒ Object
readonly
Returns the value of attribute mime_type.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Instance Method Summary collapse
- #active_storage? ⇒ Boolean
- #audio? ⇒ Boolean
- #content ⇒ Object
- #encoded ⇒ Object
- #for_llm ⇒ Object
- #format ⇒ Object
- #image? ⇒ Boolean
-
#initialize(source, filename: nil) ⇒ Attachment
constructor
A new instance of Attachment.
- #io_like? ⇒ Boolean
- #path? ⇒ Boolean
- #pdf? ⇒ Boolean
- #save(path) ⇒ Object
- #text? ⇒ Boolean
- #to_h ⇒ Object
- #type ⇒ Object
- #url? ⇒ Boolean
- #video? ⇒ Boolean
Constructor Details
#initialize(source, filename: nil) ⇒ Attachment
Returns a new instance of Attachment.
8 9 10 11 12 13 14 |
# File 'lib/ruby_llm/attachment.rb', line 8 def initialize(source, filename: nil) @source = source @source = source_type_cast @filename = filename || source_filename determine_mime_type end |
Instance Attribute Details
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
6 7 8 |
# File 'lib/ruby_llm/attachment.rb', line 6 def filename @filename end |
#mime_type ⇒ Object (readonly)
Returns the value of attribute mime_type.
6 7 8 |
# File 'lib/ruby_llm/attachment.rb', line 6 def mime_type @mime_type end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
6 7 8 |
# File 'lib/ruby_llm/attachment.rb', line 6 def source @source end |
Instance Method Details
#active_storage? ⇒ Boolean
28 29 30 31 32 33 34 |
# File 'lib/ruby_llm/attachment.rb', line 28 def active_storage? return false unless defined?(ActiveStorage) @source.is_a?(ActiveStorage::Blob) || @source.is_a?(ActiveStorage::Attached::One) || @source.is_a?(ActiveStorage::Attached::Many) end |
#audio? ⇒ Boolean
94 95 96 |
# File 'lib/ruby_llm/attachment.rb', line 94 def audio? RubyLLM::MimeType.audio? mime_type end |
#content ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/ruby_llm/attachment.rb', line 36 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 |
#encoded ⇒ Object
55 56 57 |
# File 'lib/ruby_llm/attachment.rb', line 55 def encoded Base64.strict_encode64(content) end |
#for_llm ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/ruby_llm/attachment.rb', line 67 def for_llm case type when :text "<file name='#{filename}' mime_type='#{mime_type}'>#{content}</file>" else "data:#{mime_type};base64,#{encoded}" end end |
#format ⇒ Object
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/ruby_llm/attachment.rb', line 98 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 |
#image? ⇒ Boolean
86 87 88 |
# File 'lib/ruby_llm/attachment.rb', line 86 def image? RubyLLM::MimeType.image? mime_type end |
#io_like? ⇒ Boolean
24 25 26 |
# File 'lib/ruby_llm/attachment.rb', line 24 def io_like? @source.respond_to?(:read) && !path? && !active_storage? end |
#path? ⇒ Boolean
20 21 22 |
# File 'lib/ruby_llm/attachment.rb', line 20 def path? @source.is_a?(Pathname) || (@source.is_a?(String) && !url?) end |
#pdf? ⇒ Boolean
109 110 111 |
# File 'lib/ruby_llm/attachment.rb', line 109 def pdf? RubyLLM::MimeType.pdf? mime_type end |
#save(path) ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/ruby_llm/attachment.rb', line 59 def save(path) return unless io_like? File.open(path, 'w') do |f| f.puts(@source.read) end end |
#text? ⇒ Boolean
113 114 115 |
# File 'lib/ruby_llm/attachment.rb', line 113 def text? RubyLLM::MimeType.text? mime_type end |
#to_h ⇒ Object
117 118 119 |
# File 'lib/ruby_llm/attachment.rb', line 117 def to_h { type: type, source: @source } end |
#type ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'lib/ruby_llm/attachment.rb', line 76 def type return :image if image? return :video if video? return :audio if audio? return :pdf if pdf? return :text if text? :unknown end |
#url? ⇒ Boolean
16 17 18 |
# File 'lib/ruby_llm/attachment.rb', line 16 def url? @source.is_a?(URI) || (@source.is_a?(String) && @source.match?(%r{^https?://})) end |
#video? ⇒ Boolean
90 91 92 |
# File 'lib/ruby_llm/attachment.rb', line 90 def video? RubyLLM::MimeType.video? mime_type end |