Class: LLM::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/llm/message.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(role, content, extra = {}) ⇒ LLM::Message

Returns a new message

Parameters:

  • role (Symbol)
  • content (String)
  • extra (Hash) (defaults to: {})


26
27
28
29
30
# File 'lib/llm/message.rb', line 26

def initialize(role, content, extra = {})
  @role = role.to_s
  @content = content
  @extra = extra
end

Instance Attribute Details

#roleSymbol (readonly)

Returns the role of the message

Returns:

  • (Symbol)


8
9
10
# File 'lib/llm/message.rb', line 8

def role
  @role
end

#contentString (readonly)

Returns the content of the message

Returns:

  • (String)


13
14
15
# File 'lib/llm/message.rb', line 13

def content
  @content
end

#extraHash (readonly)

Returns extra context associated with the message

Returns:

  • (Hash)


18
19
20
# File 'lib/llm/message.rb', line 18

def extra
  @extra
end

Instance Method Details

#to_hHash

Returns a hash representation of the message

Returns:

  • (Hash)


35
36
37
# File 'lib/llm/message.rb', line 35

def to_h
  {role:, content:}
end

#==(other) ⇒ Boolean Also known as: eql?

Returns true when two objects have the same role and content

Parameters:

  • other (Object)

    The other object to compare

Returns:

  • (Boolean)


44
45
46
47
48
49
50
# File 'lib/llm/message.rb', line 44

def ==(other)
  if other.respond_to?(:to_h)
    to_h == other.to_h
  else
    false
  end
end

#assistant?Boolean

Returns true when the message is from the LLM

Returns:

  • (Boolean)


56
57
58
# File 'lib/llm/message.rb', line 56

def assistant?
  role == "assistant" || role == "model"
end

#inspectString

Returns a string representation of the message

Returns:

  • (String)


63
64
65
66
# File 'lib/llm/message.rb', line 63

def inspect
  "#<#{self.class.name}:0x#{object_id.to_s(16)} " \
  "role=#{role.inspect} content=#{content.inspect}>"
end