Module: LLM::Sequel::Plugin
- Defined in:
- lib/llm/sequel/plugin.rb
Overview
Sequel plugin for persisting LLM::Context state.
This plugin 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 Sequel handling JSON typecasting for the model.
Defined Under Namespace
Modules: ClassMethods, InstanceMethods
Constant Summary collapse
- EMPTY_HASH =
{}.freeze
- DEFAULT_USAGE_COLUMNS =
{ input_tokens: :input_tokens, output_tokens: :output_tokens, total_tokens: :total_tokens }.freeze
- DEFAULTS =
{ provider_column: :provider, model_column: :model, data_column: :data, format: :string, usage_columns: DEFAULT_USAGE_COLUMNS, provider: EMPTY_HASH, context: EMPTY_HASH }.freeze
Class Method Summary collapse
-
.apply(model) ⇒ void
Called by Sequel when the plugin is first applied to a model class.
-
.configure(model, options = EMPTY_HASH) ⇒ void
Called by Sequel after Plugin.apply with the options passed to ‘plugin :llm, …`.
Class Method Details
.apply(model) ⇒ void
This method returns an undefined value.
Called by Sequel when the plugin is first applied to a model class.
This hook installs the plugin’s class- and instance-level behavior on the target model. It runs before configure, so it should only attach methods and not depend on per-model plugin options.
43 44 45 46 |
# File 'lib/llm/sequel/plugin.rb', line 43 def self.apply(model, **) model.extend ClassMethods model.include InstanceMethods end |
.configure(model, options = EMPTY_HASH) ⇒ void
This method returns an undefined value.
Called by Sequel after apply with the options passed to ‘plugin :llm, …`.
This hook merges plugin defaults with the model’s explicit settings and stores the resolved configuration on the model class for later use by instance methods such as LLM::Sequel::Plugin::InstanceMethods#llm and LLM::Sequel::Plugin::InstanceMethods#ctx.
63 64 65 66 67 68 69 70 |
# File 'lib/llm/sequel/plugin.rb', line 63 def self.configure(model, = EMPTY_HASH) = DEFAULTS.merge() usage_columns = DEFAULT_USAGE_COLUMNS.merge([:usage_columns] || EMPTY_HASH) model.instance_variable_set( :@llm_plugin_options, .merge(usage_columns: usage_columns.freeze).freeze ) end |