Class: LLM::ActiveRecord::Agent

Inherits:
ActiveRecord::Base show all
Defined in:
lib/llm/active_record/agent.rb

Overview

The Agent class represents the jsonb column belonging to an agent as a SQL view that can be accessed via an ActiveRecord model whose table never materializes in the database.

Class Method Summary collapse

Methods included from ActsAsLLM

#acts_as_llm

Methods included from ActsAsAgent

#acts_as_agent

Class Method Details

.load_schema!

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.

The derived table does not exist. There is no schema to load.



18
19
20
# File 'lib/llm/active_record/agent.rb', line 18

def self.load_schema!
  @columns_hash = {}.freeze
end

.for(record:) ⇒ ActiveRecord::Relation

Parameters:

Returns:

  • (ActiveRecord::Relation)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/llm/active_record/agent.rb', line 26

def self.for(record:)
  klass      = record.class
  connection = klass.connection
  table      = connection.quote_table_name(klass.table_name)
  options    = klass.llm_plugin_options
  column     = connection.quote_column_name(options.fetch(:data_column))
  usage_sql  = LLM::Usage.fields.map do |field|
    ", ((#{table}.#{column} -> 'token_usage') ->> '#{field}')::int AS #{field}"
  end.join
  from(<<~SQL).where(id: record&.id)
    (SELECT #{table}.id                                       AS id,
            (#{table}.#{column} ->> 'context_used')::int      AS context_used,
            (#{table}.#{column} ->> 'context_window')::int    AS context_window,
            (#{table}.#{column} ->> 'model')::text            AS model
            #{usage_sql}
     FROM #{table})
     AS llm_agent_context
  SQL
end