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
- #ask(message, with: nil) ⇒ Object (also: #say)
- #complete ⇒ Object
- #create_user_message(content, with: nil) ⇒ Object
- #on_end_message(&block) ⇒ Object
- #on_new_message(&block) ⇒ Object
- #on_tool_call ⇒ Object
- #on_tool_result ⇒ Object
- #to_llm(context: nil) ⇒ Object
- #with_context(context) ⇒ Object
- #with_headers ⇒ Object
- #with_instructions(instructions, replace: false) ⇒ Object
- #with_model ⇒ Object
- #with_params ⇒ Object
- #with_schema ⇒ Object
- #with_temperature ⇒ Object
- #with_tool ⇒ Object
- #with_tools ⇒ Object
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(, with: nil, &) (, with:) complete(&) end |
#complete ⇒ Object
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 if &.persisted? && .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 (content, with: nil) = .create!(role: :user, content: content) persist_content(, with) if with.present? 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 (&block) to_llm existing_callback = @chat.instance_variable_get(:@on)[:end_message] @chat. 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 (&block) to_llm existing_callback = @chat.instance_variable_get(:@on)[:new_message] @chat. do existing_callback&.call block&.call end self end |
#on_tool_call ⇒ Object
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_result ⇒ Object
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. .each do |msg| @chat.(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_headers ⇒ Object
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 .where(role: :system).destroy_all if replace .create!(role: :system, content: instructions) end to_llm.with_instructions(instructions) self end |
#with_model ⇒ Object
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_params ⇒ Object
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_schema ⇒ Object
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_temperature ⇒ Object
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_tool ⇒ Object
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_tools ⇒ Object
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 |