Module: RubyLLM::ActiveRecord::ChatMethods

Extended by:
ActiveSupport::Concern
Defined in:
lib/ruby_llm/active_record/acts_as.rb

Overview

Methods mixed into chat models to handle message persistence and provide a conversation interface.

Instance Method Summary collapse

Instance Method Details

#chatObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ruby_llm/active_record/acts_as.rb', line 39

def chat
  @chat ||= begin
    chat = RubyLLM.chat(model: model_id)

    # Load existing messages into chat
    messages.each do |msg|
      chat.add_message(msg.to_llm)
    end

    # Set up message persistence
    chat.on_new_message { |msg| persist_new_message(msg) }
        .on_end_message { |msg| persist_message_completion(msg) }

    chat
  end
end