Module: RubyLLM::Protocols::Anthropic::Media

Defined in:
lib/ruby_llm/protocols/anthropic/media.rb

Overview

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

Class Method Summary collapse

Class Method Details

.enable_citations(document, attachment) ⇒ Object



134
135
136
137
# File 'lib/ruby_llm/protocols/anthropic/media.rb', line 134

def enable_citations(document, attachment)
  document[:title] = attachment.filename if attachment.filename
  document[:citations] = { enabled: true }
end

.format_content(content, attachments = [], citations: false) ⇒ Object



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

def format_content(content, attachments = [], citations: false)
  parts = []
  parts << format_text(content) unless content.nil? || content.empty?

  attachments.each do |attachment|
    if attachment.provider_file?
      parts << format_provider_file(attachment, citations: citations)
      next
    end

    case attachment.type
    when :image
      parts << format_image(attachment)
    when :pdf
      parts << format_pdf(attachment, citations: citations)
    when :text
      parts << (citations ? format_text_document(attachment) : format_text_file(attachment))
    else
      raise UnsupportedAttachmentError, attachment.mime_type
    end
  end

  parts
end

.format_image(image) ⇒ Object



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

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

.format_pdf(pdf, citations: false) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/ruby_llm/protocols/anthropic/media.rb', line 87

def format_pdf(pdf, citations: false)
  document = {
    type: 'document',
    source: pdf_source(pdf)
  }

  enable_citations(document, pdf) if citations
  document
end

.format_provider_file(file, citations: false) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ruby_llm/protocols/anthropic/media.rb', line 63

def format_provider_file(file, citations: false)
  return format_provider_image(file) if file.image?

  document = {
    type: 'document',
    source: {
      type: 'file',
      file_id: file.provider_file_id
    }
  }
  enable_citations(document, file) if citations
  document
end

.format_provider_image(image) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/ruby_llm/protocols/anthropic/media.rb', line 77

def format_provider_image(image)
  {
    type: 'image',
    source: {
      type: 'file',
      file_id: image.provider_file_id
    }
  }
end

.format_text(text) ⇒ Object



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

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

.format_text_document(text_file) ⇒ Object

Text attachments become citable documents when citations are enabled.



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/ruby_llm/protocols/anthropic/media.rb', line 120

def format_text_document(text_file)
  document = {
    type: 'document',
    source: {
      type: 'text',
      media_type: 'text/plain',
      data: text_file.content
    }
  }

  enable_citations(document, text_file)
  document
end

.format_text_file(text_file) ⇒ Object



112
113
114
115
116
117
# File 'lib/ruby_llm/protocols/anthropic/media.rb', line 112

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

.pdf_source(pdf) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/ruby_llm/protocols/anthropic/media.rb', line 97

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