Skip to content

< all problems115 · Level 05, Agents

Vote on the Answer, Not the Reasoning

medium · implement · LLM Fundamentals

Self-consistency samples several chain-of-thought paths for one question and keeps the answer most of them reach. The paths differ, so the vote is over each path's final answer, read out of free text first. Implement final_answer(text), which reads one path's answer, and vote(texts), which counts them.

  1. A number is an optional - right before the first digit, digits that may be grouped in threes by commas (1,200 is one number, 2,3,5 is three), and an optional decimal part (3.5). A full stop with no digit after it ends a sentence and is not part of the number. What surrounds the number, such as $, ** or apples, is ignored.
  2. An answer statement is answer is or answer:, in any case. When the text has one, the answer is the first number after the last statement.
  3. Otherwise, or when no number follows the last statement, the answer is the last number in the text. A text with no number at all has no answer: final_answer returns None.
  4. Write the answer one way, as a string: no commas, no trailing zeros after the decimal point, and no point when nothing is left after it. $1,200.00, 1200.0 and 1200 all come back as "1200", 0.50 as "0.5", and 100 stays "100".
  5. vote(texts) returns (answer, count): the final answer the most texts share and how many share it. Texts with no answer are left out. On a tie, the answer that appears first in texts wins. With no answers at all it returns (None, 0).

The catch. The last number in a path is often working, not the answer. "The answer is 16 apples, 2 fewer than the 18 she picked." ends on 18. A path that states its answer has said where to look; the last number decides only when no number follows a statement.