boxcars 0.3.2 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 463f57f1436cfea29e0a60bbfce071afd355a5d0cadbe1d69f96adc7393eacd6
4
- data.tar.gz: 282e528fb8cb8b532b621db5c2e7ce2b82c4607057c01f0feb1eaacfe98ba09b
3
+ metadata.gz: d42ec321ce559b15e1b9da6b7d0338f13ce1faca17a5c70dfc1b516eb51062f9
4
+ data.tar.gz: 2b8841ca63703ef49426bf004207e808267bc7a692a33188f45d34bdf02c54ea
5
5
  SHA512:
6
- metadata.gz: c76d2772db0925f71779c0bac1a2e92af20bcf398cc6e7cf943edeadb2014426554fba607b034fd68ac88bd26f2d42eef114de57332e83a4fe76558a9b2f0cea
7
- data.tar.gz: '0489fffef87c32fbf6a7a9af4fbca77ef991437353211174617b6ea8e229e49accc85c39af93414ce1628a4472cc208d8075570abba5e8269b30cfb66000a40e'
6
+ metadata.gz: e793cc40fd92d2ea1e249f3f613b4a4bd51954a794c02a201e171d6f38a84da95d851464feab60e11729813c80b90bf8091dfb4750fa4be0084439b94ee8d9a2
7
+ data.tar.gz: 147e84f7839647fe86ac26d3ed0c9be34a450540dc29d43bd86b6b2fac944718fecb6ba0be324048dd836999ae8f1f0ad83e6f1d50e5975f8510905b35ded432
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- boxcars (0.3.2)
4
+ boxcars (0.3.4)
5
5
  google_search_results (~> 2.2)
6
6
  gpt4all (~> 0.0.4)
7
7
  hnswlib (~> 0.8)
@@ -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
@@ -90,7 +90,7 @@ module Boxcars
90
90
  # @param inputs [Hash] The inputs to use for the prompt.
91
91
  # @return [Hash] The formatted prompt { messages: ...}
92
92
  def as_messages(inputs = nil)
93
- { messages: no_history.map { |ln| { role: ln.first, content: ln.last % inputs } } }
93
+ { messages: no_history.map { |ln| { role: ln.first, content: cformat(ln.last, inputs) } } }
94
94
  rescue ::KeyError => e
95
95
  first_line = e.message.to_s.split("\n").first
96
96
  Boxcars.error "Missing prompt input key: #{first_line}"
@@ -102,14 +102,20 @@ module Boxcars
102
102
  # @return [Hash] The formatted prompt { prompt: "..."}
103
103
  def as_prompt(inputs = nil)
104
104
  if show_roles
105
- no_history.map { |ln| format("#{ln.first}: #{ln.last}", inputs) }.compact.join("\n\n")
105
+ no_history.map { |ln| cformat("#{ln.first}: #{ln.last}", inputs) }.compact.join("\n\n")
106
106
  else
107
- no_history.map { |ln| format(ln.last, inputs) }.compact.join("\n\n")
107
+ no_history.map { |ln| cformat(ln.last, inputs) }.compact.join("\n\n")
108
108
  end
109
109
  rescue ::KeyError => e
110
110
  first_line = e.message.to_s.split("\n").first
111
111
  Boxcars.error "Missing prompt input key: #{first_line}"
112
112
  raise KeyError, "Prompt format error: #{first_line}"
113
113
  end
114
+
115
+ # special format that replaces lone percent signs with double percent signs
116
+ def cformat(*args)
117
+ args[0].gsub!(/%(?!<)/, '%%') if args.length > 1
118
+ format(*args)
119
+ end
114
120
  end
115
121
  end
@@ -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
@@ -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
- attr_reader :status, :answer, :explanation, :suggestions, :added_context
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 =~ /#{close_tag(thought_prefix)}/
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.run(output.boxcar_input))
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
- # rubocop:disable Lint/RedundantStringCoercion
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 }, "")
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Boxcars
4
4
  # The current version of the gem.
5
- VERSION = "0.3.2"
5
+ VERSION = "0.3.4"
6
6
  end
@@ -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
- raise XmlError, "XML is not valid: #{doc.errors.map { |e| "#{e.line}:#{e.column} #{e.message}" }}" if doc.errors.any?
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
 
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.3.2
4
+ version: 0.3.4
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-07-10 00:00:00.000000000 Z
12
+ date: 2023-07-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: google_search_results