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
- #ask(message, with: nil) ⇒ Object (also: #say)
- #complete ⇒ 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, replace: false) ⇒ Object
- #with_model(model_name, provider: nil, assume_exists: false) ⇒ Object
- #with_params ⇒ Object
- #with_schema ⇒ Object
- #with_temperature ⇒ Object
- #with_tool ⇒ Object
- #with_tools ⇒ Object
Instance Attribute Details
#assume_model_exists ⇒ Object
Returns the value of attribute assume_model_exists.
14 15 16 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 14 def assume_model_exists @assume_model_exists end |
#context ⇒ Object
Returns the value of attribute context.
14 15 16 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 14 def context @context end |
Instance Method Details
#ask(message, with: nil) ⇒ Object Also known as: say
177 178 179 180 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 177 def ask(, with: nil, &) (, with:) complete(&) end |
#complete ⇒ Object
184 185 186 187 188 189 190 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 184 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
163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 163 def (content, with: nil) content_text, , content_raw = prepare_content_for_storage(content) = .build(role: :user) .content = content_text .content_raw = content_raw if .respond_to?(:content_raw=) .save! persist_content(, with) if with.present? persist_content(, ) if .present? end |
#model=(value) ⇒ Object
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 16 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
31 32 33 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 31 def model_id model_association&.model_id end |
#model_id=(value) ⇒ Object
27 28 29 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 27 def model_id=(value) @model_string = value end |
#on_end_message ⇒ Object
148 149 150 151 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 148 def (&) to_llm.(&) self end |
#on_new_message ⇒ Object
143 144 145 146 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 143 def (&) to_llm.(&) self end |
#on_tool_call ⇒ Object
153 154 155 156 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 153 def on_tool_call(...) to_llm.on_tool_call(...) self end |
#on_tool_result ⇒ Object
158 159 160 161 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 158 def on_tool_result(...) to_llm.on_tool_result(...) self end |
#provider ⇒ Object
39 40 41 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 39 def provider model_association&.provider end |
#provider=(value) ⇒ Object
35 36 37 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 35 def provider=(value) @provider_string = value end |
#to_llm ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 79 def to_llm model_record = model_association @chat ||= (context || RubyLLM).chat( model: model_record.model_id, provider: model_record.provider.to_sym ) @chat. .each do |msg| @chat.(msg.to_llm) end setup_persistence_callbacks end |
#with_headers ⇒ Object
133 134 135 136 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 133 def with_headers(...) to_llm.with_headers(...) self end |
#with_instructions(instructions, replace: false) ⇒ Object
94 95 96 97 98 99 100 101 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 94 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(model_name, provider: nil, assume_exists: false) ⇒ Object
113 114 115 116 117 118 119 120 121 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 113 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_params ⇒ Object
128 129 130 131 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 128 def with_params(...) to_llm.with_params(...) self end |
#with_schema ⇒ Object
138 139 140 141 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 138 def with_schema(...) to_llm.with_schema(...) self end |
#with_temperature ⇒ Object
123 124 125 126 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 123 def with_temperature(...) to_llm.with_temperature(...) self end |
#with_tool ⇒ Object
103 104 105 106 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 103 def with_tool(...) to_llm.with_tool(...) self end |
#with_tools ⇒ Object
108 109 110 111 |
# File 'lib/ruby_llm/active_record/chat_methods.rb', line 108 def with_tools(...) to_llm.with_tools(...) self end |