Module: RubyLLM::Providers::Anthropic::Media

Included in:
RubyLLM::Providers::Anthropic, Bedrock::Media
Defined in:
lib/ruby_llm/providers/anthropic/media.rb

Overview

Handles formatting of media content (images, PDFs, audio) for Anthropic

Class Method Summary collapse

Class Method Details

.format_content(content) ⇒ Object

rubocop:disable Metrics/PerceivedComplexity



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

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

  parts = []
  parts << 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 << format_text_file(attachment)
    else
      raise UnsupportedAttachmentError, attachment.mime_type
    end
  end

  parts
end

.format_image(image) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ruby_llm/providers/anthropic/media.rb', line 41

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

.format_pdf(pdf) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ruby_llm/providers/anthropic/media.rb', line 62

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

.format_text(text) ⇒ Object



34
35
36
37
38
39
# File 'lib/ruby_llm/providers/anthropic/media.rb', line 34

def format_text(text)
  {
    type: 'text',
    text: text
  }
end

.format_text_file(text_file) ⇒ Object



83
84
85
86
87
88
# File 'lib/ruby_llm/providers/anthropic/media.rb', line 83

def format_text_file(text_file)
  {
    type: 'text',
    text: text_file.for_llm
  }
end