Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Spcl Graph of thoughts SortingPrompter

From Leeroopedia
Knowledge Sources
Domains Prompt_Engineering, Sorting
Source File examples/sorting/sorting_032.py, Lines 24-273
Superclass graph_of_thoughts.prompter.Prompter (ABC)
Implements Principle Principle:Spcl_Graph_of_thoughts_Sorting_Prompt_Design
Last Updated 2026-02-14

Overview

SortingPrompter is a domain-specific prompter that generates prompt strings for LLM-based sorting of integer lists. It subclasses the abstract Prompter base class and implements its five abstract methods. The class is defined directly in the sorting example file, not as a reusable package import.

Description

The SortingPrompter class contains six class-level prompt template strings and five methods that select and format the appropriate template based on the current reasoning method and execution phase:

Class Attribute Prompt Templates

Attribute Purpose Used By
sort_prompt Direct sort of the full list with few-shot examples IO, ToT (initial), GoT (phase 1)
sort_prompt_cot Chain-of-thought decomposition sort with approach steps CoT
tot_improve_prompt Correct an incorrectly sorted list by comparing digit frequencies ToT (refinement), GoT (phase 2)
got_split_prompt Split a 32-element list into 2 sublists of 16, output as JSON GoT (phase 0)
got_merge_prompt Merge two sorted sublists into one using merge sort approach GoT (aggregation)

Code Reference

Key Methods

class SortingPrompter(prompter.Prompter):
    def generate_prompt(self, num_branches: int, original: str, current: str, method: str, **kwargs) -> str:
        """
        Routes to the appropriate prompt template based on method and phase.
        - IO: sort_prompt with full input
        - CoT: sort_prompt_cot with full input
        - ToT: sort_prompt (initial) or tot_improve_prompt (refinement)
        - GoT phase 0: got_split_prompt (split into sublists)
        - GoT phase 1: sort_prompt (sort each sublist)
        - GoT phase 2: tot_improve_prompt (refine merged result)
        """

    def aggregation_prompt(self, state_dicts: List[Dict], **kwargs) -> str:
        """
        Generates a merge prompt for two sorted sublists.
        Asserts exactly 2 states. Computes sublist lengths and
        formats got_merge_prompt with both sublists and their sizes.
        """

    def improve_prompt(self, **kwargs) -> str:
        """Not implemented for sorting (returns None)."""

    def validation_prompt(self, **kwargs) -> str:
        """Not implemented for sorting (returns None)."""

    def score_prompt(self, state_dicts: List[Dict], **kwargs) -> str:
        """Not implemented for sorting (scoring is programmatic via num_errors)."""

Instantiation

This class is defined within the example file, not as a package-level import. It is instantiated directly:

# From examples/sorting/sorting_032.py, line 693
executor = controller.Controller(
    lm,
    operations_graph,
    SortingPrompter(),
    SortingParser(),
    {
        "original": data[1],
        "current": "",
        "phase": 0,
        "method": method.__name__,
    },
)

I/O Contract

Input

Parameter Type Description
num_branches int Number of LLM responses to request (typically 1)
original str The full unsorted list as a string, e.g. "[3, 7, 0, 2, ...]"
current str The intermediate solution (sublist or partial sort); empty string initially
method str Reasoning method name: "io", "cot", "tot", "got"
**kwargs Dict Must include phase (int) for GoT; may include unsorted_sublist

Output

All methods return a str containing the formatted prompt ready to send to the language model.

State Dictionary Keys

Key Type Description
original str Full unsorted input list
current str Current intermediate or final sorted list
phase int Execution phase: 0 (split), 1 (sort sublists), 2 (refine)
method str Reasoning approach identifier
part str Sublist identifier, e.g. "List 1", "List 2" (GoT only)
unsorted_sublist str The original unsorted sublist before sorting (GoT only)

Usage Examples

IO Prompt Generation

prompter = SortingPrompter()
prompt = prompter.generate_prompt(
    num_branches=1,
    original="[3, 7, 0, 2, 8, 1, 2, 2]",
    current="",
    method="io"
)
# Returns the sort_prompt template with the input list substituted

GoT Aggregation (Merge) Prompt

prompter = SortingPrompter()
prompt = prompter.aggregation_prompt([
    {"current": "[0, 1, 2, 3, 5, 7, 7, 8]", "part": "List 1", "unsorted_sublist": "[3, 7, 0, 2, 8, 1, 2, 2, 2, 4, 7, 8, 5, 5, 3, 9]"},
    {"current": "[1, 2, 3, 3, 4, 5, 6, 9]", "part": "List 2", "unsorted_sublist": "[4, 3, 5, 6, 6, 4, 4, 5, 2, 0, 9, 3, 3, 9, 2, 1]"},
])
# Returns the got_merge_prompt with both sorted sublists and computed lengths

Related Pages

Page Connections

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