"""01 SELF-REFINE. Generate, critique your own output, rewrite. The loop you can build tonight. Paper: Madaan et al. 2023, arxiv.org/abs/2303.17651 (~20% average gain across 7 tasks). ES: Genera, critica tu propia salida, reescribe. El bucle que puedes construir esta noche. Run: python 01_self_refine.py "a cold email inviting a CTO to a 15-min demo of our AI tool" Needs: Claude Code installed (the `claude` CLI on your PATH). No API keys. """ import os, shutil, subprocess, sys MODEL = os.environ.get("METAPROMPT_MODEL", "haiku") ROUNDS = int(os.environ.get("ROUNDS", "2")) def llm(prompt: str) -> str: exe = shutil.which("claude") if not exe: sys.exit("claude CLI not found. Install Claude Code first: https://claude.com/claude-code") r = subprocess.run([exe, "-p", prompt, "--model", MODEL], capture_output=True, text=True, encoding="utf-8") return (r.stdout or "").strip() def main(): task = sys.argv[1] if len(sys.argv) > 1 else "a 3-sentence product pitch for a prompt-optimization tool" print(f"== TASK ==\n{task}\n") draft = llm(f"Write {task}. Output only the artifact, no preamble.") print(f"== DRAFT 0 ==\n{draft}\n") for i in range(1, ROUNDS + 1): # The metaprompting move: the model critiques ITS OWN output, specifically and actionably. feedback = llm( "You wrote the draft below. Give specific, actionable feedback: what is weak, " "what to cut, what to sharpen. Max 4 bullets, no praise.\n\n" f"TASK: {task}\n\nDRAFT:\n{draft}" ) print(f"== FEEDBACK {i} ==\n{feedback}\n") draft = llm( "Rewrite the draft applying every point of the feedback. " "Output only the rewritten artifact, no preamble.\n\n" f"TASK: {task}\n\nDRAFT:\n{draft}\n\nFEEDBACK:\n{feedback}" ) print(f"== DRAFT {i} ==\n{draft}\n") print("Done. The final draft absorbed its own critique. / El borrador final absorbio su propia critica.") if __name__ == "__main__": main()