class RubyLLM::Attachment
An Attachment is a file sent to the model alongside a message. The source can be a local path, an http(s) URL, an IO-like object, an ActiveStorage object, or a provider-managed UploadedFile. Chat#ask builds attachments for you from its with: option:
chat.ask "What's in this image?", with: "ruby_conf.jpg"
Build one explicitly to return a file from a Tool:
RubyLLM::Attachment.new(doc.download_path)
Constants
- DOCUMENT_EXTENSIONS
-
File extensions recognized as document attachments when the MIME type alone is inconclusive.
Attributes
The filename given at construction or derived from the source. May be nil.
The detected MIME type string, such as "image/png".
The underlying source: a URI, Pathname, IO-like object, ActiveStorage object, or UploadedFile.
Public Class Methods
Source
# File lib/ruby_llm/attachment.rb, line 66 def initialize(source, filename: nil) @source = source @source = source_type_cast @filename = filename || source_filename determine_mime_type end
Creates an attachment from source: a file path, URL, IO-like object, ActiveStorage object, or UploadedFile. Derives the filename from the source when filename: is not given, then detects the MIME type.
RubyLLM::Attachment.new("diagram.png") RubyLLM::Attachment.new(StringIO.new(data), filename: "report.pdf")
Public Instance Methods
Source
# File lib/ruby_llm/attachment.rb, line 154 def audio? RubyLLM::MimeType.audio? mime_type end
Returns whether the attachment is audio.
Source
# File lib/ruby_llm/attachment.rb, line 107 def content if provider_file? raise Error, "Provider-managed file #{provider_file_id} cannot be read as inline attachment content" end load_content if !defined?(@content) || @content.nil? normalize_text_encoding @content end
Returns the raw bytes of the attachment, fetching or reading the source on the first call. Text content is returned as UTF-8.
Raises RubyLLM::Error if the attachment is a provider-managed file, which has no local content.
Source
# File lib/ruby_llm/attachment.rb, line 176 def document? return false if pdf? || text? RubyLLM::MimeType.document?(mime_type) || DOCUMENT_EXTENSIONS.include?(extension) end
Returns whether the attachment is a non-PDF, non-text document format such as Word or Excel, judged by MIME type or file extension.
Source
# File lib/ruby_llm/attachment.rb, line 144 def image? RubyLLM::MimeType.image? mime_type end
Returns whether the attachment is an image.
Source
# File lib/ruby_llm/attachment.rb, line 170 def pdf? RubyLLM::MimeType.pdf? mime_type end
Returns whether the attachment is a PDF.
Source
# File lib/ruby_llm/attachment.rb, line 188 def text? RubyLLM::MimeType.text? mime_type end
Returns whether the attachment is textual, like source code or CSV.
Source
# File lib/ruby_llm/attachment.rb, line 132 def type return :image if image? return :video if video? return :audio if audio? return :pdf if pdf? return :text if text? return :document if document? :unknown end
Returns the attachment category as a Symbol: :image, :video, :audio, :pdf, :text, :document, or :unknown.
Source
# File lib/ruby_llm/attachment.rb, line 149 def video? RubyLLM::MimeType.video? mime_type end
Returns whether the attachment is a video.