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



194
195
196
197
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 194

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

#completeObject



201
202
203
204
205
206
207
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 201

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



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

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(&block) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 166

def on_end_message(&block)
  to_llm

  existing_callback = @chat.instance_variable_get(:@on)[:end_message]

  @chat.on_end_message do |msg|
    existing_callback&.call(msg)
    block&.call(msg)
  end
  self
end

#on_new_message(&block) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 154

def on_new_message(&block)
  to_llm

  existing_callback = @chat.instance_variable_get(:@on)[:new_message]

  @chat.on_new_message do
    existing_callback&.call
    block&.call
  end
  self
end

#on_tool_callObject



178
179
180
181
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 178

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

#on_tool_resultObject



183
184
185
186
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 183

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

#to_llm(context: nil) ⇒ Object



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

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



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

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

#with_headersObject



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

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

#with_instructions(instructions, replace: false) ⇒ Object



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

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_modelObject



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

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

#with_paramsObject



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

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

#with_schemaObject



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

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

#with_temperatureObject



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

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

#with_toolObject



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

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

#with_toolsObject



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

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