Module: RubyLLM::Providers::Bedrock::Media
- Included in:
- RubyLLM::Providers::Bedrock
- Defined in:
- lib/ruby_llm/providers/bedrock/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
Class Method Summary collapse
- .document_format(attachment) ⇒ Object
- .empty_content?(content) ⇒ Boolean
- .render_attachment(attachment, used_document_names:) ⇒ Object
- .render_content(content, used_document_names: nil) ⇒ Object
- .render_content_object(content, used_document_names) ⇒ Object
- .render_document_attachment(attachment, used_document_names:) ⇒ Object
- .render_image_attachment(attachment) ⇒ Object
- .render_raw_content(content) ⇒ Object
- .render_text_attachment(attachment) ⇒ 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
88 89 90 |
# File 'lib/ruby_llm/providers/bedrock/media.rb', line 88 def document_format() .extension || .format end |
.empty_content?(content) ⇒ Boolean
19 20 21 |
# File 'lib/ruby_llm/providers/bedrock/media.rb', line 19 def empty_content?(content) content.nil? || (content.respond_to?(:empty?) && content.empty?) end |
.render_attachment(attachment, used_document_names:) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/ruby_llm/providers/bedrock/media.rb', line 37 def (, used_document_names:) case .type when :image () when :pdf, :document (, used_document_names:) when :text () else raise UnsupportedAttachmentError, .mime_type end end |
.render_content(content, used_document_names: nil) ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/ruby_llm/providers/bedrock/media.rb', line 10 def render_content(content, used_document_names: nil) return [] if empty_content?(content) return render_raw_content(content) if content.is_a?(RubyLLM::Content::Raw) return [{ text: content.to_json }] if content.is_a?(Hash) || content.is_a?(Array) return [{ text: content }] unless content.is_a?(RubyLLM::Content) render_content_object(content, used_document_names || {}) end |
.render_content_object(content, used_document_names) ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/ruby_llm/providers/bedrock/media.rb', line 23 def render_content_object(content, used_document_names) blocks = [] blocks << { text: content.text } if content.text content..each do || blocks << (, used_document_names:) end blocks end |
.render_document_attachment(attachment, used_document_names:) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/ruby_llm/providers/bedrock/media.rb', line 67 def (, used_document_names:) 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 } } } end |
.render_image_attachment(attachment) ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/ruby_llm/providers/bedrock/media.rb', line 52 def () { image: { format: .format, source: { bytes: .encoded } } } end |
.render_raw_content(content) ⇒ Object
32 33 34 35 |
# File 'lib/ruby_llm/providers/bedrock/media.rb', line 32 def render_raw_content(content) value = content.value value.is_a?(Array) ? value : [value] end |
.render_text_attachment(attachment) ⇒ Object
63 64 65 |
# File 'lib/ruby_llm/providers/bedrock/media.rb', line 63 def () { text: .for_llm } end |
.sanitize_document_name(filename) ⇒ Object
92 93 94 95 96 |
# File 'lib/ruby_llm/providers/bedrock/media.rb', line 92 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
84 85 86 |
# File 'lib/ruby_llm/providers/bedrock/media.rb', line 84 def supported_document_format?() SUPPORTED_DOCUMENT_FORMATS.include?(document_format()) end |
.unique_document_name(base_name, used_names) ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/ruby_llm/providers/bedrock/media.rb', line 98 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 |