Class: RubyLLM::Content

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_llm/content.rb,
lib/ruby_llm/content.rb

Overview

Represents the content sent to or received from an LLM.

Defined Under Namespace

Classes: Raw

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text = nil, attachments = nil) ⇒ Content

Returns a new instance of Content.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
# File 'lib/ruby_llm/content.rb', line 8

def initialize(text = nil, attachments = nil)
  @text = text
  @attachments = []

  process_attachments(attachments)
  raise ArgumentError, 'Text and attachments cannot be both nil' if @text.nil? && @attachments.empty?
end

Instance Attribute Details

#attachmentsObject (readonly)

Returns the value of attribute attachments.



6
7
8
# File 'lib/ruby_llm/content.rb', line 6

def attachments
  @attachments
end

#textObject (readonly)

Returns the value of attribute text.



6
7
8
# File 'lib/ruby_llm/content.rb', line 6

def text
  @text
end

Instance Method Details

#add_attachment(source, filename: nil) ⇒ Object



16
17
18
19
# File 'lib/ruby_llm/content.rb', line 16

def add_attachment(source, filename: nil)
  @attachments << Attachment.new(source, filename:)
  self
end

#formatObject



21
22
23
24
25
26
27
# File 'lib/ruby_llm/content.rb', line 21

def format
  if @text && @attachments.empty?
    @text
  else
    self
  end
end

#to_hObject

For Rails serialization



30
31
32
# File 'lib/ruby_llm/content.rb', line 30

def to_h
  { text: @text, attachments: @attachments.map(&:to_h) }
end