# ozyyshr/structchem architecture

> Architecture diagram of the GitHub repository ozyyshr/structchem, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ozyyshr/structchem
- Repository: https://github.com/ozyyshr/structchem

## Overview

##### Project type and purpose
- This is a research-oriented pipeline for “Structured Chemistry Reasoning with LLMs” (StructChem).
- Purpose: generate, evaluate, and fine-tune large-language-model prompts for step-by-step chemical reasoning. It automates data preparation, prompt orchestration, runtime execution (via OpenAI API), post-processing, evaluation, and supports fine-tuning open-source LLMs.

##### File structure
Top-level
- README.md – overview, instructions
- run.py, get_accuracy.py, post_process.py – main orchestration scripts
- datasets/ – raw and solution JSON, Python-formatted problem files
- outputs/ – model answers per dataset
- prompts/ – prompt templates, instructions, demonstration files
- finetune/ – data-generation, cleaning, fine-tuning, inference scripts
- human_annotation/ – error-case annotations
- docs/ + assets/ – static website and images/videos

Patterns
- Pipeline directories (datasets → prompts → run.py → outputs → get_accuracy)
- A parallel fine-tuning sub-pipeline (finetune/data_generation → finetune scripts)
- Web docs generation separate from core pipeline

##### README insights
- Core workflow: “python run.py” invokes all four SciBench datasets via OpenAI API
- Evaluation: “python get_accuracy.py”
- Fine-tuning uses GPT-based data generation, cleaning, then shell script finetune.sh
- Human annotation supplements error analysis

a. Main components
1. Data Layer
- datasets folder (atkins.json, chemmc.json, quan.json, matter.json + Python dumps)
- prompts folder (instruction templates, demonstrations, feedback)
2. Generation Engine
- run.py – orchestrates prompt assembly, API calls
- post_process.py – cleans and formats LLM outputs
3. Evaluation
- get_accuracy.py – compares outputs vs. solutions
- human_annotation – manual error categorization
4. Fine-tuning Pipeline
- finetune/data_generation – GPT-driven synthetic problem generation + cleaning
- finetune/finetune.py + finetune.sh – model training
- finetune/inference.py + inference.sh – testing fine-tuned model
5. Documentation & Assets
- docs/static – website code, CSS, JS, videos
- assets – illustrative images
6. External Services
- OpenAI API (LLM calls)
- SciBench dataset source

b. Relationships & interactions
- Data Layer feeds into Generation Engine
- Generation Engine calls OpenAI API → generates raw outputs
- post_process transforms outputs → saved to outputs/
- get_accuracy consumes outputs + datasets/…_sol.json → evaluation metrics
- finetune pipeline uses data_generation → cleaned data → finetune → inference → optional evaluation
- docs component is isolated; built separately for website

c. Architectural patterns & principles
- Modular pipeline: distinct stages (data prep → prompt → inference → evaluation → post-proc)
- Separation of concerns: core inference vs. fine-tuning vs. documentation
- Directory-per-responsibility: clear boundaries (datasets, prompts, finetune, docs)
- Scripts as entry points → orchestrate multi-stage workflows

d. Key technologies & frameworks
- Python (core scripts)
- OpenAI API (large-language model)
- JSON / JSONL for data interchange
- Bulma CSS + vanilla JS for docs
- Shell scripts (.sh) for multi-step automation

##### Detail
- Intermediate files: generated_problems.jsonl, outputs/*.jsonl.
- Evaluation loops (e.g., get_accuracy feedback leads to refined prompts in next iteration).
- Don’t over-abstract: include all key scripts and their relationships.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Data Layer
    subgraph "Data Layer" 
        direction TB
        D1[/"atkins.json"/]:::data
        D2[/"atkins_sol.json"/]:::data
        D3[/"chemmc.json"/]:::data
        D4[/"chemmc_sol.json"/]:::data
        D5[/"matter.json"/]:::data
        D6[/"matter_sol.json"/]:::data
        D7[/"quan.json"/]:::data
        D8[/"quan_sol.json"/]:::data
        D9[/"python/ (formatted)"/]:::data
    end

    %% Prompt Templates
    subgraph "Prompt Templates" 
        direction TB
        P1["instruction.py"]:::data
        P2["refine_prompt.py"]:::data
        P3["feedback_formula.txt"]:::data
        P4["feedback_reasoning.txt"]:::data
        P5["skills.txt"]:::data
        P6["verified_instruction.txt"]:::data
        P7["demonstrations/"]:::data
    end

    %% Generation Engine
    subgraph "Generation Engine" 
        direction TB
        R["run.py (orchestrator)"]:::inference
        PP["post_process.py"]:::inference
    end

    %% External API
    OA((OpenAI API)):::external

    %% Outputs
    subgraph "Model Outputs" 
        direction TB
        O1[/"atkins_res.jsonl"/]:::inference
        O2[/"chemmc_res.jsonl"/]:::inference
        O3[/"matter_res.jsonl"/]:::inference
        O4[/"quan_res.jsonl"/]:::inference
    end

    %% Evaluation
    subgraph "Evaluation" 
        direction TB
        G["get_accuracy.py"]:::inference
        H["human_annotation/"]:::inference
    end

    %% Fine-tuning Pipeline
    subgraph "Fine-tuning Pipeline"
        direction TB
        subgraph "Data Generation & Cleaning"
            direction TB
            FG1["gpt_generation.py"]:::fine
            FG2["data_cleaning.py"]:::fine
            FG3[/"generated_problems.jsonl"/]:::fine
            FG4[/"generated_problems_new.jsonl"/]:::fine
            FG5[/"problems_cleaned.json"/]:::fine
            FG6["prompt.txt"]:::fine
            FG7["prompt_new.txt"]:::fine
        end
        subgraph "Model Training"
            direction TB
            FT1["finetune.py"]:::fine
            FT2["finetune.sh"]:::fine
        end
        subgraph "Post-Fine-tune Inference"
            direction TB
            FI1["inference.py"]:::fine
            FI2["inference.sh"]:::fine
            FI3["generate.py"]:::fine
        end
    end

    %% Documentation & Assets
    subgraph "Docs & Assets"
        direction TB
        Ddoc["index.html"]:::docs
        Dstatic["static/"]:::docs
        Aimg["assets/"]:::docs
    end

    %% Connections
    D1 & D2 & D3 & D4 & D5 & D6 & D7 & D8 & D9 --> P1
    P1 & P2 & P3 & P4 & P5 & P6 & P7 --> R
    R --> OA
    OA --> PP
    PP --> O1 & O2 & O3 & O4
    O1 & O2 & O3 & O4 --> G
    D1 & D2 & D3 & D4 & D5 & D6 & D7 & D8 --> G
    G --> H

    D1 & D2 & D3 & D4 & D5 & D6 & D7 & D8 --> FG1
    FG1 --> FG2 --> FG3 --> FG4 --> FG5
    FG6 & FG7 --> FG1
    FG5 --> FT1 --> FT2 --> FI1 --> FI2 --> FI3 --> O1

    %% Docs isolated
    Ddoc & Dstatic --> Aimg

    %% Click Events
    click D1 "https://github.com/ozyyshr/structchem/blob/main/datasets/atkins.json"
    click D2 "https://github.com/ozyyshr/structchem/blob/main/datasets/atkins_sol.json"
    click D3 "https://github.com/ozyyshr/structchem/blob/main/datasets/chemmc.json"
    click D4 "https://github.com/ozyyshr/structchem/blob/main/datasets/chemmc_sol.json"
    click D5 "https://github.com/ozyyshr/structchem/blob/main/datasets/matter.json"
    click D6 "https://github.com/ozyyshr/structchem/blob/main/datasets/matter_sol.json"
    click D7 "https://github.com/ozyyshr/structchem/blob/main/datasets/quan.json"
    click D8 "https://github.com/ozyyshr/structchem/blob/main/datasets/quan_sol.json"
    click D9 "https://github.com/ozyyshr/structchem/tree/main/datasets/python/"
    click P1 "https://github.com/ozyyshr/structchem/blob/main/prompts/instruction.py"
    click P2 "https://github.com/ozyyshr/structchem/blob/main/prompts/refine_prompt.py"
    click P3 "https://github.com/ozyyshr/structchem/blob/main/prompts/feedback_formula.txt"
    click P4 "https://github.com/ozyyshr/structchem/blob/main/prompts/feedback_reasoning.txt"
    click P5 "https://github.com/ozyyshr/structchem/blob/main/prompts/skills.txt"
    click P6 "https://github.com/ozyyshr/structchem/blob/main/prompts/verified_instruction.txt"
    click P7 "https://github.com/ozyyshr/structchem/tree/main/prompts/demonstrations/"
    click R "https://github.com/ozyyshr/structchem/blob/main/run.py"
    click PP "https://github.com/ozyyshr/structchem/blob/main/post_process.py"
    click O1 "https://github.com/ozyyshr/structchem/blob/main/outputs/atkins_res.jsonl"
    click O2 "https://github.com/ozyyshr/structchem/blob/main/outputs/chemmc_res.jsonl"
    click O3 "https://github.com/ozyyshr/structchem/blob/main/outputs/matter_res.jsonl"
    click O4 "https://github.com/ozyyshr/structchem/blob/main/outputs/quan_res.jsonl"
    click G "https://github.com/ozyyshr/structchem/blob/main/get_accuracy.py"
    click H "https://github.com/ozyyshr/structchem/tree/main/human_annotation/"
    click FG1 "https://github.com/ozyyshr/structchem/blob/main/finetune/data_generation/gpt_generation.py"
    click FG2 "https://github.com/ozyyshr/structchem/blob/main/finetune/data_generation/data_cleaning.py"
    click FG3 "https://github.com/ozyyshr/structchem/blob/main/finetune/data_generation/generated_problems.jsonl"
    click FG4 "https://github.com/ozyyshr/structchem/blob/main/finetune/data_generation/generated_problems_new.jsonl"
    click FG5 "https://github.com/ozyyshr/structchem/blob/main/finetune/data_generation/problems_cleaned.json"
    click FG6 "https://github.com/ozyyshr/structchem/blob/main/finetune/data_generation/prompt.txt"
    click FG7 "https://github.com/ozyyshr/structchem/blob/main/finetune/data_generation/prompt_new.txt"
    click FT1 "https://github.com/ozyyshr/structchem/blob/main/finetune/finetune.py"
    click FT2 "https://github.com/ozyyshr/structchem/blob/main/finetune/finetune.sh"
    click FI1 "https://github.com/ozyyshr/structchem/blob/main/finetune/inference.py"
    click FI2 "https://github.com/ozyyshr/structchem/blob/main/finetune/inference.sh"
    click FI3 "https://github.com/ozyyshr/structchem/blob/main/finetune/generate.py"
    click Ddoc "https://github.com/ozyyshr/structchem/blob/main/docs/index.html"
    click Dstatic "https://github.com/ozyyshr/structchem/tree/main/docs/static/"
    click Aimg "https://github.com/ozyyshr/structchem/tree/main/assets/"

    %% Styles
    classDef data fill:#cce5ff,stroke:#004085,color:#004085
    classDef inference fill:#d4edda,stroke:#155724,color:#155724
    classDef fine fill:#fff3cd,stroke:#856404,color:#856404
    classDef docs fill:#e2e3e5,stroke:#383d41,color:#383d41
    classDef external fill:#f8d7da,stroke:#721c24,color:#721c24
```
