How tsinghuaise/alchemy works
1. Overall split: two layers
- The repo is intentionally divided into ai_scientist/ and research_environment/.
- ai_scientist is the reasoning/iteration layer: it proposes hypotheses, writes code, selects hyperparameters, and learns from results.
- research_environment is the execution layer: it runs experiments, schedules GPUs, executes inside containers, and collects metrics.
2. Scientist-side control loop
- The entrypoint is python -m ai_scientist via ai_scientist/__main__.py and ai_scientist/main.py.
- The loop described in the README is: load seed baseline → generate hypothesis → generate code + hyperparameters → run evaluation → ingest feedback → iterate.
- This layer is where the LLM is used, so the architecture depends on prompt generation and response parsing more than on classical training code.
3. Scientist-side internal subsystems
- ai_scientist/agent/ contains the agent orchestration logic.
- Key pieces are agent.py for the main agent flow, context.py for state, evaluator.py for judging outputs, and response_parser.py for extracting structured results from model responses.
- ai_scientist/utils/llm_client.py is the external model boundary; it is the main dependency on an LLM provider.
4. Prompting and knowledge memory
- Prompt templates live in ai_scientist/agent/prompts/*.jinja and are assembled by ai_scientist/agent/prompt.py.
- The agent can consume domain knowledge from ai_scientist/domain_knowledge/**/knowledge.md.
- There is also a memory/learning path under ai_scientist/agent/memory/domain_knowledge/ and ai_scientist/tools/knowledge_learner.py, which suggests a feedback loop where experiment outcomes can be distilled into reusable task knowledge.
5. Seed baseline as the starting corpus
- ai_scientist/seed_baseline/ stores prior implementations for supported domains and tasks.
- Each seed baseline typically has algorithm.py, hyperparameter.yaml, and metadata files.
- This is the main source from which the agent derives new candidate algorithms rather than starting from scratch.
6. Research environment orchestration
- research_environment/orchestrator.py is the high-level runtime coordinator.
- The environment is plugin-driven: each task area has a plugin.py and task-local config.yaml.
- Task-specific logic is separated from the core scheduler/executor so new domains can be added without changing the scientist layer.
7. Execution boundary: containers
- The environment can execute through either Docker or Singularity (research_environment/executor/docker.py, singularity.py).
- Each task ships a container/ directory with a Dockerfile and environment definition, making runtime dependencies explicit and isolated.
- This is a strong architectural boundary: the scientist outputs code/config, but the environment owns reproducible execution.
8. Scheduling and resource management
- research_environment/scheduler/gpu_scheduler.py handles GPU allocation and concurrency control.
- research_environment/config.yaml controls executor choice, GPU IDs, and max_per_gpu.
- This means the system is built for batch/parallel experimentation rather than single-model execution.
9. Task collector and result flow
- Results are gathered by research_environment/collector/result_table.py and task-specific parsing logic in plugins.
- The README notes that each task plugin must implement expand_hp() and parse_output(), which is the interface between raw experiment output and the scientist’s feedback loop.
- So the data flow is: candidate code/config → container run → stdout/files/metrics → parser → structured results.
10. Task families and pipelines
- The repo currently includes two major published task families: Recsys/MMRec and TimeSeries.
- TimeSeries is further split into anomaly detection, classification, imputation, long-term forecasting, short-term forecasting.
- Each task family contains its own pipeline, configs, scripts, models, and dataset wiring, but follows the same orchestration pattern.
11. Task pipelines are self-contained experiment frameworks
- Under each research_environment/tasks/{Domain}/{Task}/pipeline/, you can see:
- dataset configs,
- model configs,
- experiment classes in exp/,
- data loading in data_provider/,
- model implementations in models/,
- shared utilities in utils/.
- For TimeSeries, the pipelines are broad enough to support many model families, including transformer variants, Mamba-like models, and task-specific baselines.
12. Script-to-config conversion layer
- Many task pipelines include large scripts/ trees of .sh launchers and sh2yaml/ converters.
- This indicates the project preserves an older shell-script experiment style but normalizes it into YAML configs for orchestration.
- Architecturally, this acts as an adapter layer between legacy benchmarking workflows and the newer managed environment.
13. External dependencies that matter
- The scientist layer depends on an external LLM endpoint configured in ai_scientist/config.yaml.
- The environment layer depends on local GPU resources and container runtime support.
- The repository is therefore a split-brain system: cloud/remote inference for idea generation, local/HPC-style infrastructure for experiment execution.
14. Key diagram boundaries to show
- A useful architecture diagram should draw a hard boundary between:
- LLM-driven agent loop on the left,
- task execution platform on the right.
- Inside the platform, show shared orchestration components first, then task plugins, then task pipelines, then containers/datasets/models.
- The critical feedback arrow is from experiment results back to the agent memory/evaluator, because that is what drives the next round of algorithm generation.
Main entry points: agent.py Agent core · result_table.py Result table · prompt.py Prompt builder · base.py Executor base
Components 22
Scientist layer
LLM-driven hypothesis generation, code synthesis, evaluation, and memory.
- Scientist CLI · Package entrypoint that starts the iteration loop.
ai_scientist/__main__.py - Main loop · Runs seed loading, hypothesis generation, execution, feedback, and iteration.
ai_scientist/main.py - Agent core · Coordinates the scientist-side reasoning workflow.
ai_scientist/agent/agent.py - Agent state · Holds mutable experiment state across iterations.
ai_scientist/agent/context.py - Prompt builder · Combines templates, baseline context, and knowledge into LLM prompts.
ai_scientist/agent/prompt.py - Response parser · Extracts code, hyperparameters, and decisions from model output.
ai_scientist/agent/response_parser.py - Evaluator · Scores candidate outputs and turns run results into feedback.
ai_scientist/agent/evaluator.py - LLM client · External model-provider interface used by the agent.
ai_scientist/utils/llm_client.py - Seed baseline · Reference implementations and hyperparameters used as the starting point.
ai_scientist/seed_baseline - Domain knowledge · Curated domain notes consumed by prompts and memory.
ai_scientist/domain_knowledge - Knowledge learner · Distills outcomes into reusable domain knowledge.
ai_scientist/tools/knowledge_learner.py
Execution layer
GPU scheduling, containerized runs, task plugins, and result collection.
- Orchestrator · High-level coordinator for execution, plugins, and result flow.
research_environment/orchestrator.py - Plugin API · Task-family interface for expanding hyperparameters and parsing outputs.
research_environment/plugin.py - GPU scheduler · Allocates GPUs and controls concurrency.
research_environment/scheduler/gpu_scheduler.py - Executor base · Common execution interface for container backends.
research_environment/executor/base.py - Docker exec · Runs tasks inside Docker containers.
research_environment/executor/docker.py - Singularity exec · Runs tasks inside Singularity containers.
research_environment/executor/singularity.py - Result table · Collects and structures experiment outputs and metrics.
research_environment/collector/result_table.py
Recsys tasks
MMRec benchmark pipeline and plugin wiring.
- MMRec plugin · Recsys task adapter for config expansion and output parsing.
research_environment/tasks/Recsys/MMRec/plugin.py - MMRec pipeline · Recommendation benchmark runtime and model zoo.
research_environment/tasks/Recsys/MMRec/pipeline/src/main.py
TimeSeries tasks
Forecasting, classification, imputation, and anomaly pipelines.
- TimeSeries plugin · TimeSeries adapter for the shared orchestration contract.
research_environment/tasks/TimeSeries/long_term_forecast/plugin.py - TS pipeline · Shared pipeline for forecasting, classification, imputation, and anomaly tasks.
research_environment/tasks/TimeSeries/long_term_forecast/pipeline/run.py
Connections 25
- Scientist CLI → Main loop: starts · CLI entrypoint launches the main loop.
- Main loop → Agent core: drives · Main program delegates iteration to the agent.
- Agent core → Agent state: reads/writes · Agent updates experiment context over time.
- Agent core → Prompt builder: builds · Agent assembles prompts for hypothesis and code generation.
- Agent core → LLM client: queries · Agent calls the external language model.
- Agent core → Response parser: parses · Structured outputs are extracted from model responses.
- Agent core → Evaluator: scores · Candidate results are judged before the next iteration.
- Prompt builder → Seed baseline: uses · Baseline implementations seed prompt context.
- Prompt builder → Domain knowledge: uses · Curated knowledge is injected into prompts.
- Evaluator → Knowledge learner: feeds · Evaluation outcomes can be distilled into memory.
- Knowledge learner → Domain knowledge: updates · Learner writes reusable domain knowledge.
- Agent core → Orchestrator: submits · Scientist sends candidate code/config for execution.
- Orchestrator → Plugin API: dispatches · Orchestrator routes work through task plugins.
- Plugin API → GPU scheduler: requests GPUs · Plugins rely on shared scheduling for parallel runs.
- GPU scheduler → Executor base: allocates · Scheduler hands work to an execution backend.
- Executor base → Docker exec: implements · Docker is one concrete execution backend.
- Executor base → Singularity exec: implements · Singularity is the other concrete execution backend.
- Docker exec → MMRec pipeline: runs · Container backend executes the Recsys benchmark pipeline.
- Docker exec → TS pipeline: runs · Container backend executes the TimeSeries benchmark pipeline.
- MMRec plugin → MMRec pipeline: adapts · Recsys plugin connects orchestration to the pipeline.
- TimeSeries plugin → TS pipeline: adapts · TimeSeries plugin connects orchestration to the pipeline.
- MMRec pipeline → Result table: reports · Pipeline outputs are summarized into structured results.
- TS pipeline → Result table: reports · Pipeline outputs are summarized into structured results.
- Result table → Evaluator: feedback · Collected results feed back into evaluation.
- Result table → Agent state: updates · Structured outcomes are stored in agent context.