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
-
#assume_model_exists ⇒ Object
Returns the value of attribute assume_model_exists.
-
#context ⇒ Object
Returns the value of attribute context.
Instance Method Summary collapse
- #add_message(message_or_attributes) ⇒ Object
- #after_message ⇒ Object
- #after_tool_result ⇒ Object
- #ask(message = nil, with: nil) ⇒ Object (also: #say)
- #before_message ⇒ Object
- #before_tool_call ⇒ Object
- #complete ⇒ Object
- #cost ⇒ Object
- #create_user_message(content, with: nil) ⇒ Object
- #model=(value) ⇒ Object
- #model_id ⇒ Object
- #model_id=(value) ⇒ Object
- #on_end_message ⇒ Object
- #on_new_message ⇒ Object
- #on_tool_call ⇒ Object
- #on_tool_result ⇒ Object
- #provider ⇒ Object
- #provider=(value) ⇒ Object
- #to_llm ⇒ Object
- #with_headers ⇒ Object
- #with_instructions(instructions, append: false, replace: nil) ⇒ Object
- #with_model(model_name, provider: nil, assume_exists: false) ⇒ Object
- #with_params ⇒ Object
- #with_runtime_instructions(instructions, append: false, replace: nil) ⇒ Object
- #with_schema ⇒ Object
- #with_temperature ⇒ Object
- #with_thinking ⇒ Object
- #with_tool ⇒ Object
- #with_tools ⇒ Object
Instance Attribute Details
#assume_model_exists ⇒ Object
Returns the value of attribute assume_model_exists.
15 16 17 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 15 def assume_model_exists @assume_model_exists end |
#context ⇒ Object
Returns the value of attribute context.
15 16 17 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 15 def context @context end |
Instance Method Details
#add_message(message_or_attributes) ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 199 def () = .is_a?(RubyLLM::Message) ? : RubyLLM::Message.new() content_text, , content_raw = prepare_content_for_storage(.content) attrs = { role: .role, content: content_text } parent_tool_call_assoc = .klass.reflect_on_association(:parent_tool_call) if parent_tool_call_assoc && .tool_call_id tool_call_id = find_tool_call_id(.tool_call_id) attrs[parent_tool_call_assoc.foreign_key] = tool_call_id if tool_call_id end = .create!(attrs) .update!(content_raw:) if .respond_to?(:content_raw=) persist_content(, ) if .present? persist_tool_calls(.tool_calls, message_record:) if .tool_calls.present? end |
#after_message ⇒ Object
174 175 176 177 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 174 def (...) to_llm.(...) self end |
#after_tool_result ⇒ Object
184 185 186 187 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 184 def after_tool_result(...) to_llm.after_tool_result(...) self end |
#ask(message = nil, with: nil) ⇒ Object Also known as: say
227 228 229 230 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 227 def ask( = nil, with: nil, &) (role: :user, content: build_content(, with)) complete(&) end |
#before_message ⇒ Object
169 170 171 172 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 169 def (...) to_llm.(...) self end |
#before_tool_call ⇒ Object
179 180 181 182 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 179 def before_tool_call(...) to_llm.before_tool_call(...) self end |
#complete ⇒ Object
234 235 236 237 238 239 240 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 234 def complete(...) to_llm.complete(...) rescue RubyLLM::Error => e if @message&.persisted? && @message.content.blank? cleanup_orphaned_tool_results raise e end |
#cost ⇒ Object
219 220 221 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 219 def cost RubyLLM::Cost.aggregate(.map(&:cost)) end |
#create_user_message(content, with: nil) ⇒ Object
223 224 225 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 223 def (content, with: nil) (role: :user, content: build_content(content, with)) end |
#model=(value) ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 17 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_id ⇒ Object
32 33 34 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 32 def model_id model_association&.model_id end |
#model_id=(value) ⇒ Object
28 29 30 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 28 def model_id=(value) @model_string = value end |
#on_end_message ⇒ Object
164 165 166 167 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 164 def (&) to_llm.(&) self end |
#on_new_message ⇒ Object
159 160 161 162 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 159 def (&) to_llm.(&) self end |
#on_tool_call ⇒ Object
189 190 191 192 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 189 def on_tool_call(...) to_llm.on_tool_call(...) self end |
#on_tool_result ⇒ Object
194 195 196 197 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 194 def on_tool_result(...) to_llm.on_tool_result(...) self end |
#provider ⇒ Object
40 41 42 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 40 def provider model_association&.provider end |
#provider=(value) ⇒ Object
36 37 38 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 36 def provider=(value) @provider_string = value end |
#to_llm ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 80 def to_llm model_record = model_association @chat ||= (context || RubyLLM).chat( model: model_record.model_id, provider: model_record.provider.to_sym, assume_model_exists: assume_model_exists || false ) @chat. = (.to_a) .each do |msg| @chat.(msg.to_llm) end reapply_runtime_instructions(@chat) setup_persistence_callbacks end |
#with_headers ⇒ Object
149 150 151 152 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 149 def with_headers(...) to_llm.with_headers(...) self end |
#with_instructions(instructions, append: false, replace: nil) ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 98 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 |
#with_model(model_name, provider: nil, assume_exists: false) ⇒ Object
124 125 126 127 128 129 130 131 132 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 124 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_association.model_id, provider: model_association.provider.to_sym, assume_exists:) self end |
#with_params ⇒ Object
144 145 146 147 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 144 def with_params(...) to_llm.with_params(...) self end |
#with_runtime_instructions(instructions, append: false, replace: nil) ⇒ Object
106 107 108 109 110 111 112 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 106 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 |
#with_schema ⇒ Object
154 155 156 157 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 154 def with_schema(...) to_llm.with_schema(...) self end |
#with_temperature ⇒ Object
134 135 136 137 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 134 def with_temperature(...) to_llm.with_temperature(...) self end |
#with_thinking ⇒ Object
139 140 141 142 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 139 def with_thinking(...) to_llm.with_thinking(...) self end |
#with_tool ⇒ Object
114 115 116 117 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 114 def with_tool(...) to_llm.with_tool(...) self end |
#with_tools ⇒ Object
119 120 121 122 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 119 def with_tools(...) to_llm.with_tools(...) self end |