Module: RubyLLM::Providers::Perplexity::Media

Defined in:
lib/ruby_llm/providers/perplexity/media.rb

Overview

Handles Perplexity Sonar media content.

Constant Summary collapse

SUPPORTED_DOCUMENT_EXTENSIONS =
%w[pdf doc docx txt rtf].freeze

Class Method Summary collapse

Class Method Details

.format_content(content) ⇒ Object

rubocop:disable Metrics/PerceivedComplexity



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ruby_llm/providers/perplexity/media.rb', line 12

def format_content(content) # rubocop:disable Metrics/PerceivedComplexity
  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 << OpenAI::Media.format_text(content.text) if content.text

  content.attachments.each do |attachment|
    case attachment.type
    when :image
      parts << OpenAI::Media.format_image(attachment)
    when :pdf, :document
      parts << format_document(attachment)
    when :text
      parts << format_text_attachment(attachment)
    else
      raise UnsupportedAttachmentError, attachment.mime_type
    end
  end

  parts
end

.format_document(attachment) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/ruby_llm/providers/perplexity/media.rb', line 39

def format_document(attachment)
  raise UnsupportedAttachmentError, attachment.mime_type unless supported_file?(attachment)

  {
    type: 'file_url',
    file_url: {
      url: attachment.url? ? attachment.source.to_s : attachment.encoded
    }
  }
end

.format_text_attachment(attachment) ⇒ Object



50
51
52
# File 'lib/ruby_llm/providers/perplexity/media.rb', line 50

def format_text_attachment(attachment)
  OpenAI::Media.format_text_file(attachment)
end

.supported_file?(attachment) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
# File 'lib/ruby_llm/providers/perplexity/media.rb', line 54

def supported_file?(attachment)
  return true if attachment.pdf?

  SUPPORTED_DOCUMENT_EXTENSIONS.include?(attachment.extension)
end