module RubyLLM::ActiveRecord::ChatLegacyMethods
Methods mixed into chat models.
Public Instance Methods
# File lib/ruby_llm/active_record/acts_as_legacy.rb, line 206 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, attachments_to_persist = prepare_content_for_storage(llm_message.content) attrs = { role: llm_message.role, content: } tool_call_foreign_key = messages.klass.tool_call_foreign_key if llm_message.tool_call_id && tool_call_foreign_key tool_call_id = find_tool_call_id(llm_message.tool_call_id) attrs[tool_call_foreign_key] = tool_call_id if tool_call_id end message_record = messages.create!(attrs) persist_content(message_record, attachments_to_persist) if attachments_to_persist.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/acts_as_legacy.rb, line 181 def after_message(...) to_llm.after_message(...) self end
Source
# File lib/ruby_llm/active_record/acts_as_legacy.rb, line 191 def after_tool_result(...) to_llm.after_tool_result(...) self end
# File lib/ruby_llm/active_record/acts_as_legacy.rb, line 232 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/acts_as_legacy.rb, line 176 def before_message(...) to_llm.before_message(...) self end
Source
# File lib/ruby_llm/active_record/acts_as_legacy.rb, line 186 def before_tool_call(...) to_llm.before_tool_call(...) self end
Source
# File lib/ruby_llm/active_record/acts_as_legacy.rb, line 239 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
# File lib/ruby_llm/active_record/acts_as_legacy.rb, line 224 def create_user_message(content, with: nil) RubyLLM.deprecator.warn( '`create_user_message` is deprecated and will be removed in RubyLLM 2.0. ' \ 'Use `add_message(role: :user, content: ...)` instead.' ) add_message(role: :user, content: build_content(content, with)) end
Source
# File lib/ruby_llm/active_record/acts_as_legacy.rb, line 171 def on_end_message(&) to_llm.on_end_message(&) self end
Source
# File lib/ruby_llm/active_record/acts_as_legacy.rb, line 166 def on_new_message(&) to_llm.on_new_message(&) self end
Source
# File lib/ruby_llm/active_record/acts_as_legacy.rb, line 196 def on_tool_call(...) to_llm.on_tool_call(...) self end
Source
# File lib/ruby_llm/active_record/acts_as_legacy.rb, line 201 def on_tool_result(...) to_llm.on_tool_result(...) self end
Source
# File lib/ruby_llm/active_record/acts_as_legacy.rb, line 92 def to_llm(context: nil) # model_id is a string that RubyLLM can resolve @chat ||= if context context.chat(model: model_id) else RubyLLM.chat(model: model_id) end @chat.reset_messages! ordered_messages = order_messages_for_llm(messages.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/acts_as_legacy.rb, line 146 def with_context(context) to_llm(context: context) self end
Source
# File lib/ruby_llm/active_record/acts_as_legacy.rb, line 156 def with_headers(...) to_llm.with_headers(...) self end
# File lib/ruby_llm/active_record/acts_as_legacy.rb, line 110 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
Source
# File lib/ruby_llm/active_record/acts_as_legacy.rb, line 136 def with_model(...) update(model_id: to_llm.with_model(...).model.id) self end
Source
# File lib/ruby_llm/active_record/acts_as_legacy.rb, line 151 def with_params(...) to_llm.with_params(...) self end
# File lib/ruby_llm/active_record/acts_as_legacy.rb, line 118 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/acts_as_legacy.rb, line 161 def with_schema(...) to_llm.with_schema(...) self end
Source
# File lib/ruby_llm/active_record/acts_as_legacy.rb, line 141 def with_temperature(...) to_llm.with_temperature(...) self end
Source
# File lib/ruby_llm/active_record/acts_as_legacy.rb, line 126 def with_tool(...) to_llm.with_tool(...) self end
Source
# File lib/ruby_llm/active_record/acts_as_legacy.rb, line 131 def with_tools(...) to_llm.with_tools(...) self end