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_audio(audio) ⇒ Object



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

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

.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
33
34
# File 'lib/ruby_llm/providers/openai/media.rb', line 10

def format_content(content) # rubocop:disable Metrics/PerceivedComplexity
  return content.value if content.is_a?(RubyLLM::Content::Raw)
  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|
    case attachment.type
    when :image
      parts << format_image(attachment)
    when :pdf
      parts << format_pdf(attachment)
    when :audio
      parts << format_audio(attachment)
    when :text
      parts << format_text_file(attachment)
    else
      raise UnsupportedAttachmentError, attachment.type
    end
  end

  parts
end

.format_image(image) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/ruby_llm/providers/openai/media.rb', line 36

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

.format_pdf(pdf) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/ruby_llm/providers/openai/media.rb', line 45

def format_pdf(pdf)
  {
    type: 'file',
    file: {
      filename: pdf.filename,
      file_data: pdf.for_llm
    }
  }
end

.format_text(text) ⇒ Object



72
73
74
75
76
77
# File 'lib/ruby_llm/providers/openai/media.rb', line 72

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

.format_text_file(text_file) ⇒ Object



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

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