跳到主要內容

Darwinian Evolver

使用 Imbue 的進化循環來演化提示詞(prompts)、正則表達式、SQL 或代碼。

技能元數據

來源可選 — 使用 hermes skills install official/research/darwinian-evolver 安裝
路徑optional-skills/research/darwinian-evolver
版本0.1.0
作者Bihruze (Asahi0x), Hermes Agent
許可證MIT
平臺linux, macos
標籤evolution, optimization, prompt-engineering, research
相關技能arxiv, jupyter-live-kernel

參考:完整 SKILL.md

信息

以下是 Hermes 在觸發此技能時加載的完整技能定義。這是技能激活時代理(agent)看到的指令。

Darwinian Evolver

運行 Imbue 的 darwinian_evolver —— 一個 由大語言模型(LLM)驅動的進化搜索循環 —— 以針對適應度函數優化 提示詞、正則表達式、SQL 查詢 或小段代碼

狀態:上游工具的薄封裝層。該技能會安裝它,引導代理編寫 Problem 定義(生物體 + 評估器 + 變異器), 並通過上游 CLI 或小型自定義 Python 驅動程序驅動循環。

許可證: 上游工具採用 AGPL-3.0 許可證。本技能僅通過上游 CLI 或 subprocess/uv run 調用 來調用它( mere aggregation,單純聚合)。切勿將上游類導入 Hermes 本身。

何時使用

  • 用戶說“優化這個提示詞”、“為 X 演化一個正則表達式”、“自動改進這段代碼/SQL”、“搜索更好的指令”。
  • 你有一個評分器(精確匹配、正則通過率、單元測試、LLM 評判、運行時指標)AND 一個起始候選者(生物體)。如果你沒有評分器,請停止並先定義一個——那是最難的部分。
  • 成本可接受:典型運行需要 50–500 次 LLM 調用。在 gpt-4o-mini 上只需幾美分; 在 Claude Sonnet 上可能需要幾美元。

不要在以下情況使用:

  • 優化目標是可微分的(使用梯度下降 / DSPy)。
  • 你只需要嘗試 2–3 個變體——直接手動編寫它們。
  • 適應度信號純粹是主觀的,沒有任何可衡量的標準。

前置條件

  • Python ≥3.11
  • git, uv(或 pip
  • 以下之一:OPENROUTER_API_KEY, ANTHROPIC_API_KEY, 或 OPENAI_API_KEY

該技能附帶一個小型 parrot_openrouter.py 驅動程序,它通過 OpenAI SDK 使用 OPENROUTER_API_KEY, 因此 OpenRouter 上的任何模型均可使用。上游 CLI 本身硬編碼了 Anthropic 並需要 ANTHROPIC_API_KEY

安裝(一次性)

通過 terminal 工具運行:

mkdir -p ~/.hermes/cache/darwinian-evolver && cd ~/.hermes/cache/darwinian-evolver
[ -d darwinian_evolver ] || git clone --depth 1 https://github.com/imbue-ai/darwinian_evolver.git
cd darwinian_evolver && uv sync

驗證:

cd ~/.hermes/cache/darwinian-evolver/darwinian_evolver \
&& uv run darwinian_evolver --help | head -5

快速入門 — 內置 Parrot 示例

小型煙霧測試(需要 ANTHROPIC_API_KEY):

cd ~/.hermes/cache/darwinian-evolver/darwinian_evolver
uv run darwinian_evolver parrot \
--num_iterations 2 \
--num_parents_per_iteration 2 \
--mutator_concurrency 2 --evaluator_concurrency 2 \
--output_dir /tmp/parrot_demo

輸出:

  • /tmp/parrot_demo/snapshots/iteration_N.pkl — 每次迭代的序列化種群
  • /tmp/parrot_demo/<jsonl> — 每次迭代的 JSON 日誌(路徑在末尾打印)

在瀏覽器中打開 ~/.hermes/cache/darwinian-evolver/darwinian_evolver/darwinian_evolver/lineage_visualizer.html 並加載 JSON 日誌以查看進化樹。

快速入門 — OpenRouter 驅動程序(無需 Anthropic 密鑰)

該技能附帶 scripts/parrot_openrouter.py —— 相同的 parrot 問題,但 LLM 調用通過 OpenRouter 進行,因此任何提供商均可使用。

# From wherever the skill is installed:
SKILL_DIR=~/.hermes/skills/research/darwinian-evolver
DE_DIR=~/.hermes/cache/darwinian-evolver/darwinian_evolver

cd "$DE_DIR" && \
EVOLVER_MODEL='openai/gpt-4o-mini' \
uv run --with openai python "$SKILL_DIR/scripts/parrot_openrouter.py" \
--num_iterations 3 --num_parents_per_iteration 2 \
--output_dir /tmp/parrot_or

使用 scripts/show_snapshot.py 檢查結果:

uv run --with openai python "$SKILL_DIR/scripts/show_snapshot.py" \
/tmp/parrot_or/snapshots/iteration_3.pkl

預期輸出:7 個按分數排名的演化提示詞模板,最佳得分約為 0.6–0.8(種子 Say {{ phrase }} 得分為 0.000)。

定義自定義問題

該技能附帶 templates/custom_problem_template.py —— 複製、編輯、運行。 你必須定義三件事:

  1. Organism — 一個 Pydantic BaseModel 子類,持有正在演化的工件(prompt_template: str, regex_pattern: str, sql_query: str, code_block: str 等)。添加一個 run(*args) 方法來執行它。

  2. Evaluator.evaluate(organism) -> EvaluationResult(score=..., trainable_failure_cases=[...], holdout_failure_cases=[...], is_viable=True)

    • score 範圍在 [0, 1]。越高越好。
    • trainable_failure_cases — 變異器看到的內容。包含足夠的 上下文(輸入、預期、實際)以便 LLM 進行診斷。
    • holdout_failure_cases — 對變異器不可見。使用這些 來檢測過擬合。
    • is_viable=True 除非生物體完全損壞(拋出異常、 返回 None 等)。得分為 0 的生物體也是可行的——它只是在父代選擇中權重降低。
  3. Mutator.mutate(organism, failure_cases, learning_log_entries) -> list[Organism]。 通常:構建一個包含當前生物體 + 失敗案例 + 請求提出修復方案的 LLM 提示詞;解析 LLM 的響應;返回 一個新的 Organism。如果解析失敗則返回 [] —— 循環會處理這種情況。

然後編寫一個驅動腳本,將 Problem(initial_organism, evaluator, [mutators]) 接入 EvolveProblemLoop,並迭代執行 loop.run(num_iterations=N) —— 已提供的 scripts/parrot_openrouter.py 可作為參考。

真正重要的超參數

標誌默認值何時更改
--num_iterations5一旦你信任評估器,就增加到 10–20
--num_parents_per_iteration4為了低成本探索,降至 2
--mutator_concurrency10降至 2–4 以避免速率限制
--evaluator_concurrency10同上;評估器也會調用 LLM
--batch_size1一旦你的變異器能處理多個失敗案例,就提高到 3–5
--verify_mutationsoff一旦變異器變得浪費(根據 Imbue 的說法,後續運行每次可節省 >10× 成本),就開啟
--midpoint_scorep75除非分數聚集,否則保持不變
--sharpness10保持不變

陷阱

  1. 初始生物體必須可行 — 即使種子得分為 0,也要在你的 EvaluationResult 中設置 is_viable=True。循環會拒絕不可行的生物體,因為它們意味著循環沒有可進化的基礎。
  2. 提供商的內容過濾器會終止運行。 Azure 支持的 OpenRouter 模型會以 HTTP 400 拒絕諸如 "ignore previous instructions" 之類的短語。將 LLM 調用包裹在 try/except 中,並返回 f"<LLM_ERROR: {e}>" — 進化器只會將該生物體評分為 0 並繼續。
  3. loop.run() 是一個生成器 — 調用它並不會立即運行任何內容,直到你進行迭代。使用 for snap in loop.run(num_iterations=N):
  4. 快照是嵌套的 pickle 對象。 iteration_N.pkl 包含一個字典,其中有 population_snapshot(更多 pickle 字節)。要反 pickle,你必須確保 Organism 類在其被 pickle 時的相同點號路徑下可導入。
  5. 併發默認值過於激進。 10/10 會在大多數提供商處觸發速率限制。從 2/2 開始。
  6. CLI 硬編碼為 Anthropic。 uv run darwinian_evolver <problem> 會使用 ANTHROPIC_API_KEY 並調用 Claude Sonnet。要使用任何其他提供商,請編寫像 parrot_openrouter.py 這樣的驅動腳本。
  7. AGPL。 切勿在 Hermes 核心內部使用 from darwinian_evolver import ...。位於 ~/.hermes/skills/... 下的自定義驅動腳本屬於用戶側,是允許的。
  8. 無 PyPI 包。 pip install darwinian-evolver 會拉取錯誤的東西。始終從 GitHub 倉庫安裝。

驗證

安裝並運行一次 parrot 後,以下命令退出碼為 0 即足夠:

DE_DIR=~/.hermes/cache/darwinian-evolver/darwinian_evolver
ls "$DE_DIR/darwinian_evolver/lineage_visualizer.html" >/dev/null && \
cd "$DE_DIR" && uv run darwinian_evolver --help >/dev/null && \
echo "darwinian-evolver: OK"

參考資料