Module: LLM::ActiveRecord::ActsAsLLM

Included in:
ActiveRecord::Base
Defined in:
lib/llm/active_record/acts_as_llm.rb

Overview

ActiveRecord integration for persisting LLM::Context state.

This wrapper maps model columns onto provider selection, model selection, usage accounting, and serialized context data while leaving application- specific concerns such as credentials, associations, and UI shaping to the host app.

Context state can be stored as a JSON string (format: :string, the default) or as a structured object (format: :json / :jsonb) for databases such as PostgreSQL that can persist JSON natively. :json and :jsonb expect a real JSON column type with ActiveRecord handling JSON typecasting for the model.

The model implements set_provider (required) and set_context (optional), and the wrapper resolves them by name. A tracer is not one of those callbacks: give it to acts_as_llm as tracer: - a tracer, a proc, or a method name - and only then is it assigned to the provider.

Defined Under Namespace

Modules: Hooks, InstanceMethods

Instance Method Summary collapse

Instance Method Details

#acts_as_llm(options = EMPTY_HASH)

This method returns an undefined value.

Installs the acts_as_llm wrapper on an ActiveRecord model.

Parameters:

  • options (Hash) (defaults to: EMPTY_HASH)

Options Hash (options):

  • :format (Symbol) —

    Storage format for the serialized context. Use :string for text columns, or :json / :jsonb for structured JSON columns with ActiveRecord JSON typecasting enabled.

  • :tracer (Proc, Symbol, LLM::Tracer, nil) —

    Optional tracer, method name, or proc that resolves to one and is assigned through llm.tracer = ... on the resolved provider. There is no set_tracer callback: a tracer is given here, or not at all.

  • :provider (Proc, Symbol, LLM::Provider) —

    Must resolve to an LLM::Provider instance for the current record.



55
56
57
58
59
60
# File 'lib/llm/active_record/acts_as_llm.rb', line 55

def acts_as_llm(options = EMPTY_HASH)
  options = DEFAULTS.merge(options)
  class_attribute :llm_plugin_options, instance_accessor: false, default: DEFAULTS unless respond_to?(:llm_plugin_options)
  self.llm_plugin_options = options.freeze
  extend Hooks
end