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

#thinkingObject



26
27
28
29
30
31
# File 'lib/ruby_llm/active_record/message_methods.rb', line 26

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

#to_llmObject



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ruby_llm/active_record/message_methods.rb', line 14

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



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ruby_llm/active_record/message_methods.rb', line 43

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



33
34
35
36
37
38
39
40
41
# File 'lib/ruby_llm/active_record/message_methods.rb', line 33

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



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

def tool_error_message
  payload_error_message(content)
end