Class: Boxcars::SQLBase Abstract
- Inherits:
-
EngineBoxcar
- Object
- Boxcar
- EngineBoxcar
- Boxcars::SQLBase
- Defined in:
- lib/boxcars/boxcar/sql_base.rb
Overview
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
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
-
#approval_callback ⇒ Object
Returns the value of attribute approval_callback.
-
#connection ⇒ Object
Returns the value of attribute connection.
-
#context ⇒ Object
Returns the value of attribute context.
-
#read_only ⇒ Object
Returns the value of attribute read_only.
-
#the_tables ⇒ Object
Returns the value of attribute the_tables.
Attributes inherited from EngineBoxcar
#engine, #prompt, #stop, #top_k
Attributes inherited from Boxcar
#description, #name, #parameters, #return_direct
Instance Method Summary collapse
-
#initialize(connection: nil, tables: nil, except_tables: nil, context: nil, read_only: nil, approval_callback: nil, **kwargs) ⇒ SQLBase
constructor
A new instance of SQLBase.
-
#prediction_additional(_inputs) ⇒ Hash
The additional variables for this boxcar.
-
#read_only? ⇒ Boolean
Whether this boxcar is in read-only mode.
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.
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_callback ⇒ Object
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 |
#connection ⇒ Object
Returns the value of attribute connection.
16 17 18 |
# File 'lib/boxcars/boxcar/sql_base.rb', line 16 def connection @connection end |
#context ⇒ Object
Returns the value of attribute context.
16 17 18 |
# File 'lib/boxcars/boxcar/sql_base.rb', line 16 def context @context end |
#read_only ⇒ Object
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_tables ⇒ Object
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.
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.
66 67 68 |
# File 'lib/boxcars/boxcar/sql_base.rb', line 66 def read_only? read_only end |