Class: Riffer::Messages::Assistant

Inherits:
Base
  • Object
show all
Defined in:
lib/riffer/messages/assistant.rb

Overview

Represents an assistant (LLM) message in a conversation; may include tool calls when the LLM requests tool execution.

Defined Under Namespace

Classes: ToolCall

Instance Attribute Summary collapse

Attributes inherited from Base

#content, #id

Instance Method Summary collapse

Methods inherited from Base

from_hash

Constructor Details

#initialize(content, id: nil, tool_calls: [], token_usage: nil, structured_output: nil) ⇒ Assistant

– : (String, ?id: String?, ?tool_calls: Array, ?token_usage: Riffer::Providers::TokenUsage?, ?structured_output: Hash[Symbol, untyped]?) -> void



20
21
22
23
24
25
# File 'lib/riffer/messages/assistant.rb', line 20

def initialize(content, id: nil, tool_calls: [], token_usage: nil, structured_output: nil)
  super(content, id: id)
  @tool_calls = tool_calls
  @token_usage = token_usage
  @structured_output = structured_output
end

Instance Attribute Details

#structured_outputObject (readonly)

Parsed structured output hash, or nil when not applicable.



16
17
18
# File 'lib/riffer/messages/assistant.rb', line 16

def structured_output
  @structured_output
end

#token_usageObject (readonly)

Token usage data for this response.



13
14
15
# File 'lib/riffer/messages/assistant.rb', line 13

def token_usage
  @token_usage
end

#tool_callsObject (readonly)

Array of tool calls requested by the assistant.



10
11
12
# File 'lib/riffer/messages/assistant.rb', line 10

def tool_calls
  @tool_calls
end

Instance Method Details

#+(other) ⇒ Object

– : (Riffer::Messages::Assistant) -> Riffer::Messages::Assistant



47
48
49
# File 'lib/riffer/messages/assistant.rb', line 47

def +(other)
  self.class.new("#{content}\n\n#{other.content}", tool_calls: tool_calls + other.tool_calls)
end

#has_tool_calls?Boolean

– : () -> bool

Returns:

  • (Boolean)


41
42
43
# File 'lib/riffer/messages/assistant.rb', line 41

def has_tool_calls?
  !@tool_calls.empty?
end

#roleObject

– : () -> Symbol



29
30
31
# File 'lib/riffer/messages/assistant.rb', line 29

def role
  :assistant
end

#structured_output?Boolean

– : () -> bool

Returns:

  • (Boolean)


35
36
37
# File 'lib/riffer/messages/assistant.rb', line 35

def structured_output?
  !@structured_output.nil?
end

#to_hObject

Converts the message to a hash.

– : () -> Hash[Symbol, untyped]



55
56
57
58
59
60
61
62
# File 'lib/riffer/messages/assistant.rb', line 55

def to_h
  hash = {role: role, content: content} #: Hash[Symbol, untyped]
  hash[:id] = id unless id.nil?
  hash[:tool_calls] = tool_calls.map(&:to_h) unless tool_calls.empty?
  hash[:token_usage] = token_usage.to_h if token_usage
  hash[:structured_output] = structured_output if structured_output?
  hash
end