class Riffer::Messages::Assistant
rbs_inline: enabled
Constants
- REASONING_TEXT_TYPES
-
The other reasoning part types carry no prose.
Attributes
Public Class Methods
Source
# File lib/riffer/messages/assistant.rb, line 10 def self.from_hash(msg) return msg if msg.is_a?(Riffer::Messages::Assistant) new( msg[:content], id: msg[:id], tool_calls: (msg[:tool_calls] || []).map { |tc| Riffer::Messages::Assistant::ToolCall.from_hash(tc) }, reasoning: (msg[:reasoning] || []).map { |part| Riffer::Messages::Assistant::ReasoningPart.from_hash(part) }, token_usage: msg[:token_usage] && Riffer::Providers::TokenUsage.from_hash(msg[:token_usage]), structured_output: msg[:structured_output], finish_reason: msg[:finish_reason]&.to_sym, finish_reason_raw: msg[:finish_reason_raw], ) end
Source
# File lib/riffer/messages/assistant.rb, line 48 def initialize( content, id: nil, tool_calls: [], reasoning: [], token_usage: nil, structured_output: nil, finish_reason: nil, finish_reason_raw: nil ) if finish_reason && !Riffer::Providers::FinishReason::VALUES.include?(finish_reason) values = Riffer::Providers::FinishReason::VALUES.inspect raise Riffer::ArgumentError, "finish_reason must be one of #{values}, got #{finish_reason.inspect}" end super(content, id: id) @tool_calls = tool_calls @reasoning = reasoning @token_usage = token_usage @structured_output = structured_output @finish_reason = finish_reason @finish_reason_raw = finish_reason_raw end
Calls superclass method
Riffer::Messages::Base::new
Public Instance Methods
Source
# File lib/riffer/messages/assistant.rb, line 105 def +(other) self.class.new( "#{content}\n\n#{other.content}", tool_calls: tool_calls + other.tool_calls, reasoning: reasoning + other.reasoning, ) end
Source
# File lib/riffer/messages/assistant.rb, line 86 def has_tool_calls? !@tool_calls.empty? end
Source
# File lib/riffer/messages/assistant.rb, line 92 def reasoning? !@reasoning.empty? end
Source
# File lib/riffer/messages/assistant.rb, line 98 def reasoning_text texts = reasoning.filter_map { |part| part.text if REASONING_TEXT_TYPES.include?(part.type) } texts.empty? ? nil : texts.join("\n\n") end
Source
# File lib/riffer/messages/assistant.rb, line 80 def structured_output? !@structured_output.nil? end
Source
# File lib/riffer/messages/assistant.rb, line 115 def to_h hash = { role: role, content: content } #: Hash[Symbol, untyped] hash[:id] = id if id hash[:tool_calls] = tool_calls.map(&:to_h) unless tool_calls.empty? hash[:reasoning] = reasoning.map(&:to_h) if reasoning? 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[:finish_reason_raw] = finish_reason_raw if finish_reason_raw hash end