# spcl/graph-of-thoughts architecture

> Architecture diagram of the GitHub repository spcl/graph-of-thoughts, 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/spcl/graph-of-thoughts
- Repository: https://github.com/spcl/graph-of-thoughts

## Overview

##### Project Type and Purpose
- This is a Python framework/library (“Graph of Thoughts”) for solving complex problems by modeling them as a Graph of Operations (GoO) and automatically executing them via a Large Language Model (LLM).
- It is intended both as a user-facing tool (installable from PyPI) and as a developer-extensible codebase (editable installation from source).

##### File Structure
- Top level:
– examples/ : end-to-end scripts illustrating usage (sorting, keyword counting, set intersection, document merging)
– graph_of_thoughts/ : core framework, subdivided into controller, language_models, operations, parser, prompter
– paper/ : reproducibility and plotting scripts for publication results
– config files: pyproject.toml, LICENSE, README.md
- Key modules:
– controller.Controller: orchestrates execution
– operations.GraphOfOperations, Generate, Score, GroundTruth: define and evaluate thought graph
– language_models: abstract interface + concrete ChatGPT and HF implementations
– parser.Parser: interprets LLM outputs into graph nodes
– prompter.Prompter: constructs prompts for each operation

##### README for Additional Insights
- Core flow: user defines a GoO, configures an LLM, creates a Controller with a Prompter and Parser, then calls run() and output_graph().
- Extensibility: new operations, new language models, and problem-specific Prompter/Parser pairs.

a. Main Components
- User Code (examples or custom scripts)
- Controller Module
- Graph of Operations (operations package)
- Prompter Module
- Parser Module
- Language Model Adapter (ChatGPT, HF)
- External LLM Service/API (OpenAI, local‐HF)
- Storage/Output (JSON graph, plots)
b. Relationships and Data Flow
1. User Code → instantiates GraphOfOperations, selects operations
2. User Code → configures LLM adapter via language_models
3. Controller invokes Prompter → generates prompt text
4. Controller sends prompt to Language Model Adapter → calls External LLM API
5. Raw LLM response → Parser → structured Thought nodes
6. Controller updates GraphOfOperations → loops until termination
7. Controller.output_graph → writes JSON, paper/plots.py reads results
c. Architectural Patterns
- Modularity: each concern in its own package
- Strategy pattern: interchangeable Prompter and Parser for each problem
- Chain of Responsibility / Pipeline: Controller mediates between modules
- Pluggable backends: language_models package with abstract base class
d. Key Technologies and Frameworks
- Python 3.8+
- OpenAI API (ChatGPT) and Hugging Face for LLM backends
- JSON for graph serialization
- Matplotlib (paper/plots.py) for results visualization

##### Elements
- Clear, consistent labels for each package/module
– Rectangles for internal modules
– Clouds for external LLM services

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% User Layer
    subgraph "User Layer"
        UserCode["User Code (examples/)"] 
    end

    %% Core Framework Layer
    subgraph "Core Framework (graph_of_thoughts)"
        subgraph "Controller Module"
            ControllerModule["Controller Module"] 
        end
        subgraph "Operations Package"
            GraphOfOperations["GraphOfOperations Core"]
            OperationsDefinitions["Generate, Score, GroundTruth"]
            ThoughtNode["Thought Node Model"]
        end
        subgraph "Prompter Module"
            Prompter["Prompter Module"]
        end
        subgraph "Parser Module"
            Parser["Parser Module"]
        end
        subgraph "Language Models Package"
            AbstractLM["Abstract Language Model Interface"]
            ChatGPTAdapter["ChatGPT Adapter (OpenAI API)"]
            HFAdapter["Hugging Face Adapter"]
            ConfigTemplate["Config Template JSON"]
        end
    end

    %% External Services and Storage
    ExternalLLM["External LLM Service"]:::external
    subgraph "Storage/Output"
        JSONGraph["JSON Graph Storage"]
        Plots["Plots (Matplotlib)"]
    end

    %% Data Flow
    UserCode -->|"defines GoO & config LLM"| ControllerModule
    ControllerModule -->|"constructs prompt text"| Prompter
    Prompter -->|"prompt text"| ChatGPTAdapter
    Prompter -->|"prompt text"| HFAdapter
    ChatGPTAdapter -->|"API call"| ExternalLLM
    HFAdapter -->|"API call"| ExternalLLM
    ExternalLLM -->|"raw LLM response"| Parser
    Parser -->|"structured Thought nodes"| ControllerModule
    ControllerModule -->|"updates graph"| GraphOfOperations
    GraphOfOperations --> OperationsDefinitions
    GraphOfOperations --> ThoughtNode
    ControllerModule -->|"loop until complete"| Prompter
    ControllerModule -->|"output_graph() writes JSON"| JSONGraph
    JSONGraph -->|"read by plotting"| Plots

    %% Config linkage
    ConfigTemplate --> AbstractLM

    %% Click Events
    click ControllerModule "https://github.com/spcl/graph-of-thoughts/blob/main/graph_of_thoughts/controller/controller.py"
    click GraphOfOperations "https://github.com/spcl/graph-of-thoughts/blob/main/graph_of_thoughts/operations/graph_of_operations.py"
    click OperationsDefinitions "https://github.com/spcl/graph-of-thoughts/blob/main/graph_of_thoughts/operations/operations.py"
    click ThoughtNode "https://github.com/spcl/graph-of-thoughts/blob/main/graph_of_thoughts/operations/thought.py"
    click Prompter "https://github.com/spcl/graph-of-thoughts/blob/main/graph_of_thoughts/prompter/prompter.py"
    click Parser "https://github.com/spcl/graph-of-thoughts/blob/main/graph_of_thoughts/parser/parser.py"
    click AbstractLM "https://github.com/spcl/graph-of-thoughts/blob/main/graph_of_thoughts/language_models/abstract_language_model.py"
    click ChatGPTAdapter "https://github.com/spcl/graph-of-thoughts/blob/main/graph_of_thoughts/language_models/chatgpt.py"
    click HFAdapter "https://github.com/spcl/graph-of-thoughts/blob/main/graph_of_thoughts/language_models/llamachat_hf.py"
    click ConfigTemplate "https://github.com/spcl/graph-of-thoughts/blob/main/graph_of_thoughts/language_models/config_template.json"
    click UserCode "https://github.com/spcl/graph-of-thoughts/tree/main/examples/"
    click Plots "https://github.com/spcl/graph-of-thoughts/blob/main/paper/plots.py"
    click JSONGraph "https://github.com/spcl/graph-of-thoughts/blob/main/pyproject.toml"

    %% Styling
    classDef module fill:#FFD580,stroke:#333,stroke-width:1px
    classDef external fill:#A0D3E8,stroke:#333,stroke-width:1px
    classDef storage fill:#C1E1C1,stroke:#333,stroke-width:1px

    class ControllerModule,Prompter,Parser,GraphOfOperations,OperationsDefinitions,ThoughtNode,AbstractLM,ChatGPTAdapter,HFAdapter module
    class JSONGraph,Plots storage
    class ExternalLLM external
```
