module RubyLLM::ActiveRecord::ChatMethods
Methods mixed into chat models.
Attributes
Public Instance Methods
# File lib/ruby_llm/active_record/chat_methods.rb, line 199 def add_message(message_or_attributes) llm_message = message_or_attributes.is_a?(RubyLLM::Message) ? message_or_attributes : RubyLLM::Message.new(message_or_attributes) content_text, attachments, content_raw = prepare_content_for_storage(llm_message.content) attrs = { role: llm_message.role, content: content_text } parent_tool_call_assoc = messages_association.klass.reflect_on_association(:parent_tool_call) if parent_tool_call_assoc && llm_message.tool_call_id tool_call_id = find_tool_call_id(llm_message.tool_call_id) attrs[parent_tool_call_assoc.foreign_key] = tool_call_id if tool_call_id end message_record = messages_association.create!(attrs) message_record.update!(content_raw:) if message_record.respond_to?(:content_raw=) persist_content(message_record, attachments) if attachments.present? persist_tool_calls(llm_message.tool_calls, message_record:) if llm_message.tool_calls.present? message_record end
Source
# File lib/ruby_llm/active_record/chat_methods.rb, line 174 def after_message(...) to_llm.after_message(...) self end
Source
# File lib/ruby_llm/active_record/chat_methods.rb, line 184 def after_tool_result(...) to_llm.after_tool_result(...) self end
# File lib/ruby_llm/active_record/chat_methods.rb, line 227 def ask(message = nil, with: nil, &) add_message(role: :user, content: build_content(message, with)) complete(&) end
Also aliased as: say
Source
# File lib/ruby_llm/active_record/chat_methods.rb, line 169 def before_message(...) to_llm.before_message(...) self end
Source
# File lib/ruby_llm/active_record/chat_methods.rb, line 179 def before_tool_call(...) to_llm.before_tool_call(...) self end
Source
# File lib/ruby_llm/active_record/chat_methods.rb, line 234 def complete(...) to_llm.complete(...) rescue RubyLLM::Error => e cleanup_failed_messages if @message&.persisted? && @message.content.blank? cleanup_orphaned_tool_results raise e end
Source
# File lib/ruby_llm/active_record/chat_methods.rb, line 219 def cost RubyLLM::Cost.aggregate(messages_association.map(&:cost)) end
# File lib/ruby_llm/active_record/chat_methods.rb, line 223 def create_user_message(content, with: nil) add_message(role: :user, content: build_content(content, with)) end
Source
# File lib/ruby_llm/active_record/chat_methods.rb, line 17 def model=(value) @model_string = value if value.is_a?(String) return if value.is_a?(String) if self.class.model_association_name == :model super else self.model_association = value end end
Calls superclass method
Source
# File lib/ruby_llm/active_record/chat_methods.rb, line 32 def model_id model_association&.model_id end
Source
# File lib/ruby_llm/active_record/chat_methods.rb, line 28 def model_id=(value) @model_string = value end
Source
# File lib/ruby_llm/active_record/chat_methods.rb, line 164 def on_end_message(&) to_llm.on_end_message(&) self end
Source
# File lib/ruby_llm/active_record/chat_methods.rb, line 159 def on_new_message(&) to_llm.on_new_message(&) self end
Source
# File lib/ruby_llm/active_record/chat_methods.rb, line 189 def on_tool_call(...) to_llm.on_tool_call(...) self end
Source
# File lib/ruby_llm/active_record/chat_methods.rb, line 194 def on_tool_result(...) to_llm.on_tool_result(...) self end
Source
# File lib/ruby_llm/active_record/chat_methods.rb, line 40 def provider model_association&.provider end
Source
# File lib/ruby_llm/active_record/chat_methods.rb, line 36 def provider=(value) @provider_string = value end
Source
# File lib/ruby_llm/active_record/chat_methods.rb, line 80 def to_llm model_record = model_association @chat ||= (context || RubyLLM).chat( model: model_record.model_id, provider: model_record.provider.to_sym, assume_model_exists: assume_model_exists || false ) @chat.reset_messages! ordered_messages = order_messages_for_llm(messages_association.to_a) ordered_messages.each do |msg| @chat.add_message(msg.to_llm) end reapply_runtime_instructions(@chat) setup_persistence_callbacks end
Source
# File lib/ruby_llm/active_record/chat_methods.rb, line 149 def with_headers(...) to_llm.with_headers(...) self end
# File lib/ruby_llm/active_record/chat_methods.rb, line 98 def with_instructions(instructions, append: false, replace: nil) append = append_instructions?(append:, replace:) persist_system_instruction(instructions, append:) to_llm.with_instructions(instructions, append:, replace:) self end
# File lib/ruby_llm/active_record/chat_methods.rb, line 124 def with_model(model_name, provider: nil, assume_exists: false) self.model = model_name self.provider = provider if provider self.assume_model_exists = assume_exists resolve_model_from_strings save! to_llm.with_model(model_association.model_id, provider: model_association.provider.to_sym, assume_exists:) self end
Source
# File lib/ruby_llm/active_record/chat_methods.rb, line 144 def with_params(...) to_llm.with_params(...) self end
# File lib/ruby_llm/active_record/chat_methods.rb, line 106 def with_runtime_instructions(instructions, append: false, replace: nil) append = append_instructions?(append:, replace:) store_runtime_instruction(instructions, append:) to_llm.with_instructions(instructions, append:, replace:) self end
Source
# File lib/ruby_llm/active_record/chat_methods.rb, line 154 def with_schema(...) to_llm.with_schema(...) self end
Source
# File lib/ruby_llm/active_record/chat_methods.rb, line 134 def with_temperature(...) to_llm.with_temperature(...) self end
Source
# File lib/ruby_llm/active_record/chat_methods.rb, line 139 def with_thinking(...) to_llm.with_thinking(...) self end
Source
# File lib/ruby_llm/active_record/chat_methods.rb, line 114 def with_tool(...) to_llm.with_tool(...) self end
Source
# File lib/ruby_llm/active_record/chat_methods.rb, line 119 def with_tools(...) to_llm.with_tools(...) self end