Module: RubyLLM::Protocols::Converse::Media
- Defined in:
- lib/ruby_llm/protocols/converse/media.rb
Overview
Media formatting for Bedrock Converse content blocks.
Constant Summary collapse
- SUPPORTED_DOCUMENT_FORMATS =
%w[pdf csv doc docx xls xlsx html txt md].freeze
- SUPPORTED_AUDIO_FORMATS =
%w[mp3 opus wav aac flac mp4 ogg mkv mka x-aac m4a mpeg mpga pcm webm].freeze
- SUPPORTED_VIDEO_FORMATS =
%w[mkv mov mp4 webm flv mpeg mpg wmv three_gp].freeze
- AUDIO_FORMAT_ALIASES =
{ 'x-flac' => 'flac', 'x-m4a' => 'm4a' }.freeze
- VIDEO_FORMAT_ALIASES =
{ 'quicktime' => 'mov', 'x-matroska' => 'mkv', 'x-ms-wmv' => 'wmv', '3gp' => 'three_gp', '3gpp' => 'three_gp' }.freeze
Class Method Summary collapse
- .document_format(attachment) ⇒ Object
- .format_attachment(attachment, used_document_names:, citations: false) ⇒ Object
- .format_audio_attachment(attachment) ⇒ Object
- .format_content(content, attachments = [], used_document_names: nil, citations: false) ⇒ Object
- .format_document_attachment(attachment, used_document_names:, citations: false) ⇒ Object
- .format_image_attachment(attachment) ⇒ Object
- .format_provider_file_attachment(attachment, used_document_names:, citations: false) ⇒ Object
- .format_text_attachment(attachment) ⇒ Object
-
.format_text_document_attachment(attachment, used_document_names:) ⇒ Object
Text attachments become citable documents when citations are enabled.
- .format_video_attachment(attachment) ⇒ Object
- .media_format(attachment, aliases, supported) ⇒ Object
- .sanitize_document_name(filename) ⇒ Object
- .supported_document_format?(attachment) ⇒ Boolean
- .unique_document_name(base_name, used_names) ⇒ Object
Class Method Details
.document_format(attachment) ⇒ Object
158 159 160 |
# File 'lib/ruby_llm/protocols/converse/media.rb', line 158 def document_format() .extension || .format end |
.format_attachment(attachment, used_document_names:, citations: false) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/ruby_llm/protocols/converse/media.rb', line 20 def (, used_document_names:, citations: false) if .provider_file? return (, used_document_names:, citations:) end case .type when :image () when :audio () when :video () when :pdf, :document (, used_document_names:, citations:) when :text if citations (, used_document_names:) else () end else raise UnsupportedAttachmentError, .mime_type end end |
.format_audio_attachment(attachment) ⇒ Object
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/ruby_llm/protocols/converse/media.rb', line 71 def () { audio: { format: media_format(, AUDIO_FORMAT_ALIASES, SUPPORTED_AUDIO_FORMATS), source: { bytes: .encoded } } } end |
.format_content(content, attachments = [], used_document_names: nil, citations: false) ⇒ Object
10 11 12 13 14 15 16 17 18 |
# File 'lib/ruby_llm/protocols/converse/media.rb', line 10 def format_content(content, = [], used_document_names: nil, citations: false) used_document_names ||= {} blocks = [] blocks << { text: content } unless content.nil? || content.empty? .each do || blocks << (, used_document_names:, citations:) end blocks end |
.format_document_attachment(attachment, used_document_names:, citations: false) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/ruby_llm/protocols/converse/media.rb', line 105 def (, used_document_names:, citations: false) format = document_format() raise UnsupportedAttachmentError, .mime_type unless supported_document_format?() document_name = unique_document_name(sanitize_document_name(.filename), used_document_names) document = { format: format, name: document_name, source: { bytes: .encoded } } document[:citations] = { enabled: true } if citations { document: document } end |
.format_image_attachment(attachment) ⇒ Object
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/ruby_llm/protocols/converse/media.rb', line 60 def () { image: { format: .format, source: { bytes: .encoded } } } end |
.format_provider_file_attachment(attachment, used_document_names:, citations: false) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/ruby_llm/protocols/converse/media.rb', line 137 def (, used_document_names:, citations: false) raise UnsupportedAttachmentError, .mime_type unless supported_document_format?() document_name = unique_document_name(sanitize_document_name(.filename), used_document_names) document = { format: document_format(), name: document_name, source: { s3Location: { uri: .provider_file_uri || .provider_file_id } } } document[:citations] = { enabled: true } if citations { document: document } end |
.format_text_attachment(attachment) ⇒ Object
101 102 103 |
# File 'lib/ruby_llm/protocols/converse/media.rb', line 101 def () { text: .for_llm } end |
.format_text_document_attachment(attachment, used_document_names:) ⇒ Object
Text attachments become citable documents when citations are enabled. Bedrock rejects text-format documents sent as bytes when citations are on, so the text rides in the source's text member.
125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/ruby_llm/protocols/converse/media.rb', line 125 def (, used_document_names:) document_name = unique_document_name(sanitize_document_name(.filename), used_document_names) { document: { format: 'txt', name: document_name, source: { text: .content }, citations: { enabled: true } } } end |
.format_video_attachment(attachment) ⇒ Object
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/ruby_llm/protocols/converse/media.rb', line 82 def () { video: { format: media_format(, VIDEO_FORMAT_ALIASES, SUPPORTED_VIDEO_FORMATS), source: { bytes: .encoded } } } end |
.media_format(attachment, aliases, supported) ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/ruby_llm/protocols/converse/media.rb', line 93 def media_format(, aliases, supported) format = .format.to_s format = aliases.fetch(format, format) raise UnsupportedAttachmentError, .mime_type unless supported.include?(format) format end |
.sanitize_document_name(filename) ⇒ Object
162 163 164 165 166 |
# File 'lib/ruby_llm/protocols/converse/media.rb', line 162 def sanitize_document_name(filename) base = File.basename(filename.to_s, '.*') safe = base.gsub(/[^a-zA-Z0-9_-]/, '_') safe.empty? ? 'document' : safe end |
.supported_document_format?(attachment) ⇒ Boolean
154 155 156 |
# File 'lib/ruby_llm/protocols/converse/media.rb', line 154 def supported_document_format?() SUPPORTED_DOCUMENT_FORMATS.include?(document_format()) end |
.unique_document_name(base_name, used_names) ⇒ Object
168 169 170 171 172 173 174 |
# File 'lib/ruby_llm/protocols/converse/media.rb', line 168 def unique_document_name(base_name, used_names) count = used_names[base_name].to_i used_names[base_name] = count + 1 return base_name if count.zero? "#{base_name}_#{count + 1}" end |