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
- .format_attachment(attachment, document_attachments:, image_attachments:, audio_attachments:) ⇒ Object
- .format_audio(audio) ⇒ Object
- .format_content(content, attachments = [], document_attachments: :pdf, image_attachments: true, audio_attachments: true) ⇒ Object
- .format_document(document) ⇒ Object
- .format_document_attachment(attachment, strategy) ⇒ Object
- .format_image(image) ⇒ Object
-
.format_parts(content, attachments = []) ⇒ Object
Shared preamble and attachment loop for OpenAI-compatible providers.
- .format_provider_file(file, document_attachments:) ⇒ Object
- .format_text(text) ⇒ Object
- .format_text_file(text_file) ⇒ Object
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 (, document_attachments:, image_attachments:, audio_attachments:) return format_provider_file(, document_attachments:) if .provider_file? case .type when :image raise UnsupportedAttachmentError, .mime_type unless format_image() when :audio raise UnsupportedAttachmentError, .mime_type unless format_audio() when :pdf, :document (, ) when :text format_text_file() else raise UnsupportedAttachmentError, .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, = [], document_attachments: :pdf, image_attachments: true, audio_attachments: true) format_parts(content, ) do || ( , 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 (, strategy) return format_document() if strategy == :all return format_document() if strategy == :pdf && .pdf? raise UnsupportedAttachmentError, .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, = []) return content if .empty? parts = [] parts << format_text(content) if content .each do || parts << yield() 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 == :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 |