Spaces:
Sleeping
Sleeping
README: full dev/ops doc
Browse files11 unmodified lines121314151617181920212223242526272829303132333435363711 unmodified lines# ChemGraph LoopA small, guarded API around the **real ChemGraph agent** β Argonne LeadershipComputing Facility's agentic computational-chemistry framework (LangGraph + ASE),running the workflow-correctness hardening by **Lu Zhang**.Give it a small molecule and a fast backend (EMT or TBLite / GFN2-xTB). The LLMagent resolves the molecule (PubChem), builds a 3D structure, runs the physicsengine for a single-point energy, and **audits its own result** against adeterministic workflow guard. The API returns the structured agentic trace so asite can show the loop running.Backs the live demo at **sciencesloop.com/agent**.## Endpoint```GET / β health + the molecule / calculator allow-listPOST /run { "molecule": "water", "calculator": "emt" | "tblite" }```Public-safe guards: molecule + calculator allow-list, single-point-energy only,per-call timeout, hourly rate limit. `OPENAI_API_KEY` is a Space secret.ChemGraph: https://github.com/argonne-lcf/ChemGraph Β· Apache-2.0.11 unmodified lines1213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710811 unmodified lines# ChemGraph LoopA guarded, **natural-language API around the real ChemGraph agent** β ArgonneLeadership Computing Facility's agentic computational-chemistry framework(LangGraph + ASE), running the workflow-correctness hardening by **Lu Zhang**.Backs the live demo at **sciencesloop.com/agent** (frontend =`lu-zhang-site/src/components/ChemGraphDemo.astro`).- **GitHub (source):** `github.com/rockyzl/chemgraph-loop` (private)- **Deploy target:** HF Space `rockyaaos/chemgraph-loop` (Docker, free CPU-basic)- **Upstream ChemGraph:** https://github.com/argonne-lcf/ChemGraph (Apache-2.0)## How it works (request lifecycle)1. **LLM intent router** (`llm_parse_query`, PRIMARY) β one `gpt-4o-mini` JSONcall reads the plain-language question and returns `{molecule, task,calculator}`, resolving the molecule by ANY name/synonym/formula("dihydrogen monoxide"/"H2O"/"NH3"/"CO2"/"EtOH"/"benzol" β canonical). Itrefuses to match derivatives to a parent (dimethoxybenzene β benzene) andemits `nearest`/`note` for out-of-scope molecules. `keyword_fallback`(token match, no regex) runs ONLY if the model call fails. This isintentionally agentic, not a rule engine.2. **Route by physics cost:**- `energy` / `dipole` = single-point (~12β25 s) β run the **real ChemGraphagent LIVE** on a canonical, guard-matched query built from the intent.- `ir` / `thermo` = finite-difference Hessian (too slow for free CPU) βserved from **precomputed real agent runs** in `precomputed/`(`cached:true`, shown as "precomputed real run").3. **Extract** (`cg_extract.extract`) β unified rich payload: molecule/formula/smiles, `structure.atoms` (parsed from molecule.xyz, drives the 3D viewer),`steps[]` (tool calls + folded detail), task-specific `result`(energy / dipole vector+magnitude / ir.modes / thermo H,S,G), `verified`.4. **Clarification node** β out-of-scope molecule or an unavailable heavy taskreturns `{clarify:true, message, suggestions:[{label,query}]}` (HTTP 200),NOT an error. The frontend renders it as a conversational bubble + chips.## Endpoints```GET / β health: molecules, tasks, live_tasks, ir_molecules, thermo_moleculesPOST /run { "query": "IR spectrum of water" } # natural-language (primary)POST /run { "molecule": "water", "calculator": "emt" } # legacy, energy only````/run` returns one of: a rich result payload Β· a `clarify` payload Β· an error(`429` rate limit Β· `504` timeout Β· `500` agent error).## Coverage & reliability (IMPORTANT)- **energy / dipole:** LIVE for all 10 allow-listed molecules (water, methane,ammonia, methanol, ethanol, carbon dioxide, benzene, acetic acid,formaldehyde, hydrogen peroxide).- **thermochemistry:** precomputed for **all 10** (thermo uses plain`Vibrations`; works for large molecules).- **IR / vibrations:** precomputed for **β€4-atom molecules only** β water,ammonia, carbon dioxide, formaldehyde, hydrogen peroxide. The ASE `Infrared`intensity step returns **empty modes for β₯5-atom molecules** (methane,methanol, ethanol, acetic acid, benzene all fail β confirmed). Do NOT addβ₯5-atom IR; the clarification node covers them.- Guard-matched queries only (single-point / dipole / ir / thermo). "geometryoptimization" phrasing makes the agent loop β never expose it.- Display-layer IR filter: keep modes with `cm1 β₯ 100` & `intensity > 1e-3`(drops spurious near-zero / translation-rotation modes).## Files| File | Role ||---|---|| `app.py` | FastAPI: intent router, routing, clarification node, guards (CORS, rate limit, timeout) || `cg_extract.py` | shared payload extractor (structure/steps/results) β used by app + precompute || `precompute.py` | offline heavy-task generator: `python precompute.py <molecule...>` || `precomputed/<slug>__<task>.json` | bundled real agent runs for ir/thermo || `Dockerfile` | CPU torch β `pip install .[calculators]` β copies wrappers LAST (fast layer-cached rebuilds) || `src/` | vendored copy of Lu's hardened ChemGraph fork |## Deploy / operateThe HF Space builds from its own git; push files with `huggingface_hub`(`~/.local` env at `/tmp/hfenv/bin/python` on Lu's box) β `upload_file` /`upload_folder` / `delete_file` each trigger a rebuild (~2β4 min, layer-cachedsince wrappers copy late). `OPENAI_API_KEY` is a Space **secret**.```pythonfrom huggingface_hub import HfApiHfApi().upload_file(path_or_fileobj="app.py", path_in_repo="app.py",repo_id="rockyaaos/chemgraph-loop", repo_type="space",commit_message="...")```**Add a spectrum/thermo molecule:** run `precompute.py <name>`, validate the IRmodes are non-empty & physical (`cm1β₯100`), `upload_file` the JSON, let the Spacerebuild (its `_avail(task)` picks it up automatically). Only β€4-atom moleculesget valid IR.CORS allows `sciencesloop.com`, `*.vercel.app`, and `localhost:4321`.
README.md+86-15