class Riffer::Messages::Assistant
Represents an assistant (LLM) message in a conversation; may include tool calls when the LLM requests tool execution.
Constants
- ToolCall
Attributes
Normalized reason the provider finished this response, when reported (see Riffer::Providers::FinishReason::VALUES).
Parsed structured output hash, or nil when not applicable.
Token usage data for this response.
Array of tool calls requested by the assistant.
Public Class Methods
Source
# 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
Raises Riffer::ArgumentError when finish_reason is outside the normalized vocabulary.
Calls superclass method
Riffer::Messages::Base::new
Public Instance Methods
Source
# 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
Source
# File lib/riffer/messages/assistant.rb, line 52 def has_tool_calls? !@tool_calls.empty? end
Source
# File lib/riffer/messages/assistant.rb, line 46 def structured_output? !@structured_output.nil? end
Source
# 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
Converts the message to a hash.