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
- #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
- #create_user_message(content, with: nil) ⇒ Object
- #on_end_message ⇒ Object
- #on_new_message ⇒ Object
- #on_tool_call ⇒ Object
- #on_tool_result ⇒ Object
- #to_llm(context: nil) ⇒ Object
- #with_context(context) ⇒ Object
- #with_headers ⇒ Object
- #with_instructions(instructions, append: false, replace: nil) ⇒ Object
- #with_model ⇒ Object
- #with_params ⇒ Object
- #with_runtime_instructions(instructions, append: false, replace: nil) ⇒ Object
- #with_schema ⇒ Object
- #with_temperature ⇒ Object
- #with_tool ⇒ Object
- #with_tools ⇒ Object
Instance Method Details
#add_message(message_or_attributes) ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 206 def () = .is_a?(RubyLLM::Message) ? : RubyLLM::Message.new() content, = prepare_content_for_storage(.content) attrs = { role: .role, content: } tool_call_foreign_key = .klass.tool_call_foreign_key if .tool_call_id && tool_call_foreign_key tool_call_id = find_tool_call_id(.tool_call_id) attrs[tool_call_foreign_key] = tool_call_id if tool_call_id end = .create!(attrs) persist_content(, ) if .present? persist_tool_calls(.tool_calls, message_record:) if .tool_calls.present? end |
#after_message ⇒ Object
181 182 183 184 |
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 181 def (...) to_llm.(...) self end |
#after_tool_result ⇒ Object
191 192 193 194 |
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 191 def after_tool_result(...) to_llm.after_tool_result(...) self end |
#ask(message = nil, with: nil) ⇒ Object Also known as: say
232 233 234 235 |
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 232 def ask( = nil, with: nil, &) (role: :user, content: build_content(, with)) complete(&) end |
#before_message ⇒ Object
176 177 178 179 |
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 176 def (...) to_llm.(...) self end |
#before_tool_call ⇒ Object
186 187 188 189 |
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 186 def before_tool_call(...) to_llm.before_tool_call(...) self end |
#complete ⇒ Object
239 240 241 242 243 244 245 |
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 239 def complete(...) to_llm.complete(...) rescue RubyLLM::Error => e if @message&.persisted? && @message.content.blank? cleanup_orphaned_tool_results raise e end |
#create_user_message(content, with: nil) ⇒ Object
224 225 226 227 228 229 230 |
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 224 def (content, with: nil) RubyLLM.logger.warn( '`create_user_message` is deprecated and will be removed in RubyLLM 2.0. ' \ 'Use `add_message(role: :user, content: ...)` instead.' ) (role: :user, content: build_content(content, with)) end |
#on_end_message ⇒ Object
171 172 173 174 |
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 171 def (&) to_llm.(&) self end |
#on_new_message ⇒ Object
166 167 168 169 |
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 166 def (&) to_llm.(&) self end |
#on_tool_call ⇒ Object
196 197 198 199 |
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 196 def on_tool_call(...) to_llm.on_tool_call(...) self end |
#on_tool_result ⇒ Object
201 202 203 204 |
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 201 def on_tool_result(...) to_llm.on_tool_result(...) self end |
#to_llm(context: nil) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 92 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. = (.to_a) .each do |msg| @chat.(msg.to_llm) end reapply_runtime_instructions(@chat) setup_persistence_callbacks end |
#with_context(context) ⇒ Object
146 147 148 149 |
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 146 def with_context(context) to_llm(context: context) self end |
#with_headers ⇒ Object
156 157 158 159 |
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 156 def with_headers(...) to_llm.with_headers(...) self end |
#with_instructions(instructions, append: false, replace: nil) ⇒ Object
110 111 112 113 114 115 116 |
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 110 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 ⇒ Object
136 137 138 139 |
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 136 def with_model(...) update(model_id: to_llm.with_model(...).model.id) self end |
#with_params ⇒ Object
151 152 153 154 |
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 151 def with_params(...) to_llm.with_params(...) self end |
#with_runtime_instructions(instructions, append: false, replace: nil) ⇒ Object
118 119 120 121 122 123 124 |
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 118 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
161 162 163 164 |
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 161 def with_schema(...) to_llm.with_schema(...) self end |
#with_temperature ⇒ Object
141 142 143 144 |
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 141 def with_temperature(...) to_llm.with_temperature(...) self end |
#with_tool ⇒ Object
126 127 128 129 |
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 126 def with_tool(...) to_llm.with_tool(...) self end |
#with_tools ⇒ Object
131 132 133 134 |
# File 'lib/ruby_llm/active_record/acts_as_legacy.rb', line 131 def with_tools(...) to_llm.with_tools(...) self end |