Module: RubyLLM::Providers::Bedrock::Media

Extended by:
Anthropic::Media
Included in:
RubyLLM::Providers::Bedrock
Defined in:
lib/ruby_llm/providers/bedrock/media.rb

Overview

Media handling methods for the Bedrock API integration NOTE: Bedrock does not support url attachments

Class Method Summary collapse

Methods included from Anthropic::Media

format_text, format_text_file

Class Method Details

.format_content(content) ⇒ Object

rubocop:disable Metrics/PerceivedComplexity



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ruby_llm/providers/bedrock/media.rb', line 13

def format_content(content) # rubocop:disable Metrics/PerceivedComplexity
  return content.value if content.is_a?(RubyLLM::Content::Raw)
  return [Anthropic::Media.format_text(content.to_json)] if content.is_a?(Hash) || content.is_a?(Array)
  return [Anthropic::Media.format_text(content)] unless content.is_a?(Content)

  parts = []
  parts << Anthropic::Media.format_text(content.text) if content.text

  content.attachments.each do |attachment|
    case attachment.type
    when :image
      parts << format_image(attachment)
    when :pdf
      parts << format_pdf(attachment)
    when :text
      parts << Anthropic::Media.format_text_file(attachment)
    else
      raise UnsupportedAttachmentError, attachment.type
    end
  end

  parts
end

.format_image(image) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/ruby_llm/providers/bedrock/media.rb', line 37

def format_image(image)
  {
    type: 'image',
    source: {
      type: 'base64',
      media_type: image.mime_type,
      data: image.encoded
    }
  }
end

.format_pdf(pdf) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/ruby_llm/providers/bedrock/media.rb', line 48

def format_pdf(pdf)
  {
    type: 'document',
    source: {
      type: 'base64',
      media_type: pdf.mime_type,
      data: pdf.encoded
    }
  }
end