Module: LLM::ActiveRecord::ActsAsLLM::InstanceMethods
- Defined in:
- lib/llm/active_record/acts_as_llm.rb
Instance Method Summary collapse
-
#llm ⇒ LLM::Provider
Returns the resolved provider instance for this record.
-
#ask ⇒ LLM::Response
Continues the stored context with new input.
-
#wait ⇒ Array<LLM::Function::Return>
Waits for queued tool work to finish.
- #mode ⇒ Symbol
-
#messages ⇒ ActiveRecord::Relation, LLM::Buffer
Returns the messages this record holds.
-
#messages! ⇒ LLM::Buffer
Returns the messages the runtime holds.
- #pending_functions ⇒ Array<LLM::Function>
- #pending_functions? ⇒ Boolean
- #returns ⇒ Array<LLM::Function::Return>
- #cost ⇒ LLM::Cost
- #model! ⇒ String
- #compactor ⇒ LLM::Compactor?
- #compactor_options ⇒ Hash
-
#context_window ⇒ Integer?
Returns the overall size of the context window.
-
#context_used ⇒ Integer?
Returns how many tokens have been used.
-
#context_usage ⇒ Rational?
Returns context window usage as a Rational.
-
#token_usage ⇒ LLM::Usage
(also: #usage)
Returns the accumulated token usage.
- #interrupt! ⇒ nil (also: #cancel!)
- #prompt ⇒ LLM::Prompt (also: #build_prompt)
- #image_url ⇒ LLM::Object
- #local_file ⇒ LLM::Object
- #remote_file ⇒ LLM::Object
- #tracer ⇒ LLM::Tracer
-
#unwrap! ⇒ LLM::Context, LLM::Agent
Returns the runtime this record wraps.
-
#talk ⇒ LLM::Response
Continues the stored context with new input.
Instance Method Details
#llm ⇒ LLM::Provider
Returns the resolved provider instance for this record.
302 303 304 305 306 307 308 |
# File 'lib/llm/active_record/acts_as_llm.rb', line 302 def llm = self.class. return @llm if @llm @llm = Utils.resolve_provider(self, , EMPTY_HASH) @llm.tracer = Utils.resolve_option(self, [:tracer]) if [:tracer] @llm end |
#ask ⇒ LLM::Response
Continues the stored context with new input.
The conversation is saved by LLM::Step at each request.
79 80 81 |
# File 'lib/llm/active_record/acts_as_llm.rb', line 79 def ask(...) __context__.ask(...) end |
#wait ⇒ Array<LLM::Function::Return>
Waits for queued tool work to finish.
87 88 89 |
# File 'lib/llm/active_record/acts_as_llm.rb', line 87 def wait(...) __context__.wait(...) end |
#mode ⇒ Symbol
94 95 96 |
# File 'lib/llm/active_record/acts_as_llm.rb', line 94 def mode __context__.mode end |
#messages ⇒ ActiveRecord::Relation, LLM::Buffer
A record that stores its state as jsonb reads them from the column, as a relation: the conversation can be filtered, counted and ordered in the database, and nothing here needs a provider to be built. Every other format loads the runtime and hands back the messages it holds.
Returns the messages this record holds.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/llm/active_record/acts_as_llm.rb', line 112 def if defined?(@messages) @messages else @messages = begin = self.class. if [:format] == :jsonb LLM::ActiveRecord::Message.for(record: self) else __context__. end end end end |
#messages! ⇒ LLM::Buffer
Returns the messages the runtime holds
131 132 133 |
# File 'lib/llm/active_record/acts_as_llm.rb', line 131 def __context__. end |
#pending_functions ⇒ Array<LLM::Function>
138 139 140 |
# File 'lib/llm/active_record/acts_as_llm.rb', line 138 def pending_functions __context__.pending_functions end |
#pending_functions? ⇒ Boolean
145 146 147 |
# File 'lib/llm/active_record/acts_as_llm.rb', line 145 def pending_functions? __context__.pending_functions? end |
#returns ⇒ Array<LLM::Function::Return>
152 153 154 |
# File 'lib/llm/active_record/acts_as_llm.rb', line 152 def returns __context__.returns end |
#cost ⇒ LLM::Cost
159 160 161 |
# File 'lib/llm/active_record/acts_as_llm.rb', line 159 def cost __context__.cost end |
#model! ⇒ String
166 167 168 169 170 171 172 173 |
# File 'lib/llm/active_record/acts_as_llm.rb', line 166 def model! if defined?(@model) @model else @model = __data__&.dig("model") || __context__.model end end |
#compactor ⇒ LLM::Compactor?
178 179 180 |
# File 'lib/llm/active_record/acts_as_llm.rb', line 178 def compactor __context__.compactor end |
#compactor_options ⇒ Hash
185 186 187 |
# File 'lib/llm/active_record/acts_as_llm.rb', line 185 def __context__. end |
#context_window ⇒ Integer?
Returns the overall size of the context window.
193 194 195 196 197 198 199 200 |
# File 'lib/llm/active_record/acts_as_llm.rb', line 193 def context_window if defined?(@context_window) @context_window else @context_window = __data__&.dig("context_window") || __context__.context_window end end |
#context_used ⇒ Integer?
Returns how many tokens have been used.
206 207 208 209 210 211 212 |
# File 'lib/llm/active_record/acts_as_llm.rb', line 206 def context_used if defined?(@context_used) @context_used else @context_used = __data__&.dig("context_used") end end |
#context_usage ⇒ Rational?
Returns context window usage as a Rational
217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/llm/active_record/acts_as_llm.rb', line 217 def context_usage if defined?(@context_usage) @context_usage else @context_usage = begin if [context_used, context_window].any?(&:nil?) nil else Rational(context_used, context_window) end end end end |
#token_usage ⇒ LLM::Usage Also known as: usage
Returns the accumulated token usage
235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/llm/active_record/acts_as_llm.rb', line 235 def token_usage if defined?(@token_usage) @token_usage else @token_usage = begin usage = __data__&.dig("token_usage") || {} fields = LLM::Usage.fields.to_h { [_1.to_sym, usage[_1]] } LLM::Usage.new(**fields.transform_values { _1 || 0 }) end end end |
#interrupt! ⇒ nil Also known as: cancel!
251 252 253 |
# File 'lib/llm/active_record/acts_as_llm.rb', line 251 def interrupt! __context__.interrupt! end |
#prompt ⇒ LLM::Prompt Also known as: build_prompt
259 260 261 |
# File 'lib/llm/active_record/acts_as_llm.rb', line 259 def prompt(&) __context__.prompt(&) end |
#image_url ⇒ LLM::Object
267 268 269 |
# File 'lib/llm/active_record/acts_as_llm.rb', line 267 def image_url(...) __context__.image_url(...) end |
#local_file ⇒ LLM::Object
274 275 276 |
# File 'lib/llm/active_record/acts_as_llm.rb', line 274 def local_file(...) __context__.local_file(...) end |
#remote_file ⇒ LLM::Object
281 282 283 |
# File 'lib/llm/active_record/acts_as_llm.rb', line 281 def remote_file(...) __context__.remote_file(...) end |
#tracer ⇒ LLM::Tracer
288 289 290 |
# File 'lib/llm/active_record/acts_as_llm.rb', line 288 def tracer __context__.tracer end |
#unwrap! ⇒ LLM::Context, LLM::Agent
Returns the runtime this record wraps.
295 296 297 |
# File 'lib/llm/active_record/acts_as_llm.rb', line 295 def unwrap! __context__ end |
#talk ⇒ LLM::Response
Continues the stored context with new input.
The conversation is saved by LLM::Step at each request.
69 70 71 |
# File 'lib/llm/active_record/acts_as_llm.rb', line 69 def talk(...) __context__.talk(...) end |