Class: Boxcars::ActiveRecord
- Inherits:
-
EngineBoxcar
- Object
- Boxcar
- EngineBoxcar
- Boxcars::ActiveRecord
- Defined in:
- lib/boxcars/boxcar/active_record.rb
Overview
A Boxcar that interprets a prompt and executes Active Record code to get answers. rubocop:disable Metrics/ClassLength
Constant Summary collapse
- ARDESC =
Default description for this 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", "%<context>s" ), user("Question: %<question>s") ].freeze
Constants inherited from Boxcar
Boxcar::SCHEMA_KEY_ALIASES, Boxcar::TYPE_ALIASES
Instance Attribute Summary collapse
-
#approval_callback ⇒ Object
Returns the value of attribute approval_callback.
-
#code_only ⇒ Object
Returns the value of attribute code_only.
-
#connection ⇒ Object
Returns the value of attribute connection.
-
#context ⇒ Object
Returns the value of attribute context.
-
#except_models ⇒ Object
readonly
Returns the value of attribute except_models.
-
#read_only ⇒ Object
Returns the value of attribute read_only.
-
#requested_models ⇒ Object
Returns the value of attribute requested_models.
Attributes inherited from EngineBoxcar
#engine, #prompt, #stop, #top_k
Attributes inherited from Boxcar
#description, #name, #parameters, #return_direct
Instance Method Summary collapse
-
#initialize(models: nil, except_models: nil, read_only: nil, approval_callback: nil, context: nil, **kwargs) ⇒ ActiveRecord
constructor
A new instance of ActiveRecord.
-
#prediction_additional(_inputs) ⇒ Hash
The additional variables for this boxcar.
Methods inherited from EngineBoxcar
#apply, #call, #extract_code, #generate, #input_keys, #output_key, #output_keys, #predict, #prediction_variables
Methods inherited from Boxcar
#apply, assi, #call, #conduct, #conduct_result, hist, #input_keys, #output_keys, #parameters_json_schema, #run, #run_result, #schema, syst, #tool_call_name, #tool_definition, #tool_spec, user, #validate_inputs, #validate_outputs
Constructor Details
#initialize(models: nil, except_models: nil, read_only: nil, approval_callback: nil, context: nil, **kwargs) ⇒ ActiveRecord
Returns a new instance of ActiveRecord.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/boxcars/boxcar/active_record.rb', line 20 def initialize(models: nil, except_models: nil, read_only: nil, approval_callback: nil, context: nil, **kwargs) @context = context Boxcars::OptionalDependency.require!( "activerecord", feature: "Boxcars::ActiveRecord", require_as: "active_record" ) 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_callback ⇒ Object
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_only ⇒ Object
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 |
#connection ⇒ Object
Returns the value of attribute connection.
11 12 13 |
# File 'lib/boxcars/boxcar/active_record.rb', line 11 def connection @connection end |
#context ⇒ Object
Returns the value of attribute context.
11 12 13 |
# File 'lib/boxcars/boxcar/active_record.rb', line 11 def context @context end |
#except_models ⇒ Object (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_only ⇒ Object
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_models ⇒ Object
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) ⇒ Hash
Returns The additional variables for this boxcar.
38 39 40 41 42 |
# File 'lib/boxcars/boxcar/active_record.rb', line 38 def prediction_additional(_inputs) ctx = @context.to_s.strip context_str = ctx.empty? ? "" : "\n\nAdditional context:\n#{ctx}" { model_info:, context: context_str }.merge super end |