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

#annotationsArray<LLM::Object>

Note:

This method might return annotations for assistant messages, and it returns an empty array for non-assistant messages

Returns annotations associated with the message

Returns:



123
124
125
# File 'lib/llm/message.rb', line 123

def annotations
  @annotations ||= LLM::Object.from(extra["annotations"] || [])
end

#assistant?Boolean

Returns true when the message is an assistant message

Returns:

  • (Boolean)


74
75
76
# File 'lib/llm/message.rb', line 74

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

#content!Hash

Try to parse JSON content

Returns:

  • (Hash)

    Returns the parsed content as a Hash



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

def content!
  LLM.json.load(content)
end

#functionsArray<LLM::Function>

Returns:



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

def functions
  @functions ||= tool_calls.map do |fn|
    function = tools.find { _1.name.to_s == 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)


142
143
144
145
146
# File 'lib/llm/message.rb', line 142

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

#responseLLM::Response?

Note:

This method returns a response for assistant messages, and it returns nil for non-assistant messages

Returns the response associated with the message, or nil

Returns:

  • (LLM::Response, nil)

    Returns the response associated with the message, or nil



113
114
115
# File 'lib/llm/message.rb', line 113

def response
  extra[:response]
end

#system?Boolean

Returns true when the message is a system message

Returns:

  • (Boolean)


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

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



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

def tool_call?
  tool_calls.any?
end

#tool_return?Boolean

Returns true when the message represents a function return

Returns:

  • (Boolean)

    Returns true when the message represents a function return



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

def tool_return?
  LLM::Function::Return === content ||
    [*content].grep(LLM::Function::Return).any?
end

#usageLLM::Object? Also known as: token_usage

Note:

This method returns token usage for assistant messages, and it returns nil for non-assistant messages

Returns token usage statistics

Returns:



133
134
135
136
# File 'lib/llm/message.rb', line 133

def usage
  return nil unless response
  @usage ||= response.usage
end

#user?Boolean

Returns true when the message is a user message

Returns:

  • (Boolean)


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

def user?
  role == "user"
end