boxcars 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +25 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +4 -1
- data/lib/boxcars/boxcar/active_record.rb +64 -19
- data/lib/boxcars/boxcar/google_search.rb +1 -1
- data/lib/boxcars/engine/openai.rb +1 -1
- data/lib/boxcars/ruby_repl.rb +2 -2
- data/lib/boxcars/train.rb +1 -1
- data/lib/boxcars/version.rb +1 -1
- data/lib/boxcars.rb +10 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 666f9b91451e8b6db61eb1162cb6508a566a46b676d646f1fb0000175714cede
|
4
|
+
data.tar.gz: bee7b55be2288bf9361012bd562f88d91f64ce7972b0cf5c495a5fce87eea77c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '0681f5bd00bbfe2afd67011d76c561640a0575a9656b20913d3c60193208ea33361003ee652d01802b49702b106bd4f3d5bc91c1c7f2023bc2e8224edca14a8d'
|
7
|
+
data.tar.gz: 794fe20c2f9b8f14402db177bdee3d3e5318ab652e69c02929f9ae79d6daca096ae386a4e85730da4004927ab31aff53cc3a8d960ca661d6a5dd192432ffeabb
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,30 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## [Unreleased](https://github.com/BoxcarsAI/boxcars/tree/HEAD)
|
4
|
+
|
5
|
+
[Full Changelog](https://github.com/BoxcarsAI/boxcars/compare/v0.1.5...HEAD)
|
6
|
+
|
7
|
+
**Implemented enhancements:**
|
8
|
+
|
9
|
+
- Add a callback function for Boxcars::ActiveRecord to approve changes [\#24](https://github.com/BoxcarsAI/boxcars/issues/24)
|
10
|
+
|
11
|
+
**Merged pull requests:**
|
12
|
+
|
13
|
+
- Add approval callback function for Boxcars::ActiveRecord for changes to the data [\#25](https://github.com/BoxcarsAI/boxcars/pull/25) ([francis](https://github.com/francis))
|
14
|
+
- \[fix\] Fixed specs which required a key [\#23](https://github.com/BoxcarsAI/boxcars/pull/23) ([AKovtunov](https://github.com/AKovtunov))
|
15
|
+
|
16
|
+
## [v0.1.5](https://github.com/BoxcarsAI/boxcars/tree/v0.1.5) (2023-02-22)
|
17
|
+
|
18
|
+
[Full Changelog](https://github.com/BoxcarsAI/boxcars/compare/v0.1.4...v0.1.5)
|
19
|
+
|
20
|
+
**Implemented enhancements:**
|
21
|
+
|
22
|
+
- Make Boxcars::ActiveRecord read\_only by default [\#20](https://github.com/BoxcarsAI/boxcars/issues/20)
|
23
|
+
|
24
|
+
**Merged pull requests:**
|
25
|
+
|
26
|
+
- Active Record readonly [\#21](https://github.com/BoxcarsAI/boxcars/pull/21) ([francis](https://github.com/francis))
|
27
|
+
|
3
28
|
## [v0.1.4](https://github.com/BoxcarsAI/boxcars/tree/v0.1.4) (2023-02-22)
|
4
29
|
|
5
30
|
[Full Changelog](https://github.com/BoxcarsAI/boxcars/compare/v0.1.3...v0.1.4)
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
boxcars (0.1.
|
4
|
+
boxcars (0.1.6)
|
5
5
|
google_search_results (~> 2.2)
|
6
6
|
ruby-openai (~> 3.0)
|
7
7
|
|
@@ -56,6 +56,8 @@ GEM
|
|
56
56
|
faraday-http-cache (2.4.1)
|
57
57
|
faraday (>= 0.8)
|
58
58
|
faraday-net_http (3.0.2)
|
59
|
+
faraday-retry (2.0.0)
|
60
|
+
faraday (~> 2.0)
|
59
61
|
fiber-local (1.0.0)
|
60
62
|
github_changelog_generator (1.16.4)
|
61
63
|
activesupport
|
@@ -171,6 +173,7 @@ DEPENDENCIES
|
|
171
173
|
boxcars!
|
172
174
|
debug (~> 1.1)
|
173
175
|
dotenv (~> 2.8)
|
176
|
+
faraday-retry (~> 2.0)
|
174
177
|
github_changelog_generator (~> 1.16)
|
175
178
|
rake (~> 13.0)
|
176
179
|
rspec (~> 3.2)
|
@@ -7,18 +7,20 @@ module Boxcars
|
|
7
7
|
# the description of this engine boxcar
|
8
8
|
ARDESC = "useful for when you need to query a database for an application named %<name>s."
|
9
9
|
LOCKED_OUT_MODELS = %w[ActiveRecord::SchemaMigration ActiveRecord::InternalMetadata ApplicationRecord].freeze
|
10
|
-
attr_accessor :connection, :input_key, :requested_models, :read_only
|
10
|
+
attr_accessor :connection, :input_key, :requested_models, :read_only, :approval_callback
|
11
11
|
attr_reader :except_models
|
12
12
|
|
13
13
|
# @param engine [Boxcars::Engine] The engine to user for this boxcar. Can be inherited from a train if nil.
|
14
14
|
# @param models [Array<ActiveRecord::Model>] The models to use for this boxcar. Will use all if nil.
|
15
|
-
# @param read_only [Boolean] Whether to use read only models. Defaults to true.
|
15
|
+
# @param read_only [Boolean] Whether to use read only models. Defaults to true unless you pass an approval function.
|
16
|
+
# @param approval_callback [Proc] A function to call to approve changes. Defaults to nil.
|
16
17
|
# @param kwargs [Hash] Any other keyword arguments. These can include:
|
17
18
|
# :name, :description, :prompt, :input_key, :output_key and :except_models
|
18
|
-
def initialize(engine: nil, models: nil, read_only:
|
19
|
+
def initialize(engine: nil, models: nil, read_only: nil, approval_callback: nil, **kwargs)
|
19
20
|
check_models(models)
|
20
21
|
@except_models = LOCKED_OUT_MODELS + kwargs[:except_models].to_a
|
21
|
-
@
|
22
|
+
@approval_callback = approval_callback
|
23
|
+
@read_only = read_only.nil? ? !approval_callback : read_only
|
22
24
|
@input_key = kwargs[:input_key] || :question
|
23
25
|
@output_key = kwargs[:output_key] || :answer
|
24
26
|
the_prompt = kwargs[prompt] || my_prompt
|
@@ -85,28 +87,28 @@ module Boxcars
|
|
85
87
|
end
|
86
88
|
|
87
89
|
# to be safe, we wrap the code in a transaction and rollback
|
88
|
-
|
89
|
-
|
90
|
+
def rollback_after_running
|
91
|
+
rv = nil
|
90
92
|
::ActiveRecord::Base.transaction do
|
91
|
-
yield
|
93
|
+
rv = yield
|
92
94
|
ensure
|
93
95
|
raise ::ActiveRecord::Rollback
|
94
96
|
end
|
95
|
-
|
97
|
+
rv
|
96
98
|
end
|
97
|
-
# rubocop:enable Lint/SuppressedException
|
98
99
|
|
100
|
+
# check for dangerous code that is outside of ActiveRecord
|
99
101
|
def safe_to_run?(code)
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
102
|
+
bad_words = %w[commit drop_constraint drop_constraint! drop_extension drop_extension! drop_foreign_key drop_foreign_key! \
|
103
|
+
drop_index drop_index! drop_join_table drop_join_table! drop_materialized_view drop_materialized_view! \
|
104
|
+
drop_partition drop_partition! drop_schema drop_schema! drop_table drop_table! drop_trigger drop_trigger! \
|
105
|
+
drop_view drop_view! eval execute reset revoke rollback truncate].freeze
|
104
106
|
without_strings = code.gsub(/('([^'\\]*(\\.[^'\\]*)*)'|"([^"\\]*(\\.[^"\\]*)*"))/, 'XX')
|
105
107
|
word_list = without_strings.split(/[.,()]/)
|
106
108
|
|
107
109
|
bad_words.each do |w|
|
108
110
|
if word_list.include?(w)
|
109
|
-
puts "code included destructive instruction: #{w} #{code}"
|
111
|
+
puts "code included destructive instruction: #{w} #{code}".colorize(:red)
|
110
112
|
return false
|
111
113
|
end
|
112
114
|
end
|
@@ -114,22 +116,64 @@ module Boxcars
|
|
114
116
|
true
|
115
117
|
end
|
116
118
|
|
117
|
-
def
|
118
|
-
code
|
119
|
-
puts code.colorize(:yellow)
|
120
|
-
raise SecurityError, "Can not run code that makes changes in read-only mode" unless safe_to_run?(code)
|
119
|
+
def evaluate_input(code)
|
120
|
+
raise SecurityError, "Found unsafe code while evaluating: #{code}" unless safe_to_run?(code)
|
121
121
|
|
122
122
|
# rubocop:disable Security/Eval
|
123
|
-
|
123
|
+
eval code
|
124
124
|
# rubocop:enable Security/Eval
|
125
|
+
end
|
126
|
+
|
127
|
+
def change_count(changes_code)
|
128
|
+
return 0 unless changes_code
|
129
|
+
|
130
|
+
rollback_after_running do
|
131
|
+
puts "computing change count with: #{changes_code}".colorize(:yellow)
|
132
|
+
evaluate_input changes_code
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def approved?(changes_code, code)
|
137
|
+
# find out how many changes there are
|
138
|
+
changes = change_count(changes_code)
|
139
|
+
return true unless changes&.positive?
|
140
|
+
|
141
|
+
puts "Pending Changes: #{changes}".colorize(:yellow, style: :bold)
|
142
|
+
change_str = "#{changes} change#{'s' if changes.to_i > 1}"
|
143
|
+
raise SecurityError, "Can not run code that makes #{change_str} in read-only mode" if read_only?
|
144
|
+
|
145
|
+
return approval_callback.call(changes, code) if approval_callback.is_a?(Proc)
|
146
|
+
|
147
|
+
true
|
148
|
+
end
|
149
|
+
|
150
|
+
def run_active_record_code(code)
|
151
|
+
puts code.colorize(:yellow)
|
152
|
+
if read_only?
|
153
|
+
rollback_after_running do
|
154
|
+
evaluate_input code
|
155
|
+
end
|
156
|
+
else
|
157
|
+
evaluate_input code
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
def get_active_record_answer(text)
|
162
|
+
code = text[/^ARCode: (.*)/, 1]
|
163
|
+
changes_code = text[/^ARChanges: (.*)/, 1]
|
164
|
+
raise SecurityError, "Permission to run code that makes changes denied" unless approved?(changes_code, code)
|
165
|
+
|
166
|
+
output = run_active_record_code(code)
|
125
167
|
output = 0 if output.is_a?(Array) && output.empty?
|
126
168
|
output = output.first if output.is_a?(Array) && output.length == 1
|
169
|
+
output = output[output.keys.first] if output.is_a?(Hash) && output.length == 1
|
127
170
|
"Answer: #{output.inspect}"
|
128
171
|
rescue StandardError => e
|
129
172
|
"Error: #{e.message}"
|
130
173
|
end
|
131
174
|
|
132
175
|
def get_answer(text)
|
176
|
+
# debugger
|
133
177
|
case text
|
134
178
|
when /^ARCode:/
|
135
179
|
get_active_record_answer(text)
|
@@ -154,6 +198,7 @@ module Boxcars
|
|
154
198
|
Use the following format:
|
155
199
|
Question: "Question here"
|
156
200
|
ARCode: "Active Record code to run"
|
201
|
+
ARChanges: "Active Record code to compute the number of records going to change" - Only add this line if the ARCode on the line before will make data changes
|
157
202
|
Answer: "Final answer here"
|
158
203
|
|
159
204
|
Only use the following Active Record models:
|
@@ -13,7 +13,7 @@ module Boxcars
|
|
13
13
|
# @param name [String] The name of the boxcar. Defaults to classname.
|
14
14
|
# @param description [String] A description of the boxcar. Defaults to SERPDESC.
|
15
15
|
# @param serpapi_api_key [String] The API key to use for the SerpAPI. Defaults to Boxcars.configuration.serpapi_api_key.
|
16
|
-
def initialize(name: "Search", description: SERPDESC, serpapi_api_key:
|
16
|
+
def initialize(name: "Search", description: SERPDESC, serpapi_api_key: nil)
|
17
17
|
super(name: name, description: description)
|
18
18
|
api_key = Boxcars.configuration.serpapi_api_key(serpapi_api_key: serpapi_api_key)
|
19
19
|
::GoogleSearch.api_key = api_key
|
@@ -38,7 +38,7 @@ module Boxcars
|
|
38
38
|
# @param openai_access_token [String] The access token to use when asking the engine.
|
39
39
|
# Defaults to Boxcars.configuration.openai_access_token.
|
40
40
|
# @param kwargs [Hash] Additional parameters to pass to the engine if wanted.
|
41
|
-
def client(prompt:, openai_access_token:
|
41
|
+
def client(prompt:, openai_access_token: nil, **kwargs)
|
42
42
|
access_token = Boxcars.configuration.openai_access_token(openai_access_token: openai_access_token)
|
43
43
|
organization_id = Boxcars.configuration.organization_id
|
44
44
|
clnt = ::OpenAI::Client.new(access_token: access_token, organization_id: organization_id)
|
data/lib/boxcars/ruby_repl.rb
CHANGED
@@ -6,14 +6,14 @@ module Boxcars
|
|
6
6
|
# Execute ruby code
|
7
7
|
# @param code [String] The code to run
|
8
8
|
def call(code:)
|
9
|
-
puts "RubyREPL: #{code}".colorize(:
|
9
|
+
puts "RubyREPL: #{code}".colorize(:yellow)
|
10
10
|
output = ""
|
11
11
|
IO.popen("ruby", "r+") do |io|
|
12
12
|
io.puts code
|
13
13
|
io.close_write
|
14
14
|
output = io.read
|
15
15
|
end
|
16
|
-
puts "Answer: #{output}".colorize(:
|
16
|
+
puts "Answer: #{output}".colorize(:yellow, style: :bold)
|
17
17
|
output
|
18
18
|
end
|
19
19
|
|
data/lib/boxcars/train.rb
CHANGED
@@ -112,7 +112,7 @@ module Boxcars
|
|
112
112
|
# @param intermediate_steps [Array<Hash>] The intermediate steps.
|
113
113
|
# @return [Hash] The final output.
|
114
114
|
def pre_return(output, intermediate_steps)
|
115
|
-
puts output.log.colorize(:yellow)
|
115
|
+
puts output.log.colorize(:yellow, style: :bold)
|
116
116
|
final_output = output.return_values
|
117
117
|
final_output["intermediate_steps"] = intermediate_steps if return_intermediate_steps
|
118
118
|
final_output
|
data/lib/boxcars/version.rb
CHANGED
data/lib/boxcars.rb
CHANGED
@@ -68,7 +68,7 @@ module Boxcars
|
|
68
68
|
end
|
69
69
|
|
70
70
|
def key_lookup(key, kwargs)
|
71
|
-
rv = if kwargs.key?(key) && kwargs[key]
|
71
|
+
rv = if kwargs.key?(key) && !kwargs[key].nil?
|
72
72
|
# override with kwargs if present
|
73
73
|
kwargs[key]
|
74
74
|
elsif (set_val = instance_variable_get("@#{key}"))
|
@@ -77,7 +77,6 @@ module Boxcars
|
|
77
77
|
else
|
78
78
|
# otherwise, dig out of the environment
|
79
79
|
new_key = ENV.fetch(key.to_s.upcase, nil)
|
80
|
-
send("#{key}=", new_key) if new_key
|
81
80
|
new_key
|
82
81
|
end
|
83
82
|
check_key(key, rv)
|
@@ -107,6 +106,15 @@ module Boxcars
|
|
107
106
|
def self.engine
|
108
107
|
configuration.default_engine || Boxcars::Openai
|
109
108
|
end
|
109
|
+
|
110
|
+
# return a proc that will ask the user for input
|
111
|
+
def self.ask_user
|
112
|
+
proc do |changes, _code|
|
113
|
+
puts "This request will make #{changes} changes. Are you sure you want to run it? (y/[n])"
|
114
|
+
answer = gets.chomp
|
115
|
+
answer.downcase == 'y'
|
116
|
+
end
|
117
|
+
end
|
110
118
|
end
|
111
119
|
|
112
120
|
require "boxcars/version"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: boxcars
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Francis Sullivan
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2023-02-
|
12
|
+
date: 2023-02-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: debug
|