Class: Boxcars::SQLBase Abstract

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

Overview

This class is abstract.

A Boxcar that interprets a prompt and executes SQL code to get answers Use one of the subclasses for ActiveRecord or Sequel

Direct Known Subclasses

SQLActiveRecord, SQLSequel

Constant Summary collapse

SQLDESC =

the description of this engine boxcar

"useful for when you need to query a database for %<name>s."
LOCKED_OUT_TABLES =
%w[schema_migrations ar_internal_metadata].freeze
CTEMPLATE =
[
  syst("Given an input question, first create a syntactically correct %<dialect>s SQL query to run, ",
       "then look at the results of the query and return the answer. Unless the user specifies ",
       "in her question a specific number of examples he wishes to obtain, always limit your query ",
       "to at most %<top_k>s results using a LIMIT clause. You can order the results by a relevant column ",
       "to return the most interesting examples in the database.\n",
       "Never query for all the columns from a specific table, only ask for the elevant columns given the question.\n",
       "Pay attention to use only the column names that you can see in the schema description. Be careful to ",
       "not query for columns that do not exist. Also, pay attention to which column is in which table.\n",
       "Use the following format:\n",
       "Question: 'Question here'\n",
       "SQLQuery: 'SQL Query to run'\n",
       "SQLResult: 'Result of the SQLQuery'\n",
       "Answer: 'Final answer here'"),
  syst("Only use the following tables:\n%<schema>s"),
  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, syst, user, #validate_inputs, #validate_outputs

Constructor Details

#initialize(connection: nil, tables: nil, except_tables: nil, **kwargs) ⇒ SQLBase

Returns a new instance of SQLBase.

Parameters:

  • connection (ActiveRecord::Connection) (defaults to: nil)

    or [Sequel Object] The SQL connection to use for this boxcar.

  • tables (Array<String>) (defaults to: nil)

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

  • except_tables (Array<String>) (defaults to: nil)

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

  • kwargs (Hash)

    Any other keyword arguments to pass to the parent class. This can include :name, :description, :prompt, :top_k, :stop, and :engine



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

def initialize(connection: nil, tables: nil, except_tables: nil, **kwargs)
  @connection = connection
  check_tables(tables, except_tables)
  kwargs[:name] ||= "Database"
  kwargs[:description] ||= format(SQLDESC, name:)
  kwargs[:prompt] ||= my_prompt
  kwargs[:stop] ||= ["SQLResult:"]

  super(**kwargs)
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



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

def connection
  @connection
end

#the_tablesObject

Returns the value of attribute the_tables.



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

def the_tables
  @the_tables
end

Instance Method Details

#prediction_additional(_inputs) ⇒ Object

Returns Hash The additional variables for this boxcar.

Returns:

  • Hash The additional variables for this boxcar.



31
32
33
# File 'lib/boxcars/boxcar/sql_base.rb', line 31

def prediction_additional(_inputs)
  { schema:, dialect: }.merge super
end