Module: RubyLLM::ActiveRecord::ChatMethods

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

Overview

Methods mixed into chat models.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#assume_model_existsObject

Returns the value of attribute assume_model_exists.



13
14
15
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 13

def assume_model_exists
  @assume_model_exists
end

#contextObject

Returns the value of attribute context.



13
14
15
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 13

def context
  @context
end

Instance Method Details

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



190
191
192
193
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 190

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

#completeObject



197
198
199
200
201
202
203
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 197

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



176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 176

def create_user_message(content, with: nil)
  content_text, attachments, content_raw = prepare_content_for_storage(content)

  message_record = messages_association.build(role: :user)
  message_record.content = content_text
  message_record.content_raw = content_raw if message_record.respond_to?(:content_raw=)
  message_record.save!

  persist_content(message_record, with) if with.present?
  persist_content(message_record, attachments) if attachments.present?

  message_record
end

#model=(value) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 15

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

#model_idObject



30
31
32
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 30

def model_id
  model_association&.model_id
end

#model_id=(value) ⇒ Object



26
27
28
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 26

def model_id=(value)
  @model_string = value
end

#on_end_message(&block) ⇒ Object



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

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



142
143
144
145
146
147
148
149
150
151
152
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 142

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



166
167
168
169
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 166

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

#on_tool_resultObject



171
172
173
174
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 171

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

#providerObject



38
39
40
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 38

def provider
  model_association&.provider
end

#provider=(value) ⇒ Object



34
35
36
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 34

def provider=(value)
  @provider_string = value
end

#to_llmObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 78

def to_llm
  model_record = model_association
  @chat ||= (context || RubyLLM).chat(
    model: model_record.model_id,
    provider: model_record.provider.to_sym
  )
  @chat.reset_messages!

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

  setup_persistence_callbacks
end

#with_headersObject



132
133
134
135
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 132

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

#with_instructions(instructions, replace: false) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 93

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

#with_model(model_name, provider: nil, assume_exists: false) ⇒ Object



112
113
114
115
116
117
118
119
120
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 112

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.model_id, provider: model.provider.to_sym, assume_exists:)
  self
end

#with_paramsObject



127
128
129
130
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 127

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

#with_schemaObject



137
138
139
140
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 137

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

#with_temperatureObject



122
123
124
125
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 122

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

#with_toolObject



102
103
104
105
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 102

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

#with_toolsObject



107
108
109
110
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 107

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