Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Spcl Graph of thoughts Controller Run

From Leeroopedia
Knowledge Sources
Domains Graph_Reasoning, LLM_Orchestration
Last Updated 2026-02-14 12:00 GMT

Overview

Concrete tool for executing a Graph of Operations as a BFS traversal provided by the graph-of-thoughts framework.

Description

The Controller class is the central execution engine of the Graph of Thoughts framework. It manages the complete execution flow of a reasoning pipeline by wiring together four collaborators:

  • AbstractLanguageModel -- the LLM backend (e.g., ChatGPT, LLaMA-2)
  • GraphOfOperations -- the DAG of operations defining the reasoning plan
  • Prompter -- domain-specific prompt generation logic
  • Parser -- domain-specific response parsing logic

The Controller also accepts a problem_parameters dictionary that carries initial state (e.g., the input data, method name, ground truth) into the first Thought objects.

Key methods:

  • run() -- Performs BFS traversal of the Graph of Operations. Starts with root operations (those with no predecessors), executes each one, then enqueues successors whose predecessors are all complete. Modifies the graph in-place by populating thought states on each operation.
  • get_final_thoughts() -- Returns the thoughts from all leaf operations (those with no successors). These represent the final reasoning outputs after the full graph has been executed.
  • output_graph(path) -- Serializes the entire executed graph (operations, thoughts, scores, ground truth results) to a JSON file for analysis and reproducibility.

Usage

Import the Controller when you need to run a fully configured GoT pipeline. You must first construct a GraphOfOperations, implement a Prompter and Parser for your domain, and initialize a language model. The Controller orchestrates their interaction.

Code Reference

Source graph_of_thoughts/controller/controller.py
Repository https://github.com/spcl/graph-of-thoughts
Lines L18-152

Signature

class Controller:
    def __init__(
        self,
        lm: AbstractLanguageModel,
        graph: GraphOfOperations,
        prompter: Prompter,
        parser: Parser,
        problem_parameters: dict,
    ) -> None: ...

    def run(self) -> None: ...

    def get_final_thoughts(self) -> List[List[Thought]]: ...

    def output_graph(self, path: str) -> None: ...

Import

from graph_of_thoughts.controller import Controller

I/O Contract

Inputs

Parameter Type Required Description
lm AbstractLanguageModel Yes The language model backend used for LLM calls
graph GraphOfOperations Yes The DAG of operations defining the reasoning plan
prompter Prompter Yes Domain-specific prompt generation logic
parser Parser Yes Domain-specific response parsing logic
problem_parameters dict Yes Initial state dict for the first Thought (e.g., input data, method name)

Outputs

Method Return Type Description
run() None Executes the graph in-place; modifies operation thoughts
get_final_thoughts() List[List[Thought]] Returns thoughts from leaf operations (final reasoning outputs)
output_graph(path) None Writes the executed graph to a JSON file at the given path

Usage Example

from graph_of_thoughts.controller import Controller
from graph_of_thoughts.language_models import ChatGPT
from graph_of_thoughts.operations import GraphOfOperations, Generate, Score, KeepBestN, GroundTruth

# 1. Initialize language model
lm = ChatGPT("config.json", model_name="chatgpt")

# 2. Build graph of operations
goo = GraphOfOperations()
goo.append_operation(Generate(1, 1))
goo.append_operation(Score(scoring_function=my_scorer))
goo.append_operation(KeepBestN(1))
goo.append_operation(GroundTruth(my_evaluator))

# 3. Create controller and run
ctrl = Controller(
    lm,
    goo,
    my_prompter,
    my_parser,
    {"original": data, "current": data, "method": "got"},
)
ctrl.run()

# 4. Get results
final = ctrl.get_final_thoughts()
ctrl.output_graph("results.json")

This example demonstrates a basic pipeline: generate a single thought, score it, keep the best result, and evaluate against ground truth. The Controller's run() method traverses these four operations in BFS order, populating each operation's thoughts as it goes.

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment