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

#inspectString

Returns a string representation of the message

Returns:

  • (String)


162
163
164
165
166
# File 'lib/llm/message.rb', line 162

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

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

#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:



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

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

#read!void

This method returns an undefined value.

Marks the message as read



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

def read!
  @read = true
end

#read?Boolean

Returns true when the message has been read

Returns:

  • (Boolean)


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

def read?
  @read
end

#assistant?Boolean

Returns true when the message is an assistant message

Returns:

  • (Boolean)


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

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

#system?Boolean

Returns true when the message is a system message

Returns:

  • (Boolean)


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

def system?
  role == "system"
end

#user?Boolean

Returns true when the message is a user message

Returns:

  • (Boolean)


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

def user?
  role == "user"
end

#tool_call?Boolean

Returns true when the message requests a function call

Returns:

  • (Boolean)

    Returns true when the message requests a function call



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

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



115
116
117
118
# File 'lib/llm/message.rb', line 115

def tool_return?
  LLM::Function::Return === content ||
    [*content].grep(LLM::Function::Return).any?
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



126
127
128
# File 'lib/llm/message.rb', line 126

def response
  extra[:response]
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:



136
137
138
# File 'lib/llm/message.rb', line 136

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

#usageLLM::Object Also known as: token_usage

Note:

This method returns token usage for assistant messages, and it returns an empty object for non-assistant messages

Returns token usage statistics

Returns:



146
147
148
149
150
151
152
153
154
155
156
# File 'lib/llm/message.rb', line 146

def usage
  @usage ||= if response
    LLM::Object.from_hash({
      input_tokens: response.prompt_tokens || 0,
      output_tokens: response.completion_tokens || 0,
      total_tokens: response.total_tokens || 0
    })
  else
    LLM::Object.from_hash({})
  end
end