boxcars 0.3.2 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/boxcars/boxcar/url_text.rb +10 -1
- data/lib/boxcars/observation.rb +6 -1
- data/lib/boxcars/result.rb +1 -1
- data/lib/boxcars/train/xml_train.rb +4 -1
- data/lib/boxcars/train.rb +15 -5
- data/lib/boxcars/version.rb +1 -1
- data/lib/boxcars/x_node.rb +7 -3
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4c6fc81621333c6663dd04a49215f01a1fbd5fa247551b80b652676a4c06f155
|
4
|
+
data.tar.gz: a956727b4b986d42cc9f7b2b609dba561473f1976cce5f16ce2db641de778dfc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cac0c355836d68387a0fe66e7c65efe341c4ec746627eb275411198a0ba0092291e7add9cb8fbd93aee00da1d61420e36eaa1c90d97621629f3db5f56c0b134d
|
7
|
+
data.tar.gz: 799149ab51920e7d95d136ad5b4e57ac98c7e344ce277bcbd6e0ee8d89e8cc0d8eea23f37d821712818b16eacf8f0b236bb8c5730633250dc02c209b647f7b8f
|
data/Gemfile.lock
CHANGED
@@ -18,11 +18,20 @@ module Boxcars
|
|
18
18
|
# @return [String] The text for the url.
|
19
19
|
def run(url)
|
20
20
|
url = URI.parse(url)
|
21
|
-
get_answer(url)
|
21
|
+
do_encoding(get_answer(url))
|
22
22
|
end
|
23
23
|
|
24
24
|
private
|
25
25
|
|
26
|
+
def do_encoding(answer)
|
27
|
+
if answer.is_a?(Result)
|
28
|
+
answer.explanation = answer.explanation.encode(xml: :text)
|
29
|
+
answer
|
30
|
+
else
|
31
|
+
answer.encode(xml: :text)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
26
35
|
def html_to_text(url, response)
|
27
36
|
Nokogiri::HTML(response.body).css(%w[h1 h2 h3 h4 h5 h6 p a].join(",")).map do |e|
|
28
37
|
itxt = e.inner_text.strip
|
data/lib/boxcars/observation.rb
CHANGED
@@ -29,7 +29,12 @@ module Boxcars
|
|
29
29
|
|
30
30
|
# @return [String] An explanation of the result
|
31
31
|
def to_s
|
32
|
-
note
|
32
|
+
note.to_s
|
33
|
+
end
|
34
|
+
|
35
|
+
# @return [String] An explanation of the result
|
36
|
+
def to_text
|
37
|
+
to_s
|
33
38
|
end
|
34
39
|
|
35
40
|
# create a new Observaton from a text string with a status of :ok
|
data/lib/boxcars/result.rb
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
module Boxcars
|
4
4
|
# used by Boxcars to return structured result and additional context
|
5
5
|
class Result
|
6
|
-
|
6
|
+
attr_accessor :status, :answer, :explanation, :suggestions, :added_context
|
7
7
|
|
8
8
|
# @param status [Symbol] :ok or :error
|
9
9
|
# @param answer [String] The answer to the question
|
@@ -13,6 +13,7 @@ module Boxcars
|
|
13
13
|
# @param kwargs [Hash] Additional arguments including: name, description, top_k, return_direct, and stop
|
14
14
|
# @abstract
|
15
15
|
def initialize(boxcars:, prompt:, engine: nil, **kwargs)
|
16
|
+
@using_xml = true
|
16
17
|
super
|
17
18
|
end
|
18
19
|
|
@@ -41,7 +42,9 @@ module Boxcars
|
|
41
42
|
end
|
42
43
|
|
43
44
|
def build_output(text)
|
44
|
-
if text
|
45
|
+
if text.end_with?("</usetool>")
|
46
|
+
"<data>#{engine_prefix}#{text}</output></data>"
|
47
|
+
elsif text =~ /#{close_tag(thought_prefix)}/
|
45
48
|
"<data>#{engine_prefix}#{text}</data>"
|
46
49
|
else
|
47
50
|
"<data>#{text}</data>"
|
data/lib/boxcars/train.rb
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
module Boxcars
|
4
4
|
# @abstract
|
5
5
|
class Train < EngineBoxcar
|
6
|
-
attr_reader :boxcars, :return_values, :return_intermediate_steps,
|
6
|
+
attr_reader :boxcars, :return_values, :return_intermediate_steps, :using_xml,
|
7
7
|
:max_iterations, :early_stopping_method, :name_to_boxcar_map,
|
8
8
|
:observation_prefix, :thought_prefix, :final_answer_prefix, :answer_prefix, :question_prefix, :engine_prefix
|
9
9
|
|
@@ -184,6 +184,18 @@ module Boxcars
|
|
184
184
|
end
|
185
185
|
end
|
186
186
|
|
187
|
+
def get_boxcar_result(boxcar, boxcar_input)
|
188
|
+
boxcar_result = boxcar.run(boxcar_input)
|
189
|
+
return boxcar_result unless using_xml
|
190
|
+
|
191
|
+
if boxcar_result.is_a?(Result)
|
192
|
+
boxcar_result.answer = boxcar_result.answer.encode(xml: :text)
|
193
|
+
else
|
194
|
+
boxcar_result = boxcar_result.encode(xml: :text)
|
195
|
+
end
|
196
|
+
boxcar_result
|
197
|
+
end
|
198
|
+
|
187
199
|
# execute the train train
|
188
200
|
# @param inputs [Hash] The inputs.
|
189
201
|
# @return [Hash] The output.
|
@@ -197,7 +209,7 @@ module Boxcars
|
|
197
209
|
|
198
210
|
if (boxcar = name_to_boxcar_map[output.boxcar])
|
199
211
|
begin
|
200
|
-
observation = Observation.ok(boxcar
|
212
|
+
observation = Observation.ok(get_boxcar_result(boxcar, output.boxcar_input))
|
201
213
|
return_direct = boxcar.return_direct
|
202
214
|
rescue Boxcars::ConfigurationError, Boxcars::SecurityError => e
|
203
215
|
raise e
|
@@ -212,9 +224,7 @@ module Boxcars
|
|
212
224
|
observation = Observation.err("Error - #{output.boxcar} is not a valid action, try again.")
|
213
225
|
return_direct = false
|
214
226
|
end
|
215
|
-
#
|
216
|
-
Boxcars.debug "Observation: #{observation.to_s}", :green
|
217
|
-
# rubocop:enable Lint/RedundantStringCoercion
|
227
|
+
Boxcars.debug "Observation: #{observation}", :green
|
218
228
|
intermediate_steps.append([output, observation])
|
219
229
|
if return_direct
|
220
230
|
output = TrainFinish.new({ return_values[0] => observation }, "")
|
data/lib/boxcars/version.rb
CHANGED
data/lib/boxcars/x_node.rb
CHANGED
@@ -10,7 +10,6 @@ module Boxcars
|
|
10
10
|
@node = node
|
11
11
|
@valid_names = []
|
12
12
|
@children = {}
|
13
|
-
# @attributes = node.attributes.transform_values(&:value)
|
14
13
|
@attributes = node.attributes.values.to_h { |a| [a.name.to_sym, a.value] }
|
15
14
|
|
16
15
|
node.children.each do |child|
|
@@ -30,8 +29,13 @@ module Boxcars
|
|
30
29
|
|
31
30
|
def self.from_xml(xml)
|
32
31
|
doc = Nokogiri::XML.parse(xml)
|
33
|
-
|
34
|
-
|
32
|
+
if doc.errors.any?
|
33
|
+
Boxcars.debug("XML: #{xml}", :yellow)
|
34
|
+
# rubocop:disable Lint/Debugger
|
35
|
+
debugger if ENV.fetch("DEBUG_XML", false)
|
36
|
+
# rubocop:enable Lint/Debugger
|
37
|
+
raise XmlError, "XML is not valid: #{doc.errors.map { |e| "#{e.line}:#{e.column} #{e.message}" }}"
|
38
|
+
end
|
35
39
|
XNode.new(doc.root)
|
36
40
|
end
|
37
41
|
|