Class: Riffer::Messages::Assistant
- 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
-
#finish_reason ⇒ Object
readonly
Normalized reason the provider finished this response, when reported (see
Riffer::Providers::FinishReason::VALUES). -
#structured_output ⇒ Object
readonly
Parsed structured output hash, or nil when not applicable.
-
#token_usage ⇒ Object
readonly
Token usage data for this response.
-
#tool_calls ⇒ Object
readonly
Array of tool calls requested by the assistant.
Attributes inherited from Base
Instance Method Summary collapse
-
#+(other) ⇒ Object
– : (Riffer::Messages::Assistant) -> Riffer::Messages::Assistant.
-
#has_tool_calls? ⇒ Boolean
– : () -> bool.
-
#initialize(content, id: nil, tool_calls: [], token_usage: nil, structured_output: nil, finish_reason: nil) ⇒ Assistant
constructor
Raises Riffer::ArgumentError when
finish_reasonis outside the normalized vocabulary. -
#role ⇒ Object
– : () -> Symbol.
-
#structured_output? ⇒ Boolean
– : () -> bool.
-
#to_h ⇒ Object
Converts the message to a hash.
Methods inherited from Base
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_reason ⇒ Object (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_output ⇒ Object (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_usage ⇒ Object (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_calls ⇒ Object (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
52 53 54 |
# File 'lib/riffer/messages/assistant.rb', line 52 def has_tool_calls? !@tool_calls.empty? end |
#role ⇒ Object
– : () -> Symbol
40 41 42 |
# File 'lib/riffer/messages/assistant.rb', line 40 def role :assistant end |
#structured_output? ⇒ Boolean
– : () -> bool
46 47 48 |
# File 'lib/riffer/messages/assistant.rb', line 46 def structured_output? !@structured_output.nil? end |
#to_h ⇒ Object
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 |