Class: Legion::Extensions::Llm::Attachment

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/llm/attachment.rb

Overview

A class representing a file attachment.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, filename: nil) ⇒ Attachment

Returns a new instance of Attachment.



13
14
15
16
17
18
19
# File 'lib/legion/extensions/llm/attachment.rb', line 13

def initialize(source, filename: nil)
  @source = source
  @source = source_type_cast
  @filename = filename || source_filename

  determine_mime_type
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



11
12
13
# File 'lib/legion/extensions/llm/attachment.rb', line 11

def filename
  @filename
end

#mime_typeObject (readonly)

Returns the value of attribute mime_type.



11
12
13
# File 'lib/legion/extensions/llm/attachment.rb', line 11

def mime_type
  @mime_type
end

#sourceObject (readonly)

Returns the value of attribute source.



11
12
13
# File 'lib/legion/extensions/llm/attachment.rb', line 11

def source
  @source
end

Instance Method Details

#active_storage?Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
39
# File 'lib/legion/extensions/llm/attachment.rb', line 33

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

Returns:

  • (Boolean)


101
102
103
# File 'lib/legion/extensions/llm/attachment.rb', line 101

def audio?
  Legion::Extensions::Llm::MimeType.audio? mime_type
end

#contentObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/legion/extensions/llm/attachment.rb', line 41

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
    Legion::Extensions::Llm.logger.warn(
      "Source is neither a URL, path, ActiveStorage, nor IO-like: #{@source.class}"
    )
    nil
  end

  @content
end

#encodedObject



62
63
64
# File 'lib/legion/extensions/llm/attachment.rb', line 62

def encoded
  Base64.strict_encode64(content)
end

#for_llmObject



74
75
76
77
78
79
80
81
# File 'lib/legion/extensions/llm/attachment.rb', line 74

def for_llm
  case type
  when :text
    "<file name='#{filename}' mime_type='#{mime_type}'>#{content}</file>"
  else
    "data:#{mime_type};base64,#{encoded}"
  end
end

#formatObject



105
106
107
108
109
110
111
112
113
114
# File 'lib/legion/extensions/llm/attachment.rb', line 105

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

Returns:

  • (Boolean)


93
94
95
# File 'lib/legion/extensions/llm/attachment.rb', line 93

def image?
  Legion::Extensions::Llm::MimeType.image? mime_type
end

#io_like?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/legion/extensions/llm/attachment.rb', line 29

def io_like?
  @source.respond_to?(:read) && !path? && !active_storage?
end

#path?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/legion/extensions/llm/attachment.rb', line 25

def path?
  @source.is_a?(Pathname) || (@source.is_a?(String) && !url?)
end

#pdf?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/legion/extensions/llm/attachment.rb', line 116

def pdf?
  Legion::Extensions::Llm::MimeType.pdf? mime_type
end

#save(path) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/legion/extensions/llm/attachment.rb', line 66

def save(path)
  return unless io_like?

  File.open(path, 'w') do |f|
    f.puts(@source.read)
  end
end

#text?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/legion/extensions/llm/attachment.rb', line 120

def text?
  Legion::Extensions::Llm::MimeType.text? mime_type
end

#to_hObject



124
125
126
# File 'lib/legion/extensions/llm/attachment.rb', line 124

def to_h
  { type: type, source: @source }
end

#typeObject



83
84
85
86
87
88
89
90
91
# File 'lib/legion/extensions/llm/attachment.rb', line 83

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

Returns:

  • (Boolean)


21
22
23
# File 'lib/legion/extensions/llm/attachment.rb', line 21

def url?
  @source.is_a?(URI) || (@source.is_a?(String) && @source.match?(%r{^https?://}))
end

#video?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/legion/extensions/llm/attachment.rb', line 97

def video?
  Legion::Extensions::Llm::MimeType.video? mime_type
end