Module: RubyLLM::ActiveRecord::MessageMethods

Extended by:
ActiveSupport::Concern
Includes:
PayloadHelpers
Defined in:
lib/ruby_llm/active_record/message_methods.rb

Overview

Methods mixed into message models.

Instance Method Summary collapse

Instance Method Details

#cache_read_tokensObject



50
51
52
# File 'lib/ruby_llm/active_record/message_methods.rb', line 50

def cache_read_tokens
  cached_value
end

#cache_write_tokensObject



54
55
56
# File 'lib/ruby_llm/active_record/message_methods.rb', line 54

def cache_write_tokens
  cache_creation_value
end

#costObject



46
47
48
# File 'lib/ruby_llm/active_record/message_methods.rb', line 46

def cost
  RubyLLM::Cost.new(tokens:, model: model_association)
end

#thinkingObject



29
30
31
32
33
34
# File 'lib/ruby_llm/active_record/message_methods.rb', line 29

def thinking
  RubyLLM::Thinking.build(
    text: thinking_text_value,
    signature: thinking_signature_value
  )
end

#to_llmObject



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ruby_llm/active_record/message_methods.rb', line 17

def to_llm
  RubyLLM::Message.new(
    role: role.to_sym,
    content: extract_content,
    thinking: thinking,
    tokens: tokens,
    tool_calls: extract_tool_calls,
    tool_call_id: extract_tool_call_id,
    model_id: model_association&.model_id
  )
end

#to_partial_pathObject



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ruby_llm/active_record/message_methods.rb', line 58

def to_partial_path
  partial_prefix = self.class.name.underscore.pluralize
  role_partial = if to_llm.tool_call?
                   'tool_calls'
                 elsif role.to_s == 'tool'
                   'tool'
                 else
                   role.to_s.presence || 'assistant'
                 end
  "#{partial_prefix}/#{role_partial}"
end

#tokensObject



36
37
38
39
40
41
42
43
44
# File 'lib/ruby_llm/active_record/message_methods.rb', line 36

def tokens
  RubyLLM::Tokens.build(
    input: input_tokens,
    output: output_tokens,
    cached: cached_value,
    cache_creation: cache_creation_value,
    thinking: thinking_tokens_value
  )
end

#tool_error_messageObject



70
71
72
# File 'lib/ruby_llm/active_record/message_methods.rb', line 70

def tool_error_message
  payload_error_message(content)
end