scripts/parrot_openrouter.py
scripts/parrot_openrouter.pyBrowse 4 files
1,839 tokens
7,794 bytes
Token encoding: o200k_base
Snapshot 24fd22b
← Back to SKILL.md
1"""2parrot_openrouter: same as the upstream `parrot` example but the LLM call goes3through OpenRouter (OpenAI SDK) instead of Anthropic native. Lets us run an4end-to-end evolution with whatever model the user already has paid access to.5 6Run with:7 uv --project darwinian_evolver run python parrot_openrouter.py \8 --num_iterations 3 --output_dir /tmp/parrot_out9 10Reads `OPENROUTER_API_KEY` from the environment.11"""12from __future__ import annotations13 14import argparse15import os16import sys17from pathlib import Path18 19import jinja220from openai import OpenAI21 22# Vendored problem types from upstream (AGPL — only run via subprocess in production)23from darwinian_evolver.cli_common import build_hyperparameter_config_from_args24from darwinian_evolver.cli_common import register_hyperparameter_args25from darwinian_evolver.cli_common import parse_learning_log_view_type26from darwinian_evolver.evolve_problem_loop import EvolveProblemLoop27from darwinian_evolver.learning_log import LearningLogEntry28from darwinian_evolver.problem import EvaluationFailureCase29from darwinian_evolver.problem import EvaluationResult30from darwinian_evolver.problem import Evaluator31from darwinian_evolver.problem import Mutator32from darwinian_evolver.problem import Organism33from darwinian_evolver.problem import Problem34 35DEFAULT_MODEL = os.environ.get("EVOLVER_MODEL", "openai/gpt-4o-mini")36 37 38def _client() -> OpenAI:39 key = os.environ.get("OPENROUTER_API_KEY")40 if not key:41 sys.exit("OPENROUTER_API_KEY is not set")42 return OpenAI(api_key=key, base_url="https://openrouter.ai/api/v1")43 44 45def _prompt_llm(prompt: str) -> str:46 try:47 r = _client().chat.completions.create(48 model=DEFAULT_MODEL,49 max_tokens=1024,50 messages=[{"role": "user", "content": prompt}],51 )52 return r.choices[0].message.content or ""53 except Exception as e:54 # Treat any provider error (rate limit, content filter, schema reject)55 # as a failed response. The evolver will simply see this as a low score56 # on this organism and move on — much friendlier than killing the run.57 return f"<LLM_ERROR: {type(e).__name__}: {e}>"58 59 60class ParrotOrganism(Organism):61 prompt_template: str62 63 def run(self, phrase: str) -> str:64 try:65 prompt = jinja2.Template(self.prompt_template).render(phrase=phrase)66 except jinja2.exceptions.TemplateError as e:67 return f"Error rendering prompt: {e}"68 if not prompt:69 return ""70 return _prompt_llm(prompt)71 72 73class ParrotEvaluationFailureCase(EvaluationFailureCase):74 phrase: str75 response: str76 77 78class ImproveParrotMutator(Mutator[ParrotOrganism, ParrotEvaluationFailureCase]):79 IMPROVEMENT_PROMPT_TEMPLATE = """80We want to build a prompt that causes an LLM to repeat back a given phrase verbatim.81 82The current prompt template is:83```84{{ organism.prompt_template }}85```86 87Unfortunately, on this phrase:88```89{{ failure_case.phrase }}90```91the LLM responded with:92```93{{ failure_case.response }}94```95 96Diagnose what went wrong, then propose an improved prompt template. Put the new97template in the LAST triple-backtick block of your response.98""".strip()99 100 def mutate(101 self,102 organism: ParrotOrganism,103 failure_cases: list[ParrotEvaluationFailureCase],104 learning_log_entries: list[LearningLogEntry],105 ) -> list[ParrotOrganism]:106 fc = failure_cases[0]107 prompt = jinja2.Template(self.IMPROVEMENT_PROMPT_TEMPLATE).render(108 organism=organism, failure_case=fc109 )110 try:111 resp = _prompt_llm(prompt)112 parts = resp.split("```")113 if len(parts) < 3:114 return []115 new_tpl = parts[-2].strip()116 return [ParrotOrganism(prompt_template=new_tpl)]117 except Exception as e:118 print(f"mutate error: {e}", file=sys.stderr)119 return []120 121 122class ParrotEvaluator(Evaluator[ParrotOrganism, EvaluationResult, ParrotEvaluationFailureCase]):123 TRAINABLE_PHRASES = [124 "Hello world.",125 "bla",126 "Bla",127 "bla.",128 '"bla bla".',129 "Just say 'foo' once with no extra words.",130 ]131 HOLDOUT_PHRASES = [132 "bla, but only once.",133 "'bla'",134 ]135 136 def evaluate(self, organism: ParrotOrganism) -> EvaluationResult:137 train_fails: list[ParrotEvaluationFailureCase] = []138 hold_fails: list[ParrotEvaluationFailureCase] = []139 for i, p in enumerate(self.TRAINABLE_PHRASES):140 r = organism.run(p)141 if r != p:142 train_fails.append(ParrotEvaluationFailureCase(143 phrase=p, response=r, data_point_id=f"trainable_{i}"))144 for i, p in enumerate(self.HOLDOUT_PHRASES):145 r = organism.run(p)146 if r != p:147 hold_fails.append(ParrotEvaluationFailureCase(148 phrase=p, response=r, data_point_id=f"holdout_{i}"))149 n_total = len(self.TRAINABLE_PHRASES) + len(self.HOLDOUT_PHRASES)150 n_ok = n_total - len(train_fails) - len(hold_fails)151 return EvaluationResult(152 score=n_ok / n_total,153 trainable_failure_cases=train_fails,154 holdout_failure_cases=hold_fails,155 # Always viable. Even a 0-score seed is a valid starting point; the156 # mutator should still get a chance to fix it.157 is_viable=True,158 )159 160 161def make_problem() -> Problem:162 return Problem[ParrotOrganism, EvaluationResult, ParrotEvaluationFailureCase](163 evaluator=ParrotEvaluator(),164 mutators=[ImproveParrotMutator()],165 initial_organism=ParrotOrganism(prompt_template="Say {{ phrase }}"),166 )167 168 169def main() -> int:170 ap = argparse.ArgumentParser()171 register_hyperparameter_args(ap.add_argument_group("hyperparameters"))172 ap.add_argument("--num_iterations", type=int, default=3)173 ap.add_argument("--mutator_concurrency", type=int, default=4)174 ap.add_argument("--evaluator_concurrency", type=int, default=4)175 ap.add_argument("--output_dir", type=str, required=True)176 args = ap.parse_args()177 178 out = Path(args.output_dir)179 out.mkdir(parents=True, exist_ok=True)180 181 hp = build_hyperparameter_config_from_args(args)182 loop = EvolveProblemLoop(183 problem=make_problem(),184 learning_log_view_type=parse_learning_log_view_type(hp.learning_log_view_type),185 num_parents_per_iteration=hp.num_parents_per_iteration,186 mutator_concurrency=args.mutator_concurrency,187 evaluator_concurrency=args.evaluator_concurrency,188 fixed_midpoint_score=hp.fixed_midpoint_score,189 midpoint_score_percentile=hp.midpoint_score_percentile,190 sharpness=hp.sharpness,191 novelty_weight=hp.novelty_weight,192 batch_size=hp.batch_size,193 should_verify_mutations=hp.verify_mutations,194 )195 196 import json197 log_path = out / "results.jsonl"198 snap_dir = out / "snapshots"199 snap_dir.mkdir(exist_ok=True)200 print("Evaluating initial organism...")201 for snap in loop.run(num_iterations=args.num_iterations):202 (snap_dir / f"iteration_{snap.iteration}.pkl").write_bytes(snap.snapshot)203 _, best_eval = snap.best_organism_result204 print(f"iter={snap.iteration} pop={snap.population_size} "205 f"best_score={best_eval.score:.3f}")206 with log_path.open("a") as f:207 f.write(json.dumps({208 "iteration": snap.iteration,209 "best_score": best_eval.score,210 "pop_size": snap.population_size,211 "score_percentiles": {str(k): v for k, v in snap.score_percentiles.items()},212 }) + "\n")213 print(f"\nDone. Results in: {out}")214 return 0215 216 217if __name__ == "__main__":218 sys.exit(main())219 Referenced from SKILL.md
SKILL.mdView in source ↗
Source excerpt starting at line 92.SKILL.mdView in source ↗92The skill ships `scripts/parrot_openrouter.py` — same parrot problem, but the93LLM call goes through OpenRouter so any provider works.
Source excerpt starting at line 142.142into `EvolveProblemLoop` and iterates over `loop.run(num_iterations=N)` — the143shipped `scripts/parrot_openrouter.py` is the reference.