Skip to content

rugbedbugg/ReAgent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ReAgent

Plans retrosynthetic routes for a target molecule and scores each candidate along six independent objectives (feasibility, precursor availability, cost, safety, sustainability, efficiency). Chemistry facts are computed deterministically with RDKit and cheminformatics data; LLM-backed specialist agents interpret those facts, weigh them, and emit a cited rationale for the selected route.

Single-step model and tree search come from AiZynthFinder. ReAgent adds the agentic evaluation, multi-objective aggregation, retrieval grounding, and adaptive layers on top.

Requirements

  • Python 3.10 or 3.11
  • Agent layer needs one of: local Ollama (--local), or ANTHROPIC_API_KEY (--assess). Planning, features, RAG, and evaluation run without either.

Setup

py -3.11 -m venv .venv
.venv\Scripts\activate
pip install -e .

# Pretrained single-step model + building-block stock
download_public_data data

Usage

# Plan routes (no scoring)
reagent plan "CC(=O)Oc1ccccc1C(=O)O"

# Print the deterministic feature vector per route
reagent plan "CC(=O)Oc1ccccc1C(=O)O" --show-features

plan flags

  • --permissive-stock N: treat any molecule with <= N heavy atoms as purchasable. Stand-in for a fuller catalogue; raises solve-rate and diversity (eval set: 9/10 to 10/10 at N=11). Heuristic, not a real catalogue, so treat extra hits as optimistic.
  • --iterations N: MCTS search budget (default 100). Larger budget finds routes the default misses (300 solves one more eval target); run time roughly linear in N. Gain is confined to borderline targets (100 to 500 changed nothing on the hard set), so budget is not a substitute for a better model or stock.
  • --local [MODEL]: score with a local Ollama model (offline, no API key). Default qwen2.5:3b-instruct; server from OLLAMA_HOST (default http://localhost:11434). A 3B instruct model follows the rubric reliably; smaller models are less consistent.
  • --hybrid: score objectives deterministically (rubrics applied numerically), LLM writes the rationale only. Removes small-model arithmetic drift over multi-step routes (hazard counts, cost sums) while keeping the prose.
  • --ghs: score safety from real GHS H-codes fetched from PubChem instead of the offline Brenk screen. Cached to data/ghs_cache.json; missing record or offline falls back to Brenk. Score is the worst hazard tier among reagents and intermediates, excluding the target (common to every route), so routes are compared on reagents that differ.
  • --assess: score with the Anthropic API (ANTHROPIC_API_KEY from env or .env).
  • --rag: ground each disconnection in retrieved USPTO precedent (most similar templates by RDKit reaction fingerprint), feed corpus occurrence into the feasibility agent, cite as evidence. Index built once from data/uspto_templates.csv.gz, cached to data/rag_index.npz. Works alone (prints precedents, no scoring) or with --assess / --local.
reagent plan "CC(C)Cc1ccc(C(C)C(=O)O)cc1" --permissive-stock 11 --iterations 300
reagent plan "CC(=O)Oc1ccccc1C(=O)O" --local --hybrid --ghs
reagent plan "CC(=O)Oc1ccccc1C(=O)O" --local --rag

Scoring runs also:

  • print each route's confidence (weakest step's model probability) and flag a recommended route the base model distrusts, rather than presenting it as trustworthy;
  • print a rationale (why the top route won, why others were passed over, with cited precedent);
  • log the run to episodic memory (data/episodes.jsonl).

feedback

reagent feedback "CC(=O)Oc1ccccc1C(=O)O" --prefer 2

Shifts objective weights toward those that distinguish your preferred route (saved to data/weights.json), applied by later runs, which also recall similar past targets.

Evaluation

reagent evaluate --max-targets 10

Plans a fixed set of drug-like targets and compares two selection strategies over the same candidate routes: a feasibility-only baseline vs. ReAgent's weighted multi-objective selection. Reports solve-rate and mean safety, sustainability, and cost under two weight profiles. Scoring is deterministic (rubrics applied numerically), so the measurement isolates selection strategy, not LLM variance.

Results on the bundled 10-target set:

  • Solve-rate 0.90 for both strategies (selection does not change solvability).
  • Feasibility-led default weights: pick changes on 1/10, mean safety 0.43 to 0.49.
  • Safety-tilted weights: pick changes on 4/10, mean safety 0.43 to 0.63 at the same solve-rate.
  • The multi-objective layer earns its value when objectives beyond feasibility are weighted, which is what the feedback loop tunes. (Figures use the offline Brenk screen; exact values shift with the objective data sources chosen.)

Objective signals:

  • feasibility: expansion model prior (policy_probability) x filter model forward-plausibility (filter_feasibility), so a confidently-suggested route is still penalised if a step is judged implausible.
  • cost: RDKit synthetic-accessibility of the building blocks (proxy, no live prices).
  • safety: Brenk structural-alert screen (medchem liabilities: reactive, toxic, unstable groups). A real published signal, not a GHS reagent-hazard classification; read as "structural alerts", not formal safety. --ghs substitutes real GHS data.
  • sustainability: atom-economy / mass-intensity proxies (no solvent data).
reagent check-agents --local --max-targets 5

Scores real routes with the agent team and reports per-objective mean absolute error, ranking agreement, and parse-failure rate vs. the deterministic reference. On a 3B model the agents track the reference closely on short routes; residual error concentrates on arithmetic-heavy objectives (cost; safety on multi-step routes). --hybrid removes that drift.

Package layout

Module Responsibility Status
reagent/core RDKit helpers, data models (Route, Reaction, Assessment), config implemented
reagent/singlestep Single-step retrosynthesis backend adapter (AiZynthFinder) implemented
reagent/features Deterministic chemistry facts (incl. Brenk alerts, SA score) feeding the agents implemented
reagent/agents Orchestrator, specialist evaluators, rationale, LLM adapters (Anthropic, Ollama) implemented
reagent/optimize Weighted-sum and Pareto route aggregation implemented
reagent/rag Reaction-precedent fingerprints, index, retrieval implemented
reagent/adaptive Episodic memory and feedback-driven weight tuning implemented
reagent/eval Solve-rate, deterministic scoring, baseline-vs-ReAgent harness implemented
reagent/search Alternative search backend adapter placeholder

Development hardware and its consequences

Developed, run, and benchmarked entirely on modest consumer hardware without a GPU and with limited memory. No model was trained here: the single-step model is AiZynthFinder's pretrained USPTO model reused unchanged, and the local agent model (qwen2.5:3b-instruct) is downloaded, not trained. That environment shaped the system and every number here:

  • Small local agent model. Defaults to 3B because larger models do not fit alongside the planner and stock in memory. Larger local models not benchmarked.
  • Memory-bound pipeline. The full stack (17M-molecule stock, ONNX planner, local model) exhausts memory and swaps, so scored runs are slow. --iterations is left at the low default for this reason.
  • Heuristics stand in for missing data. --permissive-stock approximates a catalogue; cost and greenness are proxies; default safety is Brenk. Only --ghs uses real hazard data, and only online.
  • Benchmark numbers reflect these choices. Measured with this model, stock, and budget on a small drug-like target set with short-to-moderate routes. They characterize this configuration, not an upper bound with a better model, a real catalogue, or a GPU.

Ceiling on route quality is the pretrained single-step model and stock, neither improvable in this environment. ReAgent is the evaluation, selection, grounding, and adaptation layer on top.

Known limitations

  • Route generation is the ceiling. Reuses AiZynthFinder's pretrained model; does not improve generation. Better routes need proprietary reaction data (e.g. Reaxys) and/or GPU training. Two no-training levers had no effect (ringbreaker policy; raising the filter cutoff to prune during search); search budget helps only borderline targets, then plateaus.
  • Greenness is atom economy only. No solvent-driven PMI or E-factor without reaction-condition data.
  • Cost is a proxy from synthetic accessibility, not supplier prices.
  • Default safety is a structural-alert screen, not reagent safety. Real GHS data only with --ghs (online, PubChem); its score is the worst hazard among reagents, blunt when routes share a reagent.
  • Small-model rationale can misstate values even when the score is correct. Hybrid mode keeps scores exact.
  • Memory-bound and slow. A rationale-batching speedup and a lighter (bloom-filter) stock were identified but not implemented.
  • Narrow validation. Small drug-like target set, short-to-moderate routes.
  • Multi-objective advantage is conditional on objectives beyond feasibility carrying weight (what the feedback loop tunes).
  • Anthropic backend unverified. Implemented but exercised only via the local model.
  • --permissive-stock is a heuristic, not a real catalogue.
  • reagent/search is a placeholder.

About

An AI retrosynthetic agentic framework with evidence-grounded multi-objective scoring and forward-validating filter-model checks.

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Contributors

Languages