lib/boxcars/boxcar/calculator.rb in boxcars-0.2.1 vs lib/boxcars/boxcar/calculator.rb in boxcars-0.2.2
- old
+ new
@@ -19,11 +19,12 @@
end
private
def get_embedded_ruby_answer(text)
- code = text[8..-4].split("```").first.strip
+ code = text.split("```ruby\n").last.split("```").first.strip
+ # code = text[8..-4].split("```").first.strip
ruby_executor = Boxcars::RubyREPL.new
ruby_executor.call(code: code)
end
def get_answer(text)
@@ -31,54 +32,42 @@
when /^```ruby/
get_embedded_ruby_answer(text)
when /^Answer:/
Result.from_text(text)
else
- Result.new(status: :error, explanation: "Unknown format from engine: #{text}")
+ Result.new(status: :error,
+ explanation: "Error: expecting your response to begin with '```ruby'. Try answering the question again.")
end
end
# our template
- # rubocop:disable Style/RedundantHeredocDelimiterQuotes
- TEMPLATE = <<~'IPT'
- You are GPT-3, and you can't do math.
- You can do basic math, and your memorization abilities are impressive, but you can't do any complex calculations that a human could not do in their head. You also have an annoying tendency to just make up highly specific, but wrong, answers.
- So we hooked you up to a Ruby 3 kernel, and now you can execute code written in the Ruby programming language. If anyone gives you a hard math problem, just use this format and we’ll take care of the rest:
+ CTEMPLATE = [
+ syst("You can do basic math, but for any hard calculations that a human could not do ",
+ "in their head, use the following approach instead. ",
+ "Return code written in the Ruby programming language that prints the results. ",
+ "If anyone gives you a hard math problem, just ",
+ "use the following format and we’ll take care of the rest:\n",
+ "${{Question with hard calculation.}}\n",
+ "reply only with the following format:\n",
+ "```ruby\n${{only Ruby code that prints the answer}}\n```\n",
+ "```output\n${{Output of your code}}\n```\n\n",
+ "Otherwise, you should use this simpler format:\n",
+ "${{Question without hard calculation}}\n",
+ "Answer: ${{Answer}}\n\n",
+ "Do not give an explanation of the answer and make sure your answer starts with either 'Answer:' or '```ruby'."),
+ syst("here is a hard example:\n", "the user asks: What is 37593 * 67?\n",
+ "your answer: ```ruby\nputs(37593 * 67)\n```\n```output\n2518731\n```\nAnswer: 2518731"),
+ syst("basic example:\n", "user asks: What is 2518731 + 0?\n", "you answer: Answer: 2518731"),
+ syst("Begin."),
+ user("%<question>s")
+ ].freeze
- Question: ${{Question with hard calculation.}}
- ```ruby
- ${{Code that prints what you need to know}}
- ```
- ```output
- ${{Output of your code}}
- ```
- Answer: ${{Answer}}
-
- Otherwise, use this simpler format:
-
- Question: ${{Question without hard calculation}}
- Answer: ${{Answer}}
-
- Begin.
-
- Question: What is 37593 * 67?
- ```ruby
- puts(37593 * 67)
- ```
- ```output
- 2518731
- ```
- Answer: 2518731
-
- Question: what is 2518731 + 0?
- Answer: 2518731
-
- Question: %<question>s
- IPT
- # rubocop:enable Style/RedundantHeredocDelimiterQuotes
-
# The prompt to use for the engine.
def my_prompt
- @my_prompt ||= Prompt.new(input_variables: [:question], output_variables: [:answer], template: TEMPLATE)
+ @conversation ||= Conversation.new(lines: CTEMPLATE)
+ @my_prompt ||= ConversationPrompt.new(
+ conversation: @conversation,
+ input_variables: [:question],
+ output_variables: [:answer])
end
end
end