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

#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

#roleSymbol (readonly)

Returns the role of the message

Returns:

  • (Symbol)


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

def role
  @role
end

Instance Method Details

#==(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)


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

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

#content!Hash

Try to parse the content as JSON

Returns:

  • (Hash)


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

def content!
  JSON.parse(content)
end

#functionsArray<LLM::Function>

Returns:



69
70
71
72
73
74
75
# File 'lib/llm/message.rb', line 69

def functions
  @functions ||= tool_calls.map do |fn|
    function = LLM.functions[fn.name].dup
    function.tap { _1.id = fn.id }
    function.tap { _1.arguments = fn.arguments }
  end
end

#inspectString

Returns a string representation of the message

Returns:

  • (String)


108
109
110
111
112
# File 'lib/llm/message.rb', line 108

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

#read!void

This method returns an undefined value.

Marks the message as read



94
95
96
# File 'lib/llm/message.rb', line 94

def read!
  @read = true
end

#read?Boolean

Returns true when the message has been read

Returns:

  • (Boolean)


101
102
103
# File 'lib/llm/message.rb', line 101

def read?
  @read
end

#system?Boolean

Returns true when the message is a system message

Returns:

  • (Boolean)


87
88
89
# File 'lib/llm/message.rb', line 87

def system?
  role == "system"
end

#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

#tool_call?Boolean

Returns true when the message requests a function call

Returns:

  • (Boolean)

    Returns true when the message requests a function call



80
81
82
# File 'lib/llm/message.rb', line 80

def tool_call?
  tool_calls.any?
end