class RubyLLM::Message
A single message in a chat conversation.
Constants
- ROLES
Attributes
Public Class Methods
Source
# File lib/ruby_llm/message.rb, line 11 def initialize(options = {}) @role = options.fetch(:role).to_sym @tool_calls = options[:tool_calls] @content = normalize_content(options.fetch(:content), role: @role, tool_calls: @tool_calls) @model_id = options[:model_id] @tool_call_id = options[:tool_call_id] @tokens = options[:tokens] || Tokens.build( input: options[:input_tokens], output: options[:output_tokens], cached: options[:cached_tokens], cache_creation: options[:cache_creation_tokens], thinking: options[:thinking_tokens], reasoning: options[:reasoning_tokens] ) @raw = options[:raw] @thinking = options[:thinking] ensure_valid_role end
Public Instance Methods
Source
# File lib/ruby_llm/message.rb, line 63 def cache_creation_tokens tokens&.cache_creation end
Source
# File lib/ruby_llm/message.rb, line 67 def cache_read_tokens tokens&.cache_read end
Source
# File lib/ruby_llm/message.rb, line 71 def cache_write_tokens tokens&.cache_write end
Source
# File lib/ruby_llm/message.rb, line 59 def cached_tokens tokens&.cached end
Source
# File lib/ruby_llm/message.rb, line 31 def content if @content.is_a?(Content) && @content.text && @content.attachments.empty? @content.text else @content end end
Source
# File lib/ruby_llm/message.rb, line 83 def cost(model: nil) Cost.new(tokens:, model: model || model_info) end
Source
# File lib/ruby_llm/message.rb, line 99 def instance_variables super - [:@raw] end
Calls superclass method
Source
# File lib/ruby_llm/message.rb, line 103 def model_info return unless model_id @model_info ||= RubyLLM.models.find(model_id) rescue ModelNotFoundError nil end
Source
# File lib/ruby_llm/message.rb, line 55 def output_tokens tokens&.output end
Source
# File lib/ruby_llm/message.rb, line 79 def reasoning_tokens tokens&.thinking end
Source
# File lib/ruby_llm/message.rb, line 75 def thinking_tokens tokens&.thinking end
Source
# File lib/ruby_llm/message.rb, line 87 def to_h { role: role, content: content, model_id: model_id, tool_calls: tool_calls, tool_call_id: tool_call_id, thinking: thinking&.text, thinking_signature: thinking&.signature }.merge(tokens ? tokens.to_h : {}).compact end
Source
# File lib/ruby_llm/message.rb, line 39 def tool_call? !tool_calls.nil? && !tool_calls.empty? end
Source
# File lib/ruby_llm/message.rb, line 43 def tool_result? !tool_call_id.nil? && !tool_call_id.empty? end
Source
# File lib/ruby_llm/message.rb, line 47 def tool_results content if tool_result? end