Module: RubyLLM::Protocols::ChatCompletions::Media

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

Overview

Handles formatting of media content (images, audio) for OpenAI APIs

Class Method Summary collapse

Class Method Details

.format_attachment(attachment, document_attachments:, image_attachments:, audio_attachments:) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ruby_llm/protocols/chat_completions/media.rb', line 37

def format_attachment(attachment, document_attachments:, image_attachments:, audio_attachments:)
  return format_provider_file(attachment, document_attachments:) if attachment.provider_file?

  case attachment.type
  when :image
    raise UnsupportedAttachmentError, attachment.mime_type unless image_attachments

    format_image(attachment)
  when :audio
    raise UnsupportedAttachmentError, attachment.mime_type unless audio_attachments

    format_audio(attachment)
  when :pdf, :document
    format_document_attachment(attachment, document_attachments)
  when :text
    format_text_file(attachment)
  else
    raise UnsupportedAttachmentError, attachment.mime_type
  end
end

.format_audio(audio) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/ruby_llm/protocols/chat_completions/media.rb', line 95

def format_audio(audio)
  {
    type: 'input_audio',
    input_audio: {
      data: audio.encoded,
      format: audio.format
    }
  }
end

.format_content(content, attachments = [], document_attachments: :pdf, image_attachments: true, audio_attachments: true) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ruby_llm/protocols/chat_completions/media.rb', line 10

def format_content(content, attachments = [], document_attachments: :pdf, image_attachments: true,
                   audio_attachments: true)
  format_parts(content, attachments) do |attachment|
    format_attachment(
      attachment,
      document_attachments:,
      image_attachments:,
      audio_attachments:
    )
  end
end

.format_document(document) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/ruby_llm/protocols/chat_completions/media.rb', line 67

def format_document(document)
  {
    type: 'file',
    file: {
      filename: document.filename,
      file_data: document.for_llm
    }
  }
end

.format_document_attachment(attachment, strategy) ⇒ Object



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

def format_document_attachment(attachment, strategy)
  return format_document(attachment) if strategy == :all
  return format_document(attachment) if strategy == :pdf && attachment.pdf?

  raise UnsupportedAttachmentError, attachment.mime_type
end

.format_image(image) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/ruby_llm/protocols/chat_completions/media.rb', line 58

def format_image(image)
  {
    type: 'image_url',
    image_url: {
      url: image.url? ? image.source.to_s : image.for_llm
    }
  }
end

.format_parts(content, attachments = []) ⇒ Object

Shared preamble and attachment loop for OpenAI-compatible providers. The block formats a single attachment in the provider's dialect.



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ruby_llm/protocols/chat_completions/media.rb', line 24

def format_parts(content, attachments = [])
  return content if attachments.empty?

  parts = []
  parts << format_text(content) if content

  attachments.each do |attachment|
    parts << yield(attachment)
  end

  parts
end

.format_provider_file(file, document_attachments:) ⇒ Object



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

def format_provider_file(file, document_attachments:)
  raise UnsupportedAttachmentError, file.mime_type if document_attachments == :none

  {
    type: 'file',
    file: {
      file_id: file.provider_file_id
    }
  }
end

.format_text(text) ⇒ Object



105
106
107
108
109
110
# File 'lib/ruby_llm/protocols/chat_completions/media.rb', line 105

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

.format_text_file(text_file) ⇒ Object



88
89
90
91
92
93
# File 'lib/ruby_llm/protocols/chat_completions/media.rb', line 88

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