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 =

Default description for this boxcar.

"useful for when you need to query a database for %<name>s."
LOCKED_OUT_TABLES =
%w[schema_migrations ar_internal_metadata].freeze
WRITE_SQL_KEYWORDS =

SQL keywords that indicate a write operation.

%w[INSERT UPDATE DELETE DROP ALTER CREATE TRUNCATE REPLACE MERGE UPSERT
GRANT REVOKE LOCK CALL EXEC EXECUTE].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 relevant 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%<context>s"),
  user("Question: %<question>s")
].freeze

Constants inherited from Boxcar

Boxcar::SCHEMA_KEY_ALIASES, Boxcar::TYPE_ALIASES

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, #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, syst, #tool_call_name, #tool_definition, #tool_spec, user, #validate_inputs, #validate_outputs

Constructor Details

#initialize(connection: nil, tables: nil, except_tables: nil, context: nil, read_only: nil, approval_callback: 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.

  • read_only (Boolean) (defaults to: nil)

    Whether to restrict to read-only SQL. Defaults to true unless approval_callback is given.

  • approval_callback (Proc) (defaults to: nil)

    A function to call to approve write SQL. Receives the SQL string. Defaults to nil.

  • kwargs (Hash)

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



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/boxcars/boxcar/sql_base.rb', line 25

def initialize(connection: nil, tables: nil, except_tables: nil, context: nil, read_only: nil,
               approval_callback: nil, **kwargs)
  @context = context
  @connection = connection
  @approval_callback = approval_callback
  @read_only = read_only.nil? ? !approval_callback : read_only
  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

#approval_callbackObject

Returns the value of attribute approval_callback.



16
17
18
# File 'lib/boxcars/boxcar/sql_base.rb', line 16

def approval_callback
  @approval_callback
end

#connectionObject

Returns the value of attribute connection.



16
17
18
# File 'lib/boxcars/boxcar/sql_base.rb', line 16

def connection
  @connection
end

#contextObject

Returns the value of attribute context.



16
17
18
# File 'lib/boxcars/boxcar/sql_base.rb', line 16

def context
  @context
end

#read_onlyObject

Returns the value of attribute read_only.



16
17
18
# File 'lib/boxcars/boxcar/sql_base.rb', line 16

def read_only
  @read_only
end

#the_tablesObject

Returns the value of attribute the_tables.



16
17
18
# File 'lib/boxcars/boxcar/sql_base.rb', line 16

def the_tables
  @the_tables
end

Instance Method Details

#prediction_additional(_inputs) ⇒ Hash

Returns The additional variables for this boxcar.

Returns:

  • (Hash)

    The additional variables for this boxcar.



41
42
43
44
45
# File 'lib/boxcars/boxcar/sql_base.rb', line 41

def prediction_additional(_inputs)
  ctx = @context.to_s.strip
  context_str = ctx.empty? ? "" : "\n\nAdditional context:\n#{ctx}"
  { schema:, dialect:, context: context_str }.merge super
end

#read_only?Boolean

Returns Whether this boxcar is in read-only mode.

Returns:

  • (Boolean)

    Whether this boxcar is in read-only mode.



66
67
68
# File 'lib/boxcars/boxcar/sql_base.rb', line 66

def read_only?
  read_only
end