Module: RubyLLM::Sequel::ChatMethods
- Defined in:
- lib/ruby_llm/sequel/chat_methods.rb
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)
- #before_save ⇒ Object
- #complete ⇒ Object
- #create_user_message(content, with: nil) ⇒ Object
- #model=(value) ⇒ Object
- #model_id=(value) ⇒ Object
- #on_end_message(&block) ⇒ Object
- #on_new_message(&block) ⇒ 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_thinking ⇒ Object
- #with_tool ⇒ Object
- #with_tools ⇒ Object
Instance Attribute Details
#assume_model_exists ⇒ Object
Returns the value of attribute assume_model_exists.
6 7 8 |
# File 'lib/ruby_llm/sequel/chat_methods.rb', line 6 def assume_model_exists @assume_model_exists end |
#context ⇒ Object
Returns the value of attribute context.
6 7 8 |
# File 'lib/ruby_llm/sequel/chat_methods.rb', line 6 def context @context end |
Instance Method Details
#ask(message, with: nil) ⇒ Object Also known as: say
159 160 161 162 |
# File 'lib/ruby_llm/sequel/chat_methods.rb', line 159 def ask(, with: nil, &) (, with: with) complete(&) end |
#before_save ⇒ Object
8 9 10 11 |
# File 'lib/ruby_llm/sequel/chat_methods.rb', line 8 def before_save super resolve_model_from_strings end |
#complete ⇒ Object
166 167 168 169 170 171 172 |
# File 'lib/ruby_llm/sequel/chat_methods.rb', line 166 def complete(...) to_llm.complete(...) rescue ::RubyLLM::Error => e if !@message&.id.nil? && @message&.content && @message&.content != '' cleanup_orphaned_tool_results raise e end |
#create_user_message(content, with: nil) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/ruby_llm/sequel/chat_methods.rb', line 147 def (content, with: nil) raise UnsupportedFeatureError, "Cannot use 'with' parameter with Sequel integration" if with content_text, _, content_raw = prepare_content_for_storage(content) = send("add_#{self.class..to_s.singularize}", role: 'user', content: content_text) .update(content_raw: content_raw) if .columns.include?(:content_raw) && content_raw end |
#model=(value) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/ruby_llm/sequel/chat_methods.rb', line 13 def model=(value) @model_string = value if value.is_a?(String) return if value.is_a?(String) send("#{self.class.model_association_name}=", value) end |
#model_id=(value) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/ruby_llm/sequel/chat_methods.rb', line 20 def model_id=(value) if value.is_a?(Integer) || value.nil? super else @model_string = value end end |
#on_end_message(&block) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/ruby_llm/sequel/chat_methods.rb', line 125 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
113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/ruby_llm/sequel/chat_methods.rb', line 113 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
137 138 139 140 |
# File 'lib/ruby_llm/sequel/chat_methods.rb', line 137 def on_tool_call(...) to_llm.on_tool_call(...) self end |
#on_tool_result ⇒ Object
142 143 144 145 |
# File 'lib/ruby_llm/sequel/chat_methods.rb', line 142 def on_tool_result(...) to_llm.on_tool_result(...) self end |
#provider ⇒ Object
32 33 34 35 |
# File 'lib/ruby_llm/sequel/chat_methods.rb', line 32 def provider model_record = send(self.class.model_association_name) model_record&.provider end |
#provider=(value) ⇒ Object
28 29 30 |
# File 'lib/ruby_llm/sequel/chat_methods.rb', line 28 def provider=(value) @provider_string = value end |
#to_llm ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ruby_llm/sequel/chat_methods.rb', line 37 def to_llm model_record = send(self.class.model_association_name) @chat ||= (context || ::RubyLLM).chat( model: model_record.model_id, provider: model_record.provider.to_sym, assume_model_exists: assume_model_exists || false ) @chat. = self.class..constantize tool_calls_assoc = .tool_calls_association_name .eager( tool_calls_assoc, :parent_tool_call, self.class.model_association_name ).all.each do |msg| @chat.(msg.to_llm) end setup_persistence_callbacks end |
#with_headers ⇒ Object
98 99 100 101 |
# File 'lib/ruby_llm/sequel/chat_methods.rb', line 98 def with_headers(...) to_llm.with_headers(...) self end |
#with_instructions(instructions, replace: false) ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/ruby_llm/sequel/chat_methods.rb', line 58 def with_instructions(instructions, replace: false) db.transaction do .where(role: 'system').destroy if replace send("add_#{self.class..to_s.singularize}", role: 'system', content: instructions) end to_llm.with_instructions(instructions) self end |
#with_model(model_name, provider: nil, assume_exists: false) ⇒ Object
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/ruby_llm/sequel/chat_methods.rb', line 77 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 model_record = send(self.class.model_association_name) to_llm.with_model(model_record.model_id, provider: model_record.provider.to_sym, assume_exists: assume_exists) self end |
#with_params ⇒ Object
93 94 95 96 |
# File 'lib/ruby_llm/sequel/chat_methods.rb', line 93 def with_params(...) to_llm.with_params(...) self end |
#with_schema ⇒ Object
103 104 105 106 |
# File 'lib/ruby_llm/sequel/chat_methods.rb', line 103 def with_schema(...) to_llm.with_schema(...) self end |
#with_temperature ⇒ Object
88 89 90 91 |
# File 'lib/ruby_llm/sequel/chat_methods.rb', line 88 def with_temperature(...) to_llm.with_temperature(...) self end |
#with_thinking ⇒ Object
108 109 110 111 |
# File 'lib/ruby_llm/sequel/chat_methods.rb', line 108 def with_thinking(...) to_llm.with_thinking(...) self end |
#with_tool ⇒ Object
67 68 69 70 |
# File 'lib/ruby_llm/sequel/chat_methods.rb', line 67 def with_tool(...) to_llm.with_tool(...) self end |
#with_tools ⇒ Object
72 73 74 75 |
# File 'lib/ruby_llm/sequel/chat_methods.rb', line 72 def with_tools(...) to_llm.with_tools(...) self end |