Class: Legion::Extensions::Llm::Content

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/llm/content.rb,
lib/legion/extensions/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)


10
11
12
13
14
15
16
# File 'lib/legion/extensions/llm/content.rb', line 10

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.



8
9
10
# File 'lib/legion/extensions/llm/content.rb', line 8

def attachments
  @attachments
end

#textObject (readonly)

Returns the value of attribute text.



8
9
10
# File 'lib/legion/extensions/llm/content.rb', line 8

def text
  @text
end

Instance Method Details

#add_attachment(source, filename: nil) ⇒ Object



18
19
20
21
# File 'lib/legion/extensions/llm/content.rb', line 18

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

#formatObject



23
24
25
26
27
28
29
# File 'lib/legion/extensions/llm/content.rb', line 23

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

#to_hObject

Allows serializers to store the text payload without custom adapters.



32
33
34
# File 'lib/legion/extensions/llm/content.rb', line 32

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