|
| 1 | +# best_of_n_improve.omc — recursive self-improvement via best-of-N selection |
| 2 | +# |
| 3 | +# For each task: |
| 4 | +# 1. Generate N candidate solutions via batch_llm_call |
| 5 | +# 2. Judge all candidates via llm_judge |
| 6 | +# 3. Keep the best, pass it as context for next round |
| 7 | +# 4. Repeat until score >= threshold or max_rounds reached |
| 8 | +# |
| 9 | +# This demonstrates the core self-improvement loop using: |
| 10 | +# batch_llm_call, llm_judge, eval_omc, json_extract |
| 11 | + |
| 12 | +fn extract_code(text) { |
| 13 | + if str_contains(text, "```") { |
| 14 | + h parts = str_split(text, "```") |
| 15 | + if arr_len(parts) >= 3 { |
| 16 | + h block = parts[1] |
| 17 | + if str_contains(block, "\n") { |
| 18 | + h lines = str_split(block, "\n") |
| 19 | + h rest = [] |
| 20 | + h i = 1 |
| 21 | + while i < arr_len(lines) { |
| 22 | + arr_push(rest, lines[i]) |
| 23 | + i = i + 1 |
| 24 | + } |
| 25 | + return str_trim(arr_join(rest, "\n")) |
| 26 | + } |
| 27 | + return str_trim(block) |
| 28 | + } |
| 29 | + } |
| 30 | + return str_trim(text) |
| 31 | +} |
| 32 | + |
| 33 | +fn get_best(scored) { |
| 34 | + if arr_len(scored) == 0 { return null } |
| 35 | + h best_score = -1 |
| 36 | + h best_idx = 0 |
| 37 | + h i = 0 |
| 38 | + while i < arr_len(scored) { |
| 39 | + h entry = scored[i] |
| 40 | + h s = 0 |
| 41 | + if dict_has(entry, "score") { s = entry["score"] } |
| 42 | + if s > best_score { |
| 43 | + best_score = s |
| 44 | + best_idx = i |
| 45 | + } |
| 46 | + i = i + 1 |
| 47 | + } |
| 48 | + return {idx: best_idx, score: best_score} |
| 49 | +} |
| 50 | + |
| 51 | +fn best_of_n_improve(task, n_candidates, criteria, max_rounds, threshold) { |
| 52 | + print(str_concat("=== Task: ", task, " ===")) |
| 53 | + print(str_concat("N candidates: ", to_str(n_candidates), " Max rounds: ", to_str(max_rounds))) |
| 54 | + print("") |
| 55 | + |
| 56 | + h sys = "You are an expert OMC programmer. OMC uses h for variables (h x = 5), fn for functions. Write complete runnable code. Put code in ```omc blocks." |
| 57 | + h best_code = "" |
| 58 | + h best_score = 0 |
| 59 | + |
| 60 | + h round = 0 |
| 61 | + while round < max_rounds { |
| 62 | + h prompt = str_concat("Write OMC code to: ", task) |
| 63 | + if best_code != "" { |
| 64 | + prompt = str_concat(prompt, |
| 65 | + "\n\nPrevious best attempt (score ", to_str(best_score), "/10):\n```omc\n", |
| 66 | + best_code, "\n```\nImprove it.") |
| 67 | + } |
| 68 | + |
| 69 | + print(str_concat("Round ", to_str(round + 1), ": generating ", to_str(n_candidates), " candidates...")) |
| 70 | + |
| 71 | + h prompts = arr_fill({prompt: prompt, system: sys}, n_candidates) |
| 72 | + h responses = batch_llm_call(prompts) |
| 73 | + |
| 74 | + h candidates = par_map(responses, fn(r) { return extract_code(r) }) |
| 75 | + |
| 76 | + print("Judging candidates...") |
| 77 | + h scores = llm_judge(candidates, criteria) |
| 78 | + |
| 79 | + h best = get_best(scores) |
| 80 | + if best == null { |
| 81 | + print("Judge failed to score, skipping round") |
| 82 | + round = round + 1 |
| 83 | + continue |
| 84 | + } |
| 85 | + |
| 86 | + h new_score = best["score"] |
| 87 | + h new_idx = best["idx"] |
| 88 | + h new_code = candidates[new_idx] |
| 89 | + |
| 90 | + print(str_concat("Best this round: score=", to_str(new_score), "/10")) |
| 91 | + print(str_concat("Code preview: ", str_slice(new_code, 0, 80))) |
| 92 | + |
| 93 | + if new_score > best_score { |
| 94 | + best_score = new_score |
| 95 | + best_code = new_code |
| 96 | + } |
| 97 | + |
| 98 | + if best_score >= threshold { |
| 99 | + print(str_concat("CONVERGED at score ", to_str(best_score), "/10")) |
| 100 | + return {code: best_code, score: best_score, rounds: round + 1} |
| 101 | + } |
| 102 | + |
| 103 | + round = round + 1 |
| 104 | + } |
| 105 | + |
| 106 | + print(str_concat("Max rounds reached. Final score: ", to_str(best_score), "/10")) |
| 107 | + return {code: best_code, score: best_score, rounds: max_rounds} |
| 108 | +} |
| 109 | + |
| 110 | +# ── Task 1: fibonacci ───────────────────────────────────────────────────────── |
| 111 | +h r1 = best_of_n_improve( |
| 112 | + "compute the Nth Fibonacci number recursively", |
| 113 | + 3, |
| 114 | + "correctness: handles fib(0)=0, fib(1)=1, fib(10)=55; code quality; efficiency", |
| 115 | + 3, |
| 116 | + 8 |
| 117 | +) |
| 118 | +print(str_concat("Final code (", to_str(str_len(r1["code"])), " chars):")) |
| 119 | +print(r1["code"]) |
| 120 | +print("") |
| 121 | + |
| 122 | +# ── Task 2: word count ──────────────────────────────────────────────────────── |
| 123 | +h r2 = best_of_n_improve( |
| 124 | + "count words in a string (split on spaces, return count)", |
| 125 | + 3, |
| 126 | + "correctness for edge cases: empty string, single word, multiple spaces; code clarity", |
| 127 | + 2, |
| 128 | + 8 |
| 129 | +) |
| 130 | +print(str_concat("Final code (", to_str(str_len(r2["code"])), " chars):")) |
| 131 | +print(r2["code"]) |
| 132 | +print("") |
| 133 | + |
| 134 | +print("=== best_of_n_improve complete ===") |
0 commit comments