Module: RubyLLM::Mongoid::ChatMethods
- Extended by:
- ActiveSupport::Concern
- Includes:
- Transaction
- Defined in:
- lib/ruby_llm/mongoid/chat_methods.rb
Overview
Mixes into a Mongoid document that represents a persisted chat session. Mirrors RubyLLM::ActiveRecord::ChatMethods, replacing AR-specific persistence and query calls with Mongoid equivalents.
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 / provider assignment ————————————————————————-.
- #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
————————————————————————- Chat interface — mirrors the AR version ————————————————————————-.
- #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
Methods included from Transaction
Instance Attribute Details
#assume_model_exists ⇒ Object
Returns the value of attribute assume_model_exists.
19 20 21 |
# File 'lib/ruby_llm/mongoid/chat_methods.rb', line 19 def assume_model_exists @assume_model_exists end |
#context ⇒ Object
Returns the value of attribute context.
19 20 21 |
# File 'lib/ruby_llm/mongoid/chat_methods.rb', line 19 def context @context end |
Instance Method Details
#add_message(message_or_attributes) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/ruby_llm/mongoid/chat_methods.rb', line 176 def () = .is_a?(RubyLLM::Message) ? : RubyLLM::Message.new() content_text, , content_raw = prepare_content_for_storage(.content) attrs = { role: .role, content: content_text } if .tool_call_id tc_db_id = find_tool_call_db_id(.tool_call_id) attrs[:parent_tool_call_id] = tc_db_id if tc_db_id end = .create!(attrs) .update!(content_raw: content_raw) if content_raw_field?() persist_content(, ) if .present? persist_tool_calls(.tool_calls, message_record: ) if .tool_calls.present? end |
#after_message ⇒ Object
151 152 153 154 |
# File 'lib/ruby_llm/mongoid/chat_methods.rb', line 151 def (...) to_llm.(...) self end |
#after_tool_result ⇒ Object
161 162 163 164 |
# File 'lib/ruby_llm/mongoid/chat_methods.rb', line 161 def after_tool_result(...) to_llm.after_tool_result(...) self end |
#ask(message = nil, with: nil) ⇒ Object Also known as: say
204 205 206 207 |
# File 'lib/ruby_llm/mongoid/chat_methods.rb', line 204 def ask( = nil, with: nil, &) (role: :user, content: build_content(, with)) complete(&) end |
#before_message ⇒ Object
146 147 148 149 |
# File 'lib/ruby_llm/mongoid/chat_methods.rb', line 146 def (...) to_llm.(...) self end |
#before_tool_call ⇒ Object
156 157 158 159 |
# File 'lib/ruby_llm/mongoid/chat_methods.rb', line 156 def before_tool_call(...) to_llm.before_tool_call(...) self end |
#complete ⇒ Object
211 212 213 214 215 216 217 |
# File 'lib/ruby_llm/mongoid/chat_methods.rb', line 211 def complete(...) to_llm.complete(...) rescue RubyLLM::Error => e if @message&.persisted? && @message.content.blank? cleanup_orphaned_tool_results raise e end |
#cost ⇒ Object
196 197 198 |
# File 'lib/ruby_llm/mongoid/chat_methods.rb', line 196 def cost RubyLLM::Cost.aggregate(.map(&:cost)) end |
#create_user_message(content, with: nil) ⇒ Object
200 201 202 |
# File 'lib/ruby_llm/mongoid/chat_methods.rb', line 200 def (content, with: nil) (role: :user, content: build_content(content, with)) end |
#model=(value) ⇒ Object
Model / provider assignment
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/ruby_llm/mongoid/chat_methods.rb', line 25 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
44 45 46 |
# File 'lib/ruby_llm/mongoid/chat_methods.rb', line 44 def model_id model_association&.model_id end |
#model_id=(value) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/ruby_llm/mongoid/chat_methods.rb', line 36 def model_id=(value) if value.is_a?(String) @model_string = value else super end end |
#on_end_message ⇒ Object
141 142 143 144 |
# File 'lib/ruby_llm/mongoid/chat_methods.rb', line 141 def (&) to_llm.(&) self end |
#on_new_message ⇒ Object
136 137 138 139 |
# File 'lib/ruby_llm/mongoid/chat_methods.rb', line 136 def (&) to_llm.(&) self end |
#on_tool_call ⇒ Object
166 167 168 169 |
# File 'lib/ruby_llm/mongoid/chat_methods.rb', line 166 def on_tool_call(...) to_llm.on_tool_call(...) self end |
#on_tool_result ⇒ Object
171 172 173 174 |
# File 'lib/ruby_llm/mongoid/chat_methods.rb', line 171 def on_tool_result(...) to_llm.on_tool_result(...) self end |
#provider ⇒ Object
52 53 54 |
# File 'lib/ruby_llm/mongoid/chat_methods.rb', line 52 def provider model_association&.provider end |
#provider=(value) ⇒ Object
48 49 50 |
# File 'lib/ruby_llm/mongoid/chat_methods.rb', line 48 def provider=(value) @provider_string = value end |
#to_llm ⇒ Object
Chat interface — mirrors the AR version
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/ruby_llm/mongoid/chat_methods.rb', line 60 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 { |msg| @chat.(msg.to_llm) } reapply_runtime_instructions(@chat) setup_persistence_callbacks end |
#with_headers ⇒ Object
126 127 128 129 |
# File 'lib/ruby_llm/mongoid/chat_methods.rb', line 126 def with_headers(...) to_llm.with_headers(...) self end |
#with_instructions(instructions, append: false, replace: nil) ⇒ Object
76 77 78 79 80 81 |
# File 'lib/ruby_llm/mongoid/chat_methods.rb', line 76 def with_instructions(instructions, append: false, replace: nil) append = append_instructions?(append: append, replace: replace) persist_system_instruction(instructions, append: append) to_llm.with_instructions(instructions, append: append, replace: replace) self end |
#with_model(model_name, provider: nil, assume_exists: false) ⇒ Object
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/ruby_llm/mongoid/chat_methods.rb', line 100 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: assume_exists) self end |
#with_params ⇒ Object
121 122 123 124 |
# File 'lib/ruby_llm/mongoid/chat_methods.rb', line 121 def with_params(...) to_llm.with_params(...) self end |
#with_runtime_instructions(instructions, append: false, replace: nil) ⇒ Object
83 84 85 86 87 88 |
# File 'lib/ruby_llm/mongoid/chat_methods.rb', line 83 def with_runtime_instructions(instructions, append: false, replace: nil) append = append_instructions?(append: append, replace: replace) store_runtime_instruction(instructions, append: append) to_llm.with_instructions(instructions, append: append, replace: replace) self end |
#with_schema ⇒ Object
131 132 133 134 |
# File 'lib/ruby_llm/mongoid/chat_methods.rb', line 131 def with_schema(...) to_llm.with_schema(...) self end |
#with_temperature ⇒ Object
111 112 113 114 |
# File 'lib/ruby_llm/mongoid/chat_methods.rb', line 111 def with_temperature(...) to_llm.with_temperature(...) self end |
#with_thinking ⇒ Object
116 117 118 119 |
# File 'lib/ruby_llm/mongoid/chat_methods.rb', line 116 def with_thinking(...) to_llm.with_thinking(...) self end |
#with_tool ⇒ Object
90 91 92 93 |
# File 'lib/ruby_llm/mongoid/chat_methods.rb', line 90 def with_tool(...) to_llm.with_tool(...) self end |
#with_tools ⇒ Object
95 96 97 98 |
# File 'lib/ruby_llm/mongoid/chat_methods.rb', line 95 def with_tools(...) to_llm.with_tools(...) self end |