How amazon-science/diversityofthoughts works
1. This repository is an LLM-agent research harness, not a single application. Its core loop is: load a benchmark task, ask a model to produce one or more candidate solutions/thoughts, execute those candidates in a task-specific sandbox, and use the results to drive the next iteration. The main architectural idea is iterative search over solution trajectories.
2. The top-level entry scripts define the agent variants and run modes. main.py is the likely central dispatcher, while dot.py, dot_bank.py, reflexion.py, and simple.py represent the four supported policies: Diversity of Thoughts, Diversity of Thoughts with memory bank, Reflexion-style self-improvement, and a one-shot baseline. example_run.sh is the canonical launcher for experiments.
3. The generation subsystem lives under generators/. It abstracts how prompts are formed and how model outputs are parsed into structured candidate actions or code. generator_types.py, generator_utils.py, factory.py, and model.py indicate a shared interface with pluggable generators, while py_generate.py and rs_generate.py split by language/task format. This boundary isolates model prompting from the rest of the system.
4. The execution subsystem lives under executors/. It is responsible for running generated code or task actions and returning structured feedback. py_executor.py and rs_executor.py handle local Python and Rust execution, while leet_executor.py and the leetcode_env/ package support LeetCode-style problems in an external environment. executor_types.py, executor_utils.py, and factory.py provide the common contracts and selection logic.
5. The LeetCodeHardGym integration is a distinct runtime boundary. executors/leetcode_env/environment.py and its helpers implement a browser/session-backed environment that depends on external LeetCode authentication and dataset preparation. This path is materially different from local code execution because it interacts with a remote service and browser cookies rather than running purely offline.
6. The repository supports multiple benchmark sources and task formats. The benchmarks/ directory contains HumanEval and MBPP JSONL datasets, and the README points to optional LeetCodeHardGym experiments. generate_dataset.py appears to convert or prepare these tasks into the internal format consumed by the agent loop.
7. The feedback loop is what makes the architecture agentic. A generator produces a candidate; an executor runs it against tests or the environment; the result is parsed and fed back into the next generation step. That loop is governed by max_iters, which bounds the search depth. The DoT variants differ mainly in how they manage and diversify the candidate set across iterations.
8. dot_bank.py adds a task-agnostic memory bank on top of the base DoT workflow. This introduces a persistent retrieval-like store of prior thoughts/solutions that can influence later prompts, creating a second memory boundary separate from the immediate trajectory state used by the standard agents.
9. Validation and scoring are handled by standalone scripts rather than inside the agent loop. evaluate_leet_results.py, validate_py_results.py, validate_rs_results.py, and test_acc.py suggest an offline evaluation stage that reads saved trajectories or outputs and computes pass/fail or accuracy metrics. This keeps experiment execution and metric computation decoupled.
10. Utility modules such as utils.py, memory_utils.py, gpt_usage.py, and generators/parse.py provide shared support for formatting, prompt/response parsing, memory manipulation, and OpenAI usage tracking. requirements.txt and the OpenAI API key setup make it clear that the model backend is an external dependency, not embedded locally.
11. The data flow across the repository is therefore: benchmark input → agent-specific prompt generation → model call → parsed candidate output → execution in Python/Rust/LeetCode environment → result/trace storage → optional reflection or memory update → next iteration → final evaluation. That is the main architecture to capture in a diagram.
Main entry points: executors/factory.py Exec factory · generators/factory.py Gen factory · main.py Main · humaneval_full.jsonl Benchmarks
Components 28
Run modes
Top-level agent variants and experiment launchers.
- Main · Central experiment dispatcher and shared orchestration entry point.
main.py - DoT · Diversity of Thoughts search policy.
dot.py - DoT Bank · DoT variant with persistent memory bank.
dot_bank.py - Reflexion · Self-improving reflection-based policy.
reflexion.py - Baseline · One-shot baseline run mode.
simple.py - Launcher · Canonical experiment shell launcher.
example_run.sh
Generation
Prompting, model access, and output parsing.
- Gen factory · Chooses generator implementation by task/model.
generators/factory.py - Gen types · Shared generator contracts and data structures.
generators/generator_types.py - Gen utils · Prompting and generation helper utilities.
generators/generator_utils.py - Model · Model invocation and backend abstraction.
generators/model.py - Parse · Turns raw model output into structured actions.
generators/parse.py - Python gen · Python-oriented prompt/output generation.
generators/py_generate.py - Rust gen · Rust-oriented prompt/output generation.
generators/rs_generate.py
Execution
Code and environment execution backends.
- Exec factory · Chooses the executor implementation.
executors/factory.py - Exec types · Shared executor contracts and result types.
executors/executor_types.py - Python exec · Runs Python candidate code locally.
executors/py_executor.py - Rust exec · Runs Rust candidate code locally.
executors/rs_executor.py - Leet exec · Executes LeetCode-style tasks through the LeetCode environment.
executors/leet_executor.py - Leet env · Browser/session-backed LeetCode runtime boundary.
executors/leetcode_env/environment.py
Data and eval
Benchmarks, dataset prep, and offline scoring.
- Benchmarks · HumanEval and MBPP benchmark sources.
benchmarks/humaneval_full.jsonl - Prep data · Converts or prepares tasks into the internal format.
generate_dataset.py - Py validate · Validates saved Python results and trajectories.
validate_py_results.py - Rs validate · Validates saved Rust results and trajectories.
validate_rs_results.py - Leet eval · Evaluates LeetCode experiment results offline.
evaluate_leet_results.py - Accuracy · Computes aggregate accuracy metrics.
test_acc.py
Other components
- Utils · General formatting and shared repository utilities.
utils.py - Memory utils · Memory manipulation helpers used by banked variants.
memory_utils.py - Usage · Tracks model usage and cost-related metadata.
gpt_usage.py
Connections 35
- Launcher → Main: launches · Shell launcher starts the main experiment dispatcher.
- Main → DoT: selects · Dispatcher can run the DoT policy.
- Main → DoT Bank: selects · Dispatcher can run the banked DoT policy.
- Main → Reflexion: selects · Dispatcher can run the Reflexion policy.
- Main → Baseline: selects · Dispatcher can run the baseline policy.
- DoT Bank → Memory utils: uses · Banked policy reads and updates persistent memory.
- DoT → Gen factory: calls · Policies request generator implementations.
- Reflexion → Gen factory: calls · Reflection loop uses the same generator boundary.
- Baseline → Gen factory: calls · Baseline still routes through generation abstraction.
- Gen factory → Gen types: implements · Factory instantiates generator implementations conforming to shared contracts.
- Gen factory → Python gen: selects · Chooses Python-specific generation flow when needed.
- Gen factory → Rust gen: selects · Chooses Rust-specific generation flow when needed.
- Python gen → Model: calls · Python generator prompts and queries the model backend.
- Rust gen → Model: calls · Rust generator prompts and queries the model backend.
- Python gen → Parse: parses · Python outputs are normalized into structured candidates.
- Rust gen → Parse: parses · Rust outputs are normalized into structured candidates.
- Parse → Exec factory: feeds · Structured candidates are sent toward execution selection.
- Exec factory → Exec types: implements · Factory instantiates executor implementations conforming to shared contracts.
- Exec factory → Python exec: selects · Chooses the Python runtime executor.
- Exec factory → Rust exec: selects · Chooses the Rust runtime executor.
- Exec factory → Leet exec: selects · Chooses the LeetCode-style executor.
- Leet exec → Leet env: drives · Remote executor operates through the browser/session environment.
- Python exec → Benchmarks: runs against · Python executor validates candidate code against benchmark tasks.
- Rust exec → Benchmarks: runs against · Rust executor validates candidate code against benchmark tasks.
- Leet env → Benchmarks: uses tasks · LeetCode environment consumes benchmark/task definitions.
- Prep data → Benchmarks: prepares · Dataset generation and conversion starts from benchmark sources.
- Py validate → Python exec: evaluates · Offline validation consumes Python execution outputs.
- Rs validate → Rust exec: evaluates · Offline validation consumes Rust execution outputs.
- Leet eval → Leet exec: evaluates · Offline evaluation consumes LeetCode execution traces.
- Accuracy → Py validate: scores · Accuracy script aggregates evaluation outputs.
- Accuracy → Rs validate: scores · Accuracy script aggregates evaluation outputs.
- Accuracy → Leet eval: scores · Accuracy script aggregates LeetCode evaluation outputs.
- Model → Usage: tracks · Model calls are tracked for usage accounting.
- Gen utils → Utils: uses · Generation helpers rely on shared utilities.
- Exec factory → Utils: uses · Executor selection shares common utility helpers.