Module: RubyLLM::Providers::OpenAI::Media

Included in:
GPUStack::Media, RubyLLM::Providers::Ollama::Media, RubyLLM::Providers::OpenAI
Defined in:
lib/ruby_llm/providers/openai/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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ruby_llm/providers/openai/media.rb', line 33

def format_attachment(attachment, document_attachments:, image_attachments:, audio_attachments:)
  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



82
83
84
85
86
87
88
89
90
# File 'lib/ruby_llm/providers/openai/media.rb', line 82

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

.format_content(content, document_attachments: :pdf, image_attachments: true, audio_attachments: true) ⇒ Object



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

def format_content(content, document_attachments: :pdf, image_attachments: true, audio_attachments: true)
  if content.is_a?(RubyLLM::Content::Raw)
    value = content.value
    return value.is_a?(Hash) ? value.to_json : value
  end
  return content.to_json if content.is_a?(Hash) || content.is_a?(Array)
  return content unless content.is_a?(Content)

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

  content.attachments.each do |attachment|
    parts << format_attachment(
      attachment,
      document_attachments:,
      image_attachments:,
      audio_attachments:
    )
  end

  parts
end

.format_document(document) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/ruby_llm/providers/openai/media.rb', line 61

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

.format_document_attachment(attachment, strategy) ⇒ Object



99
100
101
102
103
104
# File 'lib/ruby_llm/providers/openai/media.rb', line 99

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



52
53
54
55
56
57
58
59
# File 'lib/ruby_llm/providers/openai/media.rb', line 52

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

.format_pdf(pdf) ⇒ Object



71
72
73
# File 'lib/ruby_llm/providers/openai/media.rb', line 71

def format_pdf(pdf)
  format_document(pdf)
end

.format_text(text) ⇒ Object



92
93
94
95
96
97
# File 'lib/ruby_llm/providers/openai/media.rb', line 92

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

.format_text_file(text_file) ⇒ Object



75
76
77
78
79
80
# File 'lib/ruby_llm/providers/openai/media.rb', line 75

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