Class: Boxcars::ActiveRecord

Inherits:
EngineBoxcar show all
Defined in:
lib/boxcars/boxcar/active_record.rb

Overview

A Boxcar that interprets a prompt and executes SQL code to get answers rubocop:disable Metrics/ClassLength

Constant Summary collapse

ARDESC =

the description of this engine boxcar

"useful for when you need to query a database for an application named %<name>s."
LOCKED_OUT_MODELS =
%w[ActiveRecord::SchemaMigration ActiveRecord::InternalMetadata ApplicationRecord].freeze
CTEMPLATE =
[
  syst("You are a Ruby on Rails Active Record code generator"),
  syst("Given an input question, first create a syntactically correct Rails Active Record code to run, ",
       "then look at the results of the code and return the answer. Unless the user specifies ",
       "in her question a specific number of examples she wishes to obtain, limit your code ",
       "to at most %<top_k>s results.\n",
       "Never query for all the columns from a specific model, ",
       "only ask for the relevant attributes given the question.\n",
       "Also, pay attention to which attribute is in which model.\n\n",
       "Use the following format:\n",
       "Question: ${{Question here}}\n",
       "ARChanges: ${{Active Record code to compute the number of records going to change}} - ",
       "Only add this line if the ARCode on the next line will make data changes.\n",
       "ARCode: ${{Active Record code to run}} - make sure you use valid code\n",
       "Answer: ${{Final answer here}}\n\n",
       "Only use the following Active Record models: %<model_info>s\n",
       "Pay attention to use only the attribute names that you can see in the model description.\n",
       "Do not make up variable or attribute names, and do not share variables between the code in ARChanges and ARCode\n",
       "Be careful to not query for attributes that do not exist, and to use the format specified above.\n",
       "Finally, try not to use print or puts in your code"
      ),
  user("Question: %<question>s")
].freeze

Instance Attribute Summary collapse

Attributes inherited from EngineBoxcar

#engine, #prompt, #stop, #top_k

Attributes inherited from Boxcar

#description, #name, #parameters, #return_direct

Instance Method Summary collapse

Methods inherited from EngineBoxcar

#apply, #call, #check_output_keys, #extract_code, #generate, #input_key, #input_keys, #output_key, #output_keys, #predict, #prediction_input, #prediction_variables

Methods inherited from Boxcar

#apply, assi, #call, #conduct, hist, #input_keys, #load, #output_keys, #run, #save, #schema, syst, user, #validate_inputs, #validate_outputs

Constructor Details

#initialize(models: nil, except_models: nil, read_only: nil, approval_callback: nil, **kwargs) ⇒ ActiveRecord

Returns a new instance of ActiveRecord.

Parameters:

  • models (Array<ActiveRecord::Model>) (defaults to: nil)

    The models to use for this boxcar. Will use all if nil.

  • except_models (Array<ActiveRecord::Model>) (defaults to: nil)

    The models to exclude from this boxcar. Will exclude none if nil.

  • read_only (Boolean) (defaults to: nil)

    Whether to use read only models. Defaults to true unless you pass an approval function.

  • approval_callback (Proc) (defaults to: nil)

    A function to call to approve changes. Defaults to nil.

  • kwargs (Hash)

    Any other keyword arguments. These can include: :name, :description, :prompt, :except_models, :top_k, :stop, :code_only and :engine



20
21
22
23
24
25
26
27
28
29
# File 'lib/boxcars/boxcar/active_record.rb', line 20

def initialize(models: nil, except_models: nil, read_only: nil, approval_callback: nil, **kwargs)
  check_models(models, except_models)
  @approval_callback = approval_callback
  @read_only = read_only.nil? ? !approval_callback : read_only
  @code_only = kwargs.delete(:code_only) || false
  kwargs[:name] ||= get_name
  kwargs[:description] ||= format(ARDESC, name:)
  kwargs[:prompt] ||= my_prompt
  super(**kwargs)
end

Instance Attribute Details

#approval_callbackObject

Returns the value of attribute approval_callback.



11
12
13
# File 'lib/boxcars/boxcar/active_record.rb', line 11

def approval_callback
  @approval_callback
end

#code_onlyObject

Returns the value of attribute code_only.



11
12
13
# File 'lib/boxcars/boxcar/active_record.rb', line 11

def code_only
  @code_only
end

#connectionObject

Returns the value of attribute connection.



11
12
13
# File 'lib/boxcars/boxcar/active_record.rb', line 11

def connection
  @connection
end

#except_modelsObject (readonly)

Returns the value of attribute except_models.



12
13
14
# File 'lib/boxcars/boxcar/active_record.rb', line 12

def except_models
  @except_models
end

#read_onlyObject

Returns the value of attribute read_only.



11
12
13
# File 'lib/boxcars/boxcar/active_record.rb', line 11

def read_only
  @read_only
end

#requested_modelsObject

Returns the value of attribute requested_models.



11
12
13
# File 'lib/boxcars/boxcar/active_record.rb', line 11

def requested_models
  @requested_models
end

Instance Method Details

#prediction_additional(_inputs) ⇒ Object

Returns Hash The additional variables for this boxcar.

Returns:

  • Hash The additional variables for this boxcar.



32
33
34
# File 'lib/boxcars/boxcar/active_record.rb', line 32

def prediction_additional(_inputs)
  { model_info: }.merge super
end