Module: LLM::ActiveRecord::ActsAsLLM::Utils Private
- Defined in:
- lib/llm/active_record/acts_as_llm.rb
Overview
This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.
Shared helper methods for the ORM wrapper.
These utilities keep persistence plumbing out of the wrapped model’s method namespace so the injected surface stays focused on the runtime API itself.
Class Method Summary collapse
-
.columns(options) ⇒ Hash
private
Maps wrapper options onto the record’s storage columns.
-
.resolve_option(obj, option) ⇒ Object
private
Resolves a single configured option against a model instance.
-
.resolve_options(obj, option, empty_hash) ⇒ Hash
private
Resolves hash-like wrapper options against a model instance.
-
.save(obj, ctx, options) ⇒ void
private
Persists the runtime state and usage columns back onto the record.
-
.serialize_context(ctx, format) ⇒ String, Hash
private
Serializes the runtime into the configured storage format.
Class Method Details
.columns(options) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Maps wrapper options onto the record’s storage columns.
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/llm/active_record/acts_as_llm.rb', line 80 def self.columns() usage_columns = [:usage_columns] { provider_column: [:provider_column], model_column: [:model_column], data_column: [:data_column], input_tokens: usage_columns[:input_tokens], output_tokens: usage_columns[:output_tokens], total_tokens: usage_columns[:total_tokens] }.freeze end |
.resolve_option(obj, option) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Resolves a single configured option against a model instance.
47 48 49 50 51 52 53 54 |
# File 'lib/llm/active_record/acts_as_llm.rb', line 47 def self.resolve_option(obj, option) case option when Proc then obj.instance_exec(&option) when Symbol then obj.send(option) when Hash then option.dup else option end end |
.resolve_options(obj, option, empty_hash) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Resolves hash-like wrapper options against a model instance.
59 60 61 62 63 64 |
# File 'lib/llm/active_record/acts_as_llm.rb', line 59 def self.(obj, option, empty_hash) case option when Proc, Symbol, Hash then resolve_option(obj, option) else empty_hash.dup end end |
.save(obj, ctx, options) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Persists the runtime state and usage columns back onto the record.
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/llm/active_record/acts_as_llm.rb', line 95 def self.save(obj, ctx, ) columns = self.columns() obj.assign_attributes( columns[:data_column] => serialize_context(ctx, [:format]), columns[:input_tokens] => ctx.usage.input_tokens, columns[:output_tokens] => ctx.usage.output_tokens, columns[:total_tokens] => ctx.usage.total_tokens ) obj.save! end |
.serialize_context(ctx, format) ⇒ String, Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Serializes the runtime into the configured storage format.
69 70 71 72 73 74 75 |
# File 'lib/llm/active_record/acts_as_llm.rb', line 69 def self.serialize_context(ctx, format) case format when :string then ctx.to_json when :json, :jsonb then ctx.to_h else raise ArgumentError, "Unknown format: #{format.inspect}" end end |