Verification, decomposition and search¶
Seven ways to spend more model calls on one question. Like every algorithm, each is a fold of the run's log: parallel calls may finish in any order without changing the result, a recorded run replays to the same outcome, and every one is bounded at construction (max_calls tells you the most model calls a run can make) and by the run's Budget. Text from the task, drafts and candidates is shown to the model as quoted data (<draft>...</draft>), never as instructions, and the closing tag cannot be forged from inside it.
"""Seven ways to get a better answer from the same model: self-critique, fact-check questions, best-of-N, decomposition, self-ask,
a graph of thoughts and tree search. Scripted model, offline."""
import asyncio
import json
from aaa.algos import (
LATS,
BestOfN,
ChainOfVerification,
GraphOfThoughts,
LeastToMost,
Op,
SelfAsk,
SelfRefine,
)
from aaa.llm import FakeLLMClient, LLMHandler
from aaa.llm.fake import text
from aaa.runtime import Engine
def system_of(request) -> str:
return next((m.content or "" for m in request.messages if m.role == "system"), "")
def user_of(request) -> str:
return next((m.content or "" for m in reversed(request.messages) if m.role == "user"), "")
def between(source: str, tag: str) -> str:
return source.split(f"<{tag}>")[1].split(f"</{tag}>")[0].strip()
async def run(algorithm, model, task):
async with Engine() as engine:
LLMHandler(FakeLLMClient(model)).attach(engine)
return (await engine.run(algorithm, {"task": task})).value
async def self_refine() -> None:
def model(request):
if "reviewing your own draft" in system_of(request):
ok = "units" in between(user_of(request), "draft")
return text(json.dumps({"ok": ok, "feedback": "say the unit"}))
if "Revise the draft" in system_of(request):
return text("42 km (units added)")
return text("42")
v = await run(SelfRefine(), model, "How far?")
print("self-refine:", v["answer"], "after", v["rounds"], "rewrite;", v["stopped"])
async def chain_of_verification() -> None:
def model(request):
s = system_of(request)
if "self-contained questions" in s:
return text(json.dumps({"questions": ["What is the capital of Australia?"]}))
if "briefly and factually" in s:
return text("Canberra")
if "Revise the draft so it agrees" in s:
return text("The capital of Australia is Canberra.")
return text("The capital of Australia is Sydney.")
v = await run(ChainOfVerification(), model, "Capital of Australia?")
print("verification:", v["answer"], "| draft was:", v["draft"])
async def best_of_n() -> None:
drafts = iter(["short", "a longer answer", "the longest answer of all", "mid answer"])
v = await run(
BestOfN(4, score=lambda task, answer: len(answer)), lambda r: text(next(drafts)), "Explain."
)
print("best-of-n:", v["answer"], "| scores", sorted(v["scores"]))
async def least_to_most() -> None:
def model(request):
if "Break the question" in system_of(request):
return text(json.dumps({"subquestions": ["Legs per dog?", "How many dogs?"]}))
question = between(user_of(request), "question")
return text({"Legs per dog?": "4", "How many dogs?": "3"}.get(question, "12"))
v = await run(LeastToMost(), model, "Legs of 3 dogs?")
print("least-to-most:", v["answer"], "via", [s["question"] for s in v["steps"][:-1]])
async def self_ask() -> None:
def model(request):
if "briefly and factually" in system_of(request):
return text("1969")
if "Q: " in user_of(request):
return text("So the final answer is: 1969.")
return text("Follow up: When did Apollo 11 land?")
v = await run(SelfAsk(), model, "When did people reach the moon?")
print("self-ask:", v["answer"], "after", len(v["followups"]), "follow-up")
async def graph_of_thoughts() -> None:
pool = iter("abcd")
def model(request):
s = system_of(request)
if "Rate how well the thought" in s:
return text(json.dumps({"score": len(between(user_of(request), "thought").split("+"))}))
if "Combine the thoughts" in s:
lines = between(user_of(request), "thoughts").splitlines()
return text("+".join(sorted(ln.split("] ")[1] for ln in lines)))
return text(next(pool))
graph = [
Op("gen", "generate", n=4),
Op("s1", "score", ("gen",)),
Op("top", "keep_best", ("s1",), n=2),
Op("merge", "aggregate", ("top",)),
]
algorithm = GraphOfThoughts(graph)
v = await run(algorithm, model, "Combine.")
print(
"graph-of-thoughts:",
v["answer"],
"from",
v["thoughts"],
"thoughts;",
algorithm.max_calls,
"calls at most",
)
async def lats() -> None:
def model(request):
s = system_of(request)
if "Rate how likely" in s:
last = between(user_of(request), "reasoning").splitlines()[-1]
return text(json.dumps({"score": 10 if last.startswith("FINAL") else 5}))
so_far = between(user_of(request), "reasoning_so_far")
return text("FINAL: 24" if "step" in so_far else "step 1: 8 / (3 - 8 / 3)")
v = await run(LATS(iterations=4, branching=2), model, "Make 24 from 3 3 8 8.")
print("lats:", v["answer"], "| path", v["path"], "| solved", v["solved"])
async def main() -> None:
for demo in (
self_refine,
chain_of_verification,
best_of_n,
least_to_most,
self_ask,
graph_of_thoughts,
lats,
):
await demo()
asyncio.run(main())
self-refine: 42 km (units added) after 1 rewrite; accepted
verification: The capital of Australia is Canberra. | draft was: The capital of Australia is Sydney.
best-of-n: the longest answer of all | scores [5.0, 10.0, 15.0, 25.0]
least-to-most: 12 via ['Legs per dog?', 'How many dogs?']
self-ask: 1969. after 1 follow-up
graph-of-thoughts: a+b from 11 thoughts; 9 calls at most
lats: 24 | path ['step 1: 8 / (3 - 8 / 3)', 'FINAL: 24'] | solved True
Checking and improving a draft¶
SelfRefine(max_rounds=3): one model drafts, critiques its own draft ({"ok", "feedback"}), and rewrites, until the critique accepts, a rewrite changes nothing, or max_rounds rewrites are made. It is cheaper than GeneratorCritic (no second model) and weaker for the same reason: the critic shares the writer's blind spots. Result {"answer", "rounds", "stopped"} with stopped one of accepted, unchanged, max_rounds, unchecked (a critique call failed; the draft is returned as it is). Up to 2 * max_rounds + 2 calls.
ChainOfVerification(max_questions=5): draft, plan up to max_questions fact-checking questions, answer each without seeing the draft (in parallel), then revise the draft against those answers. Answering blind is the point: a claim the draft got wrong is not repeated by the check. Result {"answer", "draft", "checks": [{"question", "answer"}], "revised"}. If no questions come back the draft is returned unchanged.
BestOfN(n, score=None, verifier_tool=None, threshold=None): n samples in parallel, each scored, the best returned. The verifier is one of
score=fn(task, candidate) -> float: pure and free of I/O, because it runs inside the algorithm (no extra model calls);verifier_tool="name": a tool called with{"task", "candidate"}that returns a number or{"score": number}, so approval, timeouts and guards apply as for any tool;- neither: a separate model call rates each candidate 0 to 10 (
judge_model=).
With threshold, the lowest-numbered candidate reaching it is returned as soon as every earlier candidate has been scored, so the answer never depends on which call finished first. A candidate whose scoring fails is passed over; if none can be scored the run fails. Result {"answer", "score", "scores", "selected", "n", "early_stop"}. SelfConsistency remains the voting algorithm for exact answers.
Breaking a question down¶
LeastToMost(max_subquestions=6): the model splits the question into at most max_subquestions easier sub-questions, which are answered in order (each seeing the earlier answers); the last step is the original question. An unusable split falls back to answering the question directly. Result {"answer", "steps": [{"question", "answer"}]}. Much cheaper than PlanAndExecute: no executor runs and no plan object exists.
SelfAsk(max_followups=4, lookup_tool=None): the model answers or writes Follow up: <question>; the follow-up is answered by a separate call or, with lookup_tool, by that tool called with {"query": question} (a search tool is the natural fit). After max_followups follow-ups the model must answer. A reply in neither form is taken as the final answer. Result {"answer", "followups", "turns"}.
Searching over thoughts¶
GraphOfThoughts(ops=None): a tree of thoughts can only branch; a graph can also merge and refine. You give a list of Op(id, kind, inputs, n, instruction):
| kind | does |
|---|---|
generate |
writes n new thoughts (from the task, or building on its inputs) |
aggregate |
merges all input thoughts into one |
refine |
improves each input thought (a failed refinement keeps the original) |
score |
rates each input thought 0 to 10 |
keep_best |
keeps the n highest scored (code, no model call) |
Operations run in the listed order, calls inside one operation in parallel. The graph is validated at construction (unique ids, inputs defined earlier, at most 16 operations, 30 thoughts per operation, 200 calls) and max_calls follows from its structure. The default graph is generate 4, score, keep 2, merge, refine, score, keep 1. Result {"answer", "score", "thoughts", "operations"}.
LATS(iterations=6, branching=3, depth_limit=5): Monte Carlo tree search over reasoning steps. Each iteration picks the most promising node that can still be expanded (UCT with exploration), writes branching next steps from it, scores them, and backs the scores up. A step starting with FINAL: ends a path; a finished path scoring below reflect_below gets a one-line lesson that later expansions see. It stops when a finished path reaches solved_score, when iterations run out, or when nothing can be expanded. Identical sibling steps count once. Result {"answer", "score", "path", "solved", "iterations", "explored", "reflections"}; with no finished path the outcome is NO_SOLUTION carrying the best partial path. At most 3 * iterations * branching calls.
Choosing¶
- You can write the check as code or a tool:
BestOfNwithscore=/verifier_tool=, orGenerateTestRepair. - You cannot, and the failure is a wrong fact:
ChainOfVerification. - A single answer that is probably fine but could be polished:
SelfRefine. - Multi-step questions where later steps depend on earlier answers:
LeastToMost, orSelfAskif the model should decide what to look up. - Problems where partial solutions can be scored and combined:
GraphOfThoughts; where they need exploring with backtracking:LATS(or the cheaperTreeOfThoughts).
None of these makes a model correct; they trade calls for fewer mistakes, and a model judge or critic is still a model.