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, finish_reason: nil) ⇒ Assistant

Raises Riffer::ArgumentError when finish_reason is outside the normalized vocabulary. – : (String, ?id: String?, ?tool_calls: Array, ?token_usage: Riffer::Providers::TokenUsage?, ?structured_output: Hash[Symbol, untyped]?, ?finish_reason: Symbol?) -> void



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/riffer/messages/assistant.rb', line 26

def initialize(content, id: nil, tool_calls: [], token_usage: nil, structured_output: nil, finish_reason: nil)
  if finish_reason && !Riffer::Providers::FinishReason::VALUES.include?(finish_reason)
    raise Riffer::ArgumentError, "finish_reason must be one of #{Riffer::Providers::FinishReason::VALUES.inspect}, got #{finish_reason.inspect}"
  end

  super(content, id: id)
  @tool_calls = tool_calls
  @token_usage = token_usage
  @structured_output = structured_output
  @finish_reason = finish_reason
end

Instance Attribute Details

#finish_reasonObject (readonly)

Normalized reason the provider finished this response, when reported (see Riffer::Providers::FinishReason::VALUES).



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

def finish_reason
  @finish_reason
end

#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



58
59
60
# File 'lib/riffer/messages/assistant.rb', line 58

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)


52
53
54
# File 'lib/riffer/messages/assistant.rb', line 52

def has_tool_calls?
  !@tool_calls.empty?
end

#roleObject

– : () -> Symbol



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

def role
  :assistant
end

#structured_output?Boolean

– : () -> bool

Returns:

  • (Boolean)


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

def structured_output?
  !@structured_output.nil?
end

#to_hObject

Converts the message to a hash.

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



66
67
68
69
70
71
72
73
74
# File 'lib/riffer/messages/assistant.rb', line 66

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[:finish_reason] = finish_reason if finish_reason
  hash
end