Module: RubyLLM::ActiveRecord::ChatLegacyMethods

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

Overview

Methods mixed into chat models.

Instance Method Summary collapse

Instance Method Details

#ask(message, with: nil) ⇒ Object Also known as: say



181
182
183
184
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 181

def ask(message, with: nil, &)
  create_user_message(message, with:)
  complete(&)
end

#complete ⇒ Object



188
189
190
191
192
193
194
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 188

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

#create_user_message(content, with: nil) ⇒ Object



175
176
177
178
179
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 175

def create_user_message(content, with: nil)
  message_record = messages.create!(role: :user, content: content)
  persist_content(message_record, with) if with.present?
  message_record
end

#on_end_message ⇒ Object



160
161
162
163
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 160

def on_end_message(&)
  to_llm.on_end_message(&)
  self
end

#on_new_message ⇒ Object



155
156
157
158
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 155

def on_new_message(&)
  to_llm.on_new_message(&)
  self
end

#on_tool_call ⇒ Object



165
166
167
168
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 165

def on_tool_call(...)
  to_llm.on_tool_call(...)
  self
end

#on_tool_result ⇒ Object



170
171
172
173
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 170

def on_tool_result(...)
  to_llm.on_tool_result(...)
  self
end

#to_llm(context: nil) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 90

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!

  messages.each do |msg|
    @chat.add_message(msg.to_llm)
  end

  setup_persistence_callbacks
end

#with_context(context) ⇒ Object



135
136
137
138
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 135

def with_context(context)
  to_llm(context: context)
  self
end

#with_headers ⇒ Object



145
146
147
148
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 145

def with_headers(...)
  to_llm.with_headers(...)
  self
end

#with_instructions(instructions, replace: false) ⇒ Object



106
107
108
109
110
111
112
113
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 106

def with_instructions(instructions, replace: false)
  transaction do
    messages.where(role: :system).destroy_all if replace
    messages.create!(role: :system, content: instructions)
  end
  to_llm.with_instructions(instructions)
  self
end

#with_model ⇒ Object



125
126
127
128
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 125

def with_model(...)
  update(model_id: to_llm.with_model(...).model.id)
  self
end

#with_params ⇒ Object



140
141
142
143
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 140

def with_params(...)
  to_llm.with_params(...)
  self
end

#with_schema ⇒ Object



150
151
152
153
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 150

def with_schema(...)
  to_llm.with_schema(...)
  self
end

#with_temperature ⇒ Object



130
131
132
133
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 130

def with_temperature(...)
  to_llm.with_temperature(...)
  self
end

#with_tool ⇒ Object



115
116
117
118
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 115

def with_tool(...)
  to_llm.with_tool(...)
  self
end

#with_tools ⇒ Object



120
121
122
123
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 120

def with_tools(...)
  to_llm.with_tools(...)
  self
end